Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast fix for yaml import #303

Merged
merged 9 commits into from
Jun 14, 2018
3 changes: 2 additions & 1 deletion bandit/plugins/yaml_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
@test.test_id('B506')
@test.checks('Call')
def yaml_load(context):
if isinstance(context.call_function_name_qual, str):
if context.is_module_imported_exact('yaml') and \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid using \ in long if statement lines. Use ( ) instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

isinstance(context.call_function_name_qual, str):
qualname_list = context.call_function_name_qual.split('.')
func = qualname_list[-1]
if 'yaml' in qualname_list and func == 'load':
Expand Down
7 changes: 7 additions & 0 deletions examples/yaml_lib_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ruamel.yaml import YAML


def not_a_vul(content):
yaml = YAML(typ='safe')
c = yaml.load(content)

4 changes: 3 additions & 1 deletion examples/yaml_load.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import json
import yaml


def test_yaml_load():
ystr = yaml.dump({'a' : 1, 'b' : 2, 'c' : 3})
ystr = yaml.dump({'a': 1, 'b': 2, 'c': 3})
y = yaml.load(ystr)
yaml.dump(y)
y = yaml.load(ystr, Loader=yaml.SafeLoader)


def test_json_load():
# no issue should be found
j = json.load("{}")
6 changes: 6 additions & 0 deletions tests/functional/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ def test_yaml(self):
}
self.check_example('yaml_load.py', expect)

expect = {
'SEVERITY': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 0, 'HIGH': 0},
'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 0, 'HIGH': 0}
}
self.check_example('yaml_lib_load.py', expect)

def test_jinja2_templating(self):
'''Test jinja templating for potential XSS bugs.'''
expect = {
Expand Down