Skip to content

Commit

Permalink
Fix issue #453 jinja2 template select_autoescape when using jinja2.se…
Browse files Browse the repository at this point in the history
…lect_autoescape (#454)

* Add unit test showing the issue

* Allow select_autoescape to be an attribute (i.e. jinja2.select_autoescape)

* Update bandit/plugins/jinja2_templates.py

* Update jinja2_templates.py

Co-authored-by: Eric Brown <ericwb@users.noreply.github.com>
  • Loading branch information
kinow and ericwb committed Jul 11, 2022
1 parent 1067978 commit f762f2e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 4 additions & 3 deletions bandit/plugins/jinja2_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ def jinja2_autoescape_false(context):
):
return
# Check if select_autoescape function is used.
elif (
isinstance(value, ast.Call)
and getattr(value.func, "id", None)
elif isinstance(value, ast.Call) and (
getattr(value.func, "attr", None)
== "select_autoescape"
or getattr(value.func, "id", None)
== "select_autoescape"
):
return
Expand Down
3 changes: 3 additions & 0 deletions examples/jinja2_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
Environment(loader=templateLoader,
autoescape=select_autoescape(['html', 'htm', 'xml']))

Environment(loader=templateLoader,
autoescape=jinja2.select_autoescape(['html', 'htm', 'xml']))


def fake_func():
return 'foobar'
Expand Down

0 comments on commit f762f2e

Please sign in to comment.