The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(approval): require approval for docker/podman daemon-redirect commands
teknium16437701228a9
diff --git a/tests/tools/test_docker_daemon_redirect.py b/tests/tools/test_docker_daemon_redirect.pynew file mode 100644index 00000000000..37793d9c162--- /dev/null+++ b/tests/tools/test_docker_daemon_redirect.py@@ -0,0 +1,155 @@+"""Docker/Podman daemon-redirect and lifecycle flag-insertion detection.++Inspired by Claude Code 2.1.214, which added permission prompts for docker+commands (including the Podman ``docker`` shim) carrying daemon-redirect+flags (``--url``, ``--connection``, ``--identity``, and Podman's remote+mode) that previously ran without one.++A daemon redirect makes a local-looking command operate on a different+(often remote) daemon, so any docker/podman invocation carrying one+requires approval regardless of subcommand.+"""++from tools.approval import detect_dangerous_command+++class TestDockerDaemonRedirect:+ def test_docker_dash_h_remote_host(self):+ is_dangerous, key, desc = detect_dangerous_command(+ "docker -H ssh://prod-host stop app")+ assert is_dangerous is True+ assert key is not None+ assert "daemon redirect" in desc++ def test_docker_long_host_flag_equals_form(self):+ is_dangerous, _, desc = detect_dangerous_command(+ "docker --host=tcp://10.0.0.5:2375 ps")+ assert is_dangerous is True+ assert "daemon redirect" in desc++ def test_docker_host_flag_after_other_global_flags(self):+ is_dangerous, _, desc = detect_dangerous_command(+ "docker --log-level debug -H tcp://10.0.0.5:2375 images")+ assert is_dangerous is True+ assert "daemon redirect" in desc++ def test_docker_context_flag(self):+ is_dangerous, _, desc = detect_dangerous_command(+ "docker --context production rm -f db")+ assert is_dangerous is True+ assert "daemon redirect" in desc++ def test_docker_context_use(self):+ is_dangerous, _, desc = detect_dangerous_command(+ "docker context use production")+ assert is_dangerous is True+ assert "context use" in desc++ def test_docker_host_env_prefix(self):+ is_dangerous, _, _ = detect_dangerous_command(+ "DOCKER_HOST=ssh://prod docker stop app")+ assert is_dangerous is True++ def test_docker_context_env_prefix(self):+ is_dangerous, _, _ = detect_dangerous_command(+ "DOCKER_CONTEXT=production docker ps")+ assert is_dangerous is True++ def test_container_host_env_prefix(self):+ is_dangerous, _, _ = detect_dangerous_command(+ "CONTAINER_HOST=ssh://root@prod:22/run/podman/podman.sock podman ps")+ assert is_dangerous is True++ def test_podman_url_flag(self):+ is_dangerous, _, desc = detect_dangerous_command(+ "podman --url ssh://core@remote:22/run/podman.sock ps")+ assert is_dangerous is True+ assert "daemon redirect" in desc++ def test_podman_connection_flag(self):+ is_dangerous, _, _ = detect_dangerous_command(+ "podman --connection prod rm -f web")+ assert is_dangerous is True++ def test_podman_identity_flag(self):+ is_dangerous, _, _ = detect_dangerous_command(+ "podman --identity ~/.ssh/id_ed25519 --url ssh://x ps")+ assert is_dangerous is True++ def test_podman_remote_mode(self):+ is_dangerous, _, desc = detect_dangerous_command("podman --remote ps")+ assert is_dangerous is True+ assert "remote mode" in desc++ def test_podman_short_remote_flag(self):+ is_dangerous, _, _ = detect_dangerous_command("podman -r images")+ assert is_dangerous is True++ # -- negatives: local docker usage stays out of the deny ----------------++ def test_plain_docker_ps_not_flagged(self):+ assert detect_dangerous_command("docker ps -a") == (False, None, None)++ def test_docker_run_not_flagged(self):+ assert detect_dangerous_command(+ "docker run --rm -it alpine sh") == (False, None, None)++ def test_docker_bare_help_flag_not_flagged(self):+ # `docker -h` alone is help; the redirect rule requires a value token.+ assert detect_dangerous_command("docker -h") == (False, None, None)++ def test_docker_run_hostname_flag_not_flagged(self):+ # `-h` in the subcommand position is `docker run --hostname`.+ assert detect_dangerous_command(+ "docker run -h myhost alpine") == (False, None, None)++ def test_docker_build_not_flagged(self):+ assert detect_dangerous_command(+ "docker build -t myimage .") == (False, None, None)++ def test_docker_context_ls_not_flagged(self):+ assert detect_dangerous_command(+ "docker context ls") == (False, None, None)++ def test_podman_local_ps_not_flagged(self):+ assert detect_dangerous_command("podman ps") == (False, None, None)++ def test_podman_local_rm_not_misattributed_to_redirect(self):+ is_dangerous, _, desc = detect_dangerous_command(+ "podman rm old-container")+ if is_dangerous:+ assert "remote" not in desc+++class TestDockerLifecycleFlagInsertion:+ """Global flags must not slip a lifecycle verb past the docker guard."""++ def test_docker_stop_still_flagged(self):+ is_dangerous, _, desc = detect_dangerous_command("docker stop app")+ assert is_dangerous is True+ assert "container lifecycle" in desc++ def test_docker_stop_with_global_flag_flagged(self):+ is_dangerous, _, desc = detect_dangerous_command(+ "docker --log-level debug stop app")+ assert is_dangerous is True+ assert "container lifecycle" in desc++ def test_docker_compose_down_with_file_flag_flagged(self):+ is_dangerous, _, desc = detect_dangerous_command(+ "docker compose -f docker-compose.prod.yml down")+ assert is_dangerous is True+ assert "container lifecycle" in desc++ def test_legacy_docker_compose_binary_down_flagged(self):+ is_dangerous, _, desc = detect_dangerous_command("docker-compose down")+ assert is_dangerous is True+ assert "container lifecycle" in desc++ def test_docker_compose_up_not_flagged(self):+ assert detect_dangerous_command(+ "docker compose -f dev.yml up -d") == (False, None, None)++ def test_docker_run_restart_policy_not_flagged(self):+ assert detect_dangerous_command(+ "docker run --restart=always -d nginx") == (False, None, None)diff --git a/tools/approval.py b/tools/approval.pyindex f7d15b8ab2a..4b27bfdfb00 100644--- a/tools/approval.py+++ b/tools/approval.py@@ -690,8 +690,40 @@ DANGEROUS_PATTERNS = [# containers without approval. These are agent-initiated lifecycle operations# that should always require user consent, just like `hermes gateway restart`# already does for the gateway process.- (r'\bdocker\s+compose\s+(restart|stop|kill|down)\b', "docker compose restart/stop/kill/down (container lifecycle)"),- (r'\bdocker\s+(restart|stop|kill)\b', "docker restart/stop/kill (container lifecycle)"),+ # Docker/Podman daemon redirect — global flags or env prefixes that point+ # the CLI at a DIFFERENT daemon, often a remote host over ssh/tcp. A+ # command that looks local (`docker -H ssh://prod stop app`) silently+ # operates on remote infrastructure, so any docker/podman invocation+ # carrying a redirect requires approval regardless of subcommand. The+ # redirect flag must appear in the global-flag position (before the+ # subcommand) and -H/--host/--context must carry a value, which keeps+ # `docker -h` (help) and subcommand flags like `docker run -h <hostname>`+ # out of the deny. Listed BEFORE the lifecycle rules so a redirected+ # lifecycle command surfaces the more specific "remote daemon" reason.+ # Inspired by Claude Code 2.1.214, which added permission prompts for+ # docker/podman commands carrying daemon-redirect flags (--url,+ # --connection, --identity, remote mode).+ (r'\bdocker\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(?:-h|--host)[=\s]+\S+',+ "docker with remote daemon redirect (-H/--host)"),+ (r'\bdocker\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(?:-c|--context)[=\s]+\S+',+ "docker with daemon redirect (--context: alternate daemon)"),+ (r'\bdocker\s+context\s+use\b',+ "docker context use (switches default daemon for future commands)"),+ (r'\bpodman\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(?:--url|--connection|--identity)[=\s]+\S+',+ "podman with remote daemon redirect (--url/--connection/--identity)"),+ (r'\bpodman\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(?:-r\b|--remote\b)',+ "podman remote mode (-r/--remote: remote daemon)"),+ (r'\b(?:docker_host|docker_context|container_host|container_connection)=\S+',+ "docker/podman daemon redirect via environment (DOCKER_HOST/CONTAINER_HOST)"),+ # Allow global flags between `docker`/`compose` and the verb (e.g.+ # `docker compose -f prod.yml down`, `docker --log-level debug stop app`)+ # and the legacy hyphenated `docker-compose` binary, so a flag can't slip+ # a lifecycle command past the guard — same treatment as the `hermes ...+ # gateway` pattern above.+ (r'\bdocker(?:-compose|\s+compose)\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(restart|stop|kill|down)\b',+ "docker compose restart/stop/kill/down (container lifecycle)"),+ (r'\bdocker\s+(?:-{1,2}\S+(?:[=\s]\S+)?\s+)*(restart|stop|kill)\b',+ "docker restart/stop/kill (container lifecycle)"),# Gateway protection: never start gateway outside systemd management(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')"),diff --git a/website/docs/user-guide/security.md b/website/docs/user-guide/security.mdindex c94ee53ea17..3e920b953a0 100644--- a/website/docs/user-guide/security.md+++ b/website/docs/user-guide/security.md@@ -182,6 +182,10 @@ The following patterns trigger approval prompts (defined in `tools/approval.py`)| `sed -i` / `sed --in-place` on `/etc/` | In-place edit of system config || `pkill`/`killall` hermes/gateway | Self-termination prevention || `gateway run` with `&`/`disown`/`nohup`/`setsid` | Prevents starting gateway outside service manager |+| `docker stop/kill/restart`, `docker compose down/stop/kill/restart` | Container lifecycle (also catches global flags and `docker-compose`) |+| `docker -H`/`--host`/`--context`, `DOCKER_HOST=`/`DOCKER_CONTEXT=` | Docker daemon redirect — the command targets a different (often remote) daemon |+| `docker context use` | Switches the default daemon for all future docker commands |+| `podman --remote`/`-r`/`--url`/`--connection`/`--identity`, `CONTAINER_HOST=` | Podman remote daemon redirect |:::info**Container bypass**: When running in `docker`, `singularity`, `modal`, or `daytona` backends, dangerous command checks are **skipped** because the container itself is the security boundary. Destructive commands inside a container can't harm the host.