Skip to content

fix(mcp): refuse a probe temp declared inside the sealed runtime parent (#8747) - #9108

Merged
iamwhatever merged 1 commit into
mainfrom
fix/pod-repro-8747
Sep 11, 2026
Merged

iamwhatever merged 1 commit into
mainfrom
fix/pod-repro-8747

Conversation

@pepmach

@pepmach pepmach commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

An MCP spec can declare its own temp key (TMPDIR, TMP or TEMP). When that path sits inside <data home>/run, the probed server gets a temp directory it cannot write. Both sandbox backends seal run read-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 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.

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_path in src/kiro_crew/sandbox.py answers the question, because the seal lives in that module. It says why a path is refused: sealed, or unclassifiable when the canonical form cannot be established. probe_server in src/kiro_crew/mcp_discovery.py drops 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: tempfile reads TMPDIR before TMP, 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-failed cause 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. realpath runs on the original spelling before any lexical pass, so <symlink-into-run>/../tmp is caught. A (st_dev, st_ino) walk up the declared path's ancestors catches a case, firmlink or normalization alias of run that realpath does not fold. The resolve is strict, so the kernel's own loop detection answers for a symlink cycle: realpath(strict=True) raises, the path is unclassifiable, 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 //path is a legal spelling of /path and 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 writable run/. classify_declared_temp_path returns None for every path on win32 and resolves nothing, since realpath and stat open 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 tmpdir is refused when sealed. When honored, it is re-emitted under the canonical TMPDIR, with the spec's own spelling and the ambient siblings dropped, so the declaration governs on Windows (one variable) and on POSIX (tempfile reads only the uppercase names).

Scope: the probe only. spawn_backend (src/kiro_crew/mcp_gateway/backend.py:4046) and the _spawn closure in src/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 three test_mcp_discovery.py tests that drive a real sealed refusal skip, because the classification returns None there by design; test_sandbox_argv.py is not collected on Windows (test/windows-collect-ignore.txt).

TestProbeTempContainment in test/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, Temp and TmP are refused alike; an unsealed tmpdir is honored under TMPDIR with 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.

TestSealedRuntimeParentPredicate in test/test_sandbox_argv.py asserts on the classification itself. sealed: paths under run in either spelling of a symlinked data home, a .. climb back into the seal through a symlink, a case alias of run (modelled APFS-style, plus a real-filesystem variant that skips where the filesystem is case-sensitive), a symlink into run, a local link chain ending inside run, a missing leaf under run. None: paths outside the seal, an empty declaration, a case alias that folds onto a directory outside run, 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. //path on POSIX is judged on containment. On win32 (platform pinned), a declaration inside the would-be-sealed parent is not refused and realpath is never called; that test fails on the code without the gate with assert 'sealed' is None.

The existing test_spec_declared_temp_suppresses_probe_containment passes unchanged: a temp declared outside the seal is honored.

Manual verification

Isolated pod on this branch (seed connections-two). Three servers added through POST /api/mcp/custom and probed through POST /api/mcp/probe, each a python3 -c server reporting its own TMPDIR facts as tool names.

Before the fix: repro-declared-sealed-existing probes ok with write_EROFS; repro-declared-sealed-missing probes ok with write_ENOENT mkdir_EROFS. That ok is the silence in the issue title.

With the fix: both probe on <pod>/run/mcp-tmp/probe-*/tmp with write_ok. parentwrite_EROFS on 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_path into spawn_backend and gatewayd.
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_workspace in 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

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable) — docs/system-specs/modules/memory-skills-hooks.md, MCP Discovery, "Probe temp"
  • No secrets, credentials, or internal references in the diff

@pepmach
pepmach requested review from a team and chenmingwei23 September 6, 2026 19:46
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 6, 2026
@github-actions

github-actions Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5) — 🟡 CONCERNS

Design-level review of 4c9a54084689cf445bacfe3960ae8c06d7792c9a — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Reformatting artifacts confirmed — no test pins were deleted; the black-baseline.txt removal is the file becoming black-clean. I have what I need for the design review.

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

@github-actions

github-actions Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5) — ✅ PASS

Premise-level review of 4c9a54084689cf445bacfe3960ae8c06d7792c9a — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

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, classify_declared_temp_path has exactly one real consumer and no existing mechanism does its job (the existing identity walks are fd-based or os.stat-based and raise on not-yet-existing paths, which is the ordinary case here), and the large test_sandbox_argv.py reformat is mandated by the black-baseline gate plus AGENTS.md's format-what-you-touched rule. Final review follows.

First-Principles-Verdict: PASS

Verify the ~500-line test_sandbox_argv.py reformat is pure black output — it rode along undeclared and could hide a semantic edit in the churn.

Not justified as shipped

  • Item 5 — rides along: a second fix (declared lowercase temp keys previously never governed on POSIX); declared in the description, named harm, fine to ship.
  • Item 8 — rides along, undeclared: whole-file black reformat + baseline graduation of test_sandbox_argv.py; mandated by the black-gate ratchet (check_black_formatting.py requires pruning a graduated file), so derived — but it is ~500 lines the description never mentions.

What this change ships

Inventory (9 items) — 7 justified

Intent: make the MCP probe stop reporting green for a server whose spec-declared temp sits inside the sealed <data home>/run (#8747) — a FIX.

  1. Probe refuses a declared temp inside the sealed runtime parent and probes with the managed temp — justified
  2. One journal WARNING names each refused key, its path, and the actual cause — justified
  3. A failing seal check refuses every declared key (fail closed, check-failed cause) — justified
  4. Managed-allocation failure after a refusal strips all temp keys; child gets platform default — justified
  5. A declared temp spelled in any case now governs, re-emitted under the canonical uppercase key with ambient variants dropped — rides along
  6. New sandbox seam classify_declared_temp_path + DeclaredTempRefusal — justified (1 counted consumer, mcp_discovery.py:1864; runtime call sites tracked in Wire classify_declared_temp_path into spawn_backend and gatewayd so runtime refuses the same sealed temp the probe refuses #9843)
  7. On Windows the classification is inert and a declared temp is honored exactly as before — justified
  8. test_sandbox_argv.py graduated from the black baseline, whole file reformatted — rides along, undeclared
  9. Probe-temp paragraph added to memory-skills-hooks.md — justified

The probe-only scope leaves 2 counted siblings (spawn_backend, gatewayd _spawn) honoring sealed declarations; the description states the level and defers to linked #9843, so it is accepted-and-deferred, not a demand.

[FIRST-PRINCIPLES-REVIEWED] 4c9a540

@github-actions

github-actions Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Opus 4.8 Review — ✅ no blocking findings

Reviewed 4c9a54084689cf445bacfe3960ae8c06d7792c9a — this comment is updated in place on each push.

Review details

I've traced the substantive logic in both mcp_discovery.py and sandbox.py, verified probe_tmp initialization (line 1950), the control-flow branches (managed / honored / sealed-alloc-failed / check-failed), the strict=True→FileNotFoundError→lenient realpath ordering, the ELOOP→OSError→unclassifiable path, the identity walk termination, and confirmed _DECLARED_TEMP_REFUSAL_REASONS covers every cause the code can emit (no KeyError). The refusal-as-a-whole, canonical-key re-emission, and fail-closed paths are internally consistent and pinned by tests.

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 4c9a54084689cf445bacfe3960ae8c06d7792c9a.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 4c9a54084689cf445bacfe3960ae8c06d7792c9a: <one-sentence reason>

@github-actions

github-actions Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — ✅ no blocking findings

GPT 5.6 completed its review of 4c9a54084689cf445bacfe3960ae8c06d7792c9a and found no blocking issues.

This comment is updated in place on each push.

Review details

FINDING -- test/test_sandbox_argv.py:1080 -- The docstring lists remote-or-device, but the classifier cannot return it -> Fix: remove remote-or-device. (origin: validation)
[GPT-REVIEWED] 4c9a540

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 4c9a54084689cf445bacfe3960ae8c06d7792c9a: <one-sentence reason>

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 6, 2026
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 6, 2026

@cixuuz cixuuz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two security issues remain:

  1. path_within_sealed_runtime_parent() calls realpath()/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.

  2. In the managed-temp rewrite, key in _CANONICAL_TEMP_KEYS is case-sensitive even though spec env keys are explicitly treated case-insensitively on Windows. A declared tmpdir/Temp can survive the rewrite. Normalize every comparison with key.upper().

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are comments about security related, please handle it. Thanks

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Sep 7, 2026
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: passed Eligible automated validation passed for the current revision merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running labels Sep 8, 2026
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Sep 8, 2026
@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: no

The runtime half is filed as #9843, labelled deferred-finding and follow-up, assigned to me, with Due: 2026-09-24 in its body. It names both call sites that honour a sealed declaration without a seal check — spawn_backend in src/kiro_crew/mcp_gateway/backend.py (:4046, pruning at :4074-4082) and the _spawn closure in src/kiro_crew/mcp_gateway/gatewayd.py (:3402, :3433) — and the rules the wiring must copy from the probe: classify off-loop, refuse as a whole, fall back to the managed temp, fail closed on a check error, one WARNING naming key, path and cause.
This PR stays scoped to the probe, as #8747 is. The body's scope paragraph says a probe-green result does not model runtime for a sealed declaration until #9843 lands, and links it. Findings that ask for the runtime wiring inside this PR are covered by this ruling.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: no

The two siblings still honour a sealed declaration: spawn_backend in src/kiro_crew/mcp_gateway/backend.py (:4046, pruning at :4074-4082) and the _spawn closure in src/kiro_crew/mcp_gateway/gatewayd.py (:3402, :3433). #9843 carries the wiring as a task someone can pick up — classify off-loop, refuse as a whole, fall back to the managed temp, fail closed on a check error, one WARNING naming key, path and cause — with deferred-finding, an assignee and Due: 2026-09-24.
The PR stays scoped to the probe, which is what #8747 reports. The body states the runtime gap and links #9843, so probe-green is not presented as modelling runtime for a sealed declaration. Findings that ask for the runtime wiring inside this PR are covered by this ruling.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: no

The clearing condition is met. #9843 exists (deferred-finding, follow-up, assigned, Due: 2026-09-24) and names the two call sites, spawn_backend in src/kiro_crew/mcp_gateway/backend.py:4046 and the _spawn closure in src/kiro_crew/mcp_gateway/gatewayd.py:3402. The PR body at 9919192 references it twice: in the scope paragraph of "What changed" and on the Related Issues line under Fixes #8747.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes

  • fixed: item 8, path_within_sealed_runtime_parent is deleted — span=8c384e4bda13

Deleted at 9919192. grep -rn path_within_sealed_runtime_parent src/ returns nothing; classify_declared_temp_path (src/kiro_crew/sandbox.py) is the single entry point and its docstring no longer points at a bool face. The one consumer, _refused_declared_temp_keys in src/kiro_crew/mcp_discovery.py, was already calling the classification.
Rule I am applying to this PR: a helper with no caller outside test/ does not ship, however small. A convenience wrapper that only tests would use is deleted rather than kept for a hypothetical caller.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes

  • fixed: Subtraction taken, the bool wrapper is gone and the tests assert on the classification — span=a205d5c7ca3d

Dropped at 9919192. Every assertion in TestSealedRuntimeParentPredicate (test/test_sandbox_argv.py) reads classify_declared_temp_path directly and checks the actual cause: == "sealed" for containment, == "remote-or-device" for UNC, device, mapped-drive and link-chain refusals, == "unclassifiable" for a symlink cycle, is None where a declaration must stay honoured. That is stronger than the bool it replaces, because the probe names the cause in its WARNING and a wrong cause misdirects the operator even when the refusal is right.
The cycle case was covered twice once both tests asserted the same classification; the duplicate is folded into test_a_symlink_cycle_is_refused_instead_of_looping. Counts at this head with -n0: test/test_sandbox_argv.py 242 passed, 3 skipped; test/test_mcp_discovery.py 217 passed, 4 skipped.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: no

  • rebutted: item 10, the reformat and the baseline removal are what the black ratchet requires — span=665856a00d2f

Once test/test_sandbox_argv.py is black-clean, scripts/check_black_formatting.py fails until its line in .github/black-baseline.txt is removed in the same change, so the formatting-only reformat and the baseline removal travel together; the reformat is harm-free.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: no

The runtime half is filed as #9843, labelled deferred-finding and follow-up, assigned to me, with Due: 2026-09-24 in its body. It names both call sites that honour a sealed declaration without a seal check — spawn_backend in src/kiro_crew/mcp_gateway/backend.py (:4046, pruning at :4074-4082) and the _spawn closure in src/kiro_crew/mcp_gateway/gatewayd.py (:3402, :3433) — and the rules the wiring must copy from the probe: classify off-loop, refuse as a whole, fall back to the managed temp, fail closed on a check error, one WARNING naming key, path and cause. When it lands, probe env and runtime env agree for a sealed declaration.
This PR stays scoped to the probe, as #8747 is. The body's scope paragraph states the window and links #9843. Findings that ask for the runtime wiring inside this PR are covered by this ruling.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: no

  • needs-a-decision: should the refusal travel in the probe result, not only the journal — span=a0730e910d6f

The observation holds. At this head the refusal is one logger.warning in probe_server (src/kiro_crew/mcp_discovery.py); McpServerInfo has no warnings or notes field, so POST /api/mcp/probe answers plain ok and the dashboard shows nothing about the ignored declaration.
Carrying it means a new McpServerInfo field (key, path, cause), its to_dict serialisation, and a dashboard rendering of it next to the probe outcome. That widens this PR from the probe's env into the probe response schema and the frontend. The other way is a separate PR after this one, alongside #9843, so the runtime wiring and the operator-facing surface land together.
Decision for the maintainer (me, in this thread): widen this PR to carry the refusal in the probe result, or keep this PR to the probe env and take the surfacing as its own change. Nothing is filed for it until that ruling, because the outcome is the ruling.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: no

  • needs-a-decision: surfacing the refusal in the probe result is the same call as the Watch item — span=ec962246b198

Agreed on the effect: a visible "TMPDIR refused: sealed" in the probe outcome would tell the operator what runtime does before #9843 lands, and shrink the harm of the divergence window. It is the same change the Watch item asks for — a refusal field on McpServerInfo, its serialisation, and a dashboard rendering — so it gets the same ruling rather than a second one.
Decision for the maintainer (me, in this thread): carry the refusal in the probe result inside this PR, or land it as its own change beside #9843. This record is answered by that ruling; no separate issue is filed for a question.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes

  • fixed: items 3–5, the Windows resolution hazard is gone with the code that had it — span=5699aa2624c9

Fixed at 0950504. classify_declared_temp_path (src/kiro_crew/sandbox.py) returns None on win32 before resolving anything, so the check never realpaths untrusted config text on the one platform where that resolution is an access. The UNC/device spelling test, the NT-prefix classifier, the mapped-drive probe and the link-chain walker that made that resolution safe are deleted, not gated: with no seal on Windows there is nothing for them to protect. test_on_windows_a_declared_temp_is_honored_because_nothing_seals_run pins the gate and asserts realpath is never called on win32.
Ruling for this PR: the classification runs only where a backend seals run. Findings that ask for Windows path screening inside it are covered by this ruling, because there is no Windows configuration in which a probe child sees a sealed run.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes

  • fixed: item 3 Watch, the win32-gated helpers are deleted and the classification returns None on win32 — span=322965c4f066

Fixed at 0950504, on the first of the two clearing conditions: classify_declared_temp_path returns None on win32, and _remote_or_device_spelling, _nt_prefixed_target, _windows_drive_is_local, _remote_or_unmountable_drive_root, _remote_or_device_in_link_chain, the five regexes, _MAX_LINK_HOPS and the ntpath / windows_acl.volume_is_local imports are gone from src/kiro_crew/sandbox.py (274 lines), with their 16 tests (27 parametrised cases) removed from test/test_sandbox_argv.py. I could not name a Windows configuration in which a probe child sees a sealed run, because there is none: Kiro Crew has no native Windows sandbox backend, so the child is either not spawned or runs unsandboxed with a writable run.
POSIX keeps the kernel's loop detection in place of the walker: realpath(strict=True) raises for a symlink cycle and the path classifies as unclassifiable; FileNotFoundError is the ordinary not-yet-created case and resolves as far as the path goes.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes

  • fixed: Subtraction taken, the Windows half of classify_declared_temp_path is an early return None on win32 — span=bf4f440a6428

Taken at 0950504 exactly as written: classify_declared_temp_path (src/kiro_crew/sandbox.py) returns None on win32 before any resolution, and _remote_or_device_spelling, _nt_prefixed_target, _windows_drive_is_local, _remote_or_unmountable_drive_root, _remote_or_device_in_link_chain, _MAX_LINK_HOPS, the five regexes and the ntpath / windows_acl imports are deleted, along with the remote-or-device cause in DeclaredTempRefusal and in _DECLARED_TEMP_REFUSAL_REASONS (src/kiro_crew/mcp_discovery.py). Four Windows-only holes were found in that guard across two heads; it is gone rather than patched a fifth time. The spec paragraph ("Probe temp" in docs/system-specs/modules/memory-skills-hooks.md) states the gate and the reason.
Net for the PR: sandbox.py goes from +440 to +145 lines and the whole diff from +1586 to +1000; both local review lanes and the server GPT lane stamped the result clean.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes

  • fixed: the Windows device guard and the resolution it protected are deleted — span=fa7ab943e399

Fixed at 0950504 by removing the mechanism, not by adding a fifth check to it. classify_declared_temp_path (src/kiro_crew/sandbox.py) returns None on win32 before touching the filesystem, so no DOS device name, junction, UNC, mapped drive or root-relative link target is ever handed to readlink or realpath from the gateway on Windows; _remote_or_device_spelling (the sandbox.py:2086 site), _nt_prefixed_target, _remote_or_unmountable_drive_root and _remote_or_device_in_link_chain no longer exist. On Windows the declared temp is honored as declared, which is correct there: Kiro Crew has no native Windows sandbox backend, so run/ is never sealed for a probe child and the declaration names a writable directory.
Class ruling: Windows path-resolution hazards in this check are closed by the platform gate, not by screening, because the check does not run on Windows. test_on_windows_a_declared_temp_is_honored_because_nothing_seals_run asserts a declared temp inside the would-be-sealed parent is not refused and that realpath is never called. The lane stamped 0950504 with no blocking findings.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: no

Both clearing conditions are filed and owned. #9843 wires classify_declared_temp_path into spawn_backend (src/kiro_crew/mcp_gateway/backend.py:4046, pruning at :4074-4082) and the _spawn closure in src/kiro_crew/mcp_gateway/gatewayd.py (:3402, :3433), copying the probe's rules: classify off-loop, refuse as a whole, fall back to the managed temp, fail closed on a check error, one WARNING naming key, path and cause. #9855 carries the refusal (key, path, cause) in the McpServerInfo probe result and renders it on the dashboard row, so a green row stops reading as runtime parity in the meantime. Both carry deferred-finding, are assigned to me, and are due 2026-09-24.
This PR stays scoped to the probe, as #8747 is; the body's scope paragraph names the window and links both issues. Findings that ask for the runtime wiring or the probe-result surfacing inside this PR are covered by this ruling.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: no

Agreed on the order. #9843 is the cheap one for exactly the reason given: the classifier exists, the two spawn sites are named with line numbers in the issue, and the rules to copy are written down, so it is a small PR and it collapses the intermediate state. #9855 (the probe-result field, to_dict, dashboard rendering) is the lower-priority half and is filed as such. Both are deferred-finding, assigned to me, due 2026-09-24.
Not folded into this PR: landing the runtime wiring here would re-open a review loop on a PR that has converged, and the wiring touches the gateway spawn path, which deserves its own red-first tests and its own review.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: no

Decided: the surfacing is its own change, not a widening of this PR. #9855 ("Surface a declared-temp refusal in the MCP probe result and dashboard, not only the journal") names the work — a refusal field on McpServerInfo (key, path, cause), its to_dict serialisation through the probe cache and the /api/mcp/probe payloads, and a dashboard rendering beside the ok row — with tests for each; it carries deferred-finding, is assigned to me, and is due 2026-09-24. This record supersedes my earlier needs-a-decision on the same span.
This PR keeps the refusal at the journal (logger.warning in probe_server, src/kiro_crew/mcp_discovery.py) and says so in the body's scope paragraph. Findings that ask for the probe result or the dashboard to carry the refusal inside this PR are covered by this ruling.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

self-added: no

Decided: the visible "TMPDIR refused: sealed" on the probe row is #9855, filed with the field, the serialisation and the rendering spelled out, deferred-finding, assigned to me, due 2026-09-24. It is sequenced behind #9843, which closes the divergence window itself; #9855 then makes the two surfaces agree in what they show. This record supersedes my earlier needs-a-decision on the same span.
Not folded into this PR, which stays scoped to the probe's environment; the body's scope paragraph links both follow-ups.

@pepmach

pepmach commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

Both open decisions are settled at 0950504.

Decision 1 — follow-up filed, scope kept. The runtime wiring is #9843 (deferred-finding, assigned to me, due 2026-09-24). It covers the two call sites that honour a declared temp without a seal check — spawn_backend in src/kiro_crew/mcp_gateway/backend.py (the "OPERATOR-DECLARED temp wins" block at :4046, pruning at :4074-4082) and the _spawn closure in src/kiro_crew/mcp_gateway/gatewayd.py (spawn_env.update(declared) at :3402, declared_temp_keys= at :3433) — and spells out the rules the wiring copies from the probe: classify off-loop, refuse as a whole, fall back to the managed temp, fail closed on a check error, one WARNING naming key, path and cause. This PR stays scoped to the probe, as #8747 is; the body's scope paragraph and the Design/First Principles dispositions point at #9843. The Design ask to carry the refusal in the probe result rather than only the journal is #9855 (McpServerInfo has no warnings field; it needs the field, to_dict, and a dashboard rendering), same labels and due date.

Decision 2 — the First Principles Subtraction is taken. classify_declared_temp_path (src/kiro_crew/sandbox.py:1990) returns None on win32 before resolving anything (:2028), so on Windows a declared temp is honoured exactly as it was before this PR. The reason is the one the review gave and the module already states: Kiro Crew has no native Windows sandbox backend (src/kiro_crew/sandbox.py:5467), so nothing seals run/ there, a probe child is either not spawned or runs unsandboxed with a writable run/, and a declared temp under it is a writable directory the operator chose. The guard that made resolution safe on that platform is deleted rather than patched again: _remote_or_device_spelling, _nt_prefixed_target, _windows_drive_is_local, _remote_or_unmountable_drive_root, _remote_or_device_in_link_chain, the five regexes, _MAX_LINK_HOPS, and the ntpath / windows_acl.volume_is_local imports, about 275 lines of sandbox.py, plus 16 tests (27 parametrised cases) in test/test_sandbox_argv.py. Four Windows-only holes were found in that guard across two heads (the junction-swap TOCTOU, DOS device stems, root- and drive-relative link targets, the mapped-drive spelling before it) — each one a fresh patch on a mechanism whose subject platform has no seal to protect, which is the review's point. The remote-or-device cause is gone from DeclaredTempRefusal (:1987) and from _DECLARED_TEMP_REFUSAL_REASONS (src/kiro_crew/mcp_discovery.py:1840); only sealed and unclassifiable remain, and test_refusal_warning_names_the_actual_cause covers those two. POSIX keeps the kernel's loop detection instead of the hand-rolled walker: realpath(strict=True) (:2047) raises for a symlink cycle, which classifies as unclassifiable; FileNotFoundError is the ordinary not-created-yet case and falls back to resolving as far as the path goes. test_a_symlink_cycle_is_refused_instead_of_looping (test/test_sandbox_argv.py:1271) pins the cycle as the leaf and as an ancestor, and test_a_missing_leaf_is_resolved_as_far_as_it_goes (:1294) pins the fallback. The win32 gate is pinned by test_on_windows_a_declared_temp_is_honored_because_nothing_seals_run (:1323), which monkeypatches sys.platform to win32, asserts a declared temp inside the would-be-sealed parent is not refused, and asserts realpath is never called; against the previous head it fails with assert 'sealed' is None. The spec paragraph is docs/system-specs/modules/memory-skills-hooks.md:1624 ("Probe temp"), which states the gate and the reason. Counts at this head with -n0: test/test_sandbox_argv.py 217 passed, 3 skipped; test/test_mcp_discovery.py 216 passed, 4 skipped.

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
@cixuuz

cixuuz commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

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: TMPDIR, TMP, and TEMP are conventional environment overrides, not resources provisioned by the MCP protocol. With no valid declaration, KiroCrew should create a writable managed temp. A usable explicit declaration can be honored without KiroCrew creating the arbitrary path. A sandbox-incompatible declaration should be replaced consistently.

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.

@pepmach

pepmach commented Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

Agreed on the split: the probe swapping a sealed declaration for the managed temp while spawn_backend and gatewayd still hand the sealed one to the child is not a state that should ship on its own. Going with the atomically sequenced pair rather than growing this PR further: #10012 is stacked on this branch and merges right after it (its base retargets to main when this lands), and I will not merge this one without it.

What #10012 does. One shared rule, classify_declared_temp_env in src/kiro_crew/sandbox.py beside classify_declared_temp_path, used by all three callers -- the probe in mcp_discovery.py, spawn_backend in mcp_gateway/backend.py, and gatewayd's _spawn. A declaration with any refused key (sealed, unclassifiable; a classifier failure refuses every declared key under check-failed) is dropped as a whole and the managed allocation runs exactly as for an undeclared spec, with one WARNING naming key, path and cause -- secret://-backed values are never written to the log (<resolved secret>), paths are repr-escaped, and security_posture treats the formatter as a non-egress module. Red-first tests: test_one_refusal_drops_the_whole_declaration and test_check_failure_refuses_every_declared_key (test/test_backend_tmp.py); test_sealed_declaration_uses_managed_temp_and_warns, test_check_failure_uses_managed_temp_and_warns, test_unsealed_declaration_is_honored_verbatim, test_secret_backed_refusal_never_logs_the_resolved_path (test/test_mcp_gateway_backend_coverage.py); test_sealed_declared_temp_is_removed_before_spawn and test_secret_temp_skips_raw_precheck_and_reaches_spawn_backend (test/test_mcp_gatewayd_coverage.py).

The docs claim is narrowed there too, in the same spec section this PR touches (docs/system-specs/modules/memory-skills-hooks.md, "MCP temp"): this rule prevents the known sealed-parent conflict; it does not certify that every arbitrarily declared temp directory exists or is writable. If you would rather see that sentence in this PR before it merges, say so and I will move it here.

#9855 stays the bounded observability follow-up you describe.

@chenmingwei23 chenmingwei23 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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>/../tmp resolves symlink-first back into the seal and is caught. Collapsing .. lexically first (the classic bug) is explicitly avoided.
  • case / firmlink / normalization alias (<home>/RUN vs <home>/run on APFS): realpath does not fold case, so _identity_within_sealed_parent walks (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 as unclassifiable and refused; the lenient form (which returns a half-resolved spelling and never raises) is deliberately not trusted.
  • missing leaf (the ordinary case): FileNotFoundError falls back to lenient realpath, 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: /var and /tmp are symlinks, but the check only refuses paths whose real location is inside <data home>/run; a temp outside the seal (the normal operator choice) classifies None and is honored. test_spec_declared_temp_suppresses_probe_containment and test_path_outside_the_data_home_is_not_sealed cover 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.

@bolichen97

Copy link
Copy Markdown
Contributor

Maintainer note (@bolichen97, agent-assisted), head 4c9a54084. The refusal itself is sound and fails closed — classify_declared_temp_path runs realpath(strict=True) on the original spelling before normpath, so a link-into-run/.. spelling resolves symlink-first and the (st_dev, st_ino) ancestor walk catches case/firmlink aliases. We are holding the merge rather than approving, for one reason: #10012 is stacked on this branch (base fix/pod-repro-8747) and completes the wiring (classify_declared_temp_env in spawn_backend and gatewayd _spawn). Landing #9108 alone ships exactly the split state @cixuuz rejected and you agreed not to ship — the probe swaps to the managed temp and reports ok while the runtime spawn still honours the sealed path.

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 main, and we merge it in the same sitting. Until then Auto-Merge is off on both.

Smaller notes on this head: mcp_discovery.py renders the untrusted spec path with f"{key}={path}" unescaped — a \n in the value forges journal lines; use %r (or land #10012's format_declared_temp_refusals). The body does not mention the .github/black-baseline.txt entry drop and the ~130-line Black reformat of test_sandbox_argv.py. TestSealedRuntimeParentPredicate docstring still names the deleted remote-or-device cause; _path_identity's no-follow rationale is moot after realpath(strict=True).

@bolichen97

Copy link
Copy Markdown
Contributor

Follow-up from the #10012 review (maintainer, agent-assisted): one finding lands in this PR's helper rather than in #10012 — classify_declared_temp_path (sandbox.py) classifies a relative declared temp (TMPDIR=run/x) against the daemon's cwd via abspath/realpath, while the child resolves it against cwd=work_dir (backend.py:4149), so a relative spelling can pass the probe and still land in the seal, or be refused spuriously. Refuse non-absolute declarations as unclassifiable, or join with work_dir before classifying. Fixing it here means #10012 inherits the fix whichever way you fold the two.

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.

MCP probe: spec-declared TMPDIR inside the sealed runtime parent is silently unwritable

5 participants