Repository navigation
Conversation
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>
Contributor
Merge Protections🔴 2 of 2 protections blocking · waiting on 🙋 you
🔴 require-ready-label-and-ciWaiting for
This rule is failing.
🔴 🚦 Auto-queueWaiting for
This rule is failing.When all merge protections are satisfied and these conditions match, this pull request will be queued automatically.
|
Contributor
|
Tick the box to add this pull request to the merge queue (same as
|
Contributor
[Medium impact] No blocking issue was found in this revision. SummaryRead-only MCP mirror tools now reject path-shaped arguments and run the companion CLI in a fresh working directory.
Reviews (5) · Last reviewed commit: "fix(cli): resolve the read-only MCP targ..." · Reviewed by Greptile |
…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>
…codes them Co-authored-by: Trevin Chow <tmchow@users.noreply.github.com>
…refusal Co-authored-by: Trevin Chow <tmchow@users.noreply.github.com>
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
A hand-written printed-CLI command marked
mcp:read-onlycan declare a string flag that names a local file (--terms FILE). Unless the author lists it inmcp:read-flags, the cobratree mirror exposes it, and because the tool carriesreadOnlyHint: 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
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, andentry.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:argstokens, each as a whole valuestringSlice, and also thekey=valuevalues forstringToStringstringandstringArrayflags and positionals are not split, so free text such asWhat 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
Findon 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 likeprivateoruserswould 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 ownstrings.Split, globbing, env expansion) is not caught, because the value itself is not a path.mcp:read-flagsstays the declaration for those, and AGENTS.md says so. Non-read-only mirrored tools are unchanged: they carry noreadOnlyHint, so hosts prompt and show the arguments. Recipe tools also carry noreadOnlyHint.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
/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 like2024/01/02, ranges like1..10, and free text are forwarded.--save-to /tmp/x) is now refused when given an absolute path. A relative value still passes and lands in the discarded child directory.os.Getwdand 3 relative./flag defaults.<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.Output Contract
Generated
internal/mcp/cobratree/shellout.go,walker.go,typemap.go, andshellout_test.gochange. Thegenerate-golden-apiandgenerate-device-blefixtures are updated. The emitted tests assert refusal for:~/..., and bare~valuesstringSliceCSV fields,stringToStringvalues, and both splits on an unknown flag type..escapesThey 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 positionalSetin the server, even withTraverseChildrenflagTypesForCommandreads real Cobra types, with a local flag shadowing an inherited oneVerification
Commands run:
scripts/verify-generator-output.sh: 15 cases passgo 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': passscripts/golden.sh verifyafter update: only cobratree files changed.dogfood-verdict-matrixandverify-runtime-matrixfail identically on unmodifiedmainin this VM because their fixture CLI build fails here.--termscommand that callsos.ReadFile(terms), driven over MCP stdio with the server started in/:~paths are refused, naming--termsetcis forwarded, and the child can't read itmcp:read-flags: terms, the parameter is gonemcp:hidden, the tool is absent.config/.localwere byte-identical apart from the Go toolchain's own telemetry countersAI / Automation Disclosure