← Back to Pull requests

fix(sre): allow compose flags in run_shell_command and validate docker compose subcommands #1002

Open
jeethgame opened this pull request with 140 lines diff across 1 files
View on GitHub
jeethgame jeethgame commented on Nov 30, 2026 07:53 UTC
Author

Summary


Closes #974

run_shell_command() in claude_agent_sdk/site_reliability_agent/sre_mcp_server.py previously checked args[1] directly against allowed_commands[executable]. This caused two issues:
1. Documented commands such as docker-compose -f config/docker-compose.yml up -d api-server failed validation because args[1] is '-f'.
2. Commands using docker compose ... only validated that args[1] == 'compose', without checking the actual compose subcommand (allowing arbitrary subcommands or bare docker compose).

Changes


  • Implemented _extract_compose_subcommand helper to parse through global flags (such as -f, --file, -p, --project-name) to find the actual compose action.

  • Validated compose actions against allowed_compose_subs ({"up", "down", "ps", "logs", "restart", "build"}) for both docker-compose and docker compose.

  • Kept root-level docker commands (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)
  • Diff: 140 lines (+109 / -31) across 1 files Reviewer: tonydzi Review duration: 9390m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.38) × (1 − Review Confidence (0.92)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.38 · 140 lines diff across 1 files
    0.38
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.96 · 2 substantive comment(s)
    0.96
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    91%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.296
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    5 items
    tonydzi tonydzi commented on Dec 01, 2026 02:53 UTC
    Reviewer

    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.

    jeethgame jeethgame commented on Dec 03, 2026 13:36 UTC
    Author

    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.

    tonydzi tonydzi commented on Dec 04, 2026 02:37 UTC
    Reviewer

    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

    jeethgame jeethgame commented on Dec 05, 2026 15:07 UTC
    Author

    Thanks again @tonydzi for catching both bypasses! I switched _extract_compose_subcommand in sre_mcp_server.py to fail closed as suggested:

  • Added an explicit boolean_flags allowlist ({"--compatibility", "--dry-run", "--all-resources"}).

  • Restricted --flag=value handling to long flags in value_flags instead of accepting arbitrary --* flags (so --workdir=/path/outside is rejected).

  • Rejected any remaining flag tokens starting with - (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.

    Reviewers
    Assignees
    jeethgame jeethgame
    Labels
    risk: low confidence: 91.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 37.6% (MEDIUM) · Higher = larger blast radius
    37.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 91.9% · Lower = rushed or shallow review
    91.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 3% · Safe to merge
    3%