[flake8-bandit] Fix truthiness: dict-only ** displays not truthy for shell (S602, S604, S609) (#20177)

## Summary
Fixes #19927
This commit is contained in:
Dan Parizher 2025-09-10 17:06:33 -04:00 committed by GitHub
parent cde5e4e343
commit 4c64ba4ee1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 42 additions and 16 deletions

View file

@ -18,3 +18,18 @@ var_string = "true"
Popen(var_string, shell=True)
Popen([var_string], shell=True)
Popen([var_string, ""], shell=True)
# Check dict display with only double-starred expressions can be falsey.
Popen("true", shell={**{}})
Popen("true", shell={**{**{}}})
# Check pattern with merged defaults/configs
class ShellConfig:
def __init__(self):
self.shell_defaults = {}
def fetch_shell_config(self, username):
return {}
def run(self, username):
Popen("true", shell={**self.shell_defaults, **self.fetch_shell_config(username)})

View file

@ -3,3 +3,6 @@ def foo(shell):
foo(shell=True)
foo(shell={**{}})
foo(shell={**{**{}}})

View file

@ -6,3 +6,6 @@ subprocess.Popen("/bin/chown root: *", shell=True)
subprocess.Popen(["/usr/local/bin/rsync", "*", "some_where:"], shell=True)
subprocess.Popen("/usr/local/bin/rsync * no_injection_here:")
os.system("tar cf foo.tar bar/*")
subprocess.Popen(["chmod", "+w", "*.py"], shell={**{}})
subprocess.Popen(["chmod", "+w", "*.py"], shell={**{**{}}})

View file

@ -124,4 +124,6 @@ S602 `subprocess` call with `shell=True` identified, security issue
19 | Popen([var_string], shell=True)
20 | Popen([var_string, ""], shell=True)
| ^^^^^
21 |
22 | # Check dict display with only double-starred expressions can be falsey.
|

View file

@ -6,4 +6,6 @@ S604 Function call with `shell=True` parameter identified, security issue
|
5 | foo(shell=True)
| ^^^
6 |
7 | foo(shell={**{}})
|

View file

@ -40,4 +40,6 @@ S609 Possible wildcard injection in call due to `*` usage
7 | subprocess.Popen("/usr/local/bin/rsync * no_injection_here:")
8 | os.system("tar cf foo.tar bar/*")
| ^^^^^^^^^^^^^^^^^^^^^^
9 |
10 | subprocess.Popen(["chmod", "+w", "*.py"], shell={**{}})
|

View file

@ -1294,15 +1294,14 @@ impl Truthiness {
return Self::Falsey;
}
if dict.items.iter().all(|item| {
matches!(
item,
DictItem {
key: None,
value: Expr::Name(..)
}
)
}) {
// If the dict consists only of double-starred items (e.g., {**x, **y}),
// consider its truthiness unknown. This matches lists/sets/tuples containing
// only starred elements, which are also Unknown.
if dict
.items
.iter()
.all(|item| matches!(item, DictItem { key: None, .. }))
{
// {**foo} / {**foo, **bar}
Self::Unknown
} else {