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

Fix a false positive condition yaml_load #927

Merged
merged 1 commit into from Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions bandit/core/context.py
Expand Up @@ -276,9 +276,8 @@ def get_call_arg_at_position(self, position_num):
"""
max_args = self.call_args_count
if max_args and position_num < max_args:
return self._get_literal_value(
self._context["call"].args[position_num]
)
arg = self._context["call"].args[position_num]
return getattr(arg, "attr", None) or self._get_literal_value(arg)
else:
return None

Expand Down
2 changes: 2 additions & 0 deletions bandit/plugins/yaml_load.py
Expand Up @@ -62,6 +62,8 @@ def yaml_load(context):
func == "load",
not context.check_call_arg_value("Loader", "SafeLoader"),
not context.check_call_arg_value("Loader", "CSafeLoader"),
not context.get_call_arg_at_position(1) == "SafeLoader",
not context.get_call_arg_at_position(1) == "CSafeLoader",
]
):
return bandit.Issue(
Expand Down
8 changes: 8 additions & 0 deletions examples/yaml_load.py
@@ -1,5 +1,7 @@
import json
import yaml
from yaml import CSafeLoader
from yaml import SafeLoader


def test_yaml_load():
Expand All @@ -18,3 +20,9 @@ def test_json_load():
j = json.load("{}")

yaml.load("{}", Loader=yaml.Loader)

# no issue should be found
yaml.load("{}", SafeLoader)
yaml.load("{}", yaml.SafeLoader)
yaml.load("{}", CSafeLoader)
yaml.load("{}", yaml.CSafeLoader)