Skip to content

fix(cli): refuse local path arguments on read-only MCP mirror tools - #5481

Open
tmchow wants to merge 9 commits into
mainfrom
tmchow/mcp-read-only-local-path-guard-acd4
Open

tmchow wants to merge 9 commits into
mainfrom
tmchow/mcp-read-only-local-path-guard-acd4

Conversation

@tmchow

@tmchow tmchow commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Intent

A hand-written printed-CLI command marked mcp:read-only can declare a string flag that names a local file (--terms FILE). Unless the author lists it in mcp:read-flags, the cobratree mirror exposes it, and because the tool carries readOnlyHint: true, hosts run it without a prompt. A prompt-injected MCP client can then make the server read any file the server account can read. Nothing enforced the annotation.

Issue: Closes #5458

Approach evaluation

Option Soundness Size / upkeep False positives Behavior change
Static Go-AST dogfood check (#5476) Fails open: every round found a new way Cobra registers or shadows a flag that the model missed +4.3k lines, never converged Low None at runtime
Narrow static check that fails closed Sound only if it flags every string flag it can't prove safe, which collapses to the next row Small Very high Shipcheck failures on reprint
Default-deny unannotated string flags in the mirror Sound Small ~22,000 string flags on hand-written read-only commands across the 615 published CLIs would need an annotation or silently leave MCP Large
Dogfood walks the real Cobra tree + a "does this flag reach a file read" signal Exposure is exact, but the read signal is not: static data flow fails open through helpers; a runtime probe fails open when the read sits behind a branch the probe never takes Medium (build, introspect, probe) Low Shipcheck only
Call-time guard in the generated mirror (chosen) Every argument passes through one point. The child runs where a plain relative name reaches nothing, and every value that could leave that directory is refused. No model of Go, no author action ~140 template lines + tests Low (see Risk) Read-only mirrored tools only

Names don't narrow it either. Real file-read flags on read-only commands in the library include from, against, in, root, plan, code, keywords, and entry.

Decision: for a tool registered with readOnlyHint, the mirror runs the child in a fresh empty working directory. It refuses the call when a value it would forward is an absolute, rooted, drive, ~, or ..-relative path once surrounding space and quotes are trimmed. The refusal names the flag (refusing --terms "/home/u/.ssh/id_rsa": ...). The check runs on:

  • flag values, array items, structured positionals, and raw args tokens, each as a whole value
  • for flags, the items pflag decodes for that flag's real Cobra type, read from the command at registration: CSV fields for stringSlice, and also the key=value values for stringToString
  • unknown or custom flag types, which get both splits so they still fail closed

string and stringArray flags and positionals are not split, so free text such as What does redirect=/docs mean? is forwarded.

The flag types only describe the command Cobra actually runs. So the mirror also resolves the final argv with Cobra's own Find on the companion's tree (cli.RootCmd()), which never parses flag values. It refuses the call if a positional would select a different command, such as a subcommand name or alias. This also stops a read-only parent tool from handing off to a non-read-only child without a prompt.

The check is purely syntactic and never touches the filesystem, so a refusal does not reveal whether a path exists. That matters because read-only tools run without a host prompt. It is sound for values used as given: in an empty directory, a relative name without .. resolves to nothing. Hosts often start MCP servers in / (Claude Desktop) or a project root, where ordinary words like private or users would match real directories. The empty directory is why those words can still be forwarded safely.

This crosses the issue's "no generated-code change" boundary on purpose: that boundary assumed a static check, and only the generated server sees the real value at call time.

Deliberately not covered: a command that builds a path from a value (filepath.Join(base, v), v + ".json", its own strings.Split, globbing, env expansion) is not caught, because the value itself is not a path. mcp:read-flags stays the declaration for those, and AGENTS.md says so. Non-read-only mirrored tools are unchanged: they carry no readOnlyHint, so hosts prompt and show the arguments. Recipe tools also carry no readOnlyHint.

Scope

Primary area: generated MCP mirror (internal/generator/templates/cobratree/)

Why this belongs in this repo: the missing annotation is a failure mode of every printed CLI with hand-written file-reading commands (the issue lists numista, pcgs, and ngccoin on main). Fixing the generated server closes it for every future print without per-CLI work.

Risk

  • A read-only mirrored tool now refuses values that start with / or ~/, or contain a .. element, even when they were not meant as local paths (for example an API path such as /v1/users). Full URLs, owner/repo, dates like 2024/01/02, ranges like 1..10, and free text are forwarded.
  • A read-only parent tool whose positional equals one of its subcommand names or aliases is refused. The caller should use that subcommand's tool instead.
  • An unannotated write-destination flag on a read-only command (for example --save-to /tmp/x) is now refused when given an absolute path. A relative value still passes and lands in the discarded child directory.
  • Read-only children no longer see the server's working directory. A read-only command that read a cwd-relative file by default would now miss it. The library has 3 hand-written read-only files that call os.Getwd and 3 relative ./ flag defaults.
  • A relative companion-CLI path that contains a separator (for example <CLI>_CLI_PATH=./bin/x) gets the server's directory prepended, uncleaned, before the directory change. Absolute paths and bare names are passed through untouched.
  • Endpoint typed tools, intents, recipes, and the read-only switch are unchanged. No skill text changes, so the skill and binary floors are unchanged.

Output Contract

Generated internal/mcp/cobratree/shellout.go, walker.go, typemap.go, and shellout_test.go change. The generate-golden-api and generate-device-ble fixtures are updated. The emitted tests assert refusal for:

  • existing and missing absolute paths
  • padded, quoted, ~/..., and bare ~ values
  • array items, stringSlice CSV fields, stringToString values, and both splits on an unknown flag type
  • .. escapes
  • structured positionals and quoted raw-args tokens

They also assert that:

  • private, users, owner/repo, a URL, 1..10, ., a file in the server's working directory, and free text containing =/ and ,/ are forwarded unchanged, as a string flag and as a positional
  • a positional that selects a subcommand by name or alias is refused, and resolving it never runs a flag's Set in the server, even with TraverseChildren
  • flagTypesForCommand reads real Cobra types, with a local flag shadowing an inherited one
  • non-read-only tools still forward paths
  • read-only children run in an empty directory, while other children keep the server directory

Verification

  • Generator/template change: verified generated output, including emitted-code assertions or compiled generated CLI output
  • Generator/template change: covered the affected fallback or variant shape, not only happy-path fixtures (BLE device prints compile; the guard uses only the standard library)
  • Generator/template change: checked emitted definitions and call sites for matching gates

Commands run:

  • scripts/verify-generator-output.sh: 15 cases pass
  • go vet ./... && go test ./... in a freshly generated stytch print: all pass (go test -race ./internal/mcp/... also passes)
  • go test ./internal/generator/ -run 'TestTemplatesNeverRedirectHomeByHand|Cobratree|Shellout|ReadOnly|ReadFlag|NovelFeature': pass
  • scripts/golden.sh verify after update: only cobratree files changed. dogfood-verdict-matrix and verify-runtime-matrix fail identically on unmodified main in this VM because their fixture CLI build fails here.
  • End to end (earlier existence-based revision; the transport and refusal path are unchanged): a generated print with a hand-written --terms command that calls os.ReadFile(terms), driven over MCP stdio with the server started in /:
    • unannotated, absolute and ~ paths are refused, naming --terms
    • bare etc is forwarded, and the child can't read it
    • with mcp:read-flags: terms, the parameter is gone
    • with mcp:hidden, the tool is absent
    • the non-read-only variant still reads the file (host-prompted)
  • AGENTS.md decoy-home proof on a fresh stytch print: .config / .local were byte-identical apart from the Go toolchain's own telemetry counters

AI / Automation Disclosure

  • No AI or automation was used
  • Human-reviewed: AI or automation was used, and a human reviewed the work for intent, fit, and obvious issues before submission
  • AI-reviewed only: an AI agent reviewed the work, but no human reviewed it before submission
  • Fully automated: generated and submitted without human review for this specific change
Open in Web Open in Cursor 

cursoragent and others added 2 commits November 24, 2026 19:55
A hand-written mcp:read-only command whose file flag is not listed in
mcp:read-flags let an MCP client read any file the server account can
read. The cobratree mirror now runs read-only children in a fresh empty
working directory and refuses a call when a forwarded value names an
existing local file or directory.

Co-authored-by: Trevin Chow <tmchow@users.noreply.github.com>
Co-authored-by: Trevin Chow <tmchow@users.noreply.github.com>
@mergify

mergify Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Merge Protections

🔴 2 of 2 protections blocking · waiting on 🙋 you

Protection Waiting on
🔴 require-ready-label-and-ci 🙋 you
🔴 🚦 Auto-queue 🙋 you

🔴 require-ready-label-and-ci

Waiting for

  • label = ready-to-merge
This rule is failing.
  • any of:
    • label = ready-to-merge
    • all of:
      • head = release-please--branches--main
      • title ~= ^chore\(main\): release
  • #changes-requested-reviews-by = 0
  • #review-threads-unresolved = 0
  • check-success = build-and-test
  • check-success = generated-test
  • check-success = go-lint
  • check-success = golden
  • check-success = pr-title
  • check-success = test
  • any of:
    • -files ~= ^(\.github/workflows/|\.github/scripts/|scripts/|\.github/CODEOWNERS$)
    • author = tmchow
    • approved-reviews-by = mvanhorn
    • approved-reviews-by = tmchow
    • author = mvanhorn
  • any of:
    • check-success = Greptile Review
    • check-neutral = Greptile Review
    • check-skipped = Greptile Review
    • head ~= ^mergify/merge-queue/
    • label = queued
    • all of:
      • head = release-please--branches--main
      • title ~= ^chore\(main\): release

🔴 🚦 Auto-queue

Waiting for

  • label = ready-to-merge
This rule is failing.

When all merge protections are satisfied and these conditions match, this pull request will be queued automatically.

  • label = ready-to-merge

@mergify

mergify Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

@greptile-apps

greptile-apps Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] No blocking issue was found in this revision.

Summary

Read-only MCP mirror tools now reject path-shaped arguments and run the companion CLI in a fresh working directory.

  • Read-only mirror calls reject local paths and command changes.

Reviews (5) · Last reviewed commit: "fix(cli): resolve the read-only MCP targ..." · Reviewed by Greptile

cursoragent and others added 2 commits November 24, 2026 20:25
…em in read-only MCP mirrors

Co-authored-by: Trevin Chow <tmchow@users.noreply.github.com>
Co-authored-by: Trevin Chow <tmchow@users.noreply.github.com>
cursoragent and others added 2 commits November 24, 2026 20:48
…codes them

Co-authored-by: Trevin Chow <tmchow@users.noreply.github.com>
…refusal

Co-authored-by: Trevin Chow <tmchow@users.noreply.github.com>
cursoragent and others added 2 commits November 24, 2026 21:15
…erent Cobra command

Co-authored-by: Trevin Chow <tmchow@users.noreply.github.com>
Co-authored-by: Trevin Chow <tmchow@users.noreply.github.com>
…ver never parses flags

Co-authored-by: Trevin Chow <tmchow@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unannotated file-read flags on hand-written commands are agent-reachable over MCP

2 participants