You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a developer does not import from jinja2 import select_autoescape, but instead imports import jinja2 and then use it as autoescape=jinja2.autoescape, it causes bandit to report it as an issue B701:jinja2_autoescape_false.
While working on Cylc started working on an issue to fix Codacy issues, and noticed that this issue was still reported regardless of the jinja2.autoescape. Here's the link to the Codacy reported issue (no idea how long Codacy keeps it though).
It looks like this was an issue in the past (https://bugs.launchpad.net/bandit/+bug/1684249 later migrated to GitHub as #271). But the problem is that the current fix only looks at the AST's function ID. Here's the current code:
Note the error reported in the terminal as follows:
$ bandit /home/kinow/Development/python/workspace/cylc/lib/cylc/reviewpy
[main] INFO profile include tests: None
[main] INFO profile exclude tests: None
[main] INFO cli include tests: None
[main] INFO cli exclude tests: None
[main] INFO running on Python 3.7.2
Run started:2019-02-10 11:18:29.313192
Test results:
No issues identified.
Code scanned:
Total lines of code: 0
Total lines skipped (#nosec): 0
Run metrics:
Total issues (by severity):
Undefined: 0
Low: 0
Medium: 0
High: 0
Total issues (by confidence):
Undefined: 0
Low: 0
Medium: 0
High: 0
Files skipped (1):
/home/kinow/Development/python/workspace/cylc/lib/cylc/reviewpy (No such file or directory)
(venv) kinow@ranma:/tmp$ bandit /home/kinow/Development/python/workspace/cylc/lib/cylc/review.py
[main] INFO profile include tests: None
[main] INFO profile exclude tests: None
[main] INFO cli include tests: None
[main] INFO cli exclude tests: None
[main] INFO running on Python 3.7.2
Run started:2019-02-10 11:18:31.650883
Test results:
>> Issue: [B701:jinja2_autoescape_false] Using jinja2 templates with autoescape=False is dangerous and can lead to XSS. Ensure autoescape=True or use the select_autoescape function to mitigate XSS vulnerabilities.
Severity: High Confidence: Medium
Location: /home/kinow/Development/python/workspace/cylc/lib/cylc/review.py:83
More Info: https://bandit.readthedocs.io/en/latest/plugins/b701_jinja2_autoescape_false.html
82 # Autoescape markup to prevent code injection from user inputs.
83 template_env = jinja2.Environment(
84 loader=jinja2.FileSystemLoader(
85 get_util_home("lib", "cylc", "cylc-review", "template")),
86 autoescape=jinja2.select_autoescape(
87 enabled_extensions=('html', 'xml'), default_for_string=True),
88 )
--------------------------------------------------
Code scanned:
Total lines of code: 799
Total lines skipped (#nosec): 0
Run metrics:
Total issues (by severity):
Undefined: 0.0
Low: 0.0
Medium: 0.0
High: 1.0
Total issues (by confidence):
Undefined: 0.0
Low: 0.0
Medium: 1.0
High: 0.0
Files skipped (0):
Then alter the code to use from jinja2.select_autoescape and modify the autoescape=select_autoescape part too. After that bandit stops reporting the issue. Both code examples are valid, and the same functionality / security.
Expected behavior
No issues reported.
Bandit version
bandit 1.5.1
python version = 3.7.2 (default, Dec 29 2018, 06:19:36) [GCC 7.3.0]
Additional context
To confirm what is going on, it is also possible to play with ast, astor, and produce some examples.
>>> import ast
>>> import astor
>>> from pprint import pprint
>>>
>>>
>>> # works with current version
... s1 = """
... from jinja2 import select_autoescape, Environment, FileSystemLoader
...
... template_env = Environment(
... loader=FileSystemLoader("/abc/templates"),
... autoescape=select_autoescape(
... enabled_extensions=('html', 'xml'))
... )
... """
>>>
>>> # broken in current version, but same as previous code
... s2 = """
... import jinja2
...
... template_env = jinja2.Environment(
... loader=jinja2.FileSystemLoader("/abc/templates"),
... autoescape=jinja2.select_autoescape(
... enabled_extensions=('html', 'xml'))
... )
... """
>>>
>>> tree1 = ast.parse(s1)
>>> tree2 = ast.parse(s2)
>>>
Note that here what we have is actually value=Call(func=Attribute(value=Name(id='jinja2'), "
"attr='select_autoescape'),\n". Where we have the ID being the module imported or alias, and the attribute being now where we can find select_autoescape.
Finally, we could discuss adding further validation for the module alias. Checking the context of the parsed tree we could find how the user aliased the module jinja2. But I am not sure if that's necessary.
Here we actually have ** "func=Attribute(value=Name(id='not_what_you_expected'), "
"attr='select_autoescape'),\n"**. Where not_what_you_expected is the alias of the jinja2 module as it was imported.
Hope that makes sense.
Thank you for bandit 👍
Bruno
The text was updated successfully, but these errors were encountered:
…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>
Describe the bug
If a developer does not import
from jinja2 import select_autoescape
, but instead importsimport jinja2
and then use it asautoescape=jinja2.autoescape
, it causes bandit to report it as an issue B701:jinja2_autoescape_false.While working on Cylc started working on an issue to fix Codacy issues, and noticed that this issue was still reported regardless of the
jinja2.autoescape
. Here's the link to the Codacy reported issue (no idea how long Codacy keeps it though).It looks like this was an issue in the past (https://bugs.launchpad.net/bandit/+bug/1684249 later migrated to GitHub as #271). But the problem is that the current fix only looks at the AST's function ID. Here's the current code:
bandit/bandit/plugins/jinja2_templates.py
Lines 109 to 112 in 3371a6d
But when you use
jinja2.select_autoescape
, the select_autoescape part is now the argument of the call function, not the ID.To Reproduce
Steps to reproduce the behavior:
example1.py
:from jinja2.select_autoescape
and modify theautoescape=select_autoescape
part too. After that bandit stops reporting the issue. Both code examples are valid, and the same functionality / security.Expected behavior
No issues reported.
Bandit version
Additional context
To confirm what is going on, it is also possible to play with
ast
,astor
, and produce some examples.Here the first tree (
tree1
) is what works now.Note the value=Call(func=Name(id='select_autoescape'),\n", where the function ID is present.
Now the
tree2
, which hasjinja2.select_autoescape
.Note that here what we have is actually value=Call(func=Attribute(value=Name(id='jinja2'), "
"attr='select_autoescape'),\n". Where we have the ID being the module imported or alias, and the attribute being now where we can find
select_autoescape
.Finally, we could discuss adding further validation for the module alias. Checking the context of the parsed tree we could find how the user aliased the module
jinja2
. But I am not sure if that's necessary.Here's what I mean.
Here we actually have ** "func=Attribute(value=Name(id='not_what_you_expected'), "
"attr='select_autoescape'),\n"**. Where not_what_you_expected is the alias of the jinja2 module as it was imported.
Hope that makes sense.
Thank you for bandit 👍
Bruno
The text was updated successfully, but these errors were encountered: