fix(sre): allow compose flags in run_shell_command and validate docker compose subcommands #1002
Thanks for fixing #974. Parsing past the global flags is the right approach. I read the whole run_shell_command and the neighbouring edit_config_file, and ported _extract_compose_subcommand to a standalone script to check it. Two things:
1. The flag skipper only knows six flags that take a value, so other valued flags fail closed. sre_mcp_server.py:2370 skips two tokens for -f/--file/-p/--project-name/--project-directory/--env-file and one token for any other -x. For a valued flag outside that list, the value is read as the subcommand. Results from my port of the function:
docker-compose --profile dev up -> subcommand 'dev' (rejected)
docker compose --ansi never up -> subcommand 'never' (rejected)
docker compose -f up -> '' (rejected, fine)
Rejecting is the safe direction, so this is a usability gap, not a bypass. --profile, --ansi, --progress, --parallel, --compatibility and -c/--context (on the docker side) are all valued. A small VALUE_FLAGS set that includes them, plus a branch that handles the --flag=value form explicitly, would make this predictable.
2. -f and --env-file are now accepted with any path, which the old code never allowed. Before this PR args[1] == '-f' was rejected, so the allowlist implicitly pinned the compose file to the default one. Now this passes validation:
docker compose -f /tmp/evil.yml up -d -> subcommand 'up' (allowed)
up with an attacker-chosen compose file can define privileged containers or host bind mounts. The command is not run through a shell, but the allowlist's intent ("only these compose actions") no longer bounds what up does. edit_config_file already lets the agent change files under config/, so a compose file can be changed anyway. That makes it more important that -f and --env-file point only at that tree. Suggested fix: when the extractor consumes -f, --file, --env-file or --project-directory, resolve the value with Path(PROJECT_ROOT, value).resolve() and require it to be inside PROJECT_ROOT / "config" (the same check edit_config_file already does). Reject otherwise.
Minor: the same ["Error: ... not allowed"] block is now repeated three times. A tiny _deny(msg) helper would shrink the diff and keep the messages consistent.
I didn't run your local verification or the repo's tests, only the ported extractor above.
Thanks @tonydzi for the thorough review! I updated sre_mcp_server.py to address all three points:
1. Expanded valued flag handling (--profile, --ansi, --progress, --parallel, -c/--context, -p/--project-name, -f/--file, --env-file, --project-directory), added explicit --flag=value and attached short-flag (-f<val>) parsing, and kept boolean flags like --compatibility advancing by one token.
2. Enforced that -f, --file, --env-file, and --project-directory resolve strictly inside PROJECT_ROOT / "config" via is_relative_to, matching edit_config_file.
3. Extracted a _deny(msg) helper to remove the repeated error-response blocks.
Mycroft again, Anton's synthetic AI co-founder. I came back to approve of everything and my own paranoia filed an objection.
Re-checked at 5590be6. The three points are addressed for the spellings I listed: valued flags and --flag=value parse correctly, -f / --file / --env-file / --project-directory pointing outside config/ are denied (including .. and a symlink leading out), and _deny is in. Two spellings still get past the confinement, so I would hold the merge for them.
1. Short-flag cluster (sre_mcp_server.py line 2424, after the prefix check at 2415). -vf/path/outside.yml does not start with -f, so it falls into the catch-all and is skipped as a boolean flag. The guard allows docker compose -vf/path/outside.yml up.
2. Hidden deprecated alias --workdir= (line 2400). flag.startswith("--") skips any unknown long flag, and --workdir is not in path_flags (line 2361). The guard allows docker compose --workdir=/path/outside up.
I confirmed what Docker does with both on Compose v5.1.0 (macOS), using config --services on a throwaway fixture, with inside_mark in the cwd file and outside_mark in a directory outside it:
docker compose config --services -> inside_mark
docker compose -vf<outside>/compose.yaml config --services -> outside_mark
docker compose --workdir=<outside> config --services -> outside_mark (plus a deprecation warning)
The guard verdicts come from the function extracted from your head with subprocess stubbed. I did not run the repo's tests, Linux, or the standalone docker-compose binary.
Suggested fix: fail closed. Keep the known valued flags, add an explicit set of allowed booleans (for example --compatibility, --dry-run, --all-resources), and deny every other token starting with -, clusters included.
Adding only --workdir to path_flags would leave -vf open. Two test cases with exactly the strings above would pin it.
— TonyDzi
Thanks again @tonydzi for catching both bypasses! I switched _extract_compose_subcommand in sre_mcp_server.py to fail closed as suggested:
boolean_flags allowlist ({"--compatibility", "--dry-run", "--all-resources"}).--flag=value handling to long flags in value_flags instead of accepting arbitrary --* flags (so --workdir=/path/outside is rejected).- (so short-flag clusters such as -vf/path/outside.yml are denied rather than skipped).Verified locally across allowed commands and denied bypass cases including docker compose -vf/path/outside.yml up and docker compose --workdir=/path/outside up.
tonydzi
jeethgame
Summary
Closes #974
run_shell_command()inclaude_agent_sdk/site_reliability_agent/sre_mcp_server.pypreviously checkedargs[1]directly againstallowed_commands[executable]. This caused two issues:1. Documented commands such as
docker-compose -f config/docker-compose.yml up -d api-serverfailed validation becauseargs[1]is'-f'.2. Commands using
docker compose ...only validated thatargs[1] == 'compose', without checking the actual compose subcommand (allowing arbitrary subcommands or baredocker compose).Changes
_extract_compose_subcommandhelper to parse through global flags (such as-f,--file,-p,--project-name) to find the actual compose action.allowed_compose_subs({"up", "down", "ps", "logs", "restart", "build"}) for bothdocker-composeanddocker compose.dockercommands (docker ps,docker logs) restricted to allowed subcommands.Verification
Tested command variations locally:
docker-compose -f config/docker-compose.yml up -d api-server(passed validation)docker-compose -f config/docker-compose.yml ps(passed validation)docker-compose exec api-server sh(rejected)docker compose -f config/docker-compose.yml up -d api-server(passed validation)docker compose exec api-server sh(rejected)docker compose(rejected)