B701: Extend Jinja2 checks to cover dynamic template source execution
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 56/100
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.
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 👍.
- Dominant language
- Python
- Stars
- 8.4k
- Forks
- 893
- Avg merge
- 6h 52m
- Merged PRs (30d)
- 3
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing 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.
More from PyCQA/bandit
-
Difficulty 1/5 1-3 hours Newbie friendliness 85/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Turn off progress bar in CIPossibly taken @silvatechf claimed this 28 days ago. Openenhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
-
`bandit -ii -ll -ii -ll` raises an `IndexError`Possibly taken @botbikamordehai2-sketch claimed this 181 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 85/100
NatLabRockies/openstudio-mcp#286 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
intersystems/ipm#1338 ·
Maintainers usually reply within 1 day
-
separate_packages splits imports from the same explicitly configured packagePossibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
The v0-through-v1 path derives ShutdownDuration its own way, silently overriding a user's definitionOpenv1
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day