mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 06:41:23 +00:00
[flake8-bandit
] Fix truthiness: dict-only **
displays not truthy for shell
(S602
, S604
, S609
) (#20177)
## Summary Fixes #19927
This commit is contained in:
parent
cde5e4e343
commit
4c64ba4ee1
7 changed files with 42 additions and 16 deletions
|
@ -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)})
|
||||
|
|
|
@ -3,3 +3,6 @@ def foo(shell):
|
|||
|
||||
|
||||
foo(shell=True)
|
||||
|
||||
foo(shell={**{}})
|
||||
foo(shell={**{**{}}})
|
||||
|
|
|
@ -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={**{**{}}})
|
||||
|
|
|
@ -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.
|
||||
|
|
||||
|
|
|
@ -6,4 +6,6 @@ S604 Function call with `shell=True` parameter identified, security issue
|
|||
|
|
||||
5 | foo(shell=True)
|
||||
| ^^^
|
||||
6 |
|
||||
7 | foo(shell={**{}})
|
||||
|
|
||||
|
|
|
@ -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={**{}})
|
||||
|
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue