Repository navigation
fix(mcp): refuse a probe temp declared inside the sealed runtime parent (#8747) - #9108
Conversation
Design Review (Fable 5) — 🟡 CONCERNSDesign-level review of Reformatting artifacts confirmed — no test pins were deleted; the Design-Verdict: CONCERNS Right layer and well-falsified approach, but sequencing inverts probe fidelity for one class: a sealed temp that breaks the handshake now probes green. Watch
Suggestions
[DESIGN-REVIEWED] 4c9a540 |
First Principles Review (Fable 5) — ✅ PASSPremise-level review of All checks are done: the defect has provenance (issue #8747, tests that fail on base), the two unfixed runtime siblings are counted and tracked by the author in #9843, First-Principles-Verdict: PASS Verify the ~500-line Not justified as shipped
What this change shipsInventory (9 items) — 7 justifiedIntent: make the MCP probe stop reporting green for a server whose spec-declared temp sits inside the sealed
The probe-only scope leaves 2 counted siblings ( [FIRST-PRINCIPLES-REVIEWED] 4c9a540 |
Opus 4.8 Review — ✅ no blocking findingsReviewed Review detailsI've traced the substantive logic in both I could not ground any behavioral defect to (a) a concrete input, (b) a call path from code I opened, and (c) an observable wrong outcome on a line this PR adds or changes. The candidate list's "no candidates" holds under falsification. No findings. [OPUS-REVIEWED] 4c9a540 Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 5.6 Review — ✅ no blocking findingsGPT 5.6 completed its review of This comment is updated in place on each push. Review detailsFINDING -- test/test_sandbox_argv.py:1080 -- The docstring lists False positive or not applicable? A repository writer can comment: |
4b14323 to
586d3fa
Compare
586d3fa to
673c46c
Compare
cixuuz
left a comment
There was a problem hiding this comment.
Two security issues remain:
-
path_within_sealed_runtime_parent()callsrealpath()/filesystem identity probing on spec-controlled input before rejecting remote Windows spellings. A UNC/device path can therefore initiate SMB or other remote resolution during a local containment check. Reject remote/device forms before any resolution and use no-follow/local identity checks. -
In the managed-temp rewrite,
key in _CANONICAL_TEMP_KEYSis case-sensitive even though spec env keys are explicitly treated case-insensitively on Windows. A declaredtmpdir/Tempcan survive the rewrite. Normalize every comparison withkey.upper().
iamwhatever
left a comment
There was a problem hiding this comment.
There are comments about security related, please handle it. Thanks
673c46c to
233fc5f
Compare
233fc5f to
3ec046b
Compare
24d1bd2 to
20bf265
Compare
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: yes
|
|
self-added: yes
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: yes
|
|
self-added: yes
|
|
self-added: yes
|
|
self-added: yes
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
Both open decisions are settled at 0950504. Decision 1 — follow-up filed, scope kept. The runtime wiring is #9843 ( Decision 2 — the First Principles Subtraction is taken. |
An MCP spec may declare its own temp key, and the discovery probe honored it by suppressing its managed allocation entirely. When the declared path resolves inside <data home>/run -- the runtime parent BOTH sandbox backends seal read-only -- the probed child received a TMPDIR it could not write: the same Bun/Koffi extraction failure #8653 fixed for the managed path, and silent, because a server that never touches temp still answers the handshake and the probe reports it green. Carving the declared path out of the seal is not the alternative: spec env is untrusted config text and extra_writable_dirs is validated for self-derived scratch only, so a validator that merely refused guarded paths would approve any other subtree under run. The declaration is therefore REFUSED and the managed temp takes over -- allocated before the wrap and carved out, exactly as an undeclared probe already is. That keeps the operator's storage intent, since a path under run named the data-home volume and so does the managed root, and it is announced with a WARNING naming the seal and the key it overrode rather than failing quietly. Refused as a whole: tempfile consults TMPDIR before TMP, so honoring a surviving sibling key would leave writability depending on which key the spec happened to spell. sandbox.py owns the seal, so it answers the question: classify_declared_temp_path tests both the lexical and the canonical spelling (a supported symlinked data home is seen independently by path-based rules) and names the cause of a refusal -- sealed or unclassifiable -- so the probe's WARNING can say which one applied instead of calling every refusal "inside the seal". It is the single entry point: a bool reduction of it had no caller in src/ and is not kept, so the tests assert on the classification itself. When the allocation itself fails, the refusal still stands and every temp key is stripped, so the child inherits the ambient temp instead of a directory already known to be read-only. Spelling alone is not enough to answer it, and neither is spelling in the wrong order. realpath runs on the ORIGINAL declaration, before any lexical pass: normalizing first collapses `..` lexically and so deletes the very symlink that `..` was climbing out of, which let a <symlink-into-run>/../tmp declaration read as outside the seal while the child's libc resolves symlink-first and lands back inside it. And on case-insensitive APFS <data home>/RUN and <data home>/run are ONE directory, which realpath does not fold either -- it walks with lstat/readlink, neither of which canonicalizes the case of an existing component -- so a differently-cased declaration passed a purely lexical predicate, the probe honored it, and the child got the sealed TMPDIR anyway. The predicate therefore asks the filesystem: it compares (st_dev, st_ino) against the sealed parent while climbing the declared path's ancestors, which also catches firmlink and normalization aliases. A declared temp usually does not exist yet, so the walk simply climbs until a component stats -- the deepest EXISTING ancestor carries the identity, and nothing below a sealed directory can climb back out of it because the path arrives resolved and normalized. Only identity is compared, never a folded spelling, so a case-SENSITIVE filesystem answers exactly as before: there RUN is a different directory, it does not exist, and nothing seals it. An unstat-able sealed parent yields the lexical answer rather than a folded guess -- the parent is primed and created a few lines above, so reaching that needs a deletion race, and folding there would mis-refuse a genuinely distinct RUN directory for no gain. On Windows the check answers None for every path and the declared temp is honoured exactly as before this change. Kiro Crew has no native Windows sandbox backend, so nothing seals run/ there: a probe child is either not spawned at all or runs unsandboxed with a writable run/, and a declared temp under it is a writable directory the operator chose. Resolving untrusted config text on Windows would also be an access in itself -- realpath and stat open the path they are handed -- so gating the classification off is what keeps it local. The review rounds had grown a lexical UNC/device refusal, an NT-prefix classifier, a mapped-drive probe and a no-follow link-chain walker to make that resolution safe on a platform where the seal it protects does not exist; four Windows-only holes were found in that guard across two heads, and it is gone rather than patched a fifth time. POSIX keeps the kernel's own loop detection instead of a hand-rolled walker: realpath(strict=True) raises ELOOP for a symlink cycle, and a raise other than FileNotFoundError classifies the path as unclassifiable -- its canonical form cannot be established, so containment cannot be verified, and it is refused like every other unverifiable declaration. FileNotFoundError is the ordinary case (a declared temp usually does not exist yet) and falls back to resolving as far as the path goes. The lenient realpath alone would return a cycle's own link with the remainder appended, a spelling that says nothing about where the temp really is and that a lexical or identity comparison would wave through. The rewrite that carries the refusal out matched temp keys case-SENSITIVELY, which handed the sealed path back through the door the refusal had just closed. Detection is case-insensitive on purpose -- Windows env keys are case-insensitive and the sanitized spec preserves the author's spelling -- so a spec declaring tmpdir was correctly SEEN, correctly refused, and then left its own key sitting in the child's env beside the managed TMPDIR, which on Windows is the same variable. Every comparison normalizes: the managed branch drops every case spelling of a temp key before merging its triple, and the honored branch re-emits the declared value under the canonical uppercase key with the spec's own spelling and the ambient siblings dropped -- on POSIX tempfile reads only TMPDIR/TMP/TEMP as spelled, so a declared tmpdir would otherwise keep its key, lose the ambient ones and get the platform default, while on Windows the two spellings are one variable and keeping both would leave writability to whichever the child's lookup reached first. Verified live against an isolated pod (seed connections-two) through POST /api/mcp/custom + POST /api/mcp/probe, with three servers reporting their own TMPDIR facts as tool names. Before: write_EROFS on a declared run/mcp-tmp and mkdir_EROFS on a declared run/custom-tmp. After: both probe on a managed run/mcp-tmp/probe-*/tmp with write_ok, while parentwrite_EROFS shows the seal itself is unchanged. Closes #8747
|
Recommendation: hold #9108 until #9843 is guaranteed to land with it—either folded into this PR or as an atomically sequenced stacked pair before any nightly or release can contain #9108 alone. The fallback policy itself is sound: The current split is not an acceptable intermediate state: the probe replaces a sealed declaration with the managed temp and can report green, while runtime still honors the sealed declaration and can fail on its first temp write. #9855 improves disclosure but does not restore correctness; #9843 is the correctness dependency. Once #9843 restores probe/runtime parity, #9855 can remain a bounded observability follow-up. The documentation claim should also stay narrow: this prevents the known sealed-parent conflict; it does not certify that every arbitrarily declared temp directory exists or is writable. |
|
Agreed on the split: the probe swapping a sealed declaration for the managed temp while What #10012 does. One shared rule, The docs claim is narrowed there too, in the same spec section this PR touches ( #9855 stays the bounded observability follow-up you describe. |
chenmingwei23
left a comment
There was a problem hiding this comment.
[code-review-sage] LGTM -- nothing blocking. The containment check is correct against every evasion I traced, and the design is a proportionate fix for a real silent-failure bug (#8747).
Assessment
classify_declared_temp_path resolves the path the way the child's libc will, then backstops the spelling comparison with a filesystem-identity walk. I traced the evasion classes this kind of check usually fails on and each is handled:
- symlink then
..:realpath(strict=True)runs on the original spelling BEFORE the lexical pass, so<symlink-into-run>/../tmpresolves symlink-first back into the seal and is caught. Collapsing..lexically first (the classic bug) is explicitly avoided. - case / firmlink / normalization alias (
<home>/RUNvs<home>/runon APFS):realpathdoes not fold case, so_identity_within_sealed_parentwalks(st_dev, st_ino)up the existing ancestors and matches the sealed parent's identity. A differently-cased path that folds OUTSIDE the seal stays honored. - symlink cycle:
realpath(strict=True)raises, caught asunclassifiableand refused; the lenient form (which returns a half-resolved spelling and never raises) is deliberately not trusted. - missing leaf (the ordinary case):
FileNotFoundErrorfalls back to lenientrealpath, and the identity walk climbs to the deepest existing ancestor. Because both spellings are normalized before the walk, no..remainder can climb back out. - false positives on macOS:
/varand/tmpare symlinks, but the check only refuses paths whose real location is inside<data home>/run; a temp outside the seal (the normal operator choice) classifiesNoneand is honored.test_spec_declared_temp_suppresses_probe_containmentandtest_path_outside_the_data_home_is_not_sealedcover this.
Fail-closed behaviour is right on both error paths: a raising check refuses every declared key under check-failed and falls to the managed temp; an allocation failure AFTER a refusal strips every temp key so the child lands on its platform default rather than the known-read-only sealed path -- never fail-open onto the sealed dir. The whole-declaration refusal (not per-key) is correct given tempfile reads TMPDIR before TMP. Windows returns None for every path by design and resolves nothing, which is right: nothing seals run/ there and resolving untrusted config text would open whatever it names.
The TestProbeTempContainment and TestSealedRuntimeParentPredicate suites cover the changed behaviour directly, including the case-alias path via a modelled APFS stat/lstat and a real-filesystem variant that skips on case-sensitive CI.
Findings
[YELLOW] Maintainability -- test/test_sandbox_argv.py (~line 1080), TestSealedRuntimeParentPredicate docstring. The class docstring lists a refusal cause that no longer exists:
``sealed``,
``remote-or-device`` or ``unclassifiable`` = refuse, and why)
DeclaredTempRefusal is Literal["sealed", "unclassifiable"] and no code path produces or asserts remote-or-device -- it is a leftover from an earlier three-cause design. Cause: a reader of the test class is told the classifier can return a cause it cannot. Mechanism: they look for (or assume) a remote-or-device branch that was never built. Consequence: wasted time or a spurious "missing coverage" concern on the next revision. Non-blocking, docstring only.
Suggestion: drop remote-or-device from the docstring so it names only sealed and unclassifiable.
Relationship to #10012
Real and declared, not a conflict. #10012 ("refuse sealed temp declarations at runtime") is stacked on this PR (its body says "merge after it; the base retargets to main when #9108 lands") and closes #9843 -- the runtime-spawn wiring (backend.py, gatewayd.py) this PR explicitly defers. It replaces this PR's classify_declared_temp_path with a generalized classify_declared_temp_env and removes most of what this PR adds to mcp_discovery.py (+20/-86 there). The sandbox.py and mcp_discovery.py overlap is a build-on, not a collision. Merge order is #9108 then #10012, as #10012 states.
|
Maintainer note (@bolichen97, agent-assisted), head Two ways forward, your pick: (a) fold #10012 into this PR (the two-commit budget allows it) and we review the pair as one; or (b) tell us you want them merged back-to-back — we approve both, merge #9108, you retarget #10012 to Smaller notes on this head: |
|
Follow-up from the #10012 review (maintainer, agent-assisted): one finding lands in this PR's helper rather than in #10012 — |
Problem / Motivation
An MCP spec can declare its own temp key (
TMPDIR,TMPorTEMP). When that path sits inside<data home>/run, the probed server gets a temp directory it cannot write. Both sandbox backends sealrunread-only. The probe still reports the server green: a server that never touches temp during the handshake never hits the read-only wall. The failure shows up at the first temp write, with nothing in the probe result to explain it. (#8747)Why it matters
The probe exists to tell an operator whether a server will run. A green probe for a server that fails on its first temp write is worse than a red one. The operator trusts it and debugs the wrong thing. Any spec that points its temp under the data home hits this. The write carve-out cannot be the fix: spec
envis untrusted config text, andextra_writable_dirsis validated for self-derived scratch only, so a validator that merely refused guarded paths would approve any other subtree underrun.What changed (motivation → approach → change)
The probe refuses a declared temp that lands inside the sealed runtime parent and probes with the managed temp instead.
classify_declared_temp_pathinsrc/kiro_crew/sandbox.pyanswers the question, because the seal lives in that module. It says why a path is refused:sealed, orunclassifiablewhen the canonical form cannot be established.probe_serverinsrc/kiro_crew/mcp_discovery.pydrops every declared temp key when any is refused, allocates the managed temp exactly as an undeclared probe does, and logs one WARNING naming each refused key, its path and the actual cause. The refusal is whole, not per key:tempfilereadsTMPDIRbeforeTMP, so honoring a surviving sibling would leave writability to whichever key the spec happened to spell.The check fails closed. If the classification itself raises, every declared temp key is refused under a
check-failedcause and the WARNING names the exception. If the managed allocation fails after a refusal, every temp key is stripped, so the child falls back to its platform default rather than the refused path.Containment is judged by the filesystem, in a safe order.
realpathruns on the original spelling before any lexical pass, so<symlink-into-run>/../tmpis caught. A(st_dev, st_ino)walk up the declared path's ancestors catches a case, firmlink or normalization alias ofrunthatrealpathdoes not fold. The resolve is strict, so the kernel's own loop detection answers for a symlink cycle:realpath(strict=True)raises, the path isunclassifiable, and the declaration is refused like any other whose real location is unknown. A component that does not exist yet is the ordinary case and resolves as far as the path goes. On POSIX//pathis a legal spelling of/pathand is judged on containment like any other path.On Windows a declared temp is honored, because nothing seals
run/there. Kiro Crew has no native Windows sandbox backend, so a probe child on Windows is either not spawned or runs unsandboxed with a writablerun/.classify_declared_temp_pathreturnsNonefor every path onwin32and resolves nothing, sincerealpathandstatopen the path they are handed on that platform. The declaration governs exactly as it did before this change.Temp keys are matched case-insensitively everywhere. A spec declaring
tmpdiris refused when sealed. When honored, it is re-emitted under the canonicalTMPDIR, with the spec's own spelling and the ambient siblings dropped, so the declaration governs on Windows (one variable) and on POSIX (tempfilereads only the uppercase names).Scope: the probe only.
spawn_backend(src/kiro_crew/mcp_gateway/backend.py:4046) and the_spawnclosure insrc/kiro_crew/mcp_gateway/gatewayd.py(:3402,:3433) honor a sealed declaration without this check, so a probe-green result does not model runtime for such a spec until they run it. Tracked in #9843. The refusal reaches the gateway journal only; carrying it in the probe result and the dashboard is #9855.Tests
test/test_mcp_discovery.py: 216 passed, 4 skipped (-n0).test/test_sandbox_argv.py: 217 passed, 3 skipped (-n0). On Windows the threetest_mcp_discovery.pytests that drive a real sealed refusal skip, because the classification returnsNonethere by design;test_sandbox_argv.pyis not collected on Windows (test/windows-collect-ignore.txt).TestProbeTempContainmentintest/test_mcp_discovery.py: a sealed declaration yields the managed temp on all three canonical keys and the sealed path reaches nothing; no temp key survives when allocation fails after a refusal;tmpdir,TempandTmPare refused alike; an unsealedtmpdiris honored underTMPDIRwith no lowercase twin; a raising check fails closed onto the managed temp with one WARNING naming the failure; the WARNING names the actual cause per refusal.TestSealedRuntimeParentPredicateintest/test_sandbox_argv.pyasserts on the classification itself.sealed: paths underrunin either spelling of a symlinked data home, a..climb back into the seal through a symlink, a case alias ofrun(modelled APFS-style, plus a real-filesystem variant that skips where the filesystem is case-sensitive), a symlink intorun, a local link chain ending insiderun, a missing leaf underrun.None: paths outside the seal, an empty declaration, a case alias that folds onto a directory outsiderun, a local link chain ending outside, a missing leaf outside.unclassifiable: a symlink cycle, as the leaf and as an ancestor of a leaf that does not exist yet.//pathon POSIX is judged on containment. Onwin32(platform pinned), a declaration inside the would-be-sealed parent is not refused andrealpathis never called; that test fails on the code without the gate withassert 'sealed' is None.The existing
test_spec_declared_temp_suppresses_probe_containmentpasses unchanged: a temp declared outside the seal is honored.Manual verification
Isolated pod on this branch (seed
connections-two). Three servers added throughPOST /api/mcp/customand probed throughPOST /api/mcp/probe, each apython3 -cserver reporting its own TMPDIR facts as tool names.Before the fix:
repro-declared-sealed-existingprobesokwithwrite_EROFS;repro-declared-sealed-missingprobesokwithwrite_ENOENT mkdir_EROFS. Thatokis the silence in the issue title.With the fix: both probe on
<pod>/run/mcp-tmp/probe-*/tmpwithwrite_ok.parentwrite_EROFSon every row shows the seal itself is untouched. The pod journal carries one WARNING per refused server naming the sealed path. Pod torn down afterwards; live gateway untouched.Related Issues
Fixes #8747
Follow-up: #9843 wires
classify_declared_temp_pathintospawn_backendandgatewayd.Follow-up: #9855 carries the refusal in the probe result and the dashboard.
Pattern harvest
Rule candidate: review-prompt
Pattern: lexical path containment (
==/startswith/commonpath) used as a security or writability boundary with no filesystem-identity check, and its sibling, normalizing a path before resolving it, which collapses..past the symlink it was climbing out of. Both make the comparison disagree with what the kernel does.assert_voice_runtime_outside_agent_workspacein the same module walks(st_dev, st_ino)for exactly this reason, so a reviewer prompt that flags a new lexical boundary check near a sealed path, and asks whether an identity walk belongs beside it and in which order the path is resolved, would catch the class. Deliberately not a semgrep rule: lexical containment is right in the many places that are not a trust boundary.Checklist
feat|fix|docs|refactor|perf|test|chore|ci|build|revert: ...)docs/system-specs/modules/memory-skills-hooks.md, MCP Discovery, "Probe temp"