Repository navigation
fix(mcp): refuse sealed temp declarations at runtime - #10012
Conversation
GPT 5.6 Review — ✅ no blocking findingsGPT 5.6 completed its review of This comment is updated in place on each push. Review detailsFINDING -- src/kiro_crew/mcp_discovery.py:2041 -- False positive or not applicable? A repository writer can comment: |
Design Review (Fable 5) — ✅ PASSDesign-level review of Design-Verdict: PASS One shared classification rule owned by Suggestions
[DESIGN-REVIEWED] 31eb9c0 |
First Principles Review (Fable 5) — 🟡 CONCERNSPremise-level review of All verification done. The key facts: First-Principles-Verdict: CONCERNS The gatewayd literal pre-check re-decides what Not justified as shipped
What this change shipsInventory (10 items) — 8 justifiedIntent: make the running MCP backend obey the same sealed-temp refusal the probe already applies, so a green probe stops certifying a server that dies on its first temp write — a FIX (#9843, gap documented in the base spec).
Watch
Subtractions
[FIRST-PRINCIPLES-REVIEWED] 31eb9c0 |
Opus 4.8 Review — ✅ no blocking findingsReviewed Review detailsNo findings. The sole candidate (case-duplicate temp keys colliding in [OPUS-REVIEWED] 31eb9c0 Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
chenmingwei23
left a comment
There was a problem hiding this comment.
[code-review-sage] LGTM - no blocking findings. The change fixes a real probe/runtime divergence and does it at the right layer.
Assessment. A green MCP probe could disagree with the running server: the probe classified a spec-declared TMPDIR/TMP/TEMP against the sandbox seal, but the runtime spawn paths honoured a declared temp with no such check, so a server could pass discovery and then fail its first write into a sealed-parent temp (#9843). This PR consolidates one rule, sandbox.classify_declared_temp_env, and runs it off the event loop at all three spawn boundaries (probe_server, gatewayd._spawn, spawn_backend). Root-cause fix, not a symptom patch.
What I verified
- Trust boundary / secret temp. A
secret://temp is NOT classified as a raw path ingatewayd._spawn(would misread URI text as a local path and echo a hostile secret name into the log). It stays provisionally declared, gets resolved in memory byresolve_secret_uris, andspawn_backendre-runsclassify_declared_temp_envon the resolved value withdeclared_temp_keys=accepted_temp_keys. A sealed resolved path is refused at the final boundary and masked as<resolved secret>viaformat_declared_temp_refusals(hidden_keys=secret_env_keys).test_secret_temp_skips_raw_precheck_and_reaches_spawn_backendandtest_secret_backed_refusal_never_logs_the_resolved_pathlock both halves; the resolved path never reaches the warning. - Whole-declaration refusal. One refused key drops the entire temp declaration (
refusedgivesaccepted_temp_keys = ()), and all canonical temp keys are stripped from the spawn env. Correct:tempfileconsultsTMPDIRbeforeTMP, so keeping a surviving sibling would make writability depend on spelling order. Verified for the mixed secret+checkable case too: a refused checkable key drops the secret key with it. - Fail-closed. A classifier exception refuses every declared key under
check-failedand names the exception in the warning (test_check_failure_uses_managed_temp_and_warns). If managed-temp allocation then fails, no refused temp value survives in the env; the child falls to its platform default rather than the refused path. - Log-forging. Refused paths are
repr-escaped after the caller's redactor runs, so an embedded newline cannot forge a second log record.test_sealed_declared_temp_is_removed_before_spawnasserts the forged-newline text does not appear in the emitted warning. - Windows.
classify_declared_temp_pathreturnsNoneonwin32without resolving (no native sandbox backend sealsrun/, andrealpath/statwould open untrusted config text). Behaviour unchanged there. - No dangling refs / imports. The removed
mcp_discoveryprivates (_CANONICAL_TEMP_KEYS,_refused_declared_temp_keys,_declared_temp_refusal_reasons) have no remaining callers.SECRET_URI_PREFIX,CANONICAL_TEMP_KEYS,redact, and the three sandbox helpers are all imported where used.SECRET_URI_PREFIXis exported with a backward-compat alias for existing call sites. - PoolKey. The PoolKey still hashes the caller's original effective env, so a refusal can conservatively split two equivalent managed-temp backends but can never collapse two distinct declarations into one pool: the safe direction.
Design notes (non-blocking)
- The bulk of
backend.py's diff (295 del / 361 add) is Black reformatting from removing it from.github/black-baseline.txt, plus the same for the two test files. The description accounts for this ("touched legacy Python surfaces are Black-clean, so three formatter-baseline entries are removed"). Description-to-diff fidelity holds in both directions. security_posture.pyadds a whole-filesandbox.pyentry to the redaction-sink allowlist. Consistent with the existing entries (a pure scrubber with no output of its own; the probe/gateway loggers are the sinks), though a file-level match is broad.
Tests cover the classification matrix, canonical-key acceptance, whole-declaration refusal, fail-closed, managed fallback, off-loop execution, and both secret boundaries: not tautological. CI is green on the head SHA (d4513b6), including the GPT / Opus / Design / First-Principles / UX review lanes.
The human owns the merge verdict.
bolichen97
left a comment
There was a problem hiding this comment.
Maintainer review (agent-assisted, on behalf of @bolichen97), head d4513b6ad. The code is correct and is the complementary second half of #9108, not a duplicate: classify_declared_temp_env (sandbox.py:2080-2135, fail-closed check-failed, one refused key drops the whole declaration) applied at gatewayd _spawn (gatewayd.py:3418-3452, secret:// temps deferred) and at the spawn_backend chokepoint on the final resolved env. Not approvable on this base:
- Blocking (process) — base is
fix/pod-repro-8747, so no CI ran on this code.ci.yml,fast-gate.ymlandbuild.ymltrigger only onpull_request: branches: [main]; of the 13 workflow runs ond4513b6anone is the test suite, Black/isort/flake8/mypy or the build, andpr-readiness.yml:530-540marks them "Not eligible" on a stacked base yet still publishedreadiness: passed. The body's "2,138 passed / 65 passed / gates passed" is therefore unverified by CI. Two ways out, as said on #9108: fold this into #9108 (merge this branch intofix/pod-repro-8747and let #9108's CI run on the combined diff, reviewed as one PR), or wait for #9108 to land and retarget this PR tomain. Either way this code merges nowhere until amain-targeted run is green on it. gatewayd.py:3436-3443,backend.py:4111-4120— the log-site census ratchet was bypassed. Two new gate-side WARNING lines read the baselineredactthroughredactor=redactinsideformat_declared_temp_refusals(sandbox.py:2171);test_security_posture.py::_wraps_baseline_callcounts only directredact(...)calls, so_BASELINE_LOG_SITE_CENSUSstaysmcp_gateway/backend.py: 2with nogatewayd.pyentry and the ratchet never fired. Add the entries (or route the two sites through a direct call) so the census reflects the code.sandbox.py:2080(inherited from #9108'sclassify_declared_temp_path) — a relative declared temp (TMPDIR=run/x) is classified against the daemon's cwd, but the child resolves it againstcwd=work_dir(backend.py:4149), so a relative spelling can pass the check and still land in the seal, or be refused spuriously. Refuse non-absolute declarations asunclassifiable, or join withwork_dirbefore classifying. Fix it once in #9108's helper.sandbox.py:2127,2138-2149— thecheck-failedfailuretext (f"{type(exc).__name__}: {exc}") is appended unredacted and notrepr-escaped, unlike the path fields; a classifier exception that echoes the declared value can carry control characters or a credential into the log line.- Tests: every runtime refusal test stubs
classify_declared_temp_path; no test drives a real<home>/run/...path throughspawn_backend. Add one with the real classifier. - Scope: roughly 1,600 of the 4,300 changed lines are a Black reformat of
backend.pyand two test files; it triples the review surface of a security fix — ship it separately. Nit: after a refusal has stripped every temp key,backend.py:4132-4135still logs "spawning with inherited temp" though the child gets the platform default.
Auto-Merge was never armed here and stays off.
|
Update (maintainer, agent-assisted): #9108 was approved and merged by another maintainer at 06:59Z ( |
d4513b6 to
a5c18b3
Compare
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
Share declared temp classification across probe and runtime spawn paths. Replace refused or unclassifiable declarations with the managed temp so probe and runtime cannot disagree.
a5c18b3 to
31eb9c0
Compare
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
self-added: no
|
|
@bolichen97 I kept this PR as the runtime half after #9108 merged. The helper notes that missed that merge are carried here with the runtime fixes, so the final one-commit diff is based directly on On relative declarations: On probe log forging: the probe routes refused key/path fields through On the stale cause list: On On the log-site census: the scanner recognizes a baseline redactor passed as a On On the real classifier path: On the fallback message and the remaining-value bug: a gateway refusal strips canonical temp keys from both the declared overlay and The whole-file Black reformats are pulled out. |
Addressed at 31eb9c0: rebuilt as one commit on main with real CI, whole-file reformats removed, relative temp declarations refused, check-failed text redacted, census counts redactor callbacks, real-classifier test, gatewayd strips canonical keys from spawn_env too. See the reply comment.
cixuuz
left a comment
There was a problem hiding this comment.
Reviewed at 31eb9c0. The runtime now applies the shared declared-temp decision at literal and final resolved boundaries, refuses relative declarations, strips the full canonical declaration on refusal, fails closed on classifier errors, masks secret-backed values, keeps warning fields redacted and single-line, records the new log sites, and exercises the real sealed classifier path. The earlier relative-path, census, exception-text, test, and formatter-scope concerns are resolved; required checks are green.
Share declared temp classification across probe and runtime spawn paths. Replace refused or unclassifiable declarations with the managed temp so probe and runtime cannot disagree.
Problem / Motivation
A green MCP probe can disagree with the running server. The probe replaces a temp path under the sealed runtime directory, but runtime spawn can pass that unwritable path to the child.
Why it matters
The server can pass discovery and fail on its first temp write. The operator then debugs a server that the probe said was healthy.
What changed (motivation → approach → change)
Probe and runtime use one declared-temp rule.
classify_declared_temp_envcanonicalizesTMPDIR/TMP/TEMP, refuses the whole declaration when one key is sealed or unclassifiable, and fails closed undercheck-failedwhen classification raises.gatewaydchecks literal declarations before forwarding them.spawn_backendchecks the final child environment before launch and selects the managed temp for every refusal. A gateway refusal removes both declared and inherited canonical temp keys, so a failed managed allocation falls to the platform default instead of the refused value. A relative declaration isunclassifiablebecause the daemon cwd and the child'swork_dirdiffer. Asecret://temp skips the raw path check, resolves in memory, and reaches the final check with its secret identity attached.Warning fields use one formatter. It hides resolved secrets, redacts ordinary paths and classifier errors, and
repr-escapes control characters into one log line. The security-posture census counts both runtime warning sites. The three legacy Black-baselined files contain only functional hunks, and their baseline entries remain.🟩 added · 🟨 changed · 🟥 removed · 🟦 unchanged
The probe and running backend make the same temp decision before the child starts.
Tests
TestDeclaredTempClassificationlocks canonical acceptance, whole-declaration refusal, redacted failure text, and fail-closed check errors.test_relative_declaration_is_unclassifiablelocks the daemon-cwd versus child-work_dirdecision.test_real_classifier_refuses_a_sealed_declarationdrives a real<home>/run/...path throughspawn_backend.test_sealed_declaration_uses_managed_temp_and_warns,test_check_failure_uses_managed_temp_and_warns, andtest_refusal_allocation_failure_uses_the_platform_defaultlock managed fallback and its diagnostics.test_refusal_warning_escapes_newlines_in_the_declared_pathandtest_sealed_declared_temp_is_removed_before_spawnlock one-line warning fields at the probe and gateway boundaries.test_secret_backed_refusal_never_logs_the_resolved_pathandtest_secret_temp_skips_raw_precheck_and_reaches_spawn_backendlock both secret-resolution boundaries.TestGateSideLogRedactorSpellinglocks callback-style baseline-redactor detection and both runtime census entries.Manual verification
Screenshots / video
Not applicable: backend-only change with no UI surface.
Related Issues
Fixes #9843
Refs #9108
Pattern harvest
Rule candidate: review-prompt
Pattern: when a probe rewrites a declared resource before reporting health, every runtime spawn boundary must consume the same decision rule.
Checklist
feat|fix|docs|refactor|perf|test|chore|ci|build|revert: ...)