Skip to content

B701: Extend Jinja2 checks to cover dynamic template source execution #1404

Description

@invoke1442

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 👍.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions