Skip to content

The agent that grows with you

refactor(approval): hand-compacted docstrings/comments, gathered option tables, packed scanner core

Teknium8b146bc8bd7e
diff --git a/tools/approval_detection.py b/tools/approval_detection.py
index add8c11b78b..9b98baf3792 100644
--- a/tools/approval_detection.py
+++ b/tools/approval_detection.py
@@ -1,9 +1,8 @@
"""Dangerous-command detection: normalization, tokenizing, and pattern tables.
-Pure command classification for :mod:`tools.approval` — no approval state,
-config reads, or prompting live here. ``tools.approval`` re-exports every
-public and private name so ``from tools.approval import X`` and
-``patch("tools.approval.X")`` keep working.
+Pure command classification for :mod:`tools.approval` — no approval state, config reads, or
+prompting live here. ``tools.approval`` re-exports every public and private name so
+``from tools.approval import X`` and ``patch("tools.approval.X")`` keep working.
"""
import functools
@@ -16,17 +15,16 @@ import unicodedata
logger = logging.getLogger("tools.approval")
-# Sensitive write targets, matched via ~ / $HOME / $HERMES_HOME spellings. The
-# resolved absolute home is folded into these forms at detection time by
-# _normalize_command_for_detection(), so no import-time path snapshot (which
-# would go stale when HERMES_HOME is set after import) lives in the patterns.
+# Sensitive write targets, matched via ~ / $HOME / $HERMES_HOME spellings. The resolved absolute
+# home is folded into these forms at detection time by _normalize_command_for_detection(), so no
+# import-time path snapshot (stale once HERMES_HOME is set after import) lives in the patterns.
_SSH_SENSITIVE_PATH = r'(?:~|\$home|\$\{home\})/\.ssh(?:/|$)'
_HERMES_ENV_PATH = (
r'(?:~\/\.hermes/|(?:\$home|\$\{home\})/\.hermes/|(?:\$hermes_home|\$\{hermes_home\})/)' r'\.env\b'
)
-# ~/.hermes/config.yaml IS the security policy (approvals.mode, yolo, allowlist)
-# and the config cache is mtime-keyed, so a write takes effect mid-session.
-# Terminal-side coverage (sed -i, tee, >, cp) pairs the file_tools deny.
+# ~/.hermes/config.yaml IS the security policy (approvals.mode, yolo, allowlist) and the config
+# cache is mtime-keyed, so a write takes effect mid-session. Terminal-side coverage (sed -i, tee,
+# >, cp) pairs the file_tools deny.
_HERMES_CONFIG_PATH = (
r'(?:~\/\.hermes/|(?:\$home|\$\{home\})/\.hermes/|(?:\$hermes_home|\$\{hermes_home\})/)' r'config\.yaml\b'
)
@@ -34,8 +32,8 @@ _PROJECT_ENV_PATH = r'(?:(?:/|\.{1,2}/)?(?:[^\s/"\'`]+/)*\.env(?:\.[^/\s"\'`]+)*
_PROJECT_CONFIG_PATH = r'(?:(?:/|\.{1,2}/)?(?:[^\s/"\'`]+/)*config\.yaml)'
_SHELL_RC_FILES = r'(?:~|\$home|\$\{home\})/\.' r'(?:bashrc|zshrc|profile|bash_profile|zprofile)\b'
_CREDENTIAL_FILES = r'(?:~|\$home|\$\{home\})/\.' r'(?:netrc|pgpass|npmrc|pypirc)\b'
-# macOS: /etc, /var, /tmp, /home are symlinks to /private/*, so /private/etc/sudoers
-# would bypass a plain "/etc/" check. Match both forms.
+# macOS: /etc, /var, /tmp, /home are symlinks to /private/*, so /private/etc/sudoers would bypass a plain
+# "/etc/" check. Match both forms.
_MACOS_PRIVATE_SYSTEM_PATH = r'/private/(?:etc|var|tmp|home)/'
_SYSTEM_CONFIG_PATH = rf'(?:/etc/|{_MACOS_PRIVATE_SYSTEM_PATH})'
_SENSITIVE_WRITE_TARGET = (
@@ -44,79 +42,65 @@ _SENSITIVE_WRITE_TARGET = (
)
_USER_SENSITIVE_WRITE_TARGET = rf'(?:{_SSH_SENSITIVE_PATH}|{_SHELL_RC_FILES}|{_CREDENTIAL_FILES})'
_PROJECT_SENSITIVE_WRITE_TARGET = rf'(?:{_PROJECT_ENV_PATH}|{_PROJECT_CONFIG_PATH})'
-# cp/mv/install: the sensitive path is a write target only as the LAST argument
-# (destination), so `cp config.yaml backup.yaml` (config.yaml as SOURCE) stays out.
+# cp/mv/install: the sensitive path is a write target only as the LAST argument (destination), so
+# `cp config.yaml backup.yaml` (config.yaml as SOURCE) stays out.
_COMMAND_TAIL = r'(?:\s*(?:&&|\|\||;).*)?$'
-# `>`/`>>`/tee: the path is ALWAYS a write target regardless of what follows, so
-# only require a shell word boundary (_COMMAND_TAIL let `echo x > .env extra`
-# and `echo x > .env # note` slip past). `#` is deliberately NOT a boundary: a
-# glued `#` is part of the filename (`.env#backup` is a distinct file).
+# `>`/`>>`/tee: the path is ALWAYS a write target regardless of what follows, so only require a
+# shell word boundary (_COMMAND_TAIL let `echo x > .env extra` / `echo x > .env # note` slip past).
+# `#` is deliberately NOT a boundary: a glued `#` is part of the filename (`.env#backup`).
_WRITE_TARGET_BOUNDARY = r'(?=[\s;&|<>"\']|$)'
-# =========================================================================
-# Hardline (unconditional) blocklist
-# =========================================================================
-#
-# Commands that NEVER run via the agent, regardless of --yolo, approvals.mode=off,
-# or cron approve mode — a floor below yolo. Applies only to environments that can
-# damage the host (local, ssh, container-host cron); containerized backends already
-# bypass the dangerous-command layer. Deliberately tiny: only things with no
-# recovery path (root wipe, raw block device writes, shutdown, DoS). Recoverable
-# operations (git reset --hard, chmod -R 777, curl|sh) stay in DANGEROUS_PATTERNS.
-
-# Start-of-command position: start of string, newline, subshell opener ($( or
-# backtick), optionally consuming sudo/env/exec/nohup/setsid/time wrappers. Keeps
-# shutdown/reboot rules from firing on "echo reboot" / "grep 'shutdown' log".
+# ---- Hardline (unconditional) blocklist ---------------------------------------------------
+# Commands that NEVER run via the agent, regardless of --yolo, approvals.mode=off, or cron approve
+# mode — a floor below yolo. Applies only to environments that can damage the host (local, ssh,
+# container-host cron); containerized backends already bypass the dangerous-command layer.
+# Deliberately tiny: only things with no recovery path (root wipe, raw block device writes,
+# shutdown, DoS). Recoverable operations (git reset --hard, chmod -R 777, curl|sh) stay in
+# DANGEROUS_PATTERNS.
+
+# Start-of-command position: start of string, newline, subshell opener ($( or backtick), optionally
+# consuming sudo/env/exec/nohup/setsid/time wrappers. Keeps shutdown/reboot rules from firing on
+# "echo reboot" / "grep 'shutdown' log". Real ;/&/| separators are converted to newlines by the
+# quote-aware _mark_command_starts pass; keeping them here mistakes quoted data
+# (grep '(safe|rm -rf /)') for commands.
_CMDPOS = (
- # Real ;/&/| separators are converted to newlines by the quote-aware
- # _mark_command_starts pass; keeping them here mistakes quoted data
- # (grep '(safe|rm -rf /)') for commands.
- r'(?:^|[\n`]|\$\()' # start position
- r'\s*' # optional whitespace
- r'(?:sudo\s+(?:-[^\s]+\s+)*)?' # optional sudo with flags
- r'(?:env\s+(?:\w+=\S*\s+)*)?' # optional env with VAR=VAL pairs
- r'(?:(?:exec|nohup|setsid|time)\s+)*' # optional wrapper commands
- r'\s*'
+ r'(?:^|[\n`]|\$\()' r'\s*' # start position, optional whitespace
+ r'(?:sudo\s+(?:-[^\s]+\s+)*)?' r'(?:env\s+(?:\w+=\S*\s+)*)?' # optional sudo with flags, env VAR=VAL pairs
+ r'(?:(?:exec|nohup|setsid|time)\s+)*' r'\s*' # optional wrapper commands
)
-# Destructive-path matcher for the rm hardline rules: accept the path fully
-# wrapped in a matching quote pair (`rm -rf "/"`, `rm -rf "$HOME"`) OR bare with
-# a terminator (whitespace, end, or `) ` ; | &` so `$(rm -rf /)` is caught).
-# A bare-token-only anchor lets the quoted spellings slip past the floor.
+# Destructive-path matcher for the rm hardline rules: accept the path fully wrapped in a matching
+# quote pair (`rm -rf "/"`, `rm -rf "$HOME"`) OR bare with a terminator (whitespace, end, or
+# `) ` ; | &` so `$(rm -rf /)` is caught). A bare-token-only anchor lets quoted spellings slip past the floor.
def _hardline_rm_path(path_alt: str, tail: str = r'(?:\s|$|[)`;|&])') -> str:
return rf'(?:["\'](?:{path_alt})["\']|(?:{path_alt}){tail})'
# Protected system roots whose recursive deletion has no recovery path.
-_HARDLINE_SYSTEM_DIRS = (
- r'/home|/home/\*|/root|/root/\*|/etc|/etc/\*|/usr|/usr/\*|'
- r'/var|/var/\*|/bin|/bin/\*|/sbin|/sbin/\*|/boot|/boot/\*|/lib|/lib/\*'
-)
+_HARDLINE_SYSTEM_DIRS = (r'/home|/home/\*|/root|/root/\*|/etc|/etc/\*|/usr|/usr/\*|'
+ r'/var|/var/\*|/bin|/bin/\*|/sbin|/sbin/\*|/boot|/boot/\*|/lib|/lib/\*')
-# `rm` plus flag group, shared by the rm hardline rules. Plain concatenation (not
-# f-string: backslashes in replacement fields are unsupported on the 3.11 floor).
-# _CMDPOS-anchored so `rm` must be an actual command word — the literal "rm -rf /"
-# as DATA in `git commit -m "…rm -rf /…"` must not trip the unconditional floor.
+# `rm` plus flag group, shared by the rm hardline rules (plain concatenation, not f-string:
+# backslashes in replacement fields are unsupported on the 3.11 floor). _CMDPOS-anchored so `rm`
+# must be an actual command word — "rm -rf /" as DATA in `git commit -m "…rm -rf /…"` must not trip the floor.
_RM_FLAG_PREFIX = _CMDPOS + r'rm\s+(-[^\s]*\s+)*'
HARDLINE_PATTERNS = [
- # Root path: any root-anchored path whose components collapse to "/" in the
- # shell ("/", "//", "/.", "/./", "/../..", optional trailing glob). Each
- # inter-slash segment must be exactly "." or "..", so "/tmp", "/.ssh", even
- # "/..." are literal dirs that fall through to the softer DANGEROUS rules.
- # The "/ \*" alt covers `rm -rf / *` (two args: "/" plus the glob).
+ # Root path: any root-anchored path whose components collapse to "/" in the shell ("/", "//",
+ # "/.", "/./", "/../..", optional trailing glob). Each inter-slash segment must be exactly "."
+ # or "..", so "/tmp", "/.ssh", even "/..." are literal dirs that fall through to the softer
+ # DANGEROUS rules. The "/ \*" alt covers `rm -rf / *` (two args: "/" plus the glob).
(_RM_FLAG_PREFIX + _hardline_rm_path(r'/(?:(?:\.\.?)?/)*(?:\.\.?)?\**|/ \*'), "recursive delete of root filesystem"),
(_RM_FLAG_PREFIX + _hardline_rm_path(_HARDLINE_SYSTEM_DIRS), "recursive delete of system directory"),
(_RM_FLAG_PREFIX + _hardline_rm_path(r'(?:~|\$\{?HOME\}?)(?:/?|/\*)?'), "recursive delete of home directory"),
- # Command-name rules (mkfs, dd, kill, shutdown...) are _CMDPOS-anchored so
- # quoted prose (`echo "does this use mkfs?"`) cannot trip the floor.
+ # Command-name rules (mkfs, dd, kill, shutdown...) are _CMDPOS-anchored so quoted prose
+ # (`echo "does this use mkfs?"`) cannot trip the floor.
(_CMDPOS + r'mkfs(\.[a-z0-9]+)?\b', "format filesystem (mkfs)"),
(_CMDPOS + r'dd\b[^\n]*\bof=/dev/(sd|nvme|hd|mmcblk|vd|xvd)[a-z0-9]*', "dd to raw block device"),
- # Positionless rules (no command-name token: `>` sits mid-command, the fork
- # bomb is a function definition) are matched against a QUOTE-MASKED variant
- # (_QUOTE_MASKED_HARDLINE_DESCRIPTIONS / _mask_quoted_prose) so quoted prose
- # cannot trip them, while sh -c / bash -c / eval payloads still scan raw.
+ # Positionless rules (no command-name token: `>` sits mid-command, the fork bomb is a function
+ # definition) are matched against a QUOTE-MASKED variant (_QUOTE_MASKED_HARDLINE_DESCRIPTIONS /
+ # _mask_quoted_prose) so quoted prose cannot trip them; sh -c / bash -c / eval payloads still scan raw.
(r'>\s*/dev/(sd|nvme|hd|mmcblk|vd|xvd)[a-z0-9]*\b', "redirect to raw block device"),
(r':\(\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;\s*:', "fork bomb"),
(_CMDPOS + r'kill\s+(-[^\s]+\s+)*-1\b', "kill all processes"),
@@ -126,20 +110,16 @@ HARDLINE_PATTERNS = [
(_CMDPOS + r'telinit\s+[06]\b', "telinit 0/6 (shutdown/reboot)"),
]
-# Patterns are pre-compiled at module load so the hot-path matcher never pays
-# the cold re.compile fan-out (re._cache can be evicted by unrelated regex work).
+# Pre-compiled at module load so the hot-path matcher never pays the cold re.compile fan-out
+# (re._cache can be evicted by unrelated regex work).
_RE_FLAGS = re.IGNORECASE | re.DOTALL
-
# Positionless hardline rules matched against quote-masked variants (see above).
_QUOTE_MASKED_HARDLINE_DESCRIPTIONS = frozenset({"redirect to raw block device", "fork bomb"})
-
HARDLINE_PATTERNS_COMPILED = [
(re.compile(p, _RE_FLAGS), d, d in _QUOTE_MASKED_HARDLINE_DESCRIPTIONS) for p, d in HARDLINE_PATTERNS
]
-
-
-# Commands that hand a quoted argument to another shell to EXECUTE: quoted text
-# is code, not prose, so quote-masked hardline rules scan the raw string.
+# Commands that hand a quoted argument to another shell to EXECUTE: quoted text is code, not
+# prose, so quote-masked hardline rules scan the raw string.
_SHELL_CARRIER_NAMES = frozenset({"eval", "sh", "bash", "zsh", "ksh", "dash", "source", "."})
@@ -152,33 +132,25 @@ def _contains_shell_carrier(command: str) -> bool:
def _mask_quoted_prose(command: str) -> str:
- """Blank out quoted string CONTENT for positionless hardline matching.
-
- Detection-only. Quote characters stay; inside double quotes `$(...)` and
- backtick spans are kept RAW because the shell really executes them. An
- unclosed quote masks to end-of-string, which cannot hide a runnable command
- (the shell would not run it either).
- """
+ """Blank out quoted string CONTENT for positionless hardline matching (detection-only).
+ Quote characters stay; inside double quotes `$(...)` and backtick spans are kept RAW because
+ the shell really executes them. An unclosed quote masks to end-of-string, which cannot hide a
+ runnable command (the shell would not run it either)."""
return "".join(
command[i:j] if quote is None or kind in ("quote", "subst") else " " * (j - i)
for kind, i, j, quote in _scan_shell(command, subst="q", naive_backtick=True)
)
-# =========================================================================
-# Sudo stdin guard — block password guessing via "sudo -S"
-# =========================================================================
-# Without SUDO_PASSWORD configured, an explicit "sudo -S" is the LLM piping a
-# guessed password via stdin (brute-force vector). Unconditional block.
+# ---- Sudo stdin guard: without SUDO_PASSWORD configured, an explicit "sudo -S" is the LLM piping
+# a guessed password via stdin (brute-force vector). Unconditional block.
_SUDO_STDIN_RE = re.compile(r'(?:^|[;&|`\n]|&&|\|\||\$\()\s*sudo\s+-S\b', re.IGNORECASE)
def _check_sudo_stdin_guard(command: str) -> tuple:
- """Detect ``sudo -S`` without configured SUDO_PASSWORD -> (is_blocked, description).
-
- When SUDO_PASSWORD is set, ``_transform_sudo_command`` injects ``-S`` itself,
- so this guard only fires when the LLM wrote it explicitly.
- """
+ """Detect ``sudo -S`` without configured SUDO_PASSWORD -> (is_blocked, description). When
+ SUDO_PASSWORD is set, ``_transform_sudo_command`` injects ``-S`` itself, so this guard only
+ fires when the LLM wrote it explicitly."""
if "SUDO_PASSWORD" not in os.environ and _SUDO_STDIN_RE.search(_normalize_command_for_detection(command).lower()):
return (True, "sudo password guessing via stdin (sudo -S)")
return (False, None)
@@ -197,10 +169,9 @@ def detect_hardline_command(command: str) -> tuple:
masked_lower: str | None = None
for pattern_re, description, quote_masked in HARDLINE_PATTERNS_COMPILED:
if quote_masked and masked_lower is None:
- # Positionless rules see quoted prose as DATA, except under
- # shell carriers (sh -c, eval, source) whose quoted argument is
- # code — those scan raw. bash -c payloads also surface as their
- # own raw variants via _execution_flag_findings.
+ # Positionless rules see quoted prose as DATA, except under shell carriers
+ # (sh -c, eval, source) whose quoted argument is code — those scan raw. bash -c
+ # payloads also surface as their own raw variants via _execution_flag_findings.
masked_lower = (
variant_lower if _contains_shell_carrier(command_variant)
else _mask_quoted_prose(command_variant).lower()
@@ -210,39 +181,31 @@ def detect_hardline_command(command: str) -> tuple:
return (False, None)
-# =========================================================================
-# Dangerous command patterns
-# =========================================================================
-
+# ---- Dangerous command patterns -----------------------------------------------------------
DANGEROUS_PATTERNS = [
(r'\brm\s+(-[^\s]*\s+)*/', "delete in root path"),
(r'\brm\s+-[^\s]*r', "recursive delete"),
(r'\brm\s+--recursive\b', "recursive delete (long flag)"),
- # GNU rm permutes options, so flags may FOLLOW operands (`rm build/ -rf`).
- # The operand run cannot cross a command separator (so `rm foo | grep -r`
- # is not attributed to rm), a quote, or a bare ` -- ` end-of-options (after
- # which `-rf` is a literal filename). The flag token must follow whitespace
- # so the `r` in long options like `--registry` does not count.
+ # GNU rm permutes options, so flags may FOLLOW operands (`rm build/ -rf`). The operand run
+ # cannot cross a command separator (so `rm foo | grep -r` is not attributed to rm), a quote,
+ # or a bare ` -- ` end-of-options (after which `-rf` is a literal filename). The flag token
+ # must follow whitespace so the `r` in long options like `--registry` does not count.
(r'\brm\s+(?!--(?:\s|$))(?:(?!\s--(?:\s|$))[^\n"\';|&])*\s' r'(?:-[a-z]*r[a-z]*\b|--recursive\b)',
"recursive delete (flags after operands)"),
- # Windows cmd/powershell destructive built-ins: gate only when executed
- # through the shell so prose/filenames containing "del"/"rd" do not trip.
+ # Windows cmd/powershell destructive built-ins: gate only when executed through the shell so
+ # prose/filenames containing "del"/"rd" do not trip.
(r'\bcmd(?:\.exe)?\s+/(?:c|k)\s+.*\b(?:del|erase|rd|rmdir)\b', "Windows cmd destructive delete"),
- # PowerShell runs the verb as default positional arg (no -Command needed);
- # anchor the verb to command position (after leading -Flag switches and
- # optional -Command/-c) so `-File c:\del-logs\run.ps1` is not caught.
+ # PowerShell runs the verb as default positional arg (no -Command needed); anchor the verb to command
+ # position (after leading -Flag switches and optional -Command/-c) so `-File c:\del-logs\run.ps1` is not caught.
(r'\b(?:powershell|pwsh)(?:\.exe)?\b(?:\s+-\S+)*\s+(?:-(?:command|c)\s+)?["\']?(?:remove-item|rmdir|erase|del|rd|ri|rm)\b', "Windows PowerShell destructive delete"),
(r'\b(?:powershell|pwsh)(?:\.exe)?\b.*\s-(?:encodedcommand|enc|e)\b', "PowerShell encoded command execution"),
- # ── Windows destructive tier ─────────────────────────────────────────
- # Native Windows EXEs/cmdlets reachable from ANY backend on a Windows host
- # (incl. git-bash). Input is lowercased by the variant loop, so patterns are
- # lowercase. Each requires the destructive flag/verb so benign usage
- # (`taskkill /IM app.exe`, `reg query`, `icacls file`) does NOT prompt.
- # Bare Remove-Item form (ACP clients, pwsh-default SSH hosts, or compound
- # commands where `powershell` appeared earlier).
+ # ── Windows destructive tier: native Windows EXEs/cmdlets reachable from ANY backend on a
+ # Windows host (incl. git-bash). Input is lowercased by the variant loop, so patterns are
+ # lowercase. Each requires the destructive flag/verb so benign usage (`taskkill /IM app.exe`,
+ # `reg query`, `icacls file`) does NOT prompt. Bare Remove-Item form (ACP clients, pwsh-default
+ # SSH hosts, or compound commands where `powershell` appeared earlier).
(r'\bremove-item\b[^\n;|&]*\s-(?:recurse|force)\b', "PowerShell destructive delete (Remove-Item)"),
- # Bare cmd builtins with /s (recurse) or /q (quiet); plain `del file.txt`
- # is covered only by the prefixed rule.
+ # Bare cmd builtins with /s (recurse) or /q (quiet); plain `del file.txt` is covered only by the prefixed rule.
(r'\b(?:del|erase|rd|rmdir)\s+(?:/[a-z]\s+)*/[sq]\b', "Windows destructive delete (recursive/quiet switch)"),
# Remote content piped to Invoke-Expression — PowerShell's `curl | sh`.
(r'\b(?:iwr|invoke-webrequest|invoke-restmethod|irm|curl|wget)\b[^\n]*\|\s*(?:iex|invoke-expression)\b', "pipe remote content to PowerShell (iwr | iex)"),
@@ -269,43 +232,41 @@ DANGEROUS_PATTERNS = [
# Windows service/system stop — analogue of systemctl stop.
(r'\bstop-service\b[^\n]*\s-force\b', "force stop service (Stop-Service -Force)"),
(r'\bsc(?:\.exe)?\s+(?:stop|delete)\b', "stop/delete service (sc)"),
- # Windows-form credential paths; the POSIX ~/.ssh patterns never match
- # drive-letter or backslash spellings.
+ # Windows-form credential paths; the POSIX ~/.ssh patterns never match drive-letter or backslash spellings.
(r'\busers[\\/][^\\/\s]+[\\/]\.ssh\b', "access to SSH keys (Windows path)"),
(r'\bappdata[\\/](?:local|roaming)[\\/]hermes[^\n]*\.env\b', "access to Hermes secrets (Windows path)"),
- # ─────────────────────────────────────────────────────────────────────
+ # ── end of Windows tier
(r'\bchmod\s+(-[^\s]*\s+)*(777|666|o\+[rwx]*w|a\+[rwx]*w)\b', "world/other-writable permissions"),
(r'\bchmod\s+--recursive\b.*(777|666|o\+[rwx]*w|a\+[rwx]*w)', "recursive world/other-writable (long flag)"),
(r'\bchown\s+(-[^\s]*)?R\s+root', "recursive chown to root"),
(r'\bchown\s+--recur[a-z]*\b.*root', "recursive chown to root (long flag)"),
- # _CMDPOS-anchored like the hardline twins: quoted prose mentioning
- # mkfs/dd must not require approval to echo.
+ # _CMDPOS-anchored like the hardline twins: quoted prose mentioning mkfs/dd must not require approval to echo.
(_CMDPOS + r'mkfs\b', "format filesystem"),
(_CMDPOS + r'dd\s+.*if=', "disk copy"),
(r'>\s*/dev/sd', "write to block device"),
(r'\bDROP\s+(TABLE|DATABASE)\b', "SQL DROP"),
- # [^\n]* not .*: under DOTALL a WHERE on the *next* line would satisfy the
- # lookahead and silently allow DELETE without WHERE.
+ # [^\n]* not .*: under DOTALL a WHERE on the *next* line would satisfy the lookahead and
+ # silently allow DELETE without WHERE.
(r'\bDELETE\s+FROM\b(?![^\n]*\bWHERE\b)', "SQL DELETE without WHERE"),
(r'\bTRUNCATE\s+(TABLE)?\s*\w', "SQL TRUNCATE"),
(rf'>\s*{_SYSTEM_CONFIG_PATH}', "overwrite system config"),
(r'\bsystemctl\s+(-[^\s]+\s+)*(stop|restart|disable|mask)\b', "stop/restart system service"),
(r'\bkill\s+-9\s+-1\b', "kill all processes"),
(r'\bpkill\s+-9\b', "force kill processes"),
- # killall with SIGKILL (-9 / -KILL / -s KILL / -SIGKILL) and `killall -r
- # <regex>` broad sweeps that can wipe unrelated processes.
+ # killall with SIGKILL (-9 / -KILL / -s KILL / -SIGKILL) and `killall -r <regex>` broad sweeps
+ # that can wipe unrelated processes.
(r'\bkillall\s+(-[^\s]*\s+)*-(9|KILL|SIGKILL)\b', "force kill processes (killall -KILL)"),
(r'\bkillall\s+(-[^\s]*\s+)*-s\s+(KILL|SIGKILL|9)\b', "force kill processes (killall -s KILL)"),
(r'\bkillall\s+(-[^\s]*\s+)*-r\b', "kill processes by regex (killall -r)"),
(r':\(\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;\s*:', "fork bomb"),
- # Shell -c is parsed structurally by _execution_flag_findings(); a regex
- # searching a dash-token for "c" also matched --norc/--rcfile/--restricted.
+ # Shell -c is parsed structurally by _execution_flag_findings(); a regex searching a dash-token
+ # for "c" also matched --norc/--rcfile/--restricted.
(r'\b(curl|wget)\b.*\|\s*(?:[/\w]*/)?(?:ba)?sh(?:\s|$|-c)', "pipe remote content to shell"),
(r'\b(bash|sh|zsh|ksh)\s+<\s*<?\s*\(\s*(curl|wget)\b', "execute remote script via process substitution"),
# eval/source/. $(curl ...) — equivalent to piping remote content to a shell.
(r'(?:\beval\b|\bsource\b|\.)\s*(?:\$\(\s*|`\s*)(?:curl|wget)\b', "execute remote content via command substitution"),
- # Decode-and-execute: `echo <base64> | base64 -d | bash` carries no dangerous
- # keywords in the raw text yet runs arbitrary commands.
+ # Decode-and-execute: `echo <base64> | base64 -d | bash` carries no dangerous keywords in the
+ # raw text yet runs arbitrary commands.
(r'\b(base64|base32|base16)\s+(?:-[dD]|--decode)\b.*\|\s*\b(bash|sh|zsh|ksh|dash)\b', "pipe decoded content to shell (possible command obfuscation)"),
# xxd uses -r for decode, not -d.
(r'\bxxd\s+-r\b.*\|\s*\b(bash|sh|zsh|ksh|dash)\b', "pipe xxd-decoded content to shell (possible command obfuscation)"),
@@ -321,27 +282,25 @@ DANGEROUS_PATTERNS = [
# -execdir has the same semantics as -exec (runs in each match's directory).
(r'\bfind\b.*-exec(?:dir)?\s+(/\S*/)?rm\b', "find -exec/-execdir rm"),
(r'\bfind\b.*-delete\b', "find -delete"),
- # Gateway lifecycle: stopping/restarting the gateway kills all running
- # agents. Global flags between `hermes` and `gateway` (`hermes -p ade
- # gateway restart`) are allowed so a profile flag can't slip past.
+ # Gateway lifecycle: stopping/restarting the gateway kills all running agents. Global flags
+ # between `hermes` and `gateway` (`hermes -p ade gateway restart`) are allowed so a profile flag can't slip past.
(r'\bhermes\s+(?:-{1,2}\S+(?:\s+\S+)?\s+)*gateway\s+(stop|restart)\b', "stop/restart hermes gateway (kills running agents)"),
(r'\bhermes\s+update\b', "hermes update (restarts gateway, kills running agents)"),
- # Docker/Podman daemon redirect — global flags or env that point the CLI at
- # a DIFFERENT (often remote) daemon: `docker -H ssh://prod stop app` looks
- # local but operates on remote infra, so any redirect requires approval
- # regardless of subcommand. The flag must be in global position (before the
- # subcommand) and -H/--host/--context must carry a value, keeping `docker -h`
- # and `docker run -h <hostname>` out. Listed BEFORE the lifecycle rules so a
- # redirected lifecycle command surfaces the more specific reason.
+ # Docker/Podman daemon redirect — global flags or env that point the CLI at a DIFFERENT (often
+ # remote) daemon: `docker -H ssh://prod stop app` looks local but operates on remote infra, so
+ # any redirect requires approval regardless of subcommand. The flag must be in global position
+ # (before the subcommand) and -H/--host/--context must carry a value, keeping `docker -h` and
+ # `docker run -h <hostname>` out. Listed BEFORE the lifecycle rules so a redirected lifecycle
+ # command surfaces the more specific reason.
(r'\bdocker\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(?:-h|--host)[=\s]+\S+', "docker with remote daemon redirect (-H/--host)"),
(r'\bdocker\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(?:-c|--context)[=\s]+\S+', "docker with daemon redirect (--context: alternate daemon)"),
(r'\bdocker\s+context\s+use\b', "docker context use (switches default daemon for future commands)"),
(r'\bpodman\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(?:--url|--connection|--identity)[=\s]+\S+', "podman with remote daemon redirect (--url/--connection/--identity)"),
(r'\bpodman\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(?:-r\b|--remote\b)', "podman remote mode (-r/--remote: remote daemon)"),
(r'\b(?:docker_host|docker_context|container_host|container_connection)=\S+', "docker/podman daemon redirect via environment (DOCKER_HOST/CONTAINER_HOST)"),
- # Container lifecycle (docker.sock mounts let the agent stop/kill containers)
- # always needs consent. Global flags between docker/compose and the verb and
- # the legacy `docker-compose` binary are allowed so a flag can't slip past.
+ # Container lifecycle (docker.sock mounts let the agent stop/kill containers) always needs
+ # consent. Global flags between docker/compose and the verb and the legacy `docker-compose`
+ # binary are allowed so a flag can't slip past.
(r'\bdocker(?:-compose|\s+compose)\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(restart|stop|kill|down)\b', "docker compose restart/stop/kill/down (container lifecycle)"),
(r'\bdocker\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(restart|stop|kill)\b', "docker restart/stop/kill (container lifecycle)"),
# Gateway protection: never start gateway outside systemd management
@@ -349,68 +308,63 @@ DANGEROUS_PATTERNS = [
(r'\bnohup\b.*gateway\s+run\b', "start gateway outside systemd (use 'systemctl --user restart hermes-gateway')"),
# Self-termination protection: prevent agent from killing its own process
(r'\b(pkill|killall)\b.*\b(hermes|gateway|cli\.py)\b', "kill hermes/gateway process (self-termination)"),
- # Self-termination via kill + $(pgrep/pidof): the substitution is opaque to
- # the name-based pattern above, so catch the structural form.
+ # Self-termination via kill + $(pgrep/pidof): the substitution is opaque to the name-based
+ # pattern above, so catch the structural form.
(r'\bkill\b.*\$\(\s*(pgrep|pidof)\b', "kill process via pgrep/pidof expansion (self-termination)"),
(r'\bkill\b.*`\s*(pgrep|pidof)\b', "kill process via backtick pgrep/pidof expansion (self-termination)"),
- # launchctl-driven gateway stop/restart on macOS (label `ai.hermes.gateway`).
- # Two independent lookaheads, NOT a sequential match: a for-loop building
- # the label from a list defined EARLIER (`for item in 'ai.hermes...'; do
- # launchctl bootout "$label"`) never has "hermes" after the verb, and that
- # slipped past and restarted 4 gateways with zero approval. Erring broad
- # is correct for an approval gate: an extra prompt is cheap.
+ # launchctl-driven gateway stop/restart on macOS (label `ai.hermes.gateway`). Two independent
+ # lookaheads, NOT a sequential match: a for-loop building the label from a list defined EARLIER
+ # (`for item in 'ai.hermes...'; do launchctl bootout "$label"`) never has "hermes" after the
+ # verb, and that slipped past and restarted 4 gateways with zero approval. Erring broad is
+ # correct for an approval gate: an extra prompt is cheap.
(r'(?=[\s\S]*\blaunchctl\s+(?:stop|kickstart|bootout|unload|kill|disable|remove)\b)(?=[\s\S]*\b(?:hermes|ai\.hermes)\b)', "stop/restart hermes launchd service (kills running agents)"),
(rf'\b(cp|mv|install)\b.*\s{_SYSTEM_CONFIG_PATH}', "copy/move file into system config path"),
(rf'\b(cp|mv|install)\b.*\s["\']?{_PROJECT_SENSITIVE_WRITE_TARGET}["\']?{_COMMAND_TAIL}', "overwrite project env/config file"),
- # cp/mv/install OVERWRITING a credential/SSH/shell-rc/Hermes file (key
- # implant, login-time injection) — pairs the tee/redirection coverage.
- # Anchored to the command tail so only the DESTINATION fires; reading OUT
- # of a sensitive path (`cp ~/.ssh/config /tmp/x`) stays safe. The trailing
- # `[^\s"\']*` consumes the rest of the destination filename.
+ # cp/mv/install OVERWRITING a credential/SSH/shell-rc/Hermes file (key implant, login-time
+ # injection) — pairs the tee/redirection coverage. Anchored to the command tail so only the
+ # DESTINATION fires; reading OUT of a sensitive path (`cp ~/.ssh/config /tmp/x`) stays safe.
+ # The trailing `[^\s"\']*` consumes the rest of the destination filename.
(rf'\b(cp|mv|install)\b.*\s["\']?{_SENSITIVE_WRITE_TARGET}[^\s"\']*["\']?{_COMMAND_TAIL}', "copy/move file into sensitive credential/SSH/shell-rc path"),
- # In-place edits mutate the file directly, bypassing redirection/tee/cp
- # coverage; gate the same startup/credential files.
+ # In-place edits mutate the file directly, bypassing redirection/tee/cp coverage; gate the same
+ # startup/credential files.
(rf'\bsed\s+-[^\s]*i.*(?:{_USER_SENSITIVE_WRITE_TARGET})[^\s"\']*', "in-place edit of sensitive credential/SSH/shell-rc path"),
(rf'\bsed\s+--in-place\b.*(?:{_USER_SENSITIVE_WRITE_TARGET})[^\s"\']*', "in-place edit of sensitive credential/SSH/shell-rc path (long flag)"),
(rf'\b(?:perl|ruby)\b.*(?:^|\s)-[^\s]*i\b.*(?:{_USER_SENSITIVE_WRITE_TARGET})[^\s"\']*', "in-place edit of sensitive credential/SSH/shell-rc path (perl/ruby)"),
(rf'\bsed\s+-[^\s]*i.*\s{_SYSTEM_CONFIG_PATH}', "in-place edit of system config"),
(rf'\bsed\s+--in-place\b.*\s{_SYSTEM_CONFIG_PATH}', "in-place edit of system config (long flag)"),
- # sed -i on Hermes config/.env bypasses the redirection/tee rules; pairs the
- # file_tools write_file/patch deny so the terminal side is not an open door.
+ # sed -i on Hermes config/.env bypasses the redirection/tee rules; pairs the file_tools
+ # write_file/patch deny so the terminal side is not an open door.
(rf'\bsed\s+-[^\s]*i.*(?:{_HERMES_CONFIG_PATH}|{_HERMES_ENV_PATH})', "in-place edit of Hermes config/env"),
(rf'\bsed\s+--in-place\b.*(?:{_HERMES_CONFIG_PATH}|{_HERMES_ENV_PATH})', "in-place edit of Hermes config/env (long flag)"),
- # perl/ruby -i: the flag may be its own token after other flags (`-p -i -e`),
- # combined (`-pi`), or carry a backup suffix (`-i.bak`), so match any flag
- # token containing `i` anywhere; `perl -e '...'` (no -i) does not trip.
+ # perl/ruby -i: the flag may be its own token after other flags (`-p -i -e`), combined (`-pi`),
+ # or carry a backup suffix (`-i.bak`), so match any flag token containing `i` anywhere;
+ # `perl -e '...'` (no -i) does not trip.
(rf'\b(?:perl|ruby)\b.*(?:^|\s)-[^\s]*i\b.*(?:{_HERMES_CONFIG_PATH}|{_HERMES_ENV_PATH})', "in-place edit of Hermes config/env (perl/ruby)"),
- # Interpreter heredocs are handled by _execution_flag_findings(); only shell
- # heredocs stay regex-based. `bash <<'EOF'` runs arbitrary commands without
- # triggering the `bash -c` path.
+ # Interpreter heredocs are handled by _execution_flag_findings(); only shell heredocs stay
+ # regex-based. `bash <<'EOF'` runs arbitrary commands without triggering the `bash -c` path.
(r'\b(bash|sh|zsh|ksh)\s+<<', "shell execution via heredoc"),
- # Git destructive operations. `git reset --hard` accepts any unambiguous
- # long-flag prefix (--h, --ha, --har): --hard is the only reset mode starting
- # with "h", and `--help` is special-cased by git before mode resolution.
+ # Git destructive operations. `git reset --hard` accepts any unambiguous long-flag prefix (--h,
+ # --ha, --har): --hard is the only reset mode starting with "h", and `--help` is special-cased
+ # by git before mode resolution.
(r'\bgit\s+reset\s+--h(?:a(?:r(?:d)?)?)?\b', "git reset --hard (destroys uncommitted changes)"),
(r'\bgit\s+push\b.*--forc[a-z]*\b', "git force push (rewrites remote history)"),
(r'\bgit\s+push\b.*-f\b', "git force push short flag (rewrites remote history)"),
(r'\bgit\s+clean\s+-[^\s]*f', "git clean with force (deletes untracked files)"),
(r'\bgit\s+branch\s+-D\b', "git branch force delete"),
- # `-D` = `-d --force`; the long spellings are different tokens, so match
- # delete+force in either order, bounded to one command segment (no
- # `;`/`|`/`&`/newline) so an unrelated later command isn't contaminated.
+ # `-D` = `-d --force`; the long spellings are different tokens, so match delete+force in either
+ # order, bounded to one command segment (no `;`/`|`/`&`/newline) so an unrelated later command
+ # isn't contaminated.
(r'\bgit\s+branch\b[^;|&\n]*?(?:-d\b|--delete\b)[^;|&\n]*?(?:-f\b|--force\b)', "git branch force delete (long flags)"),
(r'\bgit\s+branch\b[^;|&\n]*?(?:-f\b|--force\b)[^;|&\n]*?(?:-d\b|--delete\b)', "git branch force delete (long flags, force-first)"),
- # chmod +x then immediate run: the script content may hold dangerous
- # commands individual patterns miss.
+ # chmod +x then immediate run: the script content may hold dangerous commands individual patterns miss.
(r'\bchmod\s+\+x\b.*[;&|]+\s*\./', "chmod +x followed by immediate execution"),
- # Sudo stdin/askpass/shell/list-privs flags. The agent has no TTY, so sudo
- # invocations that succeed non-interactively read the password from stdin
- # (-S) or askpass (-A); -s (shell) and -a (list) are gated as privilege
- # chains (read SUDO_PASSWORD from .env -> sudo -S -s). Plain `sudo cmd` is
- # TTY-bound and excluded. Input is lowercased, so S/s and A/a collapse.
- # Lazy `[^;|&\n]*?` allows flag args without spanning separators. sudo
- # resolves unambiguous long-flag prefixes: `--stdin` is the only long option
- # starting with "st", `--askpass` the only one starting with "a".
+ # Sudo stdin/askpass/shell/list-privs flags. The agent has no TTY, so sudo invocations that
+ # succeed non-interactively read the password from stdin (-S) or askpass (-A); -s (shell) and
+ # -a (list) are gated as privilege chains (read SUDO_PASSWORD from .env -> sudo -S -s). Plain
+ # `sudo cmd` is TTY-bound and excluded. Input is lowercased, so S/s and A/a collapse. Lazy
+ # `[^;|&\n]*?` allows flag args without spanning separators. sudo resolves unambiguous
+ # long-flag prefixes: `--stdin` is the only long option starting with "st", `--askpass` the
+ # only one starting with "a".
(r'\bsudo\b[^;|&\n]*?\s+(?:-s\b|--st[a-z]*\b|-a\b|--a[a-z]*\b)', "sudo with privilege flag (stdin/askpass/shell/list)"),
# Combined short-flag form (-nS, -sa, -las).
(r'\bsudo\b[^;|&\n]*?\s+-[a-z]*[sa][a-z]*\b', "sudo with combined-flag privilege escalation"),
@@ -424,8 +378,8 @@ _REMOVED_PATTERN_KEY_ALIASES = {
"script execution via -e/-c flag": "(python[23]?|perl|ruby|node)\\s+-[ec]\\s+",
"script execution via heredoc": "(python[23]?|perl|ruby|node)\\s+<<",
}
-# description <-> legacy regex-derived key (the old approval key, kept for
-# backwards compatibility with stored allowlist/session entries), both ways.
+# description <-> legacy regex-derived key (the old approval key, kept for backwards compatibility
+# with stored allowlist/session entries), both ways.
_PATTERN_KEY_ALIASES: dict[str, set[str]] = {}
for _canonical_key, _legacy_key in [
(d, p.split(r'\b')[1] if r'\b' in p else p[:20]) for p, d in DANGEROUS_PATTERNS
@@ -435,44 +389,35 @@ for _canonical_key, _legacy_key in [
def _approval_key_aliases(pattern_key: str) -> set[str]:
- """Return all approval keys matching this pattern: the description plus the
- historical regex-derived key older allowlist/session entries may still use."""
+ """All approval keys for this pattern: the description plus the historical regex-derived key
+ older allowlist/session entries may still use."""
return _PATTERN_KEY_ALIASES.get(pattern_key, {pattern_key})
-# =========================================================================
-# Detection
-# =========================================================================
-
+# ---- Detection ----------------------------------------------------------------------------
def _normalize_command_for_detection(command: str) -> str:
- """Normalize a command before pattern matching so ANSI escapes, null bytes,
- Unicode fullwidth forms, and shell splicing tricks cannot bypass detection."""
+ """Normalize a command before pattern matching so ANSI escapes, null bytes, Unicode fullwidth
+ forms, and shell splicing tricks cannot bypass detection."""
from tools.ansi_strip import strip_ansi
-
- command = strip_ansi(command)
- command = command.replace('\x00', '')
- command = unicodedata.normalize('NFKC', command)
- # Collapse backslash-newline continuations (`rm -rf \<newline>/` runs as
- # `rm -rf /`). MUST precede the generic escape strip below, whose [^\n]
- # class skips newlines and would leave the backslash wedged between tokens,
- # defeating the structured rm/mkfs/dd patterns incl. the HARDLINE floor.
+ command = unicodedata.normalize('NFKC', strip_ansi(command).replace('\x00', ''))
+ # Collapse backslash-newline continuations (`rm -rf \<newline>/` runs as `rm -rf /`). MUST
+ # precede the generic escape strip below, whose [^\n] class skips newlines and would leave the
+ # backslash wedged between tokens, defeating the structured rm/mkfs/dd patterns incl. the HARDLINE floor.
command = re.sub(r'\\\r?\n', '', command)
- # Fold absolute user/Hermes home prefixes to ~/ and ~/.hermes/ so the static
- # patterns catch /home/alice/.bashrc and C:\Users\alice\.bashrc. Resolved at
- # detection time (not import time) so it tracks HOME/HERMES_HOME set later.
- # MUST run before the backslash strip (which would dissolve C:\Users\alice
- # to C:Usersalice). Hermes home first: on Windows it nests under the user
- # home, and folding the user home first would eat the prefix it needs.
+ # Fold absolute user/Hermes home prefixes to ~/ and ~/.hermes/ so the static patterns catch
+ # /home/alice/.bashrc and C:\Users\alice\.bashrc. Resolved at detection time (not import time)
+ # so it tracks HOME/HERMES_HOME set later. MUST run before the backslash strip (which would
+ # dissolve C:\Users\alice to C:Usersalice). Hermes home first: on Windows it nests under the
+ # user home, and folding the user home first would eat the prefix it needs.
command = _rewrite_resolved_hermes_home(command)
command = _rewrite_resolved_user_home(command)
# Strip backslash-escapes (r\m -> rm) and empty-string literals (r''m -> rm).
command = re.sub(r'\\([^\n])', r'\1', command)
command = re.sub(r"''|\"\"", '', command)
- # Collapse $IFS / ${IFS...} (incl. `${IFS:0:1}`) to a space: IFS defaults to
- # whitespace, so `rm${IFS}-rf${IFS}/` runs as `rm -rf /`, and every pattern —
- # including the hardline floor — anchors on literal \s between tokens.
- command = re.sub(r'\$\{IFS\b[^}]*\}|\$IFS\b', ' ', command)
- return command
+ # Collapse $IFS / ${IFS...} (incl. `${IFS:0:1}`) to a space: IFS defaults to whitespace, so
+ # `rm${IFS}-rf${IFS}/` runs as `rm -rf /`, and every pattern — incl. the hardline floor —
+ # anchors on literal \s between tokens.
+ return re.sub(r'\$\{IFS\b[^}]*\}|\$IFS\b', ' ', command)
# Shell metacharacters, quotes, and whitespace that terminate a path token.
@@ -483,13 +428,10 @@ _PATH_TAIL = r"(?P<tail>(?:[/\\][^/\\" + _PATH_TOKEN_STOP + r"]*)+)"
@functools.lru_cache(maxsize=64)
def _home_prefix_fold_regex(path: str):
"""Compile a regex matching *path* as an absolute directory prefix.
-
- Components match with either separator so native Windows, forward-slash,
- and mixed forms all fold; the caller normalizes the tail's backslashes to
- ``/``. A non-empty tail is required, so a bare home is never folded.
- Returns ``None`` for an unset/degenerate path (fewer than two components:
- ``/``, ``C:\\``, ``""``) so a stray HOME cannot rewrite unrelated prefixes.
- """
+ Components match with either separator so native Windows, forward-slash, and mixed forms all
+ fold; the caller normalizes the tail's backslashes to ``/``. A non-empty tail is required, so a
+ bare home is never folded. Returns ``None`` for an unset/degenerate path (fewer than two
+ components: ``/``, ``C:\\``, ``""``) so a stray HOME cannot rewrite unrelated prefixes."""
components = [c for c in re.split(r"[/\\]+", path) if c] if path else []
if len(components) < 2:
return None
@@ -498,9 +440,8 @@ def _home_prefix_fold_regex(path: str):
def _fold_home_prefixes(command: str, paths, replacement: str) -> str:
- """Fold each resolved home prefix in *command* to *replacement* (no trailing
- separator; the tail supplies it). Longest first so a deeper home folds
- before a shorter overlapping one that would otherwise clobber it."""
+ """Fold each resolved home prefix in *command* to *replacement* (no trailing separator; the tail
+ supplies it). Longest first so a deeper home folds before a shorter overlapping one that would clobber it."""
for path in dict.fromkeys(sorted((p for p in paths if p), key=len, reverse=True)):
pattern = _home_prefix_fold_regex(path)
if pattern is not None:
@@ -509,11 +450,9 @@ def _fold_home_prefixes(command: str, paths, replacement: str) -> str:
def _rewrite_resolved_user_home(command: str) -> str:
- """User home (expanduser / realpath / $HOME) -> ``~/``; no-op when the home
- is unset, degenerate, or unresolvable."""
+ """User home (expanduser / realpath / $HOME) -> ``~/``; no-op when unset, degenerate, or unresolvable."""
try:
- # expanduser, realpath, and an explicit HOME — Windows expanduser uses
- # USERPROFILE and ignores HOME.
+ # expanduser, realpath, and an explicit HOME — Windows expanduser uses USERPROFILE, not HOME.
home = os.path.expanduser("~")
paths = [home, os.path.realpath(home), os.environ.get("HOME", "")]
except Exception:
@@ -522,9 +461,8 @@ def _rewrite_resolved_user_home(command: str) -> str:
def _rewrite_resolved_hermes_home(command: str) -> str:
- """Resolved HERMES_HOME (and its realpath) -> ``~/.hermes/`` so the
- _HERMES_CONFIG_PATH / _HERMES_ENV_PATH rules match Docker/gateway
- deployments that spell the absolute path."""
+ """Resolved HERMES_HOME (and its realpath) -> ``~/.hermes/`` so the _HERMES_CONFIG_PATH /
+ _HERMES_ENV_PATH rules match Docker/gateway deployments that spell the absolute path."""
try:
from hermes_constants import get_hermes_home
home = get_hermes_home().expanduser()
@@ -541,6 +479,11 @@ _ENV_ASSIGNMENT_RE = re.compile(r"[A-Za-z_][A-Za-z0-9_]*=.*")
_COMMAND_WRAPPER_WORDS = {"sudo", "env", "exec", "nohup", "setsid", "time", "command", "builtin"}
_SUDO_OPTIONS_WITH_ARG = {"-c", "--close-from", "-g", "--group", "-h", "--host", "-p", "--prompt", "-u", "--user"}
+_INTERPRETER_NAME_RES = tuple((family, re.compile(pattern)) for family, pattern in (
+ ("python", r"py(?:\.exe)?|python[23]?(?:\.\d+)*(?:\.exe)?"), ("node", r"node(?:js)?(?:\.exe)?"),
+ ("perl", r"perl[0-9]*(?:\.\d+)*(?:\.exe)?"), ("ruby", r"ruby[0-9.]*(?:\.exe)?"), ("php", r"php(?:\.exe)?"),
+ ("powershell", r"powershell(?:\.exe)?|pwsh(?:\.exe)?"),
+))
_INTERPRETER_EXEC_FLAGS = {
"python": {"-c"}, "node": {"-e", "--eval", "-p", "--print"}, "perl": {"-e", "--eval"}, "ruby": {"-e"},
"php": {"-r"}, "powershell": {"-command", "-c", "-file", "-f"},
@@ -549,61 +492,65 @@ _INTERPRETER_WITH_ARG = {
"python": {"-W", "-X", "--check-hash-based-pycs"},
"node": {"-C", "--conditions", "--cpu-prof-dir", "--diagnostic-dir", "--icu-data-dir", "--import", "--loader",
"--openssl-config", "--require", "--title"},
- "perl": {"-0", "-F", "-I", "-M", "-m", "-x"}, "ruby": {"-C", "-E", "-F", "-I", "-K", "-r"}, "php": {"-c", "-d", "-z"},
+ "perl": {"-0", "-F", "-I", "-M", "-m", "-x"}, "ruby": {"-C", "-E", "-F", "-I", "-K", "-r"},
+ "php": {"-c", "-d", "-z"},
"powershell": {"-configurationname", "-custompipename", "-executionpolicy", "-inputformat", "-outputformat",
"-settingsfile", "-version", "-windowstyle", "-workingdirectory"},
}
_READ_TOOL_EXEC_FLAGS = {
- "sort": {"--compress-program"}, "rg": {"--pre", "--hostname-bin"},
- "ag": {"--pager"}, "man": {"--pager", "--html", "-P", "-H"},
+ "sort": {"--compress-program"}, "rg": {"--pre", "--hostname-bin"}, "ag": {"--pager"},
+ "man": {"--pager", "--html", "-P", "-H"},
}
-# Required-argument options are ownership boundaries: an option-looking next
-# token is data, not another option. These sets mirror the invocation grammar
-# of the supported binaries (ripgrep 14, GNU sort, man-db, and ag 2.2).
+# Required-argument options are ownership boundaries: an option-looking next token is data, not
+# another option. These sets mirror the invocation grammar of the supported binaries (ripgrep 14,
+# GNU sort, man-db, and ag 2.2).
_READ_TOOL_LONG_OPTIONS_WITH_ARG = {
"rg": {
"--after-context", "--before-context", "--color", "--colors", "--context", "--context-separator",
- "--dfa-size-limit", "--encoding", "--engine", "--field-context-separator", "--field-match-separator", "--file",
- "--generate", "--glob", "--hostname-bin", "--hyperlink-format", "--iglob", "--ignore-file", "--max-columns",
- "--max-count", "--max-depth", "--max-filesize", "--path-separator", "--pre", "--pre-glob", "--regex-size-limit",
- "--regexp", "--replace", "--sort", "--sortr", "--threads", "--type", "--type-add", "--type-clear", "--type-not",
+ "--dfa-size-limit", "--encoding", "--engine", "--field-context-separator", "--field-match-separator",
+ "--file", "--generate", "--glob", "--hostname-bin", "--hyperlink-format", "--iglob", "--ignore-file",
+ "--max-columns", "--max-count", "--max-depth", "--max-filesize", "--path-separator", "--pre", "--pre-glob",
+ "--regex-size-limit", "--regexp", "--replace", "--sort", "--sortr", "--threads", "--type", "--type-add",
+ "--type-clear", "--type-not",
},
"sort": {
"--batch-size", "--buffer-size", "--compress-program", "--field-separator", "--files0-from", "--key",
"--output", "--parallel", "--random-source", "--sort", "--temporary-directory",
},
"man": {
- "--config-file", "--encoding", "--extension", "--locale", "--manpath", "--pager", "--preprocessor", "--prompt",
- "--recode", "--sections", "--systems",
+ "--config-file", "--encoding", "--extension", "--locale", "--manpath", "--pager", "--preprocessor",
+ "--prompt", "--recode", "--sections", "--systems",
},
"ag": {
- "--ackmate-dir-filter", "--color-line-number", "--color-match", "--color-path", "--depth", "--filename-pattern",
- "--file-search-regex", "--ignore", "--ignore-dir", "--max-count", "--pager", "--path-to-ignore", "--width",
- "--workers",
+ "--ackmate-dir-filter", "--color-line-number", "--color-match", "--color-path", "--depth",
+ "--filename-pattern", "--file-search-regex", "--ignore", "--ignore-dir", "--max-count", "--pager",
+ "--path-to-ignore", "--width", "--workers",
},
}
_READ_TOOL_SHORT_OPTIONS_WITH_ARG = {
- "rg": frozenset("efEmjgdtTABCMr"), "sort": frozenset("koStT"),
- "man": frozenset("CRLmMSserEPp"), "ag": frozenset("gGmpW"),
+ "rg": frozenset("efEmjgdtTABCMr"), "sort": frozenset("koStT"), "man": frozenset("CRLmMSserEPp"),
+ "ag": frozenset("gGmpW"),
+}
+_GREP_OPTIONS_WITH_ARG = {
+ "--after-context", "--before-context", "--binary-files", "--context", "--directories", "--devices", "--exclude",
+ "--exclude-dir", "--exclude-from", "--include", "--label", "--max-count", "--regexp", "--file",
}
-_MAX_DETECTION_COMMAND_CHARS = 128_000
-_MAX_SEPARATOR_FREE_COMMAND_CHARS = 4_096
-_MAX_DETECTION_SEGMENTS = 25_000
+_GREP_SHORT_OPTIONS_WITH_ARG = {"A", "B", "C", "D", "d", "e", "f", "m"}
+_BASH_OPTIONS_WITH_ARG = {"-O", "+O", "-o", "+o", "--init-file", "--rcfile"}
+_BASH_SHORT_OPTION_LETTERS = frozenset("ilrsDcabefhkmnptuvxBCEHPTOo")
+_MAX_DETECTION_COMMAND_CHARS, _MAX_SEPARATOR_FREE_COMMAND_CHARS, _MAX_DETECTION_SEGMENTS = 128_000, 4_096, 25_000
_PARSER_LIMIT_DESCRIPTION = "command parser limit exceeded"
_MALFORMED_EXEC_DESCRIPTION = "command parser limit or malformed executable payload"
+_GATEWAY_LIFECYCLE_SPLICE_DESCRIPTION = "stop/restart hermes gateway via shell-spliced verb (kills running agents)"
def _command_parser_limit_exceeded(command: str) -> bool:
- """Bound all parser work before normalization/tokenization.
-
- Separator counting is deliberately conservative: quoted separators over-count,
- but crossing the ceiling fails closed rather than letting an uninspected
- suffix execute.
- """
+ """Bound all parser work before normalization/tokenization. Separator counting is deliberately
+ conservative: quoted separators over-count, but crossing the ceiling fails closed rather than
+ letting an uninspected suffix execute."""
if len(command) > _MAX_DETECTION_COMMAND_CHARS:
return True
- # Long separator-free input has no compound-command utility and would make
- # every regex inspect one giant token.
+ # Long separator-free input has no compound-command utility and makes every regex inspect one giant token.
if len(command) > _MAX_SEPARATOR_FREE_COMMAND_CHARS and not any(char in command for char in ";&|\n"):
return True
return sum(command.count(char) for char in ";&|\n") >= _MAX_DETECTION_SEGMENTS
@@ -611,19 +558,14 @@ def _command_parser_limit_exceeded(command: str) -> bool:
def _shell_tokens_with_spans(segment: str, start: int):
"""Return shell words as ``(value, start, end, quoted)`` or ``None`` on malformed quoting.
-
- Deliberately small lexer that never expands shell syntax; it exists to keep
- source spans (which ``shlex`` does not expose) for deciding which quoted grep
- operand is data rather than another command.
- """
- tokens = []
- value: list[str] = []
- token_start = quote = None
+ Deliberately small lexer that never expands shell syntax; it exists to keep source spans (which
+ ``shlex`` does not expose) for deciding which quoted grep operand is data, not another command."""
+ tokens, value, token_start, quote = [], [], None, None
def flush(end: int) -> None:
raw = segment[token_start:end]
- # Only a wholly single-quoted operand is inert shell data. Double
- # quotes still execute $() and backticks; unquoted substitutions do too.
+ # Only a wholly single-quoted operand is inert shell data. Double quotes still execute $()
+ # and backticks; unquoted substitutions do too.
inert = (raw.startswith("'") and raw.endswith("'")) or ("='" in raw and raw.endswith("'"))
tokens.append(("".join(value), token_start, end, inert))
@@ -648,21 +590,10 @@ def _shell_tokens_with_spans(segment: str, start: int):
if token_start is not None:
flush(len(segment))
return tokens
-
-
-_GREP_OPTIONS_WITH_ARG = {
- "--after-context", "--before-context", "--binary-files", "--context", "--directories", "--devices", "--exclude",
- "--exclude-dir", "--exclude-from", "--include", "--label", "--max-count", "--regexp", "--file",
-}
-_GREP_SHORT_OPTIONS_WITH_ARG = {"A", "B", "C", "D", "d", "e", "f", "m"}
-
-
def _quoted_grep_pattern_spans(command: str) -> tuple[list[tuple[int, int]], bool]:
- """Structurally locate quoted grep PCRE operands -> (spans, malformed).
-
- On an ambiguous/malformed grep parse callers fail closed and use the
- original command: no text is hidden on an uncertain parse.
- """
+ """Structurally locate quoted grep PCRE operands -> (spans, malformed). On an ambiguous or
+ malformed grep parse callers fail closed and use the original command: no text is hidden on
+ an uncertain parse."""
spans: list[tuple[int, int]] = []
offset = 0
for segment in _iter_top_level_shell_segments(command):
@@ -674,12 +605,9 @@ def _quoted_grep_pattern_spans(command: str) -> tuple[list[tuple[int, int]], boo
tokens = _shell_tokens_with_spans(segment, start)
if tokens is None:
return [], True
- args = tokens[1:]
+ args, pattern_indexes = tokens[1:], []
pcre = explicit_patterns = False
- pattern_indexes: list[int] = []
- operand_index = None
- i = 0
- options = True
+ operand_index, i, options = None, 0, True
while i < len(args):
token = args[i][0]
if options and token == "--":
@@ -700,8 +628,8 @@ def _quoted_grep_pattern_spans(command: str) -> tuple[list[tuple[int, int]], boo
pcre = pcre or char == "P"
explicit_patterns = explicit_patterns or char in {"e", "f"}
if char in _GREP_SHORT_OPTIONS_WITH_ARG:
- # The first argument-taking short option owns the rest
- # of the bundle, or the next token when it comes last.
+ # The first argument-taking short option owns the rest of the bundle,
+ # or the next token when it comes last.
attached = j + 1 < len(chars)
if not attached and i + 1 >= len(args):
return [], True
@@ -725,15 +653,13 @@ def _quoted_grep_pattern_spans(command: str) -> tuple[list[tuple[int, int]], boo
def _splice(command: str, edits) -> str:
- """Rebuild *command* with sorted, non-overlapping ``(start, end, text)`` edits
- applied in one pass (re-slicing per edit is quadratic on 10k+ segments)."""
- parts: list[str] = []
- previous = 0
+ """Apply sorted, non-overlapping ``(start, end, text)`` edits to *command* in one pass
+ (re-slicing per edit is quadratic on 10k+ segments)."""
+ parts, previous = [], 0
for start, end, text in edits:
parts.extend((command[previous:start], text))
previous = end
- parts.append(command[previous:])
- return "".join(parts)
+ return "".join(parts) + command[previous:]
def _grep_safe_detection_variant(command: str) -> tuple[str, bool]:
@@ -741,29 +667,15 @@ def _grep_safe_detection_variant(command: str) -> tuple[str, bool]:
if malformed or not spans:
return command, malformed
return _splice(command, [(start, end, " " * (end - start)) for start, end in spans]), False
-
-
-_INTERPRETER_NAME_RES = (
- ("python", re.compile(r"py(?:\.exe)?|python[23]?(?:\.\d+)*(?:\.exe)?")),
- ("node", re.compile(r"node(?:js)?(?:\.exe)?")),
- ("perl", re.compile(r"perl[0-9]*(?:\.\d+)*(?:\.exe)?")),
- ("ruby", re.compile(r"ruby[0-9.]*(?:\.exe)?")),
- ("php", re.compile(r"php(?:\.exe)?")),
- ("powershell", re.compile(r"powershell(?:\.exe)?|pwsh(?:\.exe)?")),
-)
-
-
def _interpreter_family(executable: str) -> str | None:
name = os.path.basename(executable).lower()
return next((family for family, name_re in _INTERPRETER_NAME_RES if name_re.fullmatch(name)), None)
def _shell_segment_tokens(segment: str, start: int) -> list[str] | None:
- """Tokenize an already-bounded command segment.
-
- ``None`` distinguishes malformed quoting from an empty segment so callers
- can fail closed for a program-bearing option rather than silently skip it.
- """
+ """Tokenize an already-bounded command segment. ``None`` distinguishes malformed quoting from
+ an empty segment so callers can fail closed for a program-bearing option rather than silently
+ skip it."""
try:
lexer = shlex.shlex(segment[start:], posix=True, punctuation_chars="<>")
lexer.whitespace_split, lexer.commenters = True, ""
@@ -784,11 +696,6 @@ def _iter_top_level_shell_segments(command: str):
yield command[start:]
-def _split_option(token: str) -> tuple[str, str | None]:
- option, equals, value = token.partition("=")
- return option, (value if equals else None)
-
-
def _interpreter_exec_flag(family: str, args: list[str]) -> str | None:
"""Return an execution-bearing interpreter option, if present."""
flags, with_arg = _INTERPRETER_EXEC_FLAGS[family], _INTERPRETER_WITH_ARG[family]
@@ -800,38 +707,28 @@ def _interpreter_exec_flag(family: str, args: list[str]) -> str | None:
continue
if token == "--" or (not powershell and not token.startswith("-")):
break
- option, attached = _split_option(token)
+ option, equals, _ = token.partition("=")
comparable = option.lower() if powershell else option
if comparable in flags:
return comparable
- # `-Wonce` and `ruby -rjson` attach an option value; they are not
- # short-option bundles containing an execution flag. PowerShell's
- # normal long options also use one dash, so bundle parsing never
- # applies to that family.
+ # `-Wonce` and `ruby -rjson` attach an option value; they are not short-option bundles
+ # containing an execution flag. PowerShell's normal long options also use one dash, so
+ # bundle parsing never applies to that family.
has_attached_option_value = any(
option.startswith(short) and len(option) > len(short)
for short in with_arg if short.startswith("-") and not short.startswith("--")
)
if not powershell and not option.startswith("--") and len(option) > 2 and not has_attached_option_value:
- for char in option[1:]:
- if f"-{char}" in flags:
- return f"-{char}"
- skip_value = comparable in with_arg and attached is None
+ bundled = next((f"-{char}" for char in option[1:] if f"-{char}" in flags), None)
+ if bundled:
+ return bundled
+ skip_value = comparable in with_arg and not equals
return None
-
-
-_BASH_OPTIONS_WITH_ARG = {"-O", "+O", "-o", "+o", "--init-file", "--rcfile"}
-_BASH_SHORT_OPTION_LETTERS = frozenset("ilrsDcabefhkmnptuvxBCEHPTOo")
-
-
def _bash_exec_payload(args: list[str]) -> tuple[bool, str | None]:
"""Return whether Bash ``-c`` occurs and the command string it owns.
-
- Bash's O/o options consume the following argument even when they precede a
- later ``-c`` or share its short-option bundle; the two startup-file long
- options own their next token. Parsing those first prevents both missed
- payloads and false ``-c`` hits.
- """
+ Bash's O/o options consume the following argument even when they precede a later ``-c`` or
+ share its short-option bundle; the two startup-file long options own their next token.
+ Parsing those first prevents both missed payloads and false ``-c`` hits."""
index = 0
while index < len(args):
token = args[index]
@@ -841,8 +738,8 @@ def _bash_exec_payload(args: list[str]) -> tuple[bool, str | None]:
index += 2
continue
chars = token[1:]
- # Bash option letters are case-sensitive; restricting to the documented
- # alphabet preserves invalid controls such as `-Wc`.
+ # Bash option letters are case-sensitive; restricting to the documented alphabet
+ # preserves invalid controls such as `-Wc`.
if token.startswith("--") or not set(chars) <= _BASH_SHORT_OPTION_LETTERS:
index += 1
continue
@@ -862,23 +759,24 @@ def _read_tool_exec_flag(tool: str, args: list[str]) -> tuple[str, str] | None:
token = args[index]
if token == "--":
break
- option, payload = _split_option(token)
+ option, equals, payload = token.partition("=")
+ payload = payload if equals else None
matched = option if option in flags else None
if tool == "man" and token.startswith(("-P", "-H")) and len(token) > 2:
matched, payload = token[:2], token[2:]
if matched:
if payload is None and index + 1 < len(args):
payload = args[index + 1]
- # The option owns its program argument regardless of spelling; the
- # real binaries execute a '-'-prefixed payload rather than reparsing it.
+ # The option owns its program argument regardless of spelling; the real binaries
+ # execute a '-'-prefixed payload rather than reparsing it.
if payload:
return matched, payload
index += 2 if payload is not None and "=" not in token else 1
elif option in _READ_TOOL_LONG_OPTIONS_WITH_ARG[tool] and payload is None:
index += 2
elif token.startswith("-") and not token.startswith("--") and len(token) > 1:
- # In a short bundle, the first argument-taking option owns the rest of
- # the token, or the following token when it occurs last.
+ # In a short bundle, the first argument-taking option owns the rest of the token, or
+ # the following token when it occurs last.
shorts = _READ_TOOL_SHORT_OPTIONS_WITH_ARG[tool]
owner = next((k for k, char in enumerate(token[1:], start=1) if char in shorts), None)
index += 2 if owner == len(token) - 1 else 1
@@ -901,21 +799,20 @@ def _execution_flag_findings(command: str):
continue
if not tokens:
continue
- if family:
- if _interpreter_exec_flag(family, tokens[1:]):
- yield ("script execution via -e/-c flag", None)
- continue
- if any(token.startswith("<<") for token in tokens[1:]):
- yield ("script execution via heredoc", None)
- continue
- if executable_name in {"bash", "sh", "zsh", "ksh"}:
- found, payload = _bash_exec_payload(tokens[1:])
- if found:
- yield ("shell command via -c/-lc flag", payload)
- if executable_name in _READ_TOOL_EXEC_FLAGS:
- finding = _read_tool_exec_flag(executable_name, tokens[1:])
- if finding:
- yield (f"arbitrary program execution via {executable_name} {finding[0]}", finding[1])
+ args = tokens[1:]
+ if family and _interpreter_exec_flag(family, args):
+ yield ("script execution via -e/-c flag", None)
+ elif family and any(token.startswith("<<") for token in args):
+ yield ("script execution via heredoc", None)
+ else:
+ if executable_name in {"bash", "sh", "zsh", "ksh"}:
+ found, payload = _bash_exec_payload(args)
+ if found:
+ yield ("shell command via -c/-lc flag", payload)
+ if executable_name in _READ_TOOL_EXEC_FLAGS:
+ finding = _read_tool_exec_flag(executable_name, args)
+ if finding:
+ yield (f"arbitrary program execution via {executable_name} {finding[0]}", finding[1])
def _skip_shell_whitespace(command: str, pos: int) -> int:
@@ -924,66 +821,56 @@ def _skip_shell_whitespace(command: str, pos: int) -> int:
return pos
+
def _scan_shell(text: str, start: int = 0, end: int | None = None, *, subst: str = "",
brace: bool = False, stop_unterminated: bool = False, naive_backtick: bool = False):
"""Yield ``(kind, i, j, quote)`` lexical steps over ``text[start:end]`` without expanding.
- The single quote/escape state machine behind every detection scanner. ``kind`` is
- ``"char"`` (one char), ``"esc"`` (backslash + the char it escapes; never inside
- single quotes), ``"quote"`` (an opening or closing quote char) or ``"subst"`` (a
- ``$(...)`` / `` `...` `` / ``${...}`` span). ``quote`` is the state the step was read
- in (``None``, ``'`` or ``"``). Substitutions are recognized unquoted when ``"u"`` is in
- *subst*, inside double quotes when ``"q"`` is; ``brace`` adds unquoted ``${...}``. An
- unterminated substitution falls through as plain chars unless *stop_unterminated*,
- which yields ``("subst", i, None, quote)`` and ends the scan (the caller descends
- to *end* itself). *naive_backtick* closes a backtick at the next backtick even if
- escaped (the quoted-prose masker's historical behavior).
- """
+ The single quote/escape state machine behind every detection scanner. ``kind`` is ``"char"``
+ (one char), ``"esc"`` (backslash + the char it escapes; never inside single quotes), ``"quote"``
+ (an opening/closing quote char) or ``"subst"`` (a ``$(...)`` / backtick / ``${...}`` span);
+ ``quote`` is the state the step was read in (``None``, ``'`` or ``"``). Substitutions are
+ recognized unquoted when ``"u"`` is in *subst*, inside double quotes when ``"q"`` is; *brace*
+ adds unquoted ``${...}``. An unterminated substitution falls through as plain chars unless
+ *stop_unterminated*, which yields ``("subst", i, None, quote)`` and ends the scan (the caller
+ descends to *end* itself). *naive_backtick* closes a backtick at the next backtick even if
+ escaped (the quoted-prose masker's historical behavior)."""
n = len(text) if end is None else end
quote: str | None = None
i = start
while i < n:
ch = text[i]
+ kind, j = "char", i + 1
if quote != "'" and ch == "\\" and i + 1 < n:
- yield ("esc", i, i + 2, quote)
- i += 2
+ kind, j = "esc", i + 2
elif ch == quote or (quote is None and ch in "'\""):
- yield ("quote", i, i + 1, quote)
- quote = None if quote else ch
- i += 1
+ kind = "quote"
elif quote != "'" and ("q" if quote else "u") in subst and (
ch == "`" or text.startswith("$(", i) or (brace and not quote and text.startswith("${", i))
):
if ch == "`":
- j = text.find("`", i + 1) + 1 or None if naive_backtick else _scan_backtick_end(text, i)
+ close = text.find("`", i + 1) + 1 or None if naive_backtick else _scan_backtick_end(text, i)
elif text[i + 1] == "(":
- j = _scan_dollar_paren_end(text, i)
+ close = _scan_dollar_paren_end(text, i)
else:
- j = text.find("}", i + 2) + 1 or None
- if j is not None:
- yield ("subst", i, j, quote)
- i = j
+ close = text.find("}", i + 2) + 1 or None
+ if close is not None:
+ kind, j = "subst", close
elif stop_unterminated:
yield ("subst", i, None, quote)
return
- else:
- yield ("char", i, i + 1, quote)
- i += 1
- else:
- yield ("char", i, i + 1, quote)
- i += 1
+ yield (kind, i, j, quote)
+ if kind == "quote":
+ quote = None if quote else ch
+ i = j
def _scan_dollar_paren_end(command: str, start: int) -> int | None:
"""Return the offset after a balanced ``$(...)`` command substitution."""
depth = 1
for kind, i, _, quote in _scan_shell(command, start + 2):
- if kind != "char" or quote:
- continue
- if command.startswith("$(", i):
- depth += 1
- elif command[i] == ")":
- depth -= 1
+ if kind == "char" and not quote:
+ depth += command.startswith("$(", i) - (command[i] == ")")
if depth == 0:
return i + 1
return None
@@ -1010,7 +897,7 @@ def _literal_command_substitution_output(script: str) -> str | None:
try:
tokens = shlex.split(script, posix=True)
except ValueError:
- return None
+ tokens = []
if not tokens:
return None
command, args = tokens[0].lower(), tokens[1:]
@@ -1054,9 +941,8 @@ def _strip_shell_word_syntax(word: str) -> str:
def _deobfuscate_shell_word_for_detection(word: str) -> str:
- """Approximate how shell syntax can spell a command word: collapses
- quoting/escaping plus simple literal command substitutions in the word
- itself. Intentionally narrow and non-executing."""
+ """Approximate how shell syntax can spell a command word: collapses quoting/escaping plus
+ simple literal command substitutions in the word itself. Intentionally narrow and non-executing."""
for _ in range(2):
previous = word
word = _strip_shell_word_syntax(_replace_simple_shell_expansions(word))
@@ -1065,6 +951,7 @@ def _deobfuscate_shell_word_for_detection(word: str) -> str:
return word
+
def _iter_shell_command_starts(command: str):
starts = [0]
@@ -1077,49 +964,36 @@ def _iter_shell_command_starts(command: str):
starts.append(inner)
scan(inner, end if j is None else j - 1)
elif kind == "char" and quote is None and i != skip:
- ch = command[i]
- if ch in "({;\n":
+ if command[i] in "({;\n":
starts.append(i + 1)
- elif ch in "&|":
- repeated = i + 1 < end and command[i + 1] == ch
- starts.append(i + 2 if repeated else i + 1)
- if repeated:
- skip = i + 1
+ elif command[i] in "&|":
+ repeated = i + 1 < end and command[i + 1] == command[i]
+ skip = i + 1 if repeated else skip
+ starts.append(i + 1 + repeated)
scan(0, len(command))
- seen: set[int] = set()
- for start in starts:
- start = _skip_shell_whitespace(command, start)
- if start < len(command) and start not in seen:
- seen.add(start)
- yield start
+ # First occurrence wins (dict order), so a start is yielded once even when several openers map to it.
+ yield from (s for s in dict.fromkeys(_skip_shell_whitespace(command, s) for s in starts) if s < len(command))
def _mark_command_starts(command: str) -> str:
"""Insert a newline before each real (quote-aware) command start.
-
- ``\\n`` is already a ``_CMDPOS`` separator, so this exposes subshell ``(cmd)``
- and brace-group ``{ cmd; }`` openers — which the flat pattern class omits —
- to the anchored patterns WITHOUT the quoted-prose false positives that adding
- ``(`` / ``{`` to ``_CMDPOS`` would cause: starts inside quotes are never
- produced, so ``--title "block (reboot)"`` is left as-is.
- """
+ ``\\n`` is already a ``_CMDPOS`` separator, so this exposes subshell ``(cmd)`` and brace-group
+ ``{ cmd; }`` openers — which the flat pattern class omits — to the anchored patterns WITHOUT the
+ quoted-prose false positives that adding ``(`` / ``{`` to ``_CMDPOS`` would cause: starts inside
+ quotes are never produced, so ``--title "block (reboot)"`` is left as-is."""
offsets = sorted(o for o in _iter_shell_command_starts(command) if o > 0)
return _splice(command, [(o, o, "\n") for o in offsets]) if offsets else command
def _mask_quoted_newlines(command: str) -> str:
- """Replace raw newlines inside single/double quotes with a space.
-
- Detection-only. A quoted newline is DATA to the shell, yet the flat
- ``_CMDPOS`` class treats every raw ``\\n`` as a command start, so multi-line
- quoted arguments (commit messages, heredoc text) tripped the hardline
- blocklist when a data line began with e.g. ``sudo reboot``. Quote tracking
- mirrors ``_iter_shell_command_starts``. Unquoted newlines pass through and
- ``_mark_command_starts`` still re-inserts newlines at genuine starts; an
- unclosed quote absorbs following newlines exactly as the shell would, so
- masking them cannot hide a runnable command.
- """
+ """Replace raw newlines inside single/double quotes with a space (detection-only).
+ A quoted newline is DATA to the shell, yet the flat ``_CMDPOS`` class treats every raw ``\\n``
+ as a command start, so multi-line quoted arguments (commit messages, heredoc text) tripped the
+ hardline blocklist when a data line began with e.g. ``sudo reboot``. Quote tracking mirrors
+ ``_iter_shell_command_starts``. Unquoted newlines pass through and ``_mark_command_starts``
+ still re-inserts newlines at genuine starts; an unclosed quote absorbs following newlines
+ exactly as the shell would, so masking them cannot hide a runnable command."""
if "\n" not in command:
return command
return "".join(
@@ -1155,13 +1029,12 @@ def _iter_shell_command_word_spans(command: str):
def _command_detection_variants(command: str):
- # Mask quoted newlines BEFORE normalization: normalization strips escapes
- # (\" -> ") and "" pairs, corrupting quote tracking (`echo "a\""` becomes
- # an unterminated quote) so masking afterwards could swallow a REAL
- # unquoted newline separator. The raw command carries faithful quote state.
+ # Mask quoted newlines BEFORE normalization: normalization strips escapes (\" -> ") and ""
+ # pairs, corrupting quote tracking (`echo "a\""` becomes an unterminated quote) so masking
+ # afterwards could swallow a REAL unquoted newline separator. The raw command carries faithful quote state.
normalized = _normalize_command_for_detection(_mask_quoted_newlines(command))
- # Quote-aware grep parsing hides only structurally identified pattern
- # operands; malformed/ambiguous input stays byte-for-byte intact.
+ # Quote-aware grep parsing hides only structurally identified pattern operands; malformed or
+ # ambiguous input stays byte-for-byte intact.
grep_safe, _ = _grep_safe_detection_variant(normalized)
seen = {grep_safe}
yield grep_safe
@@ -1172,40 +1045,38 @@ def _command_detection_variants(command: str):
seen.add(variant)
return True
- # Windows-path variant: normalization strips backslashes as shell escapes,
- # so `del C:\Users\me\.ssh\id_rsa` reaches the patterns as `del
- # C:Usersme.sshid_rsa`. When the RAW command has a drive-letter or UNC
- # backslash path, also yield a variant with backslashes flattened to `/`
- # BEFORE normalization. Gated on a real path shape so POSIX escape
- # semantics (`echo a\"b`) are untouched elsewhere.
+ # Windows-path variant: normalization strips backslashes as shell escapes, so `del
+ # C:\Users\me\.ssh\id_rsa` reaches the patterns as `del C:Usersme.sshid_rsa`. When the RAW
+ # command has a drive-letter or UNC backslash path, also yield a variant with backslashes
+ # flattened to `/` BEFORE normalization. Gated on a real path shape so POSIX escape semantics
+ # (`echo a\"b`) are untouched elsewhere.
if re.search(r"(?:[A-Za-z]:|\\\\)[\\\\]", command) or re.search(r"[A-Za-z]:\\", command):
win_variant = _normalize_command_for_detection(_mask_quoted_newlines(command.replace("\\", "/")))
if fresh(win_variant):
yield win_variant
- # Program-bearing options are parsed in their owning command's context;
- # surfacing only the payload lets the hardline floor inspect what will
- # actually run without promoting similar flags or quoted prose.
+ # Program-bearing options are parsed in their owning command's context; surfacing only the
+ # payload lets the hardline floor inspect what will actually run without promoting similar
+ # flags or quoted prose.
pending = [normalized]
while pending:
for _, payload in _execution_flag_findings(pending.pop()):
if fresh(payload):
yield payload
- # A payload may start with an option-looking program and then
- # invoke a hardline command after a separator; mark its starts.
+ # A payload may start with an option-looking program and then invoke a hardline command
+ # after a separator; mark its starts.
marked_payload = _mark_command_starts(payload)
if marked_payload != payload and fresh(marked_payload):
yield marked_payload
pending.append(payload)
- # Subshell `(cmd)` / brace-group `{ cmd; }` openers put `cmd` at a real
- # command position the flat `_CMDPOS` patterns can't see (adding `(`/`{`
- # there would match quoted prose like `--title "(reboot)"`). Insert a
- # newline at each start the QUOTE-AWARE tokenizer found instead; this
- # covers every `_CMDPOS` rule in one place.
+ # Subshell `(cmd)` / brace-group `{ cmd; }` openers put `cmd` at a real command position the
+ # flat `_CMDPOS` patterns can't see (adding `(`/`{` there would match quoted prose like
+ # `--title "(reboot)"`). Insert a newline at each start the QUOTE-AWARE tokenizer found
+ # instead; this covers every `_CMDPOS` rule in one place.
marked = _mark_command_starts(grep_safe)
if marked != grep_safe and fresh(marked):
yield marked
- # Quoting/escaping can spell an executable in pieces (r\m, r''m). Keep that
- # deobfuscation scoped to command words so arguments don't false-positive.
+ # Quoting/escaping can spell an executable in pieces (r\m, r''m). Keep that deobfuscation scoped
+ # to command words so arguments don't false-positive.
for word_start, word_end, word in _iter_shell_command_word_spans(normalized):
deobfuscated = _deobfuscate_shell_word_for_detection(word)
if deobfuscated and deobfuscated != word:
@@ -1230,24 +1101,15 @@ def _is_verification_artifact_cleanup(command: str) -> bool:
and os.path.dirname(os.path.realpath(operand)) == temp_dir
and re.fullmatch(r"hermes-(?:verify|ad-hoc)-[A-Za-z0-9_.-]+", basename) is not None
)
-
-
-_GATEWAY_LIFECYCLE_SPLICE_DESCRIPTION = "stop/restart hermes gateway via shell-spliced verb (kills running agents)"
-
-
def _is_shell_token_spliced_gateway_lifecycle(command: str) -> bool:
"""Catch gateway-lifecycle verbs spelled with quote splicing.
-
- Backslash splicing (``kick\\start``) is undone by normalization, but quote
- splicing is not: ``_deobfuscate_shell_word_for_detection`` is deliberately
- scoped to command-position words (widening it would let quoted prose like
- ``git commit -m "rm -rf /"`` match), and the spliced verb is an ARGUMENT, so
- ``launchctl kick"start" -k gui/501/ai.hermes.gateway`` auto-approved.
- Delegates to ``cron.lifecycle_guard`` (shlex-tokenized, anchored on a
- hermes-gateway identifier). Runs last so an ordinary pattern match keeps its
- more specific reason; this layer only prompts — the non-bypassable block
- still lives in ``cron.lifecycle_guard``.
- """
+ Backslash splicing (``kick\\start``) is undone by normalization, but quote splicing is not:
+ ``_deobfuscate_shell_word_for_detection`` is deliberately scoped to command-position words
+ (widening it would let quoted prose like ``git commit -m "rm -rf /"`` match), and the spliced
+ verb is an ARGUMENT, so ``launchctl kick"start" -k gui/501/ai.hermes.gateway`` auto-approved.
+ Delegates to ``cron.lifecycle_guard`` (shlex-tokenized, anchored on a hermes-gateway
+ identifier). Runs last so an ordinary pattern match keeps its more specific reason; this layer
+ only prompts — the non-bypassable block still lives in ``cron.lifecycle_guard``."""
try:
from cron.lifecycle_guard import contains_gateway_lifecycle_command
except Exception: