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

Improve shell #298

Merged
merged 7 commits into from Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 22 additions & 3 deletions bandit/plugins/injection_shell.py
Expand Up @@ -85,6 +85,25 @@ def gen_config(name):
}


def has_shell(context):
keywords = context.node.keywords
if 'shell' in context.call_keywords:
for key in keywords:
if key.arg == 'shell':
val = key.value
if isinstance(val, ast.Num):
return bool(val.n)
if isinstance(val, ast.List):
return bool(val.elts)
if isinstance(val, ast.Dict):
return bool(val.keys)
if isinstance(val, ast.Name):
if val.id in ['False', 'None']:
return False
return True
return False


@test.takes_config('shell_injection')
@test.checks('Call')
@test.test_id('B602')
Expand Down Expand Up @@ -180,7 +199,7 @@ def subprocess_popen_with_shell_equals_true(context, config):
.. versionadded:: 0.9.0
"""
if config and context.call_function_name_qual in config['subprocess']:
if context.check_call_arg_value('shell', 'True'):
if has_shell(context):
if len(context.call_args) > 0:
sev = _evaluate_shell_call(context)
if sev == bandit.LOW:
Expand Down Expand Up @@ -271,7 +290,7 @@ def subprocess_without_shell_equals_true(context, config):
.. versionadded:: 0.9.0
"""
if config and context.call_function_name_qual in config['subprocess']:
if not context.check_call_arg_value('shell', 'True'):
if not has_shell(context):
return bandit.Issue(
severity=bandit.LOW,
confidence=bandit.HIGH,
Expand Down Expand Up @@ -349,7 +368,7 @@ def any_other_function_with_shell_equals_true(context, config):
.. versionadded:: 0.9.0
"""
if config and context.call_function_name_qual not in config['subprocess']:
if context.check_call_arg_value('shell', 'True'):
if has_shell(context):
return bandit.Issue(
severity=bandit.MEDIUM,
confidence=bandit.LOW,
Expand Down
21 changes: 21 additions & 0 deletions examples/subprocess_shell.py
Expand Up @@ -5,6 +5,9 @@
def Popen(*args, **kwargs):
print('hi')

def __len__(self):
Copy link
Member

Choose a reason for hiding this comment

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

Is this related to this patch? Is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any object with length 0 is False, I set this in the test because this will not be detected as False, so is a known false positive.

return 0

pop('/bin/gcc --version', shell=True)
Popen('/bin/gcc --version', shell=True)

Expand All @@ -31,3 +34,21 @@ def Popen(*args, **kwargs):
subprocess.Popen(command, shell=True)

subprocess.Popen('/bin/ls && cat /etc/passwd', shell=True)

# Issue #157
Copy link
Member

Choose a reason for hiding this comment

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

You can remove references to bugs/issues. We can always use git or git blame to look up changes based on what issue.

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

command = 'pwd'
subprocess.call(command, shell='True')
subprocess.call(command, shell='False')
subprocess.call(command, shell='None')
subprocess.call(command, shell=1)

subprocess.call(command, shell=Popen())
subprocess.call(command, shell=[True])
subprocess.call(command, shell={'IS': 'True'})
subprocess.call(command, shell=command)

subprocess.call(command, shell=False)
subprocess.call(command, shell=0)
subprocess.call(command, shell=[])
subprocess.call(command, shell={})
subprocess.call(command, shell=None)
4 changes: 2 additions & 2 deletions tests/functional/test_functional.py
Expand Up @@ -417,8 +417,8 @@ def test_ssl_insecure_version(self):
def test_subprocess_shell(self):
'''Test for `subprocess.Popen` with `shell=True`.'''
expect = {
'SEVERITY': {'UNDEFINED': 0, 'LOW': 14, 'MEDIUM': 1, 'HIGH': 3},
'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 1, 'MEDIUM': 0, 'HIGH': 17}
'SEVERITY': {'UNDEFINED': 0, 'LOW': 19, 'MEDIUM': 1, 'HIGH': 11},
'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 1, 'MEDIUM': 0, 'HIGH': 30}
}
self.check_example('subprocess_shell.py', expect)

Expand Down