-
-
Notifications
You must be signed in to change notification settings - Fork 756
Fix B602: Fix IndexError on subprocess calls with keyword arguments #1342
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
Open
balaram753
wants to merge
3
commits into
PyCQA:main
Choose a base branch
from
balaram753:fix/subprocess-keyword-args
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,146 @@ | ||||||||||||
| import ast | ||||||||||||
| import unittest | ||||||||||||
| from unittest import mock | ||||||||||||
|
|
||||||||||||
| import bandit | ||||||||||||
| from bandit.core import issue | ||||||||||||
| from bandit.plugins import injection_shell | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class InjectionShellTests(unittest.TestCase): | ||||||||||||
|
|
||||||||||||
| def _get_context(self, function_name, args=None, keywords=None): | ||||||||||||
| context = mock.Mock() | ||||||||||||
| context.call_function_name_qual = function_name | ||||||||||||
| # context.call_args expects a list of arguments passed to the function call | ||||||||||||
| context.call_args = args or [] | ||||||||||||
| context.call_keywords = keywords or {} | ||||||||||||
| context.node = mock.Mock() | ||||||||||||
| context.node.args = [] | ||||||||||||
| context.node.keywords = [] | ||||||||||||
| # Helpers for getting line number | ||||||||||||
| context.get_lineno_for_call_arg.return_value = 1 | ||||||||||||
| return context | ||||||||||||
|
|
||||||||||||
| def test_subprocess_popen_with_shell_true_low_severity(self): | ||||||||||||
| # Case: subprocess.Popen('ls', shell=True) -> LOW severity (static string) | ||||||||||||
| context = self._get_context( | ||||||||||||
| "subprocess.Popen", args=[mock.Mock()], keywords={"shell": True} | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| # Mocking context.node for _evaluate_shell_call | ||||||||||||
| # It checks if args[0] is ast.Constant and value is str | ||||||||||||
| arg_node = ast.Constant(value="ls") | ||||||||||||
| context.node.args = [arg_node] | ||||||||||||
|
|
||||||||||||
| config = {"subprocess": ["subprocess.Popen"]} | ||||||||||||
|
|
||||||||||||
| with mock.patch( | ||||||||||||
| "bandit.plugins.injection_shell.has_shell", return_value=True | ||||||||||||
| ): | ||||||||||||
| result = injection_shell.subprocess_popen_with_shell_equals_true( | ||||||||||||
| context, config | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| self.assertIsInstance(result, bandit.Issue) | ||||||||||||
| self.assertEqual(bandit.LOW, result.severity) | ||||||||||||
| self.assertEqual(bandit.HIGH, result.confidence) | ||||||||||||
| self.assertEqual( | ||||||||||||
| "call with shell=True", "call with shell=True" | ||||||||||||
| ) # Dummy assertion | ||||||||||||
|
Comment on lines
+48
to
+50
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove? |
||||||||||||
|
|
||||||||||||
| def test_subprocess_popen_with_shell_true_high_severity(self): | ||||||||||||
| # Case: subprocess.Popen(cmd, shell=True) -> HIGH severity (dynamic) | ||||||||||||
| context = self._get_context( | ||||||||||||
| "subprocess.Popen", args=[mock.Mock()], keywords={"shell": True} | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| # Mocking context.node for _evaluate_shell_call | ||||||||||||
| # It checks if args[0] is ast.Constant. If not (e.g. ast.Name), it returns HIGH. | ||||||||||||
| arg_node = ast.Name(id="cmd", ctx=ast.Load()) | ||||||||||||
| context.node.args = [arg_node] | ||||||||||||
|
|
||||||||||||
| config = {"subprocess": ["subprocess.Popen"]} | ||||||||||||
|
|
||||||||||||
| with mock.patch( | ||||||||||||
| "bandit.plugins.injection_shell.has_shell", return_value=True | ||||||||||||
| ): | ||||||||||||
| result = injection_shell.subprocess_popen_with_shell_equals_true( | ||||||||||||
| context, config | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| self.assertIsInstance(result, bandit.Issue) | ||||||||||||
| self.assertEqual(bandit.HIGH, result.severity) | ||||||||||||
| self.assertEqual(bandit.HIGH, result.confidence) | ||||||||||||
|
|
||||||||||||
| def test_subprocess_without_shell_equals_true(self): | ||||||||||||
| # Case: subprocess.Popen(['ls'], shell=False) -> LOW severity | ||||||||||||
| context = self._get_context("subprocess.Popen", args=[mock.Mock()]) | ||||||||||||
| config = {"subprocess": ["subprocess.Popen"]} | ||||||||||||
|
|
||||||||||||
| with mock.patch( | ||||||||||||
| "bandit.plugins.injection_shell.has_shell", return_value=False | ||||||||||||
| ): | ||||||||||||
| result = injection_shell.subprocess_without_shell_equals_true( | ||||||||||||
| context, config | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| self.assertIsInstance(result, bandit.Issue) | ||||||||||||
| self.assertEqual(bandit.LOW, result.severity) | ||||||||||||
| self.assertEqual(bandit.HIGH, result.confidence) | ||||||||||||
| self.assertIn( | ||||||||||||
| "subprocess call - check for execution of untrusted input", | ||||||||||||
| result.text, | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| def test_any_other_function_with_shell_equals_true(self): | ||||||||||||
| # Case: custom_function(cmd, shell=True) -> MEDIUM severity | ||||||||||||
| context = self._get_context( | ||||||||||||
| "custom_function", args=[mock.Mock()], keywords={"shell": True} | ||||||||||||
| ) | ||||||||||||
| config = {"subprocess": ["subprocess.Popen"]} | ||||||||||||
|
|
||||||||||||
| with mock.patch( | ||||||||||||
| "bandit.plugins.injection_shell.has_shell", return_value=True | ||||||||||||
| ): | ||||||||||||
| result = injection_shell.any_other_function_with_shell_equals_true( | ||||||||||||
| context, config | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| self.assertIsInstance(result, bandit.Issue) | ||||||||||||
| self.assertEqual(bandit.MEDIUM, result.severity) | ||||||||||||
| self.assertEqual(bandit.LOW, result.confidence) | ||||||||||||
|
|
||||||||||||
| def test_start_process_with_a_shell(self): | ||||||||||||
| # Case: os.system('ls') -> LOW or HIGH depending on input | ||||||||||||
| # LOW if static | ||||||||||||
| context = self._get_context("os.system", args=[mock.Mock()]) | ||||||||||||
| arg_node = ast.Constant(value="ls") | ||||||||||||
| context.node.args = [arg_node] | ||||||||||||
|
|
||||||||||||
| config = {"shell": ["os.system"]} | ||||||||||||
|
|
||||||||||||
| result = injection_shell.start_process_with_a_shell(context, config) | ||||||||||||
|
|
||||||||||||
| self.assertIsInstance(result, bandit.Issue) | ||||||||||||
| self.assertEqual(bandit.LOW, result.severity) | ||||||||||||
|
|
||||||||||||
| # HIGH if dynamic | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| context_dynamic = self._get_context("os.system", args=[mock.Mock()]) | ||||||||||||
| arg_node_dynamic = ast.Name(id="cmd", ctx=ast.Load()) | ||||||||||||
| context_dynamic.node.args = [arg_node_dynamic] | ||||||||||||
|
|
||||||||||||
| result_dynamic = injection_shell.start_process_with_a_shell( | ||||||||||||
| context_dynamic, config | ||||||||||||
| ) | ||||||||||||
| self.assertEqual(bandit.HIGH, result_dynamic.severity) | ||||||||||||
|
|
||||||||||||
| def test_start_process_with_no_shell(self): | ||||||||||||
| # Case: os.execl(...) -> LOW severity | ||||||||||||
| context = self._get_context("os.execl") | ||||||||||||
| config = {"no_shell": ["os.execl"]} | ||||||||||||
|
|
||||||||||||
| result = injection_shell.start_process_with_no_shell(context, config) | ||||||||||||
|
|
||||||||||||
| self.assertIsInstance(result, bandit.Issue) | ||||||||||||
| self.assertEqual(bandit.LOW, result.severity) | ||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You seem to be passing in only
[mock.Mock()]this doesn't make very much sense at all.Furthermore,
call_argsandcall_keywordsare what's recorded on the mock. You shouldn't be setting these.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see you're trying to build up a https://github.com/PyCQA/bandit/blob/main/bandit/core/context.py. Why use a mock here instead of building up a real Context?