Skip to content

The agent that grows with you

fix(approval): give global-flag runs one parse in dangerous-command rules

unsupportedpastelseb8d21f48214
diff --git a/tests/tools/test_approval_global_flag_scan.py b/tests/tools/test_approval_global_flag_scan.py
new file mode 100644
index 00000000000..d88212cc867
--- /dev/null
+++ b/tests/tools/test_approval_global_flag_scan.py
@@ -0,0 +1,66 @@
+"""Global-flag rules must terminate on long flag runs without dropping long commands (#129281).
+
+Subprocess timeout bounds regressions without hanging pytest on the GIL.
+"""
+import os
+import subprocess
+import sys
+
+REPO_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+
+def _run(code):
+ # Pin the checkout under test: a bare `python -c` resolves `tools` through the venv's editable
+ # install (the primary clone), not through the worktree pytest is running in.
+ env = {**os.environ, "PYTHONPATH": REPO_ROOT}
+ subprocess.run([sys.executable, '-c', code], check=True, timeout=10, cwd=REPO_ROOT, env=env)
+
+
+def test_long_flag_runs_still_reach_the_target():
+ _run('''
+from tools.approval_detection import detect_dangerous_command
+run = "--opt val --flag=x -q - " * 58
+cases = {
+ f"hermes {run}gateway restart": "stop/restart hermes gateway (kills running agents)",
+ f"docker {run}-H ssh://prod ps": "docker with remote daemon redirect (-H/--host)",
+ f"docker {run}--context=prod ps": "docker with daemon redirect (--context: alternate daemon)",
+ f"podman {run}--url tcp://prod ps": "podman with remote daemon redirect (--url/--connection/--identity)",
+ f"podman {run}--remote ps": "podman remote mode (-r/--remote: remote daemon)",
+ f"docker compose {run}down": "docker compose restart/stop/kill/down (container lifecycle)",
+ f"docker {run}kill app": "docker restart/stop/kill (container lifecycle)",
+}
+for command, description in cases.items():
+ assert detect_dangerous_command(command) == (True, description, description), command[:40]
+''')
+
+
+def test_nonmatching_flag_runs_finish():
+ _run('''
+from tools.approval_detection import detect_dangerous_command
+for prefix in ("rclone lsf $HOME/.hermes --recursive --files-only", "hermes", "docker",
+ "docker compose", "podman"):
+ for run in ("--exclude " * 58, "--opt val " * 58, "--opt=val " * 58, "--opt" + " " * 100 + "val "):
+ assert detect_dangerous_command(f"{prefix} {run}ps 2>/dev/null")[0] is False, prefix
+''')
+
+
+def test_value_whitespace_decisions_are_unchanged():
+ # The fix must not move any approval decision: docker/podman values follow exactly one
+ # whitespace character, as before, while hermes values may follow a whitespace run.
+ _run('''
+from tools.approval_detection import detect_dangerous_command
+cases = {
+ "docker --log-level debug stop app": True,
+ "docker --log-level\\tdebug stop app": True,
+ "docker --log-level debug stop app": False,
+ "docker --log-level debug -H ssh://prod ps": False,
+ "docker --log-level debug --context prod ps": False,
+ "podman --log-level debug --remote ps": False,
+ "podman --log-level debug --url tcp://prod ps": False,
+ "docker compose --project-name demo down": False,
+ "docker compose --project-name demo down": True,
+ "hermes --config x.yaml gateway restart": True,
+}
+for command, dangerous in cases.items():
+ assert detect_dangerous_command(command)[0] is dangerous, command
+''')
diff --git a/tools/approval_detection.py b/tools/approval_detection.py
index aa05e27567f..1f7a94e92b1 100644
--- a/tools/approval_detection.py
+++ b/tools/approval_detection.py
@@ -30,6 +30,14 @@ _PROJECT_ENV_PATH = r'(?:(?:/|\.{1,2}/)?(?:[^\s/"\'`]+/)*\.env(?:\.[^/\s"\'`]+)*
_PROJECT_CONFIG_PATH = r'(?:(?:/|\.{1,2}/)?(?:[^\s/"\'`]+/)*config\.yaml)'
_SHELL_RC_FILES = r'(?:~|\$home|\$\{home\})/\.' r'(?:bashrc|zshrc|profile|bash_profile|zprofile)\b'
_CREDENTIAL_FILES = r'(?:~|\$home|\$\{home\})/\.' r'(?:netrc|pgpass|npmrc|pypirc)\b'
+# Global flags before a subcommand, each with an optional value. Every flag has one parse ('-' plus
+# its possessive remainder, so '--x' and '--x=v' never split two ways) and a value cannot itself be a
+# flag, so a long run that never reaches the subcommand fails in linear time instead of holding the
+# GIL for minutes (#129281). Whole groups still backtrack to expose the target flag or verb.
+_GLOBAL_FLAGS = r'(?:-\S++(?:\s++(?!-\S)\S++)?\s++)*'
+# Same grammar for the docker/podman rules, which have always taken a separate value only after exactly
+# one whitespace character; keeping that means this fix changes no approval decision.
+_CONTAINER_GLOBAL_FLAGS = r'(?:-\S++(?:\s(?!-\S)\S++)?\s++)*'
# macOS: /etc, /var, /tmp, /home are symlinks to /private/*, so /private/etc/sudoers would bypass a plain
# "/etc/" check. Match both forms.
_MACOS_PRIVATE_SYSTEM_PATH = r'/private/(?:etc|var|tmp|home)/'
@@ -339,24 +347,24 @@ DANGEROUS_PATTERNS = [
"dynamic shell word may expand to arbitrary program execution flag"),
# Gateway lifecycle: stopping/restarting the gateway kills all running agents. Global flags
# between `hermes` and `gateway` (`hermes -p ade gateway restart`) are allowed so a profile flag can't slip past.
- (r'\bhermes\s+(?:-{1,2}\S+(?:\s+\S+)?\s+)*gateway\s+(stop|restart)\b', "stop/restart hermes gateway (kills running agents)"),
+ (r'\bhermes\s+' + _GLOBAL_FLAGS + r'gateway\s+(stop|restart)\b', "stop/restart hermes gateway (kills running agents)"),
(r'\bhermes\s+update\b', "hermes update (restarts gateway, kills running agents)"),
# Docker/Podman daemon redirect — global flags or env that point the CLI at a DIFFERENT (often remote) daemon:
# `docker -H ssh://prod stop app` looks local but operates on remote infra, so any redirect requires approval
# regardless of subcommand. The flag must be in global position (before the subcommand) and -H/--host/--context
# must carry a value, keeping `docker -h` and `docker run -h <hostname>` out. Listed BEFORE the lifecycle rules so
# a redirected lifecycle command surfaces the more specific reason.
- (r'\bdocker\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(?:-h|--host)[=\s]+\S+', "docker with remote daemon redirect (-H/--host)"),
- (r'\bdocker\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(?:-c|--context)[=\s]+\S+', "docker with daemon redirect (--context: alternate daemon)"),
+ (r'\bdocker\s+' + _CONTAINER_GLOBAL_FLAGS + r'(?:-h|--host)[=\s]+\S+', "docker with remote daemon redirect (-H/--host)"),
+ (r'\bdocker\s+' + _CONTAINER_GLOBAL_FLAGS + r'(?:-c|--context)[=\s]+\S+', "docker with daemon redirect (--context: alternate daemon)"),
(r'\bdocker\s+context\s+use\b', "docker context use (switches default daemon for future commands)"),
- (r'\bpodman\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(?:--url|--connection|--identity)[=\s]+\S+', "podman with remote daemon redirect (--url/--connection/--identity)"),
- (r'\bpodman\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(?:-r\b|--remote\b)', "podman remote mode (-r/--remote: remote daemon)"),
+ (r'\bpodman\s+' + _CONTAINER_GLOBAL_FLAGS + r'(?:--url|--connection|--identity)[=\s]+\S+', "podman with remote daemon redirect (--url/--connection/--identity)"),
+ (r'\bpodman\s+' + _CONTAINER_GLOBAL_FLAGS + r'(?:-r\b|--remote\b)', "podman remote mode (-r/--remote: remote daemon)"),
(r'\b(?:docker_host|docker_context|container_host|container_connection)=\S+', "docker/podman daemon redirect via environment (DOCKER_HOST/CONTAINER_HOST)"),
# Container lifecycle (docker.sock mounts let the agent stop/kill containers) always needs
# consent. Global flags between docker/compose and the verb and the legacy `docker-compose`
# binary are allowed so a flag can't slip past.
- (r'\bdocker(?:-compose|\s+compose)\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(restart|stop|kill|down)\b', "docker compose restart/stop/kill/down (container lifecycle)"),
- (r'\bdocker\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(restart|stop|kill)\b', "docker restart/stop/kill (container lifecycle)"),
+ (r'\bdocker(?:-compose|\s+compose)\s+' + _CONTAINER_GLOBAL_FLAGS + r'(restart|stop|kill|down)\b', "docker compose restart/stop/kill/down (container lifecycle)"),
+ (r'\bdocker\s+' + _CONTAINER_GLOBAL_FLAGS + r'(restart|stop|kill)\b', "docker restart/stop/kill (container lifecycle)"),
# Gateway protection: never start gateway outside systemd management
(r'gateway\s+run\b.*(&\s*$|&\s*;|\bdisown\b|\bsetsid\b)', "start gateway outside systemd (use 'systemctl --user restart hermes-gateway')"),
(r'\bnohup\b.*gateway\s+run\b', "start gateway outside systemd (use 'systemctl --user restart hermes-gateway')"),