B701: Extend Jinja2 checks to cover dynamic template source execution
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 8.3k
- Forks
- 836
- Avg merge
- 5d 3h
- Merged PRs (30d)
- 1
Description
Is your feature request related to a problem? Please describe.
Bandit already has Jinja2-specific checks in jinja2_templates.py (B701 for autoescape=False), but it does not warn when non-literal template source is executed through Jinja2 APIs such as:
from jinja2 import Template
def render(instructions):
return Template(instructions).render()
or
from jinja2 import Environment
def render(instructions):
env = Environment()
return env.from_string(instructions).render()
These patterns are relevant for SSTI-style bugs, even when autoescape is not the issue.
Describe the solution you'd like
It would be useful to extend the existing Jinja2 checks to also flag dynamic template source execution in:
jinja2.Template(...)jinja2.Environment.from_string(...)
A small but important detail is that SandboxedEnvironment().from_string(...) should not be flagged, since that is a common fix pattern:
from jinja2.sandbox import SandboxedEnvironment
def render(instructions):
env = SandboxedEnvironment()
return env.from_string(instructions).render()
Describe alternatives you've considered
Implementing a separate rule is possible, but since Bandit already has Jinja2-specific checks, extending the existing Jinja2 rule family would likely be more consistent than adding a completely separate plugin.
Additional context
The goal here is intentionally narrow:
- warn on non-literal template source passed to Jinja2 template construction /
from_string - avoid trying to do full taint tracking
- avoid flagging
SandboxedEnvironment().from_string(...)
So this should be a relatively small, focused enhancement rather than a broad new dataflow feature.
Love this idea? Give it a 👍. We prioritize fulfilling features with the most 👍.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in jinja2_templates.py by reading the existing B701 implementation and surrounding Jinja2 checks. Trace how calls are recognized, then verify that non-literal sources passed to jinja2.Template and Environment.from_string are flagged while SandboxedEnvironment().from_string remains unflagged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- security, tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 56/100