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

#694 Bandit fails when using importlib with named arguments #701

Merged
merged 4 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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: 4 additions & 1 deletion bandit/core/blacklisting.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def blacklist(context, config):
# argument name as an actual import module name.
# Will produce None if argument is not a literal or identifier
if name in ["importlib.import_module", "importlib.__import__"]:
name = context.call_args[0]
if context.call_args_count > 0:
name = context.call_args[0]
else:
name = context.call_keywords['name']
for check in blacklists[node_type]:
for qn in check['qualnames']:
if name is not None and fnmatch.fnmatch(name, qn):
Expand Down
4 changes: 4 additions & 0 deletions examples/imports-with-importlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
# Do not crash when target is an expression
e = importlib.import_module(MODULE_MAP[key])
f = importlib.__import__(MODULE_MAP[key])

# Do not crash when target is a named argument
g = importlib.import_module(name='foo', package='bar.baz')
h = importlib.__import__(name='foo', package='bar.baz')