Skip to content

Commit

Permalink
Guard against empty call argument list (#1146)
Browse files Browse the repository at this point in the history
Although probably uncommon, it is possible to pass an empty list
to one of subprocess functions. If this is done, the injection_shell
plugin raises an IndexError while checking the contents of the
list argument given.

The fix is to simply check for a non-empty list. Test case was also
added.

Fixes: #1141

Signed-off-by: Eric Brown <[email protected]>
  • Loading branch information
ericwb committed Jun 12, 2024
1 parent ad56c78 commit 049eba0
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bandit/plugins/injection_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ def start_process_with_partial_path(context, config):
):
node = context.node.args[0]
# some calls take an arg list, check the first part
if isinstance(node, ast.List):
if isinstance(node, ast.List) and node.elts:
node = node.elts[0]

# make sure the param is a string literal and not a var name
Expand Down
1 change: 1 addition & 0 deletions examples/subprocess_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __len__(self):

subprocess.check_output(['/bin/ls', '-l'])
subprocess.check_output('/bin/ls -l', shell=True)
subprocess.check_output([], stdout=None)

subprocess.getoutput('/bin/ls -l')
subprocess.getstatusoutput('/bin/ls -l')
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ def test_ssl_insecure_version(self):
def test_subprocess_shell(self):
"""Test for `subprocess.Popen` with `shell=True`."""
expect = {
"SEVERITY": {"UNDEFINED": 0, "LOW": 23, "MEDIUM": 1, "HIGH": 11},
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 1, "MEDIUM": 0, "HIGH": 34},
"SEVERITY": {"UNDEFINED": 0, "LOW": 24, "MEDIUM": 1, "HIGH": 11},
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 1, "MEDIUM": 0, "HIGH": 35},
}
self.check_example("subprocess_shell.py", expect)

Expand Down

0 comments on commit 049eba0

Please sign in to comment.