PyCQA / PyCQA/bandit

B701: Extend Jinja2 checks to cover dynamic template source execution

Open
#1,404 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.