Skip to content
Open
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
562851d
Fixed all command injection vulnerabilites present in previous implem…
Feb 10, 2024
0d9a0e2
Correctly handle $IFS/${IFS} in commands. check_banned_executable can…
Feb 13, 2024
ffd0425
Tests now check err.value.args[0].startswith(<Expected Exception Mess…
Feb 15, 2024
3d3dab4
Convert to Pytest. Add shell expansion tests, nested shell syntax tes…
Feb 15, 2024
d447303
Correctly handle all shell expansions. Correctly handled deeply neste…
Feb 15, 2024
d5d9974
Convert to Pytest. Add shell expansion tests, nested shell syntax tes…
Feb 15, 2024
73d9c3c
Handle all shell redirection operators
Feb 15, 2024
6a18137
handle arithmetic expansion of bracket paramters and nested expansions
Feb 16, 2024
d22c6fb
- Complete Shell param/brace/sequence expansion for all ALLOWED_SHELL…
Feb 23, 2024
d078eb0
Remove redundant rmtree, remove script* and add time to BANNED_COMMAN…
Feb 27, 2024
be243f3
- check() now uses Popen kwargs to determine the initial env state an…
Feb 27, 2024
c735164
Remove unused os.environ import left by mistake
Feb 27, 2024
c774f1a
Add FuzzDB license.
Mar 5, 2024
ddb99ac
remove unnessary list conversion
Mar 5, 2024
de99993
- Optimized space complexity of command parsing and path resolution b…
Mar 6, 2024
8c12b66
cleanup comments
Mar 7, 2024
a8921a6
Improve readability. (Underlying check logic unchanged)
Mar 7, 2024
21b9ded
Same check logic but more verbose readable code.
Mar 7, 2024
743e313
- Generators used when resolving paths to avoid storing paths all in …
Mar 12, 2024
939293e
Fix sonarcloud Intentionality & Consistency issues from last PR
Mar 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Same check logic but more verbose readable code.
Split BANNED_COMMAND_CHAINING_ARGUMENTS from BANNED_COMMAND_CHAINING_EXECUTABLES.
Add -ok -okdir system( (for awk and similar) to BANNED_COMMAND_CHAINING_ARGUMENTS
Refactor check_path_is_chaining_executable and check_path_is_shell to path checks.
  • Loading branch information
Lucas Faudman Lucas Faudman
Lucas Faudman authored and Lucas Faudman committed Mar 7, 2024
commit 21b9deded258e12ee2c50214a52b98faf7807aa6
38 changes: 23 additions & 15 deletions src/security/safe_command/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@
BANNED_COMMAND_AND_PROCESS_SUBSTITUTION_OPERATORS = frozenset(
("$(", "`", "<(", ">("))
BANNED_COMMAND_CHAINING_EXECUTABLES = frozenset((
"eval", "exec", "-exec", "env", "source", "sudo", "su", "gosu", "sudoedit",
"eval", "exec", "env", "source", "sudo", "su", "gosu", "sudoedit",
"xargs", "awk", "perl", "python", "ruby", "php", "lua", "sqlplus",
"expect", "screen", "tmux", "byobu", "byobu-ugraph", "time",
"nohup", "at", "batch", "anacron", "cron", "crontab", "systemctl", "service", "init", "telinit",
"systemd", "systemd-run"
))
BANNED_COMMAND_CHAINING_ARGUMENTS = frozenset(("-exec", "-execdir", "-ok", "-okdir", "system("))
COMMON_SHELLS = frozenset(("sh", "bash", "zsh", "csh", "rsh", "tcsh", "tclsh", "ksh", "dash", "ash",
"jsh", "jcsh", "mksh", "wsh", "fish", "busybox", "powershell", "pwsh", "pwsh-preview", "pwsh-lts"))

Expand Down Expand Up @@ -528,7 +529,7 @@ def check(command: ValidCommand,

# venv is a copy to avoid modifying the original Popen_kwargs or None to default to using os.environ when env is not set
venv = dict(**Popen_env) if (Popen_env := Popen_kwargs.get("env")) is not None else None

# Check if the executable is set by the Popen Popen_kwargs (either executable or shell)
# Executable takes precedence over shell. see subprocess.py line 1593
executable_path = _resolve_executable_path(Popen_kwargs.get("executable"), venv)
Expand All @@ -549,28 +550,23 @@ def check(command: ValidCommand,
# Create local bools for each restriction to avoid membership check each iteration
prevent_uncommon_path_types = "PREVENT_UNCOMMON_PATH_TYPES" in restrictions
prevent_admin_owned_files = "PREVENT_ADMIN_OWNED_FILES" in restrictions

# Determine if path resolution is needed based on the restrictions
resolve_paths = (prevent_sensitive_files
or prevent_common_exploit_executables
or prevent_uncommon_path_types
or prevent_admin_owned_files)


# Then check the parsed command cmd_parts for the restrictions if the expanded command passes checks
num_resolved_paths = 0
for cmd_part in _recursive_shlex_split(expanded_command):

if prevent_command_chaining:
check_multiple_commands(cmd_part)

if not resolve_paths:
continue

for path in _resolve_paths_in_command_part(cmd_part, venv, rglob_dirs):
num_resolved_paths += 1
if max_resolved_paths >= 0 and num_resolved_paths > max_resolved_paths:
raise SecurityException(
f"Exceeded maximum number of resolved paths: {max_resolved_paths}")


if prevent_command_chaining:
check_path_is_chaining_executable(path)
check_path_is_shell(path)
if prevent_sensitive_files:
check_path_is_sensitive_file(path)
if prevent_common_exploit_executables:
Expand Down Expand Up @@ -627,12 +623,24 @@ def check_multiple_commands(cmd_part: str) -> None:
raise SecurityException(
f"Multiple commands not allowed. Process substitution operators found.")

if cmd_part.strip() in BANNED_COMMAND_CHAINING_EXECUTABLES | COMMON_SHELLS:
if cmd_part.strip() in BANNED_COMMAND_CHAINING_ARGUMENTS:
raise SecurityException(
f"Multiple commands not allowed. Executable {cmd_part} allows command chaining.")
f"Multiple commands not allowed. Argument {cmd_part} allows command chaining.")


# Path checks
def check_path_is_chaining_executable(path: Path) -> None:
if (banned_chaining_executable := path.name) in BANNED_COMMAND_CHAINING_EXECUTABLES:
raise SecurityException(
f"Multiple commands not allowed. Executable {banned_chaining_executable} allows command chaining.")


def check_path_is_shell(path: Path) -> None:
if (shell := path.name) in COMMON_SHELLS:
raise SecurityException(
f"Multiple commands not allowed. Shell {shell} allows command chaining.")


def check_path_is_sensitive_file(path: Path) -> None:
# Convert to string and check endswith so /private/etc/passwd on mac and /etc/passwd on linux are handled the same
path_string = str(path)
Expand Down