approval_detection.py 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  1. """Dangerous-command detection: normalization, tokenizing, and pattern tables.
  2. Pure command classification for :mod:`tools.approval` — no approval state, config reads, or
  3. prompting live here.
  4. """
  5. import bisect
  6. import fnmatch
  7. import functools
  8. import logging
  9. import os
  10. import re
  11. import shlex
  12. import tempfile
  13. import unicodedata
  14. logger = logging.getLogger("tools.approval")
  15. # Sensitive write targets, matched via ~ / $HOME / $HERMES_HOME spellings. The resolved absolute
  16. # home is folded into these forms at detection time by _normalize_command_for_detection(), so no
  17. # import-time path snapshot (stale once HERMES_HOME is set after import) lives in the patterns.
  18. _SSH_SENSITIVE_PATH = r'(?:~|\$home|\$\{home\})/\.ssh(?:/|$)'
  19. _HERMES_ENV_PATH = (
  20. r'(?:~\/\.hermes/|(?:\$home|\$\{home\})/\.hermes/|(?:\$hermes_home|\$\{hermes_home\})/)' r'\.env\b'
  21. )
  22. # ~/.hermes/config.yaml IS the security policy (approvals.mode, yolo, allowlist) and the config cache is mtime-keyed,
  23. # so a write takes effect mid-session. Terminal-side coverage (sed -i, tee, >, cp) pairs the file_tools deny.
  24. _HERMES_CONFIG_PATH = (
  25. r'(?:~\/\.hermes/|(?:\$home|\$\{home\})/\.hermes/|(?:\$hermes_home|\$\{hermes_home\})/)' r'config\.yaml\b'
  26. )
  27. _PROJECT_ENV_PATH = r'(?:(?:/|\.{1,2}/)?(?:[^\s/"\'`]+/)*\.env(?:\.[^/\s"\'`]+)*)'
  28. _PROJECT_CONFIG_PATH = r'(?:(?:/|\.{1,2}/)?(?:[^\s/"\'`]+/)*config\.yaml)'
  29. _SHELL_RC_FILES = r'(?:~|\$home|\$\{home\})/\.' r'(?:bashrc|zshrc|profile|bash_profile|zprofile)\b'
  30. _CREDENTIAL_FILES = r'(?:~|\$home|\$\{home\})/\.' r'(?:netrc|pgpass|npmrc|pypirc)\b'
  31. _HOME_PREFIX = r'(?:~|\$home|\$\{home\})/'
  32. _SECRET_FILE = (
  33. rf'(?:{_SSH_SENSITIVE_PATH}|{_HERMES_ENV_PATH}|{_CREDENTIAL_FILES}|{_PROJECT_ENV_PATH}(?![\w-])'
  34. rf'|{_HOME_PREFIX}\.(?:aws/credentials|git-credentials|config/gh/hosts\.yml|docker/config\.json|kube/config)\b'
  35. r'|/etc/(?:shadow|passwd)\b|\bid_(?:rsa|ed25519|ecdsa|dsa)\b)'
  36. )
  37. _SECRET_VAR = r'\$\{?\w*(?:key|token|secret|passwd|password|credential)\w*\}?'
  38. # Flags whose argument becomes a request body / uploaded file. Case-sensitive: curl -D dumps headers, -f fails.
  39. _UPLOAD_FLAG = (r'(?<!\S)(?:(?-i:-[a-zA-Z]*[dFT])|--data(?:-binary|-raw|-urlencode|-ascii)?|--form(?:-string)?'
  40. r'|--upload-file|--json|--post-(?:data|file)|--body-(?:data|file))')
  41. _EMOJI_RANGE = r'\U0001F000-\U0001FAFF\u2600-\u27BF'
  42. # Code points that take VS15/VS16 for text/emoji presentation: the two emoji blocks above plus the BMP
  43. # singletons with the Unicode Emoji property (© ™ ℹ, arrows, ⌚ ▶ ⬅ ⭐ 〰 ㊗ …), as in #114843. Digits,
  44. # ``#`` and ``*`` are deliberately absent: a selector after a letter or digit is the hiding signal.
  45. _EMOJI_PRESENTATION_BASES = (
  46. r'\u00a9\u00ae\u203c\u2049\u2122\u2139\u2194-\u2199\u21a9\u21aa\u231a\u231b\u2328\u23cf'
  47. r'\u23e9-\u23f3\u23f8-\u23fa\u24c2\u25aa\u25ab\u25b6\u25c0\u25fb-\u25fe\u2934\u2935'
  48. r'\u2b05-\u2b07\u2b1b\u2b1c\u2b50\u2b55\u3030\u303d\u3297\u3299' + _EMOJI_RANGE
  49. )
  50. # Matched on the RAW command: detection variants are NFKC-folded, which turns ℹ + VS16 into ``i`` + VS16.
  51. # A keycap emoji (digit, ``#`` or ``*`` + VS16 + U+20E3) is exempt only as that exact sequence, so a
  52. # selector after a digit with no keycap mark is still flagged.
  53. _HIDDEN_VARIATION_SELECTOR_RE = re.compile(
  54. rf'(?<![{_EMOJI_PRESENTATION_BASES}])(?!(?<=[0-9#*])\ufe0f\u20e3)[\ufe00-\ufe0f\U000e0100-\U000e01ef]')
  55. _INVISIBLE_UNICODE_DESCRIPTION = "invisible or bidirectional Unicode control character (possible obfuscation)"
  56. _CREDENTIAL_UPLOAD_DESCRIPTION = "upload a secret or credential file via curl/wget (possible exfiltration)"
  57. # Global flags before a subcommand, each with an optional value. Every flag has one parse ('-' plus
  58. # its possessive remainder, so '--x' and '--x=v' never split two ways) and a value cannot itself be a
  59. # flag, so a long run that never reaches the subcommand fails in linear time instead of holding the
  60. # GIL for minutes (#129281). Whole groups still backtrack to expose the target flag or verb.
  61. _GLOBAL_FLAGS = r'(?:-\S++(?:\s++(?!-\S)\S++)?\s++)*'
  62. # Same grammar for the docker/podman rules, which have always taken a separate value only after exactly
  63. # one whitespace character; keeping that means this fix changes no approval decision.
  64. _CONTAINER_GLOBAL_FLAGS = r'(?:-\S++(?:\s(?!-\S)\S++)?\s++)*'
  65. # macOS: /etc, /var, /tmp, /home are symlinks to /private/*, so /private/etc/sudoers would bypass a plain
  66. # "/etc/" check. Match both forms.
  67. _MACOS_PRIVATE_SYSTEM_PATH = r'/private/(?:etc|var|tmp|home)/'
  68. _SYSTEM_CONFIG_PATH = rf'(?:/etc/|{_MACOS_PRIVATE_SYSTEM_PATH})'
  69. _SENSITIVE_WRITE_TARGET = (
  70. rf'(?:{_SYSTEM_CONFIG_PATH}|/dev/sd|{_SSH_SENSITIVE_PATH}|{_HERMES_ENV_PATH}|{_HERMES_CONFIG_PATH}|'
  71. rf'{_SHELL_RC_FILES}|{_CREDENTIAL_FILES})'
  72. )
  73. _USER_SENSITIVE_WRITE_TARGET = rf'(?:{_SSH_SENSITIVE_PATH}|{_SHELL_RC_FILES}|{_CREDENTIAL_FILES})'
  74. _PROJECT_SENSITIVE_WRITE_TARGET = rf'(?:{_PROJECT_ENV_PATH}|{_PROJECT_CONFIG_PATH})'
  75. # cp/mv/install: the sensitive path is a write target only as the LAST argument (destination), so
  76. # `cp config.yaml backup.yaml` (config.yaml as SOURCE) stays out.
  77. _COMMAND_TAIL = r'(?:\s*(?:&&|\|\||;).*)?$'
  78. # `>`/`>>`/tee: the path is ALWAYS a write target regardless of what follows, so only require a
  79. # shell word boundary (_COMMAND_TAIL let `echo x > .env extra` / `echo x > .env # note` slip past).
  80. # `#` is deliberately NOT a boundary: a glued `#` is part of the filename (`.env#backup`).
  81. _WRITE_TARGET_BOUNDARY = r'(?=[\s;&|<>"\']|$)'
  82. # ---- Hardline (unconditional) blocklist ---------------------------------------------------
  83. # Commands that NEVER run via the agent, regardless of --yolo, approvals.mode=off, or cron approve
  84. # mode — a floor below yolo. Applies only to environments that can damage the host (local, ssh,
  85. # container-host cron); containerized backends already bypass the dangerous-command layer.
  86. # Deliberately tiny: only things with no recovery path (root wipe, raw block device writes,
  87. # shutdown, DoS). Recoverable operations (git reset --hard, chmod -R 777, curl|sh) stay in
  88. # DANGEROUS_PATTERNS.
  89. # Start-of-command position: start of string, newline, subshell opener ($( or backtick), optionally consuming
  90. # sudo/env/exec/nohup/setsid/time wrappers. Keeps shutdown/reboot rules from firing on "echo reboot" / "grep
  91. # 'shutdown' log". Real ;/&/| separators are converted to newlines by the quote-aware _mark_command_starts pass;
  92. # keeping them here mistakes quoted data (grep '(safe|rm -rf /)') for commands.
  93. _CMDPOS = (
  94. r'(?:^|[\n`]|\$\()' r'\s*' # start position, optional whitespace
  95. r'(?:sudo\s+(?:-[^\s]+\s+)*)?' r'(?:env\s+(?:\w+=\S*\s+)*)?' # optional sudo with flags, env VAR=VAL pairs
  96. r'(?:(?:exec|nohup|setsid|time)\s+)*' r'\s*' # optional wrapper commands
  97. )
  98. # Destructive-path matcher for the rm hardline rules: accept the path fully wrapped in a matching
  99. # quote pair (`rm -rf "/"`, `rm -rf "$HOME"`) OR bare with a terminator (whitespace, end, or
  100. # `) ` ; | &` so `$(rm -rf /)` is caught). A bare-token-only anchor lets quoted spellings slip past the floor.
  101. def _hardline_rm_path(path_alt: str, tail: str = r'(?:\s|$|[)`;|&])') -> str:
  102. return rf'(?:["\'](?:{path_alt})["\']|(?:{path_alt}){tail})'
  103. # Protected system roots whose recursive deletion has no recovery path.
  104. _HARDLINE_SYSTEM_DIRS = (r'/home|/home/\*|/root|/root/\*|/etc|/etc/\*|/usr|/usr/\*|'
  105. r'/var|/var/\*|/bin|/bin/\*|/sbin|/sbin/\*|/boot|/boot/\*|/lib|/lib/\*')
  106. # `rm` plus flag group, shared by the rm hardline rules (plain concatenation, not f-string:
  107. # backslashes in replacement fields are unsupported on the 3.11 floor). _CMDPOS-anchored so `rm`
  108. # must be an actual command word — "rm -rf /" as DATA in `git commit -m "…rm -rf /…"` must not trip the floor.
  109. _RM_FLAG_PREFIX = _CMDPOS + r'rm\s+(-[^\s]*\s+)*'
  110. # Package-manager global options, each optionally taking ONE non-dash operand.
  111. _PKG_OPTS = r'(?:-[^\s]+(?:\s+[^-\s][^\s]*)?\s+)*'
  112. HARDLINE_PATTERNS = [
  113. # Root path: any root-anchored path whose components collapse to "/" in the shell ("/", "//",
  114. # "/.", "/./", "/../..", optional trailing glob). Each inter-slash segment must be exactly "."
  115. # or "..", so "/tmp", "/.ssh", even "/..." are literal dirs that fall through to the softer
  116. # DANGEROUS rules. The "/ \*" alt covers `rm -rf / *` (two args: "/" plus the glob).
  117. (_RM_FLAG_PREFIX + _hardline_rm_path(r'/(?:(?:\.\.?)?/)*(?:\.\.?)?\**|/ \*'), "recursive delete of root filesystem"),
  118. (_RM_FLAG_PREFIX + _hardline_rm_path(_HARDLINE_SYSTEM_DIRS), "recursive delete of system directory"),
  119. (_RM_FLAG_PREFIX + _hardline_rm_path(r'(?:~|\$\{?HOME\}?)(?:/?|/\*)?'), "recursive delete of home directory"),
  120. # Command-name rules (mkfs, dd, kill, shutdown...) are _CMDPOS-anchored so quoted prose
  121. # (`echo "does this use mkfs?"`) cannot trip the floor.
  122. # See #93392.
  123. (_CMDPOS + r'mkfs(\.[a-z0-9]+)?\b', "format filesystem (mkfs)"),
  124. # `dd` is a command-name token, so anchor it to command position like mkfs/rm/shutdown (#93392): quoted
  125. # prose such as `git commit -m "never dd of=/dev/sda"` is an argument, not a command. The argument tail
  126. # ([^\n]*of=/dev/...) is kept so flag order doesn't matter.
  127. (_CMDPOS + r'dd\b[^\n]*\bof=/dev/(sd|nvme|hd|mmcblk|vd|xvd)[a-z0-9]*', "dd to raw block device"),
  128. # Positionless rules (no command-name token: `>` sits mid-command, the fork bomb is a function
  129. # definition) are matched against a QUOTE-MASKED variant (_QUOTE_MASKED_HARDLINE_DESCRIPTIONS /
  130. # _mask_quoted_prose) so quoted prose cannot trip them; sh -c / bash -c / eval payloads still scan raw.
  131. # The redirect rule has no command-name token to anchor (`>` appears mid-command: `cat f > /dev/sda`),
  132. # so command-position anchoring is the wrong tool. It is instead matched against a QUOTE-MASKED variant
  133. # of the command (see _QUOTE_MASKED_HARDLINE / _mask_quoted_strings) so quoted prose (`echo "cat f >
  134. # /dev/sda"`) cannot trip it, while shell-carrying wrappers (sh -c / bash -c / eval) still surface their
  135. # payload as a raw detection variant — quoting is not a bypass (#93392).
  136. (r'>\s*/dev/(sd|nvme|hd|mmcblk|vd|xvd)[a-z0-9]*\b', "redirect to raw block device"),
  137. (r':\(\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;\s*:', "fork bomb"),
  138. # Kill every process on the system — anchor the command-name token so `echo "kill -1 sends SIGHUP to
  139. # everything"` doesn't trip (#93392).
  140. (_CMDPOS + r'kill\s+(-[^\s]+\s+)*-1\b', "kill all processes"),
  141. (_CMDPOS + r'(shutdown|reboot|halt|poweroff)\b', "system shutdown/reboot"),
  142. (_CMDPOS + r'init\s+[06]\b', "init 0/6 (shutdown/reboot)"),
  143. (_CMDPOS + r'systemctl\s+(poweroff|reboot|halt|kexec)\b', "systemctl poweroff/reboot"),
  144. (_CMDPOS + r'telinit\s+[06]\b', "telinit 0/6 (shutdown/reboot)"),
  145. ]
  146. # Pre-compiled at module load so the hot-path matcher never pays the cold re.compile fan-out
  147. # (re._cache can be evicted by unrelated regex work).
  148. _RE_FLAGS = re.IGNORECASE | re.DOTALL
  149. # Positionless hardline rules matched against quote-masked variants (see above).
  150. _QUOTE_MASKED_HARDLINE_DESCRIPTIONS = frozenset({"redirect to raw block device", "fork bomb"})
  151. HARDLINE_PATTERNS_COMPILED = [
  152. (re.compile(p, _RE_FLAGS), d, d in _QUOTE_MASKED_HARDLINE_DESCRIPTIONS) for p, d in HARDLINE_PATTERNS
  153. ]
  154. # Commands that hand a quoted argument to another shell to EXECUTE: quoted text is code, not
  155. # prose, so quote-masked hardline rules scan the raw string.
  156. _SHELL_CARRIER_NAMES = frozenset({"eval", "sh", "bash", "zsh", "ksh", "dash", "source", "."})
  157. # The shell members of _SHELL_CARRIER_NAMES, as one tuple plus alternation shared by every
  158. # pipe/decode/process-substitution/heredoc pattern and the structural -c payload scan, so
  159. # the shell-name list cannot drift between them again (the drift let `curl url | zsh` and
  160. # `dash -c` through while bash/sh were flagged).
  161. _SHELL_NAMES = ("bash", "sh", "zsh", "ksh", "dash")
  162. _SHELL_NAMES_RE = "|".join(_SHELL_NAMES)
  163. # Pipe-to-shell sink: the shell may sit behind launcher words (`sudo -u root -E`, `env -i A=1`,
  164. # `/usr/bin/env`, `timeout 60`, `busybox`), be path-qualified (/bin/sh, ./zsh,
  165. # C:\\msys64\\...\\bash.exe), quoted, .exe-suffixed, or followed by a redirect instead of
  166. # whitespace. The lookahead terminator keeps `shred`, `shellcheck` and `bash-helper` out.
  167. # fish/csh/tcsh read a script on stdin too, so they count here (they take no part in the -c
  168. # payload scan). Launcher tokens fall in disjoint classes (a flag that takes a value -- sudo/env
  169. # `-u USER`, `-g`, `-C` ... -- a bare flag, VAR=value, a duration) inside atomic/possessive
  170. # groups, so each token has one parse and the scan stays linear; `sudo -u bash cat` reads `bash`
  171. # as the user, not the shell.
  172. _PIPE_SHELL_NAMES_RE = "|".join((*_SHELL_NAMES, "fish", "csh", "tcsh", "ash"))
  173. _PIPE_PATH_PREFIX_RE = r"(?>(?:[a-zA-Z]:)?(?:[.\w~-]*+[/\\])*+)"
  174. # Launchers that run their trailing words as a command, with the short/long options that take a
  175. # SEPARATE value word. One shared table made a bare flag (`time -p`, `exec -a x`) swallow the shell
  176. # name as its value; per-launcher rows let `-p` be bare for time/command and valued for sudo.
  177. _PIPE_LAUNCHER_VALUE_FLAGS = {
  178. "sudo": ("ugUCDhpRrtT", ("user", "group", "other-user", "close-from", "chdir", "host", "prompt",
  179. "role", "type", "command-timeout", "chroot")),
  180. "doas": ("uC", ()),
  181. # `-S STRING` is the command itself (split into words), never a swallowed value.
  182. "env": ("uCa", ("unset", "chdir", "argv0")),
  183. "exec": ("a", ()),
  184. "command": ("", ()),
  185. "nohup": ("", ()),
  186. "nice": ("n", ("adjustment",)),
  187. "time": ("fo", ("format", "output")),
  188. "timeout": ("sk", ("signal", "kill-after")),
  189. "stdbuf": ("ioe", ("input", "output", "error")),
  190. "busybox": ("", ()),
  191. "setsid": ("", ()),
  192. "ionice": ("cnpPu", ("class", "classdata", "pid", "pgid", "uid")),
  193. }
  194. def _pipe_launcher_re(name: str, short: str, long_opts: tuple) -> str:
  195. valued = [rf"(?-i:-[^\W{short}\d-]*+[{short}])\s++[^\s|;&<>]++"] if short else []
  196. valued += [rf"--(?:{'|'.join(long_opts)})\s++[^\s|;&<>]++"] if long_opts else []
  197. duration = r"(?:infinity|\d++(?:\.\d++)?(?:e\d++)?|\.\d++)[smhd]?(?=\s)"
  198. args = "|".join([*valued, r"-[^\s|;&<>]++", r"\w++=[^\s|;&<>]*+", duration])
  199. return rf"{name}(?:\s++(?>{args}))*+"
  200. _PIPE_WRAPPERS_RE = (
  201. rf"(?>{_PIPE_PATH_PREFIX_RE}(?:"
  202. + "|".join(_pipe_launcher_re(n, *f) for n, f in _PIPE_LAUNCHER_VALUE_FLAGS.items())
  203. + r")\s++)*+"
  204. )
  205. _PIPE_SHELL_TOKEN_RE = (
  206. rf"['\"]?{_PIPE_PATH_PREFIX_RE}(?:{_PIPE_SHELL_NAMES_RE})(?:\.exe)?(?=['\"\s;&|<>)]|$)"
  207. )
  208. # Lookup/list modes name a shell without running it (`command -v bash`, `sudo -l bash`). The `l`
  209. # must be a bare flag: in `sudo -pl bash` / `-ul` it is the VALUE of -p / -u and bash runs.
  210. _PIPE_LOOKUP_ONLY_RE = re.compile(
  211. rf"{_PIPE_PATH_PREFIX_RE}(?:command\s+-[a-zA-Z]*[vV]|sudo\s+(?:-(?-i:[^\W\dugUCDhpRrtT])*l|--list))\b",
  212. re.IGNORECASE,
  213. )
  214. _PIPE_SINK_RE = re.compile(rf"(?:\|&?\s*|>\(\s*){_PIPE_WRAPPERS_RE}{_PIPE_SHELL_TOKEN_RE}", re.IGNORECASE)
  215. _PIPE_FETCH_RE = re.compile(r"\b(?:curl|wget)\b", re.IGNORECASE)
  216. _PIPE_OPERATOR_RE = re.compile(r"\|&?\s*|>\(\s*")
  217. # The widened sink skips `||` (an or-list, not a pipe); main's raw sink keeps the old operator.
  218. _PIPE_ONLY_OPERATOR_RE = re.compile(r"(?<!\|)\|(?!\|)&?\s*|>\(\s*")
  219. # main's original sink (bare or slash-prefixed shell), scanned on the RAW text: a quoted payload handed
  220. # to any carrier (su -c, ssh, xargs, docker exec ...) stays flagged exactly as before.
  221. _PIPE_BARE_SINK_RE = re.compile(rf"\|\s*(?:[/\w]*/)?(?:{_SHELL_NAMES_RE})(?:\s|$|-c)", re.IGNORECASE)
  222. _WMIC_SHADOW_DELETE_DESCRIPTION = "delete volume shadow copies (wmic)"
  223. _WMIC_DELETE_IN_SEGMENT_RE = re.compile(r"\sshadowcopy\b.*\sdelete\b", re.IGNORECASE | re.DOTALL)
  224. # Leading switches of a carrier before its script: cmd /s /q /c, start /b "title", pwsh -NoProfile
  225. # -ExecutionPolicy Bypass -Command, doas -u x, ssh -i k -p 22 host. Option values are taken only for
  226. # the options known to take one, so `powershell -NoProfile wmic ...` keeps wmic as the script.
  227. _CARRIER_SWITCHES_RE = re.compile(
  228. r"""(?:\s*+(?:/\w++(?::\S*+)?|-(?:ExecutionPolicy|ep|ex|WindowStyle|w|Version|v|WorkingDirectory|wd"""
  229. r"""|ConfigurationName|config|SettingsFile|settings|OutputFormat|of|InputFormat|inp|if|u|p|i|o|l|J|F|a)"""
  230. r"""\s++(?:"[^"\n]*+"|'[^'\n]*+'|[^\s;&|]++)|--?\w*+))*+""",
  231. re.IGNORECASE,
  232. )
  233. # `start "title" prog`: start alone takes a quoted window title before the program.
  234. _START_TITLE_RE = re.compile(r"""\s*+"[^"\\\n]*+"(?=\s*+["\w\\/])""")
  235. class _WmicShadowDeleteMatcher:
  236. """`wmic ... shadowcopy ... delete` run as a command: the program word (bare, a full path, behind
  237. sudo/env/nohup...) is wmic and its own command carries shadowcopy and then delete. The script of a
  238. carrier (cmd /c, pwsh, powershell, start, call, doas, wsl, xargs, ssh host) is walked the same way,
  239. so `cmd /c "echo hi & wmic shadowcopy delete"` counts. Nesting is capped, so the scan stays linear."""
  240. _CARRIERS = frozenset({"cmd", "pwsh", "powershell", "start", "call", "doas", "wsl", "xargs", "ssh"})
  241. _MAX_DEPTH = 3
  242. def search(self, text: str, depth: int = 0):
  243. if "wmic" not in text.lower():
  244. return None
  245. for _, end, word in _iter_shell_command_word_spans(text):
  246. # Split the path before deobfuscating: the shell reading drops the backslashes of C:\...\wmic.
  247. name = _deobfuscate_shell_word_for_detection(re.split(r"[\\/]", word)[-1]).strip("\"'").lower().removesuffix(".exe")
  248. if name == "wmic":
  249. if match := _WMIC_DELETE_IN_SEGMENT_RE.search(" " + _shell_command_segment(text, end)):
  250. return match
  251. elif name in self._CARRIERS and depth < self._MAX_DEPTH:
  252. for script in self._carrier_script(name, _shell_command_segment(text, end)):
  253. if match := self.search(script, depth + 1):
  254. return match
  255. return None
  256. @staticmethod
  257. def _carrier_script(name: str, rest: str) -> tuple[str, ...]:
  258. rest = _CARRIER_SWITCHES_RE.sub("", rest, count=1)
  259. if name == "start":
  260. rest = _CARRIER_SWITCHES_RE.sub("", _START_TITLE_RE.sub("", rest, count=1), count=1)
  261. rest = rest.lstrip()
  262. if name == "ssh": # the first word after the options is the host
  263. rest = re.sub(r"^[^\s;&|]+", "", rest, count=1)
  264. rest = _CARRIER_SWITCHES_RE.sub("", rest, count=1).lstrip()
  265. rest = re.sub(r"^-(?:Command|c)\b\s*", "", rest, flags=re.IGNORECASE)
  266. rest = rest.rstrip().replace('\\"', '"')
  267. if rest[:1] not in ("'", '"'):
  268. return (rest,)
  269. # A leading quote opens either the whole script (`"wmic shadowcopy delete"`) or just the program
  270. # word (`"wmic" shadowcopy delete`); the normalized variant drops `\"` pairs, so its closing quote
  271. # can be missing. Read it both ways.
  272. return (rest, rest[1:-1] if len(rest) > 1 and rest[-1] == rest[0] else rest[1:])
  273. class _PipeRemoteToShellMatcher:
  274. """Linear stand-in for the pipe-to-shell regex: find the first curl/wget, then try a sink only at
  275. each later `|` / `>(` (a leading `.*` rescanned the tail from every fetch word: quadratic).
  276. Two sinks: main's bare-shell sink on the raw text, so no case main caught is lost; and the widened
  277. launcher/path/quoted sink on quote-masked text, so prose that merely mentions a wrapped pipe stays
  278. quiet (raw again under an eval / sh -c carrier, whose quoted argument is code)."""
  279. def search(self, text: str):
  280. fetch = _PIPE_FETCH_RE.search(text)
  281. if fetch is None:
  282. return None
  283. if match := self._scan(text, fetch.end(), _PIPE_BARE_SINK_RE, _PIPE_OPERATOR_RE, lookup_guard=False):
  284. return match
  285. widened = text if _contains_shell_carrier(text) else _mask_quoted_prose(text)
  286. return self._scan(widened, fetch.end(), _PIPE_SINK_RE, _PIPE_ONLY_OPERATOR_RE, lookup_guard=True)
  287. @staticmethod
  288. def _scan(text: str, pos: int, sink: re.Pattern, operator: re.Pattern, lookup_guard: bool):
  289. while (op := operator.search(text, pos)) is not None:
  290. match = sink.match(text, op.start())
  291. if match and not (lookup_guard and _PIPE_LOOKUP_ONLY_RE.match(text, op.end())):
  292. return match
  293. pos = op.end()
  294. return None
  295. def _contains_shell_carrier(command: str) -> bool:
  296. """Return whether any command-position word is a shell-carrying command."""
  297. return any(
  298. os.path.basename(_deobfuscate_shell_word_for_detection(word)).lower() in _SHELL_CARRIER_NAMES
  299. for _, _, word in _iter_shell_command_word_spans(command)
  300. )
  301. def _mask_quoted_prose(command: str) -> str:
  302. """Blank out quoted string CONTENT for positionless hardline matching (detection-only).
  303. Quote characters stay; inside double quotes `$(...)` and backtick spans are kept RAW because
  304. the shell really executes them. An unclosed quote masks to end-of-string, which cannot hide a
  305. runnable command (the shell would not run it either).
  306. Detection-only rewrite used by the quote-masked hardline rules (redirect-to-block-device, fork bomb):
  307. text inside single or double quotes is data the shell passes as an argument, so `echo "cat f >
  308. /dev/sda"` must not trip the unconditional floor (#93392). Unquoted text is untouched.
  309. """
  310. return "".join(
  311. command[i:j] if quote is None or kind in ("quote", "subst") else " " * (j - i)
  312. for kind, i, j, quote in _scan_shell(command, subst="q", naive_backtick=True)
  313. )
  314. # ---- Sudo stdin guard: without SUDO_PASSWORD configured, an explicit "sudo -S" is the LLM piping
  315. # a guessed password via stdin (brute-force vector). Unconditional block.
  316. _SUDO_STDIN_RE = re.compile(r'(?:^|[;&|`\n]|&&|\|\||\$\()\s*sudo\s+-S\b', re.IGNORECASE)
  317. def _check_sudo_stdin_guard(command: str) -> tuple:
  318. """Detect ``sudo -S`` without configured SUDO_PASSWORD -> (is_blocked, description). When
  319. SUDO_PASSWORD is set, ``_transform_sudo_command`` injects ``-S`` itself, so this guard only
  320. fires when the LLM wrote it explicitly."""
  321. if "SUDO_PASSWORD" not in os.environ and _SUDO_STDIN_RE.search(_normalize_command_for_detection(command).lower()):
  322. return (True, "sudo password guessing via stdin (sudo -S)")
  323. return (False, None)
  324. def detect_hardline_command(command: str) -> tuple:
  325. """Check hardline patterns (NEVER bypassable, even in YOLO) -> (is_hardline, description)."""
  326. if _command_parser_limit_exceeded(command):
  327. return (True, _PARSER_LIMIT_DESCRIPTION)
  328. # The malformed-quoting verdict needs the author's quote state. Normalization strips escapes
  329. # (`\"` -> `"`), so a shell-valid pattern like `grep -o "[^\"]*"` lexed as unterminated and was
  330. # reported as a hardline block (118 of 125 hardline blocks in one week of real use, every one a
  331. # benign grep). Only quoted newlines are masked: they are data, and masking keeps quoting intact.
  332. _, malformed_grep = _grep_safe_detection_variant(_mask_quoted_newlines(command))
  333. if malformed_grep:
  334. return (True, _MALFORMED_EXEC_DESCRIPTION)
  335. for command_variant in _command_detection_variants(command):
  336. variant_lower = command_variant.lower()
  337. masked_lower: str | None = None
  338. for pattern_re, description, quote_masked in HARDLINE_PATTERNS_COMPILED:
  339. if quote_masked and masked_lower is None:
  340. # Positionless rules see quoted prose as DATA, except under shell carriers
  341. # (sh -c, eval, source) whose quoted argument is code — those scan raw. bash -c
  342. # payloads also surface as their own raw variants via _execution_flag_findings.
  343. masked_lower = (
  344. variant_lower if _contains_shell_carrier(command_variant)
  345. else _mask_quoted_prose(command_variant).lower()
  346. )
  347. if pattern_re.search(masked_lower if quote_masked else variant_lower):
  348. return (True, description)
  349. return (False, None)
  350. # ---- Dangerous command patterns -----------------------------------------------------------
  351. # PowerShell accepts any unambiguous parameter prefix (``-r`` is ``-Recurse``, ``-fo`` is
  352. # ``-Force``), so spelling the switches out in full misses ``ri -r -fo``. On Remove-Item and
  353. # Remove-ItemProperty ``-f`` alone is ambiguous with ``-Filter`` and refuses to run; on
  354. # Stop-Process and Stop-Service ``-Force`` is the only F parameter, so ``-f`` already means it.
  355. _PS_RECURSE = r"-r(?:e(?:c(?:u(?:r(?:s(?:e)?)?)?)?)?)?\b"
  356. _PS_FORCE = r"-fo(?:r(?:c(?:e)?)?)?\b"
  357. _PS_FORCE_ONLY_F = r"-f(?:o(?:r(?:c(?:e)?)?)?)?\b"
  358. _PS_DELETE_VERB = r"(?:remove-item|rmdir|erase|del|rd|ri|rm)"
  359. # powershell/pwsh as the program a command runs: line start, a shell separator, or a launcher
  360. # (sudo -u bob, env FOO=1, timeout 5, xargs, wsl, cmd /c, ssh -p 22 <host>; option tokens may carry
  361. # one value; ssh and cmd /c may quote the remote command), with an optional directory prefix (`C:\\...\\powershell.exe`). The prefix excludes
  362. # `(){}` and backticks so a run of them is not re-scanned from every position (quadratic).
  363. _PS_LAUNCHER_OPTIONS = r"(?:\s+-\S+(?:\s+[^\s;&|=-][^\s=]*(?!\S))?|\s+\w+=\S*)*"
  364. _PS_EXE_AT_COMMAND_POSITION = (
  365. r"(?:^|[;&|({`\n]"
  366. r"|(?:^|[;&|({`\n])\s*+(?:sudo|doas|env|exec|nohup|start|time|nice|command|xargs|wsl|busybox|timeout\s+\S+)"
  367. + _PS_LAUNCHER_OPTIONS + r"\s"
  368. r"|\bcmd(?:\.exe)?\s+/[ck]\s+[\"']?"
  369. r"|(?:^|[;&|({`\n])[^\S\n]*+ssh(?:[^\S\n]+-[^\s;&|]+(?:[^\S\n]+[^\s;&|-][^\s;&|]*)?)*[^\S\n]+[^\s;&|-][^\s;&|]*[^\S\n]+[\"']?)"
  370. r"\s*(?:[^\s;&|\"'(){}\x60]*[\\/])?(?:powershell|pwsh)(?:\.exe)?(?=\s)"
  371. )
  372. DANGEROUS_PATTERNS = [
  373. (r'\brm\s+(-[^\s]*\s+)*/', "delete in root path"),
  374. (r'\brm\s+-[^\s]*r', "recursive delete"),
  375. (r'\brm\s+--recursive\b', "recursive delete (long flag)"),
  376. # GNU rm permutes options, so flags may FOLLOW operands (`rm build/ -rf`). The operand run
  377. # cannot cross a command separator (so `rm foo | grep -r` is not attributed to rm), a quote,
  378. # or a bare ` -- ` end-of-options (after which `-rf` is a literal filename). The flag token
  379. # must follow whitespace so the `r` in long options like `--registry` does not count.
  380. (r'\brm\s+(?!--(?:\s|$))(?:(?!\s--(?:\s|$))[^\n"\';|&])*\s' r'(?:-[a-z]*r[a-z]*\b|--recursive\b)',
  381. # GNU rm permutes options, so a recursive flag group may legally FOLLOW the operands: `rm build/ -rf`,
  382. # `rm build/ -r -f`, and `rm build/ --recursive --force` are all equivalent to the flags-first
  383. # spellings the two patterns above catch — without this rule they run with no approval prompt at all.
  384. # Port of openai/codex#33464 ("recognize force options when they follow operands").
  385. "recursive delete (flags after operands)"),
  386. # Windows cmd/powershell destructive built-ins: gate only when executed through the shell so
  387. # prose/filenames containing "del"/"rd" do not trip.
  388. (r'\bcmd(?:\.exe)?\s+/(?:c|k)\s+.*\b(?:del|erase|rd|rmdir)\b', "Windows cmd destructive delete"),
  389. # PowerShell runs the verb as default positional arg (no -Command needed); anchor the verb to command
  390. # position (after leading -Flag switches and optional -Command/-c) so `-File c:\del-logs\run.ps1` is not caught.
  391. (r'\b(?:powershell|pwsh)(?:\.exe)?\b(?:\s+-\S+)*\s+(?:-(?:command|c)\s+)?["\']?' + _PS_DELETE_VERB + r'\b', "Windows PowerShell destructive delete"),
  392. # The same delete later in the script: `powershell -NoProfile "gci C:\\data | ri -r -fo"` (Windows
  393. # PowerShell runs a positional argument as the command) puts the verb after a pipe, `;` or a
  394. # `{ }` block, where the rule above walks past it. Like that rule, no switch is required:
  395. # `gci -Recurse -File | ri` deletes every file with none. The executable must be at command
  396. # position (or behind sudo/cmd /c/ssh <host>) and the quote must open its first non-flag argument,
  397. # so `echo powershell "gci | ri"`, `pwsh -v; jq ".a | del(.b)"` and `cd ~/powershell-scripts &&
  398. # git grep "a|rd"` stay quiet, and an outer-shell `powershell -c Get-Date; rm notes.txt` is left
  399. # to the rm rules. The span runs to the OUTER quote, so a nested `'C:\\work dir'` cannot end it.
  400. (_PS_EXE_AT_COMMAND_POSITION + r'(?:\s+-\S+)*\s+(?:"[^"\n]*|\'[^\'\n]*)[|;{(]\s*' + _PS_DELETE_VERB + r'(?![\w-])',
  401. "Windows PowerShell destructive delete (later in the script)"),
  402. (r'\b(?:powershell|pwsh)(?:\.exe)?\b.*\s-(?:encodedcommand|enc|e)\b', "PowerShell encoded command execution"),
  403. # ── Windows destructive tier: native Windows EXEs/cmdlets reachable from ANY backend on a
  404. # Windows host (incl. git-bash). Input is lowercased by the variant loop, so patterns are
  405. # lowercase. Each requires the destructive flag/verb so benign usage (`taskkill /IM app.exe`,
  406. # `reg query`, `icacls file`) does NOT prompt. Bare Remove-Item form (ACP clients, pwsh-default
  407. # SSH hosts, or compound commands where `powershell` appeared earlier).
  408. # See #69472.
  409. # PowerShell accepts any unambiguous parameter prefix: `-r`/`-rec`/`-recurs` are -Recurse,
  410. # `-fo`/`-forc` are -Force (validated on Windows PowerShell 5.1, #144024).
  411. (r'\bremove-item\b[^\n;|&]*\s(?:' + _PS_RECURSE + r'|' + _PS_FORCE + r')', "PowerShell destructive delete (Remove-Item)"),
  412. # Remove-Item aliases after a pipe — `gci C:\data | ri -r -fo` runs the same recursive forced
  413. # delete from a positional script whose first command is not itself a delete verb, whether or
  414. # not a `powershell` prefix precedes it.
  415. (r'\|\s*' + _PS_DELETE_VERB + r'(?![\w-])[^\n;|&]*\s(?:' + _PS_RECURSE + r'|' + _PS_FORCE + r')', "PowerShell destructive delete (alias after pipe)"),
  416. # Bare cmd builtins with /s (recurse) or /q (quiet); plain `del file.txt` is covered only by the prefixed rule.
  417. (r'\b(?:del|erase|rd|rmdir)\s+(?:/[a-z]\s+)*/[sq]\b', "Windows destructive delete (recursive/quiet switch)"),
  418. # Remote content piped to Invoke-Expression — PowerShell's `curl | sh`.
  419. (r'\b(?:iwr|invoke-webrequest|invoke-restmethod|irm|curl|wget)\b[^\n]*\|\s*(?:iex|invoke-expression)\b', "pipe remote content to PowerShell (iwr | iex)"),
  420. (r'\b(?:iex|invoke-expression)\s*\(\s*(?:iwr|invoke-webrequest|invoke-restmethod|irm)\b', "execute remote content via Invoke-Expression"),
  421. # Force process kills — Windows analogue of pkill -9.
  422. (r'\btaskkill\b[^\n]*\s/f\b', "force kill processes (taskkill /F)"),
  423. (r'\bstop-process\b[^\n]*\s' + _PS_FORCE_ONLY_F, "force kill processes (Stop-Process -Force)"),
  424. # Volume/disk destruction — Windows analogue of mkfs / dd.
  425. (r'\bformat-volume\b', "format filesystem (Format-Volume)"),
  426. (r'\bclear-disk\b', "wipe disk (Clear-Disk)"),
  427. (r'\bdiskpart\b', "disk partitioning (diskpart)"),
  428. (r'\bformat(?:\.com)?\s+[a-z]:', "format drive (format.com)"),
  429. (r'\bcipher\s+/w\b', "wipe free space (cipher /w)"),
  430. # ACL destruction — Windows analogue of chmod 777.
  431. (r'\bicacls\b[^\n]*\s/grant\b[^\n]*\b(?:everyone|todos|jeder|tout\s+le\s+monde|\*s-1-1-0)\b', "grant Everyone access (icacls)"),
  432. (r'\bicacls\b[^\n]*\s/reset\b', "reset ACLs recursively (icacls /reset)"),
  433. # Backup/recovery destruction — classic ransomware prep.
  434. (r'\bvssadmin\b[^\n]*\bdelete\s+shadows\b', "delete volume shadow copies (vssadmin)"),
  435. # WMIC twin of the vssadmin rule — same shadow-copy destruction, older spelling. Not compiled:
  436. # DANGEROUS_PATTERNS_COMPILED swaps in _WmicShadowDeleteMatcher, which checks each command-position
  437. # word (wrappers, full paths, cmd /c, pwsh) instead of an unanchored regex over the whole line.
  438. (r'wmic shadowcopy delete', _WMIC_SHADOW_DELETE_DESCRIPTION),
  439. (r'\bwbadmin\b[^\n]*\bdelete\b', "delete backups (wbadmin)"),
  440. (r'\bbcdedit\b[^\n]*\s/set\b', "modify boot configuration (bcdedit /set)"),
  441. # Registry deletion with force flag.
  442. (r'\breg(?:\.exe)?\s+delete\b', "registry delete (reg delete)"),
  443. (r'\bremove-itemproperty\b[^\n]*\s' + _PS_FORCE, "registry value delete (Remove-ItemProperty -Force)"),
  444. # Windows service/system stop — analogue of systemctl stop.
  445. (r'\bstop-service\b[^\n]*\s' + _PS_FORCE_ONLY_F, "force stop service (Stop-Service -Force)"),
  446. (r'\bsc(?:\.exe)?\s+(?:stop|delete)\b', "stop/delete service (sc)"),
  447. # Windows-form credential paths; the POSIX ~/.ssh patterns never match drive-letter or backslash spellings.
  448. (r'\busers[\\/][^\\/\s]+[\\/]\.ssh\b', "access to SSH keys (Windows path)"),
  449. (r'\bappdata[\\/](?:local|roaming)[\\/]hermes[^\n]*\.env\b', "access to Hermes secrets (Windows path)"),
  450. # ── end of Windows tier
  451. (r'\bchmod\s+(-[^\s]*\s+)*(777|666|o\+[rwx]*w|a\+[rwx]*w)\b', "world/other-writable permissions"),
  452. (r'\bchmod\s+--recursive\b.*(777|666|o\+[rwx]*w|a\+[rwx]*w)', "recursive world/other-writable (long flag)"),
  453. (r'\bchown\s+(-[^\s]*)?R\s+root', "recursive chown to root"),
  454. (r'\bchown\s+--recur[a-z]*\b.*root', "recursive chown to root (long flag)"),
  455. # _CMDPOS-anchored like the hardline twins: quoted prose mentioning mkfs/dd must not require approval to echo.
  456. # See #93392.
  457. (_CMDPOS + r'mkfs\b', "format filesystem"),
  458. (_CMDPOS + r'dd\s+.*if=', "disk copy"),
  459. (r'>\s*/dev/sd', "write to block device"),
  460. (r'\bDROP\s+(TABLE|DATABASE)\b', "SQL DROP"),
  461. # [^\n]* not .*: under DOTALL a WHERE on the *next* line would satisfy the lookahead and
  462. # silently allow DELETE without WHERE.
  463. (r'\bDELETE\s+FROM\b(?![^\n]*\bWHERE\b)', "SQL DELETE without WHERE"),
  464. (r'\bTRUNCATE\s+(TABLE)?\s*\w', "SQL TRUNCATE"),
  465. (rf'>\s*{_SYSTEM_CONFIG_PATH}', "overwrite system config"),
  466. (r'\bsystemctl\s+(-[^\s]+\s+)*(stop|restart|disable|mask)\b', "stop/restart system service"),
  467. (r'\bkill\s+-9\s+-1\b', "kill all processes"),
  468. (r'\bpkill\s+-9\b', "force kill processes"),
  469. # killall with SIGKILL (-9 / -KILL / -s KILL / -SIGKILL) and `killall -r <regex>` broad sweeps
  470. # that can wipe unrelated processes.
  471. (r'\bkillall\s+(-[^\s]*\s+)*-(9|KILL|SIGKILL)\b', "force kill processes (killall -KILL)"),
  472. (r'\bkillall\s+(-[^\s]*\s+)*-s\s+(KILL|SIGKILL|9)\b', "force kill processes (killall -s KILL)"),
  473. (r'\bkillall\s+(-[^\s]*\s+)*-r\b', "kill processes by regex (killall -r)"),
  474. (r':\(\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;\s*:', "fork bomb"),
  475. # Shell -c is parsed structurally by _execution_flag_findings(); a regex searching a dash-token
  476. # for "c" also matched --norc/--rcfile/--restricted.
  477. # Not compiled: DANGEROUS_PATTERNS_COMPILED swaps in _PipeRemoteToShellMatcher for this description;
  478. # the string only feeds the legacy approval-key alias below, so editing it changes no verdict.
  479. (rf'\b(curl|wget)\b.*(?:\|&?\s*{_PIPE_WRAPPERS_RE}|>\(\s*){_PIPE_SHELL_TOKEN_RE}', "pipe remote content to shell"),
  480. (rf'(?<![\w.-])(?:{_SHELL_NAMES_RE})\s+<\s*<?\s*\(\s*(curl|wget)\b', "execute remote script via process substitution"),
  481. (_CMDPOS + r'(?:source|\.)\s+<\s*\(\s*(curl|wget)\b', "execute remote script via process substitution"),
  482. # eval/source/. $(curl ...) — equivalent to piping remote content to a shell.
  483. (r'(?:\beval\b|\bsource\b|\.)\s*(?:\$\(\s*|`\s*)(?:curl|wget)\b', "execute remote content via command substitution"),
  484. # Cloud instance-metadata (IMDS) credential endpoints — deterministic containment-escape
  485. # detection. On a cloud VM these serve live IAM/service-account credentials to ANY local
  486. # process with no auth, so a fetch is credential exfiltration unless the operator expects it.
  487. # The host literals have no other use, so their appearance ANYWHERE in the command (any HTTP
  488. # client, env assignment, or script argument) is the signal; lookarounds keep other 169.254.x.x
  489. # link-local addresses and longer dotted strings out. This prompts for approval (legit uses
  490. # exist on real cloud VMs) — it is NOT a hardline block. Covers the link-local IPv4 endpoint
  491. # (AWS/Azure/GCP/OpenStack), its AWS IPv6 form fd00:ec2::254, the GCP hostname, and Alibaba
  492. # Cloud's 100.100.100.200.
  493. (r'(?<![\d.])(?:169\.254\.169\.254|100\.100\.100\.200)(?![\d.])'
  494. r'|(?<![\w.-])metadata\.google\.internal(?![\w.-])'
  495. r'|fd00:ec2::254',
  496. "cloud metadata endpoint access (instance credentials)"),
  497. # Decode-and-execute: `echo <base64> | base64 -d | bash` carries no dangerous keywords in the
  498. # raw text yet runs arbitrary commands.
  499. (rf'\b(base64|base32|base16)\s+(?:-[dD]|--decode)\b.*\|\s*\b(?:{_SHELL_NAMES_RE})\b', "pipe decoded content to shell (possible command obfuscation)"),
  500. # xxd uses -r for decode, not -d.
  501. (rf'\bxxd\s+-r\b.*\|\s*\b(?:{_SHELL_NAMES_RE})\b', "pipe xxd-decoded content to shell (possible command obfuscation)"),
  502. # `echo 'eq -pe v/' | tr 'eqv' 'rmf' | bash` decodes to `rm -rf /`.
  503. (rf'\becho\b[^|]*\|\s*\btr\b[^|]*\|\s*\b(?:{_SHELL_NAMES_RE})\b', "pipe tr-transformed output to shell (possible command obfuscation)"),
  504. (rf'\bopenssl\b.*\b(?:base64|enc)\b[^|]*\s+-[dD]\b[^|]*\|\s*\b(?:{_SHELL_NAMES_RE})\b',
  505. "pipe openssl-decoded content to shell (possible command obfuscation)"),
  506. # Credential exfiltration: a curl/wget request BODY (not a header — `-H "Authorization: Bearer $KEY"` is
  507. # ordinary API use) carrying a secret-named variable or a credential file, or a credential file piped into
  508. # an uploading curl/wget. Flag letters are case-sensitive (-d/-F/-T upload; -D/-f/-t do not).
  509. (rf'{_UPLOAD_FLAG}(?:[\s=]+|(?=[@"\'$]))(?:"[^"]*(?:{_SECRET_VAR}|{_SECRET_FILE})|\'[^\']*{_SECRET_FILE}'
  510. rf'|\$\([^)]*{_SECRET_FILE}|\S*?(?:{_SECRET_VAR}|{_SECRET_FILE}))',
  511. _CREDENTIAL_UPLOAD_DESCRIPTION),
  512. (rf'{_SECRET_FILE}[^|;&\n]*\|[^;&|\n]*\b(?:curl|wget)\b[^;&|\n]*{_UPLOAD_FLAG}',
  513. "pipe a credential file into a curl/wget upload (possible exfiltration)"),
  514. # Invisible / bidirectional Unicode controls make the command the user approves differ from what runs.
  515. # ZWJ between emoji (family/flag sequences) is ordinary text; variation selectors are checked on the
  516. # raw command in detect_dangerous_command.
  517. (rf'[\u200b\u200c\u200e\u200f\u202a-\u202e\u2060-\u2064\u2066-\u2069\ufeff\u180e\U000e0000-\U000e007f]'
  518. rf'|(?<![{_EMOJI_RANGE}\ufe0f])\u200d|\u200d(?![{_EMOJI_RANGE}])',
  519. _INVISIBLE_UNICODE_DESCRIPTION),
  520. (rf'\btee\b.*["\']?{_SENSITIVE_WRITE_TARGET}', "overwrite system file via tee"),
  521. (rf'>>?\s*["\']?{_SENSITIVE_WRITE_TARGET}', "overwrite system file via redirection"),
  522. (rf'\btee\b.*["\']?{_PROJECT_SENSITIVE_WRITE_TARGET}["\']?{_WRITE_TARGET_BOUNDARY}', "overwrite project env/config via tee"),
  523. (rf'>>?\s*["\']?{_PROJECT_SENSITIVE_WRITE_TARGET}["\']?{_WRITE_TARGET_BOUNDARY}', "overwrite project env/config via redirection"),
  524. (r'\bxargs\s+.*\brm\b', "xargs with rm"),
  525. # -execdir has the same semantics as -exec (runs in each match's directory).
  526. (r'\bfind\b.*-exec(?:dir)?\s+(/\S*/)?rm\b', "find -exec/-execdir rm"),
  527. # Unquoted brace/glob spellings the shell can expand into the flags above at run time
  528. # (`find . -{delete,print}`, `find . -del*`). Additive: catches these spellings only; approval is
  529. # still decided from source text, so `$var`/`$(...)`-built words are not covered here. `find` must
  530. # be the command word and the dynamic word a whitespace-delimited token; both rules are matched
  531. # against the quote-masked variant (_QUOTE_MASKED_DANGEROUS_DESCRIPTIONS) because a quoted glob
  532. # (`find . -name 'log-del*'`) is a literal predicate argument the shell never expands.
  533. (_CMDPOS + r'find\s[^;|&\n]*(?<!\S)-(?:\{[^}\s]*(?:delete|exec(?:dir)?)[^}\s]*\}|(?:del(?:ete?)?|exec(?:dir)?)[*?\[])',
  534. "find dynamic shell word may expand to destructive flag"),
  535. (r'\bfind\b.*-delete\b', "find -delete"),
  536. # Same for program-bearing read-tool options, which _execution_flag_findings() parses structurally
  537. # only when the option is spelled literally.
  538. (r'\b(?:rg|sort|ag|man)\b[^;|&\n]*(?<!\S)--(?:pre|hostname-bin|compress-program|pager|html)(?:\{|[*?\[])',
  539. "dynamic shell word may expand to arbitrary program execution flag"),
  540. # Gateway lifecycle: stopping/restarting the gateway kills all running agents. Global flags
  541. # between `hermes` and `gateway` (`hermes -p ade gateway restart`) are allowed so a profile flag can't slip past.
  542. (r'\bhermes\s+' + _GLOBAL_FLAGS + r'gateway\s+(stop|restart)\b', "stop/restart hermes gateway (kills running agents)"),
  543. (r'\bhermes\s+update\b', "hermes update (restarts gateway, kills running agents)"),
  544. # Docker/Podman daemon redirect — global flags or env that point the CLI at a DIFFERENT (often remote) daemon:
  545. # `docker -H ssh://prod stop app` looks local but operates on remote infra, so any redirect requires approval
  546. # regardless of subcommand. The flag must be in global position (before the subcommand) and -H/--host/--context
  547. # must carry a value, keeping `docker -h` and `docker run -h <hostname>` out. Listed BEFORE the lifecycle rules so
  548. # a redirected lifecycle command surfaces the more specific reason.
  549. (r'\bdocker\s+' + _CONTAINER_GLOBAL_FLAGS + r'(?:-h|--host)[=\s]+\S+', "docker with remote daemon redirect (-H/--host)"),
  550. (r'\bdocker\s+' + _CONTAINER_GLOBAL_FLAGS + r'(?:-c|--context)[=\s]+\S+', "docker with daemon redirect (--context: alternate daemon)"),
  551. (r'\bdocker\s+context\s+use\b', "docker context use (switches default daemon for future commands)"),
  552. (r'\bpodman\s+' + _CONTAINER_GLOBAL_FLAGS + r'(?:--url|--connection|--identity)[=\s]+\S+', "podman with remote daemon redirect (--url/--connection/--identity)"),
  553. (r'\bpodman\s+' + _CONTAINER_GLOBAL_FLAGS + r'(?:-r\b|--remote\b)', "podman remote mode (-r/--remote: remote daemon)"),
  554. (r'\b(?:docker_host|docker_context|container_host|container_connection)=\S+', "docker/podman daemon redirect via environment (DOCKER_HOST/CONTAINER_HOST)"),
  555. # Container lifecycle (docker.sock mounts let the agent stop/kill containers) always needs
  556. # consent. Global flags between docker/compose and the verb and the legacy `docker-compose`
  557. # binary are allowed so a flag can't slip past.
  558. (r'\bdocker(?:-compose|\s+compose)\s+' + _CONTAINER_GLOBAL_FLAGS + r'(restart|stop|kill|down)\b', "docker compose restart/stop/kill/down (container lifecycle)"),
  559. (r'\bdocker\s+' + _CONTAINER_GLOBAL_FLAGS + r'(restart|stop|kill)\b', "docker restart/stop/kill (container lifecycle)"),
  560. # Gateway protection: never start gateway outside systemd management
  561. (r'gateway\s+run\b.*(&\s*$|&\s*;|\bdisown\b|\bsetsid\b)', "start gateway outside systemd (use 'systemctl --user restart hermes-gateway')"),
  562. (r'\bnohup\b.*gateway\s+run\b', "start gateway outside systemd (use 'systemctl --user restart hermes-gateway')"),
  563. # Self-termination protection: prevent agent from killing its own process
  564. (r'\b(pkill|killall)\b.*\b(hermes|gateway|cli\.py)\b', "kill hermes/gateway process (self-termination)"),
  565. # Self-termination via kill + $(pgrep/pidof): the substitution is opaque to the name-based
  566. # pattern above, so catch the structural form.
  567. (r'\bkill\b.*\$\(\s*(pgrep|pidof)\b', "kill process via pgrep/pidof expansion (self-termination)"),
  568. (r'\bkill\b.*`\s*(pgrep|pidof)\b', "kill process via backtick pgrep/pidof expansion (self-termination)"),
  569. # launchctl-driven gateway stop/restart on macOS (label `ai.hermes.gateway`). Two independent lookaheads, NOT a
  570. # sequential match: a for-loop building the label from a list defined EARLIER (`for item in 'ai.hermes...'; do
  571. # launchctl bootout "$label"`) never has "hermes" after the verb, and that slipped past and restarted 4 gateways
  572. # with zero approval. Erring broad is correct for an approval gate: an extra prompt is cheap.
  573. # Anchor whole-input lookaheads: re.search otherwise rescans every suffix of
  574. # long non-matching commands, holding the GIL and starving Gateway threads.
  575. (r'\A(?=[\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)"),
  576. (rf'\b(cp|mv|install)\b.*\s{_SYSTEM_CONFIG_PATH}', "copy/move file into system config path"),
  577. (rf'\b(cp|mv|install)\b.*\s["\']?{_PROJECT_SENSITIVE_WRITE_TARGET}["\']?{_COMMAND_TAIL}', "overwrite project env/config file"),
  578. # cp/mv/install OVERWRITING a credential/SSH/shell-rc/Hermes file (key implant, login-time
  579. # injection) — pairs the tee/redirection coverage. Anchored to the command tail so only the
  580. # DESTINATION fires; reading OUT of a sensitive path (`cp ~/.ssh/config /tmp/x`) stays safe.
  581. # The trailing `[^\s"\']*` consumes the rest of the destination filename.
  582. # The tee/redirection patterns above already gate _SENSITIVE_WRITE_TARGET (~/.ssh/*,
  583. # ~/.netrc/.pgpass/.npmrc/.pypirc, shell rc files, ~/.hermes/config.yaml/.env), but cp/mv/install was
  584. # only paired for /etc and project-relative env/config — so `cp evil ~/.ssh/authorized_keys` (key
  585. # implant), `cp creds ~/.netrc`, and `cp evil ~/.bashrc` (login-time command injection) slipped through
  586. # with auto-approve. Same unpaired-door rationale as #14639 / the sed-tee-redirect pairing on these
  587. # targets. `authorized_keys` after the `~/.ssh/` fragment).
  588. (rf'\b(cp|mv|install)\b.*\s["\']?{_SENSITIVE_WRITE_TARGET}[^\s"\']*["\']?{_COMMAND_TAIL}', "copy/move file into sensitive credential/SSH/shell-rc path"),
  589. # In-place edits mutate the file directly, bypassing redirection/tee/cp coverage; gate the same
  590. # startup/credential files.
  591. (rf'\bsed\s+-[^\s]*i.*(?:{_USER_SENSITIVE_WRITE_TARGET})[^\s"\']*', "in-place edit of sensitive credential/SSH/shell-rc path"),
  592. (rf'\bsed\s+--in-place\b.*(?:{_USER_SENSITIVE_WRITE_TARGET})[^\s"\']*', "in-place edit of sensitive credential/SSH/shell-rc path (long flag)"),
  593. (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)"),
  594. (rf'\bsed\s+-[^\s]*i.*\s{_SYSTEM_CONFIG_PATH}', "in-place edit of system config"),
  595. (rf'\bsed\s+--in-place\b.*\s{_SYSTEM_CONFIG_PATH}', "in-place edit of system config (long flag)"),
  596. # sed -i on Hermes config/.env bypasses the redirection/tee rules; pairs the file_tools
  597. # write_file/patch deny so the terminal side is not an open door.
  598. # In-place edit of a Hermes-managed security file (~/.hermes/config.yaml or .env). sed -i bypasses the
  599. # redirection/tee patterns above because it mutates the file directly. See #14639.
  600. (rf'\bsed\s+-[^\s]*i.*(?:{_HERMES_CONFIG_PATH}|{_HERMES_ENV_PATH})', "in-place edit of Hermes config/env"),
  601. (rf'\bsed\s+--in-place\b.*(?:{_HERMES_CONFIG_PATH}|{_HERMES_ENV_PATH})', "in-place edit of Hermes config/env (long flag)"),
  602. # perl/ruby -i: the flag may be its own token after other flags (`-p -i -e`), combined (`-pi`), or carry a backup
  603. # suffix (`-i.bak`), so match any flag token containing `i` anywhere; `perl -e '...'` (no -i) does not trip.
  604. # perl -i and ruby -i perform the same in-place mutation as sed -i but are not caught by the -e/-c
  605. # script-execution pattern above (which targets code evaluation, not file mutation). Pairs the sed -i
  606. # coverage from #14639.
  607. (rf'\b(?:perl|ruby)\b.*(?:^|\s)-[^\s]*i\b.*(?:{_HERMES_CONFIG_PATH}|{_HERMES_ENV_PATH})', "in-place edit of Hermes config/env (perl/ruby)"),
  608. # Interpreter heredocs are handled by _execution_flag_findings(); only shell heredocs stay
  609. # regex-based. `bash <<'EOF'` runs arbitrary commands without triggering the `bash -c` path.
  610. (rf'\b(?:{_SHELL_NAMES_RE})\s+<<', "shell execution via heredoc"),
  611. # Git destructive operations. `git reset --hard` accepts any unambiguous long-flag prefix (--h,
  612. # --ha, --har): --hard is the only reset mode starting with "h", and `--help` is special-cased
  613. # by git before mode resolution.
  614. (r'\bgit\s+reset\s+--h(?:a(?:r(?:d)?)?)?\b', "git reset --hard (destroys uncommitted changes)"),
  615. (r'\bgit\s+push\b.*--forc[a-z]*\b', "git force push (rewrites remote history)"),
  616. (r'\bgit\s+push\b.*-f\b', "git force push short flag (rewrites remote history)"),
  617. (r'\bgit\s+clean\s+-[^\s]*f', "git clean with force (deletes untracked files)"),
  618. # `-D` = `-d --force`: only the capital short flag is force-delete, so the group opts out of
  619. # the module-wide re.IGNORECASE and relies on _lower_preserving_flags keeping dash-prefixed
  620. # tokens' case in the detection input (every other pattern matches case-insensitively and is
  621. # unaffected). The safe merged-only -d / --delete stays ungated by design — git itself refuses
  622. # to delete a branch that is not fully merged.
  623. (r'\bgit\s+branch\s+(?-i:-D)\b', "git branch force delete"),
  624. # `-D` = `-d --force`; the long spellings are different tokens, so match delete+force in either order, bounded to
  625. # one command segment (no `;`/`|`/`&`/newline) so an unrelated later command isn't contaminated.
  626. (r'\bgit\s+branch\b[^;|&\n]*?(?:-d\b|--delete\b)[^;|&\n]*?(?:-f\b|--force\b)', "git branch force delete (long flags)"),
  627. (r'\bgit\s+branch\b[^;|&\n]*?(?:-f\b|--force\b)[^;|&\n]*?(?:-d\b|--delete\b)', "git branch force delete (long flags, force-first)"),
  628. # chmod +x then immediate run: the script content may hold dangerous commands individual patterns miss.
  629. (r'\bchmod\s+\+x\b.*[;&|]+\s*\./', "chmod +x followed by immediate execution"),
  630. # Sudo stdin/askpass/shell/list-privs flags. The agent has no TTY, so sudo invocations that succeed
  631. # non-interactively read the password from stdin (-S) or askpass (-A); -s (shell) and -a (list) are gated as
  632. # privilege chains (read SUDO_PASSWORD from .env -> sudo -S -s). Plain `sudo cmd` is TTY-bound and excluded. Input
  633. # is lowercased, so S/s and A/a collapse. Lazy `[^;|&\n]*?` allows flag args without spanning separators. sudo
  634. # resolves unambiguous long-flag prefixes: `--stdin` is the only long option starting with "st", `--askpass` the
  635. # only one starting with "a".
  636. (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)"),
  637. # Combined short-flag form (-nS, -sa, -las).
  638. (r'\bsudo\b[^;|&\n]*?\s+-[a-z]*[sa][a-z]*\b', "sudo with combined-flag privilege escalation"),
  639. # Package-manager uninstall commands can remove installed software outside
  640. # the current project (notably `npm uninstall -g`). Treat their destructive
  641. # subcommands like other state-removing operations while leaving installs
  642. # and updates alone.
  643. # _CMDPOS-anchored (quoted prose like `git commit -m "npm uninstall docs"` is data); the
  644. # option group also swallows one operand (`--prefix DIR`, `--proxy URL`, `--cwd DIR`).
  645. (_CMDPOS + r'npm\s+' + _PKG_OPTS + r'(?:uninstall|unlink|remove|rm|r|un)\b', "package manager uninstall"),
  646. (_CMDPOS + r'pnpm\s+' + _PKG_OPTS + r'(?:uninstall|remove|rm|un)\b', "package manager uninstall"),
  647. (_CMDPOS + r'yarn\s+' + _PKG_OPTS + r'(?:global\s+)?(?:uninstall|remove)\b', "package manager uninstall"),
  648. (_CMDPOS + r'pip(?:3)?\s+' + _PKG_OPTS + r'uninstall\b', "package manager uninstall"),
  649. (_CMDPOS + r'brew\s+' + _PKG_OPTS + r'(?:uninstall|remove|rm)\b', "package manager uninstall"),
  650. ]
  651. _PIPE_REMOTE_TO_SHELL_DESCRIPTION = "pipe remote content to shell"
  652. _CUSTOM_MATCHERS = {
  653. _PIPE_REMOTE_TO_SHELL_DESCRIPTION: _PipeRemoteToShellMatcher,
  654. _WMIC_SHADOW_DELETE_DESCRIPTION: _WmicShadowDeleteMatcher,
  655. }
  656. DANGEROUS_PATTERNS_COMPILED = [
  657. (_CUSTOM_MATCHERS[d]() if d in _CUSTOM_MATCHERS else re.compile(p, _RE_FLAGS), d)
  658. for p, d in DANGEROUS_PATTERNS
  659. ]
  660. # Dynamic-word rules look for glob/brace characters, which are ordinary data inside quotes
  661. # (`find . -name 'log-del*'`), so they scan the quote-masked variant like the positionless hardline rules.
  662. _QUOTE_MASKED_DANGEROUS_DESCRIPTIONS = frozenset({
  663. "find dynamic shell word may expand to destructive flag",
  664. "dynamic shell word may expand to arbitrary program execution flag",
  665. })
  666. # A quoted `| ri -r` is prose (`Write-Host "run gci | ri -r"`), unless an unquoted launcher runs the
  667. # quoted text as a script: `ssh host "gci | ri -r"`, `cmd /c "..."`, `iex "..."`, `echo "..." | pwsh -`.
  668. _PS_PIPE_ALIAS_DESCRIPTION = "PowerShell destructive delete (alias after pipe)"
  669. _PS_QUOTED_SCRIPT_LAUNCHERS = frozenset({
  670. "ssh", "iex", "invoke-expression", "invoke-command", "icm", "pwsh", "powershell", "wsl", "runas",
  671. "start-process", "start-job", "saps", "cmd", *_SHELL_CARRIER_NAMES,
  672. })
  673. # Content-level rules (ported from tirith in #133832, where they were separate findings with their own
  674. # keys). They describe WHAT the command carries rather than which verb runs it, so a command that also
  675. # matches one of them needs consent for it even when its first, verb-level rule is already approved.
  676. _CONTENT_RULE_DESCRIPTIONS = frozenset({
  677. "upload a secret or credential file via curl/wget (possible exfiltration)",
  678. "pipe a credential file into a curl/wget upload (possible exfiltration)",
  679. "invisible or bidirectional Unicode control character (possible obfuscation)",
  680. })
  681. # Preserve approvals stored under the removed interpreter regex rules.
  682. _REMOVED_PATTERN_KEY_ALIASES = {
  683. "script execution via -e/-c flag": "(python[23]?|perl|ruby|node)\\s+-[ec]\\s+",
  684. "script execution via heredoc": "(python[23]?|perl|ruby|node)\\s+<<",
  685. }
  686. # description <-> legacy regex-derived key (the old approval key, kept for backwards compatibility
  687. # with stored allowlist/session entries), both ways.
  688. _PATTERN_KEY_ALIASES: dict[str, set[str]] = {}
  689. for _canonical_key, _legacy_key in [
  690. (d, p.split(r'\b')[1] if r'\b' in p else p[:20]) for p, d in DANGEROUS_PATTERNS
  691. ] + list(_REMOVED_PATTERN_KEY_ALIASES.items()):
  692. _PATTERN_KEY_ALIASES.setdefault(_canonical_key, set()).update({_canonical_key, _legacy_key})
  693. _PATTERN_KEY_ALIASES.setdefault(_legacy_key, set()).update({_legacy_key, _canonical_key})
  694. # Scoping the force-delete flag to (?-i:-D) changed this pattern's regex-derived legacy key;
  695. # keep the pre-change spelling resolvable so approvals stored under it still match.
  696. _old_branch_key = r"git\s+branch\s+-D"
  697. _PATTERN_KEY_ALIASES.setdefault("git branch force delete", set()).add(_old_branch_key)
  698. _PATTERN_KEY_ALIASES.setdefault(_old_branch_key, set()).add("git branch force delete")
  699. def _approval_key_aliases(pattern_key: str) -> set[str]:
  700. """All approval keys for this pattern: the description plus the historical regex-derived key
  701. older allowlist/session entries may still use."""
  702. return _PATTERN_KEY_ALIASES.get(pattern_key, {pattern_key})
  703. # ---- Detection ----------------------------------------------------------------------------
  704. def _normalize_command_for_detection(command: str) -> str:
  705. """Normalize a command before pattern matching so ANSI escapes, null bytes, Unicode fullwidth
  706. forms, and shell splicing tricks cannot bypass detection."""
  707. from tools.ansi_strip import strip_ansi
  708. command = unicodedata.normalize('NFKC', strip_ansi(command).replace('\x00', ''))
  709. # Collapse backslash-newline continuations (`rm -rf \<newline>/` runs as `rm -rf /`). MUST
  710. # precede the generic escape strip below, whose [^\n] class skips newlines and would leave the
  711. # backslash wedged between tokens, defeating the structured rm/mkfs/dd patterns incl. the HARDLINE floor.
  712. command = re.sub(r'\\\r?\n', '', command)
  713. # Fold absolute user/Hermes home prefixes to ~/ and ~/.hermes/ so the static patterns catch /home/alice/.bashrc
  714. # and C:\Users\alice\.bashrc. Resolved at detection time (not import time) so it tracks HOME/HERMES_HOME set
  715. # later. MUST run before the backslash strip (which would dissolve C:\Users\alice to C:Usersalice). Hermes home
  716. # first: on Windows it nests under the user home, and folding the user home first would eat the prefix it needs.
  717. command = _rewrite_resolved_hermes_home(command)
  718. command = _rewrite_resolved_user_home(command)
  719. # Strip backslash-escapes (r\m -> rm) and empty-string literals (r''m -> rm).
  720. command = re.sub(r'\\([^\n])', r'\1', command)
  721. command = re.sub(r"''|\"\"", '', command)
  722. # Collapse $IFS / ${IFS...} (incl. `${IFS:0:1}`) to a space: IFS defaults to whitespace, so `rm${IFS}-rf${IFS}/`
  723. # runs as `rm -rf /`, and every pattern — incl. the hardline floor — anchors on literal \s between tokens.
  724. return re.sub(r'\$\{IFS\b[^}]*\}|\$IFS\b', ' ', command)
  725. def _lower_preserving_flags(command: str) -> str:
  726. """Lowercase a detection variant for the pattern pass while keeping dash-prefixed tokens
  727. byte-for-byte, so case-dependent flags keep their distinction. All dangerous patterns are
  728. compiled case-insensitively, so preserved flag case is invisible to them except where a
  729. pattern explicitly scopes a case-sensitive group. Non-flag tokens (command words, quoted
  730. prose, paths) are lowercased exactly as before; separators and whitespace are untouched."""
  731. return ''.join(t if t.startswith('-') else t.lower() for t in re.split(r'(\s+)', command))
  732. # Shell metacharacters, quotes, and whitespace that terminate a path token.
  733. _PATH_TOKEN_STOP = r"""\s'"`;|&<>()"""
  734. _PATH_TAIL = r"(?P<tail>(?:[/\\][^/\\" + _PATH_TOKEN_STOP + r"]*)+)"
  735. @functools.lru_cache(maxsize=64)
  736. def _home_prefix_fold_regex(path: str):
  737. """Compile a regex matching *path* as an absolute directory prefix.
  738. Components match with either separator so native Windows, forward-slash, and mixed forms all
  739. fold; the caller normalizes the tail's backslashes to ``/``. A non-empty tail is required, so a
  740. bare home is never folded. Returns ``None`` for an unset/degenerate path (fewer than two
  741. components: ``/``, ``C:\\``, ``""``) so a stray HOME cannot rewrite unrelated prefixes."""
  742. components = [c for c in re.split(r"[/\\]+", path) if c] if path else []
  743. if len(components) < 2:
  744. return None
  745. # Optional leading root separator; a Windows drive letter is a component.
  746. return re.compile(r"[/\\]*" + r"[/\\]+".join(re.escape(c) for c in components) + _PATH_TAIL)
  747. def _fold_home_prefixes(command: str, paths, replacement: str) -> str:
  748. """Fold each resolved home prefix in *command* to *replacement* (no trailing separator; the tail
  749. supplies it). Longest first so a deeper home folds before a shorter overlapping one that would clobber it."""
  750. for path in dict.fromkeys(sorted((p for p in paths if p), key=len, reverse=True)):
  751. pattern = _home_prefix_fold_regex(path)
  752. if pattern is not None:
  753. command = pattern.sub(lambda m: replacement + m.group("tail").replace("\\", "/"), command)
  754. return command
  755. def _rewrite_resolved_user_home(command: str) -> str:
  756. """User home (expanduser / realpath / $HOME) -> ``~/``; no-op when unset, degenerate, or unresolvable."""
  757. try:
  758. # expanduser, realpath, and an explicit HOME — Windows expanduser uses USERPROFILE, not HOME.
  759. home = os.path.expanduser("~")
  760. paths = [home, os.path.realpath(home), os.environ.get("HOME", "")]
  761. except Exception:
  762. return command
  763. return _fold_home_prefixes(command, paths, "~")
  764. def _rewrite_resolved_hermes_home(command: str) -> str:
  765. """Resolved HERMES_HOME (and its realpath) -> ``~/.hermes/`` so the _HERMES_CONFIG_PATH /
  766. _HERMES_ENV_PATH rules match Docker/gateway deployments that spell the absolute path."""
  767. try:
  768. from hermes_constants import get_hermes_home
  769. home = get_hermes_home().expanduser()
  770. paths = [str(home), str(home.resolve(strict=False))]
  771. except Exception:
  772. return command
  773. return _fold_home_prefixes(command, paths, "~/.hermes")
  774. _PARAM_REPLACEMENT_RE = re.compile(r"\$\{[^}/\s]+/[^}/]*/(?P<replacement>[^}]*)\}")
  775. _PARAM_DEFAULT_RE = re.compile(r"\$\{[^}:}\s]+:-(?P<default>[^}]*)\}")
  776. _SIMPLE_SHELL_LITERAL_RE = re.compile(r"^[A-Za-z0-9_./:@%+=,-]+$")
  777. _ENV_ASSIGNMENT_RE = re.compile(r"[A-Za-z_][A-Za-z0-9_]*=.*")
  778. _COMMAND_WRAPPER_WORDS = {"sudo", "env", "exec", "nohup", "setsid", "time", "command", "builtin",
  779. "nice", "timeout", "stdbuf", "ionice", "chrt", "taskset", "chroot"}
  780. _SUDO_OPTIONS_WITH_ARG = {"-c", "--close-from", "-g", "--group", "-h", "--host", "-p", "--prompt", "-u", "--user"}
  781. # Adapted from embwl0x's command-position work in #76063. Option operands are
  782. # data, not executable positions; option spelling remains case-sensitive.
  783. _COMMAND_WRAPPER_OPTIONS_WITH_ARG = {
  784. "chroot": {"--groups", "--userspec"},
  785. "sudo": _SUDO_OPTIONS_WITH_ARG,
  786. "env": {"-a", "--argv0", "-C", "--chdir", "-S", "--split-string", "-u", "--unset"},
  787. "exec": {"-a"}, "nice": {"-n", "--adjustment"},
  788. "time": {"-f", "--format", "-o", "--output"},
  789. "timeout": {"-k", "--kill-after", "-s", "--signal"},
  790. "stdbuf": {"-e", "--error", "-i", "--input", "-o", "--output"},
  791. "ionice": {"-c", "--class", "-n", "--classdata"},
  792. }
  793. _COMMAND_WRAPPER_NON_EXECUTING_OPTIONS = {
  794. "command": {"-v", "-V"}, "chrt": {"-p", "--pid"},
  795. "ionice": {"-p", "--pid", "--pgid", "--uid"}, "taskset": {"-p", "--pid"},
  796. }
  797. _COMMAND_WRAPPER_POSITIONAL_ARGS = {"chroot": 1, "chrt": 1, "taskset": 1, "timeout": 1}
  798. _SHELL_COMMAND_TRANSITIONS = {"if", "then", "else", "elif", "do", "while", "until", "!"}
  799. _SHELL_REDIRECTION_RE = re.compile(r"(?:[0-9]+)?(?:>>|<<|<>|>&|<&|>\||[<>])")
  800. _INTERPRETER_NAME_RES = tuple((family, re.compile(pattern)) for family, pattern in (
  801. ("python", r"py(?:\.exe)?|python[23]?(?:\.\d+)*(?:\.exe)?"), ("node", r"node(?:js)?(?:\.exe)?"),
  802. ("perl", r"perl[0-9]*(?:\.\d+)*(?:\.exe)?"), ("ruby", r"ruby[0-9.]*(?:\.exe)?"), ("php", r"php(?:\.exe)?"),
  803. ("powershell", r"powershell(?:\.exe)?|pwsh(?:\.exe)?"),
  804. ("bun", r"bun(?:\.exe)?"), ("deno", r"deno(?:\.exe)?"),
  805. ))
  806. _INTERPRETER_EXEC_FLAGS = {
  807. "python": {"-c"}, "node": {"-e", "--eval", "-p", "--print"}, "perl": {"-e", "--eval"}, "ruby": {"-e"},
  808. "php": {"-r"}, "powershell": {"-command", "-c", "-file", "-f"},
  809. "bun": {"-e", "--eval"}, "deno": {"eval", "-e", "--eval"},
  810. }
  811. _INTERPRETER_WITH_ARG = {
  812. "python": {"-W", "-X", "--check-hash-based-pycs"},
  813. "node": {"-C", "--conditions", "--cpu-prof-dir", "--diagnostic-dir", "--icu-data-dir", "--import", "--loader",
  814. "--openssl-config", "--require", "--title"},
  815. "perl": {"-0", "-F", "-I", "-M", "-m", "-x"}, "ruby": {"-C", "-E", "-F", "-I", "-K", "-r"},
  816. "php": {"-c", "-d", "-z"},
  817. "powershell": {"-configurationname", "-custompipename", "-executionpolicy", "-inputformat", "-outputformat",
  818. "-settingsfile", "-version", "-windowstyle", "-workingdirectory"},
  819. # Deno's inline-script entry is the bare `eval` subcommand; the only global option that
  820. # may precede it and take a separate value is `-L/--log-level <level>` (`--env-file[=v]`
  821. # binds with `=`; the `--unstable-*` and `--ext` flags belong after `eval`).
  822. "bun": {"--config", "--cwd", "--env-file", "--preload", "--require"}, "deno": {"-L", "--log-level"},
  823. }
  824. _READ_TOOL_EXEC_FLAGS = {
  825. "sort": {"--compress-program"}, "rg": {"--pre", "--hostname-bin"}, "ag": {"--pager"},
  826. "man": {"--pager", "--html", "-P", "-H"},
  827. }
  828. # Required-argument options are ownership boundaries: an option-looking next token is data, not another option. These
  829. # sets mirror the invocation grammar of the supported binaries (ripgrep 14, GNU sort, man-db, and ag 2.2).
  830. _READ_TOOL_LONG_OPTIONS_WITH_ARG = {
  831. "rg": {
  832. "--after-context", "--before-context", "--color", "--colors", "--context", "--context-separator",
  833. "--dfa-size-limit", "--encoding", "--engine", "--field-context-separator", "--field-match-separator",
  834. "--file", "--generate", "--glob", "--hostname-bin", "--hyperlink-format", "--iglob", "--ignore-file",
  835. "--max-columns", "--max-count", "--max-depth", "--max-filesize", "--path-separator", "--pre", "--pre-glob",
  836. "--regex-size-limit", "--regexp", "--replace", "--sort", "--sortr", "--threads", "--type", "--type-add",
  837. "--type-clear", "--type-not",
  838. },
  839. "sort": {
  840. "--batch-size", "--buffer-size", "--compress-program", "--field-separator", "--files0-from", "--key",
  841. "--output", "--parallel", "--random-source", "--sort", "--temporary-directory",
  842. },
  843. "man": {
  844. "--config-file", "--encoding", "--extension", "--locale", "--manpath", "--pager", "--preprocessor",
  845. "--prompt", "--recode", "--sections", "--systems",
  846. },
  847. "ag": {
  848. "--ackmate-dir-filter", "--color-line-number", "--color-match", "--color-path", "--depth",
  849. "--filename-pattern", "--file-search-regex", "--ignore", "--ignore-dir", "--max-count", "--pager",
  850. "--path-to-ignore", "--width", "--workers",
  851. },
  852. }
  853. _READ_TOOL_SHORT_OPTIONS_WITH_ARG = {
  854. "rg": frozenset("efEmjgdtTABCMr"), "sort": frozenset("koStT"), "man": frozenset("CRLmMSserEPp"),
  855. "ag": frozenset("gGmpW"),
  856. }
  857. _GREP_OPTIONS_WITH_ARG = {
  858. "--after-context", "--before-context", "--binary-files", "--context", "--directories", "--devices", "--exclude",
  859. "--exclude-dir", "--exclude-from", "--include", "--label", "--max-count", "--regexp", "--file",
  860. }
  861. _GREP_SHORT_OPTIONS_WITH_ARG = {"A", "B", "C", "D", "d", "e", "f", "m"}
  862. _BASH_OPTIONS_WITH_ARG = {"-O", "+O", "-o", "+o", "--init-file", "--rcfile"}
  863. _BASH_SHORT_OPTION_LETTERS = frozenset("ilrsDcabefhkmnptuvxBCEHPTOo")
  864. _MAX_DETECTION_COMMAND_CHARS, _MAX_SEPARATOR_FREE_COMMAND_CHARS, _MAX_DETECTION_SEGMENTS = 128_000, 4_096, 25_000
  865. _PARSER_LIMIT_DESCRIPTION = "command parser limit exceeded"
  866. _MALFORMED_EXEC_DESCRIPTION = "command parser limit or malformed executable payload"
  867. _GATEWAY_LIFECYCLE_SPLICE_DESCRIPTION = "stop/restart hermes gateway via shell-spliced verb (kills running agents)"
  868. def _command_parser_limit_exceeded(command: str) -> bool:
  869. """Bound all parser work before normalization/tokenization. Separator counting is deliberately
  870. conservative: quoted separators over-count, but crossing the ceiling fails closed rather than
  871. letting an uninspected suffix execute."""
  872. if len(command) > _MAX_DETECTION_COMMAND_CHARS:
  873. return True
  874. # Long separator-free input has no compound-command utility and makes every regex inspect one giant token.
  875. if len(command) > _MAX_SEPARATOR_FREE_COMMAND_CHARS and not any(char in command for char in ";&|\n"):
  876. return True
  877. return sum(command.count(char) for char in ";&|\n") >= _MAX_DETECTION_SEGMENTS
  878. def _backtick_end_from(segment: str, i: int) -> int | None:
  879. """Index of the backtick closing the one opened at ``i``, or None when none follows."""
  880. j = segment.find("`", i + 1)
  881. while j != -1 and segment[j - 1] == "\\":
  882. j = segment.find("`", j + 1)
  883. return None if j == -1 else j
  884. def _shell_tokens_with_spans(segment: str, start: int):
  885. """Return shell words as ``(value, start, end, quoted)`` or ``None`` on malformed quoting.
  886. Deliberately small lexer that never expands shell syntax; it exists to keep source spans (which
  887. ``shlex`` does not expose) for deciding which quoted grep operand is data, not another command.
  888. Lexing stops at the end of the simple command that begins at *start*: an unquoted ``;``, ``|``,
  889. ``&`` or newline, or the ``)`` / backtick that closes the substitution the command sits inside.
  890. Without that, a grep nested as ``"$(grep … | cut …)"`` was lexed together with the enclosing
  891. command's closing quote, read as unbalanced quoting, and reported as a hardline block (546
  892. blocked turns in one run, every one a false positive; ``sed -n "$(grep -n X f | cut -d: -f1),+3p" f``
  893. is the canonical shape)."""
  894. tokens, value, token_start, quote = [], [], None, None
  895. depth = 0 # $(...) nesting opened AFTER start; a closer at depth 0 ends the enclosing substitution
  896. # A backtick opened AFTER start is an operand substitution (``grep -e `cmd` f``); the matching
  897. # closer belongs to it, not to an enclosing backtick the command might sit inside.
  898. in_backtick = False
  899. def flush(end: int) -> None:
  900. raw = segment[token_start:end]
  901. # Only a wholly single-quoted operand is inert shell data. Double quotes still execute $()
  902. # and backticks; unquoted substitutions do too.
  903. inert = (raw.startswith("'") and raw.endswith("'")) or ("='" in raw and raw.endswith("'"))
  904. tokens.append(("".join(value), token_start, end, inert))
  905. end_at = len(segment)
  906. for kind, i, _, _ in _scan_shell(segment, start):
  907. ch = segment[i]
  908. if kind == "char" and not quote:
  909. if ch.isspace() and ch != "\n":
  910. if token_start is not None:
  911. flush(i)
  912. value, token_start = [], None
  913. continue
  914. if segment.startswith("$(", i):
  915. depth += 1
  916. elif ch == "`":
  917. if in_backtick:
  918. in_backtick = False
  919. elif depth == 0 and _backtick_end_from(segment, i) is None:
  920. end_at = i # unmatched: it closes the substitution this command sits inside
  921. break
  922. else:
  923. in_backtick = True
  924. elif ch == ")":
  925. if depth == 0:
  926. end_at = i
  927. break
  928. depth -= 1
  929. elif ch in ";|&\n":
  930. end_at = i
  931. break
  932. if token_start is None:
  933. token_start = i
  934. if kind == "quote":
  935. quote = None if quote else ch
  936. elif kind == "esc":
  937. value.append(segment[i + 1])
  938. elif ch == "\\" and not quote:
  939. return None # dangling backslash
  940. else:
  941. value.append(ch)
  942. if quote:
  943. return None
  944. if token_start is not None:
  945. flush(end_at)
  946. return tokens
  947. def _quoted_grep_pattern_spans(command: str) -> tuple[list[tuple[int, int]], bool]:
  948. """Structurally locate quoted grep PCRE operands -> (spans, malformed). On an ambiguous or
  949. malformed grep parse callers fail closed and use the original command: no text is hidden on
  950. an uncertain parse."""
  951. spans: list[tuple[int, int]] = []
  952. offset = 0
  953. for segment in _iter_top_level_shell_segments(command):
  954. segment_at = command.find(segment, offset)
  955. offset = segment_at + len(segment)
  956. for start, _, word in _iter_shell_command_word_spans(segment):
  957. if os.path.basename(_deobfuscate_shell_word_for_detection(word)).lower() not in {"grep", "egrep"}:
  958. continue
  959. tokens = _shell_tokens_with_spans(segment, start)
  960. if tokens is None:
  961. return [], True
  962. args, pattern_indexes = tokens[1:], []
  963. pcre = explicit_patterns = False
  964. operand_index, i, options = None, 0, True
  965. while i < len(args):
  966. token = args[i][0]
  967. if options and token == "--":
  968. options = False
  969. elif options and token.startswith("--"):
  970. option, equals, _ = token.partition("=")
  971. pcre = pcre or option == "--perl-regexp"
  972. explicit_patterns = explicit_patterns or option in {"--regexp", "--file"}
  973. takes_next = option in _GREP_OPTIONS_WITH_ARG and not equals
  974. if takes_next and i + 1 >= len(args):
  975. return [], True
  976. if option == "--regexp":
  977. pattern_indexes.append(i + 1 if takes_next else i)
  978. i += 1 if takes_next else 0
  979. elif options and token.startswith("-") and token != "-":
  980. chars = token[1:]
  981. for j, char in enumerate(chars):
  982. pcre = pcre or char == "P"
  983. explicit_patterns = explicit_patterns or char in {"e", "f"}
  984. if char in _GREP_SHORT_OPTIONS_WITH_ARG:
  985. # The first argument-taking short option owns the rest of the bundle,
  986. # or the next token when it comes last.
  987. attached = j + 1 < len(chars)
  988. if not attached and i + 1 >= len(args):
  989. return [], True
  990. if char == "e":
  991. pattern_indexes.append(i if attached else i + 1)
  992. i += 0 if attached else 1
  993. break
  994. elif operand_index is None:
  995. operand_index = i
  996. i += 1
  997. if not explicit_patterns:
  998. if operand_index is None:
  999. return [], pcre
  1000. pattern_indexes.append(operand_index)
  1001. if pcre:
  1002. spans.extend(
  1003. (segment_at + token_start, segment_at + token_end)
  1004. for _, token_start, token_end, quoted in map(args.__getitem__, pattern_indexes) if quoted
  1005. )
  1006. return spans, False
  1007. def _splice(command: str, edits) -> str:
  1008. """Apply sorted, non-overlapping ``(start, end, text)`` edits to *command* in one pass
  1009. (re-slicing per edit is quadratic on 10k+ segments)."""
  1010. parts, previous = [], 0
  1011. for start, end, text in edits:
  1012. parts.extend((command[previous:start], text))
  1013. previous = end
  1014. return "".join(parts) + command[previous:]
  1015. def _grep_safe_detection_variant(command: str) -> tuple[str, bool]:
  1016. spans, malformed = _quoted_grep_pattern_spans(command)
  1017. if malformed or not spans:
  1018. return command, malformed
  1019. return _splice(command, [(start, end, " " * (end - start)) for start, end in spans]), False
  1020. def _interpreter_family(executable: str) -> str | None:
  1021. name = os.path.basename(executable).lower()
  1022. return next((family for family, name_re in _INTERPRETER_NAME_RES if name_re.fullmatch(name)), None)
  1023. def _shell_segment_tokens(segment: str, start: int) -> list[str] | None:
  1024. """Tokenize an already-bounded command segment. ``None`` distinguishes malformed quoting from
  1025. an empty segment so callers can fail closed for a program-bearing option rather than silently
  1026. skip it."""
  1027. try:
  1028. lexer = shlex.shlex(segment[start:], posix=True, punctuation_chars="<>")
  1029. lexer.whitespace_split, lexer.commenters = True, ""
  1030. return list(lexer)
  1031. except ValueError:
  1032. return None
  1033. def _iter_top_level_shell_segments(command: str):
  1034. """Yield top-level command segments in one left-to-right pass."""
  1035. start = 0
  1036. for kind, i, j, quote in _scan_shell(command, comments=True):
  1037. if kind == "comment" or (kind == "char" and quote is None and command[i] in ";&|\n"):
  1038. if start < i:
  1039. yield command[start:i]
  1040. start = j
  1041. if start < len(command):
  1042. yield command[start:]
  1043. def _interpreter_exec_flag(family: str, args: list[str]) -> str | None:
  1044. """Return an execution-bearing interpreter option, if present."""
  1045. flags, with_arg = _INTERPRETER_EXEC_FLAGS[family], _INTERPRETER_WITH_ARG[family]
  1046. powershell = family == "powershell"
  1047. skip_value = False
  1048. for token in args:
  1049. if skip_value:
  1050. skip_value = False
  1051. continue
  1052. if token == "--" or (not powershell and not token.startswith("-")):
  1053. # Deno evaluates inline scripts via a bare `eval` subcommand rather than a dash
  1054. # flag: the FIRST positional token, after any global options (`deno -q eval ...`);
  1055. # a later positional `eval` (`deno run eval.ts`) stays data.
  1056. if family == "deno" and token.lower() == "eval":
  1057. return "eval"
  1058. break
  1059. option, equals, _ = token.partition("=")
  1060. comparable = option.lower() if powershell else option
  1061. if comparable in flags:
  1062. return comparable
  1063. # `-Wonce` and `ruby -rjson` attach an option value; they are not short-option bundles containing an execution
  1064. # flag. PowerShell's normal long options also use one dash, so bundle parsing never applies to that family.
  1065. has_attached_option_value = any(
  1066. option.startswith(short) and len(option) > len(short)
  1067. for short in with_arg if short.startswith("-") and not short.startswith("--")
  1068. )
  1069. if not powershell and not option.startswith("--") and len(option) > 2 and not has_attached_option_value:
  1070. bundled = next((f"-{char}" for char in option[1:] if f"-{char}" in flags), None)
  1071. if bundled:
  1072. return bundled
  1073. skip_value = comparable in with_arg and not equals
  1074. return None
  1075. def _bash_exec_payload(args: list[str]) -> tuple[bool, str | None]:
  1076. """Return whether Bash ``-c`` occurs and the command string it owns.
  1077. Bash's O/o options consume the following argument even when they precede a later ``-c`` or
  1078. share its short-option bundle; the two startup-file long options own their next token.
  1079. Parsing those first prevents both missed payloads and false ``-c`` hits."""
  1080. index = 0
  1081. while index < len(args):
  1082. token = args[index]
  1083. if token == "--" or not token.startswith(("-", "+")):
  1084. break
  1085. if token in _BASH_OPTIONS_WITH_ARG:
  1086. index += 2
  1087. continue
  1088. chars = token[1:]
  1089. # Bash option letters are case-sensitive; restricting to the documented alphabet
  1090. # preserves invalid controls such as `-Wc`.
  1091. if token.startswith("--") or not set(chars) <= _BASH_SHORT_OPTION_LETTERS:
  1092. index += 1
  1093. continue
  1094. consumed_option_arg = int("O" in chars or "o" in chars)
  1095. if "c" in chars:
  1096. payload_index = index + 1 + consumed_option_arg
  1097. return True, (args[payload_index] if payload_index < len(args) else None)
  1098. index += 1 + consumed_option_arg
  1099. return False, None
  1100. def _read_tool_exec_flag(tool: str, args: list[str]) -> tuple[str, str] | None:
  1101. """Return (option, program) for a read-only tool's program-running flag."""
  1102. flags = _READ_TOOL_EXEC_FLAGS[tool]
  1103. index = 0
  1104. while index < len(args):
  1105. token = args[index]
  1106. if token == "--":
  1107. break
  1108. option, equals, payload = token.partition("=")
  1109. payload = payload if equals else None
  1110. matched = option if option in flags else None
  1111. if tool == "man" and token.startswith(("-P", "-H")) and len(token) > 2:
  1112. matched, payload = token[:2], token[2:]
  1113. if matched:
  1114. if payload is None and index + 1 < len(args):
  1115. payload = args[index + 1]
  1116. # The option owns its program argument regardless of spelling; the real binaries
  1117. # execute a '-'-prefixed payload rather than reparsing it.
  1118. if payload:
  1119. return matched, payload
  1120. index += 2 if payload is not None and "=" not in token else 1
  1121. elif option in _READ_TOOL_LONG_OPTIONS_WITH_ARG[tool] and payload is None:
  1122. index += 2
  1123. elif token.startswith("-") and not token.startswith("--") and len(token) > 1:
  1124. # In a short bundle, the first argument-taking option owns the rest of the token, or
  1125. # the following token when it occurs last.
  1126. shorts = _READ_TOOL_SHORT_OPTIONS_WITH_ARG[tool]
  1127. owner = next((k for k, char in enumerate(token[1:], start=1) if char in shorts), None)
  1128. index += 2 if owner == len(token) - 1 else 1
  1129. else:
  1130. index += 1
  1131. return None
  1132. def _execution_flag_findings(command: str):
  1133. """Yield scoped execution mechanisms and any executable payloads."""
  1134. for segment in _iter_top_level_shell_segments(command):
  1135. for start, _, word in _iter_shell_command_word_spans(segment):
  1136. executable = _deobfuscate_shell_word_for_detection(word)
  1137. tokens = _shell_segment_tokens(segment, start)
  1138. executable_name = os.path.basename(executable).lower()
  1139. family = _interpreter_family(executable)
  1140. if tokens is None:
  1141. if family is not None or executable_name in _READ_TOOL_EXEC_FLAGS:
  1142. yield (_MALFORMED_EXEC_DESCRIPTION, None)
  1143. continue
  1144. if not tokens:
  1145. continue
  1146. args = tokens[1:]
  1147. if family and _interpreter_exec_flag(family, args):
  1148. yield ("script execution via -e/-c flag", None)
  1149. elif family and any(token.startswith("<<") for token in args):
  1150. yield ("script execution via heredoc", None)
  1151. else:
  1152. if executable_name in _SHELL_NAMES:
  1153. found, payload = _bash_exec_payload(args)
  1154. if found:
  1155. yield ("shell command via -c/-lc flag", payload)
  1156. if executable_name in _READ_TOOL_EXEC_FLAGS:
  1157. finding = _read_tool_exec_flag(executable_name, args)
  1158. if finding:
  1159. yield (f"arbitrary program execution via {executable_name} {finding[0]}", finding[1])
  1160. def _skip_shell_whitespace(command: str, pos: int) -> int:
  1161. while pos < len(command) and command[pos].isspace():
  1162. pos += 1
  1163. return pos
  1164. def _scan_shell(text: str, start: int = 0, end: int | None = None, *, subst: str = "",
  1165. brace: bool = False, stop_unterminated: bool = False, naive_backtick: bool = False,
  1166. comments: bool = False):
  1167. """Yield ``(kind, i, j, quote)`` lexical steps over ``text[start:end]`` without expanding.
  1168. The single quote/escape state machine behind every detection scanner. ``kind`` is ``"char"``
  1169. (one char), ``"esc"`` (backslash + the char it escapes; never inside single quotes), ``"quote"``
  1170. (an opening/closing quote char) or ``"subst"`` (a ``$(...)`` / backtick / ``${...}`` span);
  1171. With *comments*, ``"comment"`` spans are skipped without interpreting their quote syntax.
  1172. ``quote`` is the state the step was read in (``None``, ``'`` or ``"``). Substitutions are
  1173. recognized unquoted when ``"u"`` is in *subst*, inside double quotes when ``"q"`` is; *brace*
  1174. adds unquoted ``${...}``. An unterminated substitution falls through as plain chars unless
  1175. *stop_unterminated*, which yields ``("subst", i, None, quote)`` and ends the scan (the caller
  1176. descends to *end* itself). *naive_backtick* closes a backtick at the next backtick even if
  1177. escaped (the quoted-prose masker's historical behavior)."""
  1178. n = len(text) if end is None else end
  1179. quote: str | None = None
  1180. i = start
  1181. while i < n:
  1182. ch = text[i]
  1183. kind, j = "char", i + 1
  1184. if comments and quote is None and _is_shell_comment_start(text, i):
  1185. kind, j = "comment", text.find("\n", i, n)
  1186. if j < 0:
  1187. j = n
  1188. elif quote != "'" and ch == "\\" and i + 1 < n:
  1189. kind, j = "esc", i + 2
  1190. elif ch == quote or (quote is None and ch in "'\""):
  1191. kind = "quote"
  1192. elif quote != "'" and ("q" if quote else "u") in subst and (
  1193. ch == "`" or text.startswith("$(", i) or (brace and not quote and text.startswith("${", i))
  1194. ):
  1195. if ch == "`":
  1196. close = text.find("`", i + 1) + 1 or None if naive_backtick else _scan_backtick_end(text, i)
  1197. elif text[i + 1] == "(":
  1198. close = _scan_dollar_paren_end(text, i)
  1199. else:
  1200. close = text.find("}", i + 2) + 1 or None
  1201. if close is not None:
  1202. kind, j = "subst", close
  1203. elif stop_unterminated:
  1204. yield ("subst", i, None, quote)
  1205. return
  1206. yield (kind, i, j, quote)
  1207. if kind == "quote":
  1208. quote = None if quote else ch
  1209. i = j
  1210. def _scan_dollar_paren_end(command: str, start: int) -> int | None:
  1211. """Return the offset after a balanced ``$(...)`` command substitution."""
  1212. depth = 1
  1213. for kind, i, _, quote in _scan_shell(command, start + 2):
  1214. if kind == "char" and not quote:
  1215. depth += command.startswith("$(", i) - (command[i] == ")")
  1216. if depth == 0:
  1217. return i + 1
  1218. return None
  1219. def _scan_backtick_end(command: str, start: int) -> int | None:
  1220. # Backticks have no quote awareness: only a backslash escapes the next char.
  1221. match = re.compile(r"(?:\\.|[^`\\])*`", re.DOTALL).match(command, start + 1)
  1222. return match.end() if match else None
  1223. def _read_shell_word(command: str, pos: int) -> tuple[int, int, str]:
  1224. """Read one shell word without executing expansions."""
  1225. start = end = _skip_shell_whitespace(command, pos)
  1226. for kind, i, j, quote in _scan_shell(command, start, subst="u", brace=True):
  1227. if kind == "char" and quote is None and (command[i].isspace() or command[i] in ";&|<>()"):
  1228. break
  1229. end = j
  1230. return (start, end, command[start:end])
  1231. def _literal_command_substitution_output(script: str) -> str | None:
  1232. """Resolve tiny literal command substitutions without executing a shell."""
  1233. try:
  1234. tokens = shlex.split(script, posix=True)
  1235. except ValueError:
  1236. tokens = []
  1237. if not tokens:
  1238. return None
  1239. command, args = tokens[0].lower(), tokens[1:]
  1240. if command == "echo":
  1241. while args and re.fullmatch(r"-[nEe]+", args[0]):
  1242. args = args[1:]
  1243. elif command != "printf":
  1244. return None
  1245. if len(args) == 1 and _SIMPLE_SHELL_LITERAL_RE.fullmatch(args[0]):
  1246. return args[0]
  1247. if command == "printf" and len(args) == 2 and args[0] == "%s" and _SIMPLE_SHELL_LITERAL_RE.fullmatch(args[1]):
  1248. return args[1]
  1249. return None
  1250. def _replace_simple_command_substitutions(word: str) -> str:
  1251. chars: list[str] = []
  1252. i = 0
  1253. while i < len(word):
  1254. opener = 2 if word.startswith("$(", i) else 1 if word[i] == "`" else 0
  1255. end = (_scan_dollar_paren_end if opener == 2 else _scan_backtick_end)(word, i) if opener else None
  1256. replacement = _literal_command_substitution_output(word[i + opener:end - 1]) if end is not None else None
  1257. if replacement is None:
  1258. replacement, end = word[i], i + 1
  1259. chars.append(replacement)
  1260. i = end
  1261. return "".join(chars)
  1262. def _replace_simple_shell_expansions(word: str) -> str:
  1263. word = _replace_simple_command_substitutions(word)
  1264. word = _PARAM_REPLACEMENT_RE.sub(lambda match: match.group("replacement"), word)
  1265. return _PARAM_DEFAULT_RE.sub(lambda match: match.group("default"), word)
  1266. def _strip_shell_word_syntax(word: str) -> str:
  1267. return "".join(
  1268. word[i + 1] if kind == "esc" else word[i]
  1269. for kind, i, _, _ in _scan_shell(word) if kind != "quote"
  1270. )
  1271. def _deobfuscate_shell_word_for_detection(word: str) -> str:
  1272. """Approximate how shell syntax can spell a command word: collapses quoting/escaping plus
  1273. simple literal command substitutions in the word itself. Intentionally narrow and non-executing."""
  1274. for _ in range(2):
  1275. previous = word
  1276. word = _strip_shell_word_syntax(_replace_simple_shell_expansions(word))
  1277. if word == previous:
  1278. break
  1279. return word
  1280. def _is_shell_comment_start(command: str, index: int) -> bool:
  1281. return command[index] == "#" and (index == 0 or command[index - 1].isspace()
  1282. or command[index - 1] in ";&|()<>")
  1283. def _iter_shell_command_starts(command: str):
  1284. starts = [0]
  1285. def scan(start: int, end: int) -> None:
  1286. skip = -1
  1287. for kind, i, j, quote in _scan_shell(command, start, end, subst="uq", stop_unterminated=True,
  1288. comments=True):
  1289. if kind == "subst":
  1290. # Record a nested $(...)/backtick command start and scan its body.
  1291. inner = i + (1 if command[i] == "`" else 2)
  1292. starts.append(inner)
  1293. scan(inner, end if j is None else j - 1)
  1294. elif kind == "char" and quote is None and i != skip:
  1295. # `{` opens a brace group only as its own word (after whitespace or a separator): `${IFS}`
  1296. # is a parameter expansion and `-{delete,print}` a brace-expansion word, and a start
  1297. # marked inside either splits the word the flat patterns need to see intact.
  1298. if command[i] in "(;\n" or (command[i] == "{" and (i == 0 or command[i - 1].isspace()
  1299. or command[i - 1] in "(;&|)")):
  1300. starts.append(i + 1)
  1301. elif command[i] in "&|":
  1302. repeated = i + 1 < end and command[i + 1] == command[i]
  1303. skip = i + 1 if repeated else skip
  1304. starts.append(i + 1 + repeated)
  1305. scan(0, len(command))
  1306. seen = set()
  1307. for start in starts:
  1308. start = _skip_shell_whitespace(command, start)
  1309. if start >= len(command) or start in seen or _is_shell_comment_start(command, start):
  1310. continue
  1311. seen.add(start)
  1312. yield start
  1313. _, end, word = _read_shell_word(command, start)
  1314. if word in _SHELL_COMMAND_TRANSITIONS:
  1315. starts.append(end)
  1316. def _mark_command_starts(command: str, marker: str = "\n") -> str:
  1317. """Insert *marker* (a newline) before each real (quote-aware) command start.
  1318. ``\\n`` is already a ``_CMDPOS`` separator, so this exposes subshell ``(cmd)`` and brace-group
  1319. ``{ cmd; }`` openers — which the flat pattern class omits — to the anchored patterns WITHOUT the
  1320. quoted-prose false positives that adding ``(`` / ``{`` to ``_CMDPOS`` would cause: starts inside
  1321. quotes are never produced, so ``--title "block (reboot)"`` is left as-is."""
  1322. offsets = sorted(o for o in _iter_shell_command_starts(command) if o > 0)
  1323. return _splice(command, [(o, o, marker) for o in offsets]) if offsets else command
  1324. def _mask_quoted_newlines(command: str) -> str:
  1325. """Replace raw newlines inside single/double quotes with a space (detection-only).
  1326. A quoted newline is DATA to the shell, yet the flat ``_CMDPOS`` class treats every raw ``\\n``
  1327. as a command start, so multi-line quoted arguments (commit messages, heredoc text) tripped the
  1328. hardline blocklist when a data line began with e.g. ``sudo reboot``. Quote tracking mirrors
  1329. ``_iter_shell_command_starts``. Unquoted newlines pass through and ``_mark_command_starts``
  1330. still re-inserts newlines at genuine starts; an unclosed quote absorbs following newlines
  1331. exactly as the shell would, so masking them cannot hide a runnable command."""
  1332. if "\n" not in command:
  1333. return command
  1334. return _mask_quoted_newlines_span(command, 0, len(command))
  1335. def _mask_quoted_newlines_span(command: str, start: int, end: int) -> str:
  1336. """``_mask_quoted_newlines`` over ``command[start:end]``. A ``$(...)`` / backtick substitution
  1337. inside double quotes is EXECUTABLE, not data: its body is re-scanned with a fresh quote state so a
  1338. newline that separates commands inside it survives as a command boundary. Masking it as quoted
  1339. data turned ``"$(grep x f\nreboot)"`` into ``... f reboot)``, which no later stage can tell from an
  1340. operand; the pre-fix scanner only caught it by refusing the whole command as malformed."""
  1341. out: list[str] = []
  1342. for kind, i, j, quote in _scan_shell(command, start, end, subst="q"):
  1343. if kind == "subst":
  1344. # j is the index just past the closer; keep the opener and closer, recurse into the body.
  1345. body_start = i + (2 if command.startswith("$(", i) else 1)
  1346. body_end = j - 1
  1347. out.append(command[i:body_start])
  1348. out.append(_mask_quoted_newlines_span(command, body_start, body_end))
  1349. out.append(command[body_end:j])
  1350. elif quote and kind == "char" and command[i] == "\n":
  1351. out.append(" ")
  1352. else:
  1353. out.append(command[i:j])
  1354. return "".join(out)
  1355. def _iter_shell_command_word_spans(command: str):
  1356. """Yield command-position words that may be executable names."""
  1357. for pos in _iter_shell_command_starts(command):
  1358. wrapper, positionals = None, 0
  1359. options, skip_arg = True, False
  1360. while pos < len(command):
  1361. redirect = _SHELL_REDIRECTION_RE.match(command, _skip_shell_whitespace(command, pos))
  1362. if redirect:
  1363. _, pos, _ = _read_shell_word(command, redirect.end())
  1364. continue
  1365. word_start, word_end, word = _read_shell_word(command, pos)
  1366. if word_start == word_end:
  1367. break
  1368. pos = word_end
  1369. deobfuscated = _deobfuscate_shell_word_for_detection(word)
  1370. name = os.path.basename(deobfuscated).lower()
  1371. if skip_arg:
  1372. skip_arg = False
  1373. continue
  1374. if wrapper and options and deobfuscated == "--":
  1375. options = False
  1376. continue
  1377. if wrapper and options and deobfuscated.startswith("-"):
  1378. option = deobfuscated.split("=", 1)[0]
  1379. if wrapper == "env" and (option == "--split-string" or deobfuscated.startswith("-S")):
  1380. # The split string and remaining argv form ONE command, handled
  1381. # by _env_split_payload; the suffix is not a new executable.
  1382. break
  1383. queries = _COMMAND_WRAPPER_NON_EXECUTING_OPTIONS.get(wrapper, set())
  1384. if option in queries or (wrapper == "command" and not option.startswith("--")
  1385. and set(option[1:]) & {"v", "V"}):
  1386. break
  1387. skip_arg = "=" not in deobfuscated and option in _COMMAND_WRAPPER_OPTIONS_WITH_ARG.get(wrapper, set())
  1388. continue
  1389. if positionals:
  1390. positionals -= 1
  1391. continue
  1392. if _ENV_ASSIGNMENT_RE.fullmatch(word):
  1393. continue
  1394. yield (word_start, word_end, word)
  1395. if name not in _COMMAND_WRAPPER_WORDS:
  1396. break
  1397. wrapper, options = name, True
  1398. positionals = _COMMAND_WRAPPER_POSITIONAL_ARGS.get(name, 0)
  1399. def _shell_command_segment(command: str, start: int) -> str:
  1400. """Bound a candidate to its command, preserving quoted argument bytes."""
  1401. end = len(command)
  1402. for kind, i, _, quote in _scan_shell(command, start, subst="uq", brace=True, comments=True):
  1403. if kind == "comment" or (kind == "char" and quote is None and command[i] in ";&|\n)`"):
  1404. end = i
  1405. break
  1406. return command[start:end].strip()
  1407. def _split_env_string(payload: str) -> list[str] | None:
  1408. r"""Project GNU env -S literal argv, not POSIX shell words.
  1409. Dynamic ${NAME} expansion is deliberately not evaluated: the execution
  1410. backend's environment need not be this process's environment.
  1411. """
  1412. escapes = {"f": "\f", "n": "\n", "r": "\r", "t": "\t", "v": "\v",
  1413. "#": "#", "$": "$", "\"": "\"", "'": "'", "\\": "\\"}
  1414. args, word = [], []
  1415. quote, started, index = None, False, 0
  1416. while index < len(payload):
  1417. char = payload[index]
  1418. index += 1
  1419. if char == "\\":
  1420. if index == len(payload):
  1421. return None
  1422. escaped = payload[index]
  1423. if quote == "'" and escaped not in ("'", "\\"):
  1424. word.append(char)
  1425. started = True
  1426. continue
  1427. index += 1
  1428. if escaped == "c":
  1429. if quote:
  1430. return None
  1431. break
  1432. if escaped == "_" and quote is None:
  1433. if started:
  1434. args.append("".join(word))
  1435. word, started = [], False
  1436. continue
  1437. if escaped not in escapes and escaped != "_":
  1438. return None
  1439. word.append(" " if escaped == "_" else escapes[escaped])
  1440. started = True
  1441. continue
  1442. if char in ("'", '"') and (quote is None or char == quote):
  1443. quote = char if quote is None else None
  1444. started = True
  1445. continue
  1446. if quote is None and char in " \t\n\r\v\f":
  1447. if started:
  1448. args.append("".join(word))
  1449. word, started = [], False
  1450. continue
  1451. if quote is None and char == "#" and not started:
  1452. break
  1453. if char == "$" and quote != "'":
  1454. return None
  1455. word.append(char)
  1456. started = True
  1457. if quote:
  1458. return None
  1459. if started:
  1460. args.append("".join(word))
  1461. return args
  1462. def _env_split_payload(tokens: list[str]) -> str | None:
  1463. index = 1
  1464. while index < len(tokens):
  1465. token = tokens[index]
  1466. if token == "--" or not token.startswith("-"):
  1467. return None
  1468. option, equals, value = token.partition("=")
  1469. if option == "--split-string" or token.startswith("-S"):
  1470. attached = equals if option == "--split-string" else len(token) > 2
  1471. if not attached:
  1472. index += 1
  1473. payload = (value if option == "--split-string" else token[2:]) if attached else (
  1474. tokens[index] if index < len(tokens) else "")
  1475. args = _split_env_string(payload)
  1476. # Protect literal separators when reusing command-position detection;
  1477. # only a real shell -c carrier may turn these argv bytes into code.
  1478. return shlex.join(args + tokens[index + 1:]) if args is not None else None
  1479. index += 2 if not equals and option in _COMMAND_WRAPPER_OPTIONS_WITH_ARG["env"] else 1
  1480. return None
  1481. def _deny_command_variants(command: str):
  1482. """Add executable projections without reparsing normalized argument data.
  1483. Whole-input matching is retained for existing globs. New projections parse
  1484. the original quote state, preserve path-specific rules, and fold only the
  1485. executable basename (never arbitrary argument paths).
  1486. """
  1487. yield from _command_detection_variants(command)
  1488. pending, seen = [command], set()
  1489. while pending:
  1490. source = pending.pop()
  1491. if source in seen:
  1492. continue
  1493. seen.add(source)
  1494. for start, end, word in _iter_shell_command_word_spans(source):
  1495. segment = _shell_command_segment(source, start)
  1496. executable = _deobfuscate_shell_word_for_detection(word)
  1497. tail = segment[end - start:]
  1498. # Collapse only unquoted inter-word whitespace; quoted prose is data.
  1499. parts = []
  1500. for kind, i, j, quote in _scan_shell(tail):
  1501. if kind == "char" and quote is None and tail[i].isspace():
  1502. if not parts or parts[-1] != " ":
  1503. parts.append(" ")
  1504. else:
  1505. parts.append(tail[i:j])
  1506. tail = "".join(parts)
  1507. for name in dict.fromkeys((executable, os.path.basename(executable))):
  1508. candidate = name + tail
  1509. yield candidate
  1510. # Apply the existing text matching semantics only AFTER locating
  1511. # executable positions; never parse its rewritten quotes again.
  1512. yield _normalize_command_for_detection(candidate)
  1513. if os.path.basename(executable) == "env":
  1514. tokens = _shell_segment_tokens(segment, 0)
  1515. if tokens:
  1516. payload = _env_split_payload(tokens)
  1517. if payload:
  1518. pending.append(payload)
  1519. for _, payload in _execution_flag_findings(source):
  1520. if payload:
  1521. pending.append(payload)
  1522. def _command_detection_variants(command: str):
  1523. # Mask quoted newlines BEFORE normalization: normalization strips escapes (\" -> ") and ""
  1524. # pairs, corrupting quote tracking (`echo "a\""` becomes an unterminated quote) so masking
  1525. # afterwards could swallow a REAL unquoted newline separator. The raw command carries faithful quote state.
  1526. normalized = _normalize_command_for_detection(_mask_quoted_newlines(command))
  1527. # Quote-aware grep parsing hides only structurally identified pattern operands; malformed or
  1528. # ambiguous input stays byte-for-byte intact.
  1529. grep_safe, _ = _grep_safe_detection_variant(normalized)
  1530. seen = {grep_safe}
  1531. yield grep_safe
  1532. def fresh(variant: str) -> bool:
  1533. if not variant or variant in seen:
  1534. return False
  1535. seen.add(variant)
  1536. return True
  1537. # Windows-path variant: normalization strips backslashes as shell escapes, so `del C:\Users\me\.ssh\id_rsa`
  1538. # reaches the patterns as `del C:Usersme.sshid_rsa`. When the RAW command has a drive-letter or UNC backslash
  1539. # path, also yield a variant with backslashes flattened to `/` BEFORE normalization. Gated on a real path shape so
  1540. # POSIX escape semantics (`echo a\"b`) are untouched elsewhere.
  1541. # See #69472.
  1542. if re.search(r"(?:[A-Za-z]:|\\\\)[\\\\]", command) or re.search(r"[A-Za-z]:\\", command):
  1543. win_variant = _normalize_command_for_detection(_mask_quoted_newlines(command.replace("\\", "/")))
  1544. if fresh(win_variant):
  1545. yield win_variant
  1546. # Program-bearing options are parsed in their owning command's context; surfacing only the payload lets the
  1547. # hardline floor inspect what will actually run without promoting similar flags or quoted prose.
  1548. pending = [normalized]
  1549. while pending:
  1550. for _, payload in _execution_flag_findings(pending.pop()):
  1551. if fresh(payload):
  1552. yield payload
  1553. # A payload may start with an option-looking program and then invoke a hardline command
  1554. # after a separator; mark its starts.
  1555. marked_payload = _mark_command_starts(payload)
  1556. if marked_payload != payload and fresh(marked_payload):
  1557. yield marked_payload
  1558. pending.append(payload)
  1559. # Subshell `(cmd)` / brace-group `{ cmd; }` openers put `cmd` at a real command position the flat `_CMDPOS`
  1560. # patterns can't see (adding `(`/`{` there would match quoted prose like `--title "(reboot)"`). Insert a newline
  1561. # at each start the QUOTE-AWARE tokenizer found instead; this covers every `_CMDPOS` rule in one place.
  1562. marked = _mark_command_starts(grep_safe)
  1563. if marked != grep_safe and fresh(marked):
  1564. yield marked
  1565. # Every variant above tracks quotes on NORMALIZED text, where `\"` has already become `"`. That
  1566. # flips quote parity, so in `cat "f\"n.txt"; rm -rf /` the `; rm` start sat "inside" a phantom
  1567. # quote, no start was marked, and the hardline floor let it through. Mark starts on the RAW
  1568. # command (only quoted newlines masked), then normalize; the leading space keeps the marker
  1569. # from being eaten as a `\<newline>` continuation when the preceding text ends in a backslash.
  1570. faithful = _normalize_command_for_detection(_mark_command_starts(_mask_quoted_newlines(command), marker=" \n"))
  1571. if fresh(faithful):
  1572. yield faithful
  1573. # Quoting/escaping can spell an executable in pieces (r\m, r''m). Keep that deobfuscation scoped
  1574. # to command words so arguments don't false-positive.
  1575. # One variant with EVERY command word deobfuscated, not one full-length variant per word: a heredoc
  1576. # body of quoted lines has hundreds of quoted command words, and per-word variants made both
  1577. # detection passes O(words * len) — minutes of GIL-held regex on a 15 KB command (#113535).
  1578. # Spans arrive out of order (loop/conditional bodies after their keywords) and can nest (a
  1579. # backtick word and the command inside it), so apply them sorted; spans overlapping an applied
  1580. # one wait for the next round, one combined variant per nesting level.
  1581. pending = sorted(
  1582. ((word_start, word_end, deobfuscated) for word_start, word_end, word in _iter_shell_command_word_spans(normalized)
  1583. if (deobfuscated := _deobfuscate_shell_word_for_detection(word)) and deobfuscated != word),
  1584. key=lambda span: span[:2],
  1585. )
  1586. while pending:
  1587. applied, carry, cursor = [], [], 0
  1588. for span in pending:
  1589. if span[0] < cursor:
  1590. carry.append(span)
  1591. else:
  1592. applied.append(span)
  1593. cursor = span[1]
  1594. variant = _splice(normalized, applied)
  1595. if fresh(variant):
  1596. yield variant
  1597. pending = carry
  1598. def _is_verification_artifact_cleanup(command: str) -> bool:
  1599. """Return whether *command* only removes one Hermes ad-hoc temp script."""
  1600. try:
  1601. argv = shlex.split(command, posix=True)
  1602. except ValueError:
  1603. return False
  1604. if len(argv) != 3 or argv[0] != "rm" or argv[1] != "-f":
  1605. return False
  1606. operand = argv[2]
  1607. temp_dir = os.path.realpath(tempfile.gettempdir())
  1608. basename = os.path.basename(operand)
  1609. return (
  1610. operand == os.path.join(temp_dir, basename)
  1611. and os.path.dirname(os.path.realpath(operand)) == temp_dir
  1612. and re.fullmatch(r"hermes-(?:verify|ad-hoc)-[A-Za-z0-9_.-]+", basename) is not None
  1613. )
  1614. def _is_shell_token_spliced_gateway_lifecycle(command: str) -> bool:
  1615. """Catch gateway-lifecycle verbs spelled with quote splicing.
  1616. Backslash splicing (``kick\\start``) is undone by normalization, but quote splicing is not:
  1617. ``_deobfuscate_shell_word_for_detection`` is deliberately scoped to command-position words
  1618. (widening it would let quoted prose like ``git commit -m "rm -rf /"`` match), and the spliced
  1619. verb is an ARGUMENT, so ``launchctl kick"start" -k gui/501/ai.hermes.gateway`` auto-approved.
  1620. Delegates to ``cron.lifecycle_guard`` (shlex-tokenized, anchored on a hermes-gateway
  1621. identifier). Runs last so an ordinary pattern match keeps its more specific reason; this layer
  1622. only prompts — the non-bypassable block still lives in ``cron.lifecycle_guard``.
  1623. ``_normalize_command_for_detection`` strips backslash escapes, so ``kick\\start`` already reaches the
  1624. launchctl pattern above. See #80269.
  1625. """
  1626. try:
  1627. from cron.lifecycle_guard import contains_gateway_lifecycle_command
  1628. except Exception:
  1629. return False
  1630. return contains_gateway_lifecycle_command(command)
  1631. def _upload_flag_owner_ranges(command: str) -> tuple[list[int], list[int]]:
  1632. """Merged ``(starts, ends)`` of the ranges where an upload flag may belong to curl/wget.
  1633. ``awk -F``, ``cut -d`` and ``sort -t`` share the flag letters but take a delimiter and upload
  1634. nothing. A range runs from a curl/wget word (``xargs curl``, ``ssh host curl``) or a word that
  1635. may expand to one to the end of its simple command, and covers the inside of a quoted word
  1636. (``eval "curl -d …"``, ``ssh host "curl …"``) that another program may re-parse as a command.
  1637. Built once per command, so many delimiter flags cost one parse instead of one parse each."""
  1638. ranges = []
  1639. for start, _, _ in _iter_shell_command_word_spans(command):
  1640. end = start + len(_shell_command_segment(command, start))
  1641. pos = start
  1642. while pos < end:
  1643. redirect = _SHELL_REDIRECTION_RE.match(command, _skip_shell_whitespace(command, pos))
  1644. if redirect: # ``ssh host >/dev/null curl …``: a redirection does not end the command
  1645. _, pos, _ = _read_shell_word(command, redirect.end())
  1646. continue
  1647. word_start, pos, word = _read_shell_word(command, pos)
  1648. if word_start == pos:
  1649. break
  1650. if _word_may_run_curl_or_wget(word):
  1651. ranges.append((word_start, end))
  1652. break
  1653. if any(ch.isspace() for ch in command[word_start:pos]):
  1654. ranges.append((word_start + 1, pos))
  1655. starts: list[int] = []
  1656. ends: list[int] = []
  1657. for range_start, range_end in sorted(ranges):
  1658. if ends and range_start <= ends[-1]:
  1659. ends[-1] = max(ends[-1], range_end)
  1660. else:
  1661. starts.append(range_start)
  1662. ends.append(range_end)
  1663. return starts, ends
  1664. def _upload_flag_owned_by_curl_or_wget(owner_ranges: tuple[list[int], list[int]], flag_start: int) -> bool:
  1665. """True when the upload flag at *flag_start* lies in a range from :func:`_upload_flag_owner_ranges`."""
  1666. starts, ends = owner_ranges
  1667. index = bisect.bisect_right(starts, flag_start) - 1
  1668. return index >= 0 and flag_start < ends[index]
  1669. def _word_may_run_curl_or_wget(word: str) -> bool:
  1670. """True for a curl/wget word (also ``curl.exe``, ``C:\\tools\\curl.exe``) or a word whose command
  1671. name holds an unresolved expansion (``$CURL``, ``"$(which curl)"``, ``/usr/bin/$CURL``,
  1672. ``c${suffix}``): what it runs is unknown without executing the shell."""
  1673. names = (os.path.basename(_deobfuscate_shell_word_for_detection(word)), re.split(r"[\\/]", word)[-1])
  1674. if any(name.lower().removesuffix(".exe") in ("curl", "wget") for name in names):
  1675. return True
  1676. # A glob in the name (``/usr/bin/cur[l]``, ``wge*``) may match curl/wget.
  1677. if any(fnmatch.fnmatchcase(tool, name.lower()) for name in names if any(c in name for c in "*?[")
  1678. for tool in ("curl", "wget", "curl.exe", "wget.exe")):
  1679. return True
  1680. unquoted = word.lstrip("'\"")
  1681. if unquoted.startswith(("$", "`")):
  1682. return True
  1683. # An option (``-d``) or assignment (``x="$A"``) cannot name the command being run; ``c${s:=url}`` can.
  1684. return not unquoted.startswith("-") and not _ENV_ASSIGNMENT_RE.fullmatch(word) and ("$" in word or "`" in word)
  1685. def _quoted_text_is_executed(variant: str, pattern_re: "re.Pattern[str] | _PipeRemoteToShellMatcher") -> bool:
  1686. """Whether a launcher (ssh, cmd /c, iex, a shell carrier, pwsh) at command position runs the quoted
  1687. delete as code: the delete must sit in that launcher's own command, or be piped into a PowerShell
  1688. reading stdin (`echo "..." | pwsh -`). A launcher word elsewhere (`echo ssh "..."`, `pwsh -v &&
  1689. echo "..."`) leaves the quoted text prose."""
  1690. prev_start = None
  1691. for start, _, word in _iter_shell_command_word_spans(variant):
  1692. stage_start, prev_start = prev_start, start
  1693. name = os.path.basename(_deobfuscate_shell_word_for_detection(word)).lower().removesuffix(".exe")
  1694. if name not in _PS_QUOTED_SCRIPT_LAUNCHERS:
  1695. continue
  1696. segment = _lower_preserving_flags(_shell_command_segment(variant, start))
  1697. if name == "cmd" and not re.match(r"cmd(?:\.exe)?\s+/[ck]\b", segment, _RE_FLAGS):
  1698. continue
  1699. if pattern_re.search(segment):
  1700. return True
  1701. # `echo "gci | ri -r" | pwsh -`: the stage piped in is the previous command word's own text.
  1702. before = variant[stage_start or 0:start].rstrip()
  1703. if name in ("pwsh", "powershell") and stage_start is not None and before.endswith("|") \
  1704. and not before.endswith("||") and pattern_re.search(_lower_preserving_flags(before)):
  1705. return True
  1706. return False
  1707. def _iter_dangerous_descriptions(command: str, only: frozenset[str] | None = None):
  1708. """Yield the description (= pattern key) of every dangerous match, in detection order; may repeat.
  1709. *only* restricts the scan to those pattern rules and skips the parser-limit, execution-flag and
  1710. gateway-splice checks, which no content rule is."""
  1711. if _command_parser_limit_exceeded(command):
  1712. if only is None:
  1713. yield _PARSER_LIMIT_DESCRIPTION
  1714. return
  1715. if _is_verification_artifact_cleanup(command):
  1716. return
  1717. patterns = DANGEROUS_PATTERNS_COMPILED if only is None else [
  1718. (pattern_re, description) for pattern_re, description in DANGEROUS_PATTERNS_COMPILED if description in only
  1719. ]
  1720. for command_variant in _command_detection_variants(command):
  1721. command_lower = _lower_preserving_flags(command_variant)
  1722. masked_lower: str | None = None
  1723. for pattern_re, description in patterns:
  1724. if description in _QUOTE_MASKED_DANGEROUS_DESCRIPTIONS or description == _PS_PIPE_ALIAS_DESCRIPTION:
  1725. if masked_lower is None:
  1726. masked_lower = _lower_preserving_flags(
  1727. _mask_quoted_prose(command_variant)
  1728. )
  1729. if pattern_re.search(masked_lower) or (
  1730. description == _PS_PIPE_ALIAS_DESCRIPTION and pattern_re.search(command_lower)
  1731. and _quoted_text_is_executed(command_variant, pattern_re)
  1732. ):
  1733. yield description
  1734. elif description == _CREDENTIAL_UPLOAD_DESCRIPTION:
  1735. matches = list(pattern_re.finditer(command_lower))
  1736. owner_ranges = _upload_flag_owner_ranges(command_lower) if matches else ([], [])
  1737. if any(_upload_flag_owned_by_curl_or_wget(owner_ranges, match.start()) for match in matches):
  1738. yield description
  1739. elif pattern_re.search(command_lower):
  1740. yield description
  1741. if only is not None:
  1742. # The variation-selector check is a content rule too, so the content-only scan keeps it.
  1743. if _INVISIBLE_UNICODE_DESCRIPTION in only and _HIDDEN_VARIATION_SELECTOR_RE.search(command):
  1744. yield _INVISIBLE_UNICODE_DESCRIPTION
  1745. return
  1746. normalized = _normalize_command_for_detection(command)
  1747. for description, _ in _execution_flag_findings(normalized):
  1748. yield description
  1749. if _is_shell_token_spliced_gateway_lifecycle(command):
  1750. yield _GATEWAY_LIFECYCLE_SPLICE_DESCRIPTION
  1751. if _HIDDEN_VARIATION_SELECTOR_RE.search(command):
  1752. yield _INVISIBLE_UNICODE_DESCRIPTION
  1753. def detect_dangerous_command(command: str) -> tuple:
  1754. """Check dangerous patterns -> (is_dangerous, pattern_key, description) for the first match."""
  1755. description = next(_iter_dangerous_descriptions(command), None)
  1756. if description is None:
  1757. return (False, None, None)
  1758. return (True, description, description)
  1759. def detect_content_rule_keys(command: str) -> list[str]:
  1760. """Pattern keys of every content-level rule (``_CONTENT_RULE_DESCRIPTIONS``) the command matches,
  1761. wherever they fall in detection order. The approval gates require consent for these on top of
  1762. the first match, so an approved pipe-to-shell cannot wave through a credential upload."""
  1763. return list(dict.fromkeys(_iter_dangerous_descriptions(command, only=_CONTENT_RULE_DESCRIPTIONS)))