Skip to content

fix(mcp): fail closed when protected session probes raise - #12442

Merged
bolichen97 merged 1 commit into
kirodotdev:mainfrom
pstarkgit:fix/seatbelt-session-key-shadowing
Sep 28, 2026
Merged

bolichen97 merged 1 commit into
kirodotdev:mainfrom
pstarkgit:fix/seatbelt-session-key-shadowing

Conversation

@pstarkgit

@pstarkgit pstarkgit commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Keep sandboxed MCP children bootable when persistent logging is unavailable without weakening protected session identity fences.

A sandboxed kirocrew mcp-core child can be denied access to gateway.log, which currently aborts CLI logging setup before the MCP handshake. The original report also showed a protected member binding probe raising PermissionError, but treating that exception as absence would let a same-uid process make its own binding unreadable and fall through to an ambient session key.

Why it matters

Sandboxed MCP children need to start with console logging when the host denies the persistent log path. At the same time, protected member sessions must not inherit the ambient session identity, tool exclusions, or spawn authority by inducing a binding-read failure.

Not a goal

  • Make core code distinguish an expected Seatbelt denial from a process-induced unreadable binding.
  • Change the meaning of None as no protected identity or "" as an explicit refusal.
  • Claim that this core-only change fixes the original session-key symptom by itself. That symptom is fixed only when the host kit recognizes its expected sandbox denial and returns None for the unreadable binding.

What changed (motivation → approach → change)

  • Foreground CLI logging now catches OSError while opening gateway.log, warns through the already-installed console handler, preserves long-lived-command redaction, and continues without a rotating file handler.
  • Detached gateways still propagate the error because they have no console destination and must not start without persistent logging.
  • CallerContext.from_env() now maps every exception from protected_member_session_for_pid() to the existing blank protected identity refusal instead of falling through to token, environment, or pid-file identity.
  • The protected-member probe contract now tells host overrides to return None only when they can classify a missing or expected unreadable binding, and to reserve "" for an explicit invalid or revoked record.
  • Tool-policy documentation keeps a raising protected probe in the resolution_failed class.

Backwards compatibility

Compatible: successful protected probes, explicit blank refusals, normal persistent logging, and detached gateway behavior retain their existing contracts. Host kits that intentionally classify an expected sandbox denial can return None through the documented seam.

Tests

  • test_unwritable_gateway_log_soft_fails proves a console-backed MCP child can continue when the log file open is denied.
  • test_unwritable_gateway_log_still_redacts_for_long_lived proves the soft-fail path still installs log redaction.
  • test_unwritable_gateway_log_fails_loudly proves a detached gateway does not start without persistent logging.
  • test_a_raising_protected_probe_refuses_and_never_reaches_the_env_key covers PermissionError, OSError, and unexpected exceptions and proves none can reach the ambient key.
  • test_an_eperm_probe_is_resolution_failed_not_env_fallthrough proves tool-policy resolution refuses an unreadable protected binding without consulting ambient-session policy.
  • Focused local suites: 67 passed in test/test_cli_logging.py and test/test_session_token_consumer_identity.py.

Manual verification

N/A for the fail-closed core behavior because the focused tests exercise both identity consumers and both logging topologies. End-to-end recovery of the original Seatbelt symptom remains a host-kit verification and requires that kit to return None for its expected unreadable binding.

Screenshots / video

N/A, no user-visible frontend surface changes.

Related Issues

Related to #12380. This PR does not change the Pi MCP bridge wiring covered there.

Pattern harvest

Rule candidate: agents-md
Pattern: an exception from a protected identity probe is refusal, not absence; only the authority that can classify an expected sandbox denial may return the absence sentinel.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|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)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

N/A, repository placeholder retained.

@pstarkgit
pstarkgit requested review from a team and pepmach September 21, 2026 04:02
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention labels Sep 21, 2026
@dwu96

dwu96 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Problem / Motivation
  • ## Why it matters
  • ## What changed
  • ## Tests

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

7 similar comments
@dwu96

dwu96 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Problem / Motivation
  • ## Why it matters
  • ## What changed
  • ## Tests

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@dwu96

dwu96 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Problem / Motivation
  • ## Why it matters
  • ## What changed
  • ## Tests

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@dwu96

dwu96 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Problem / Motivation
  • ## Why it matters
  • ## What changed
  • ## Tests

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@dwu96

dwu96 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Problem / Motivation
  • ## Why it matters
  • ## What changed
  • ## Tests

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@dwu96

dwu96 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Problem / Motivation
  • ## Why it matters
  • ## What changed
  • ## Tests

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@dwu96

dwu96 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Problem / Motivation
  • ## Why it matters
  • ## What changed
  • ## Tests

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@dwu96

dwu96 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Problem / Motivation
  • ## Why it matters
  • ## What changed
  • ## Tests

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5, fork) — 🟡 CONCERNS

Design-level review of dac4ee1581799f18e39ea2d61adb8b6283bc27ca via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: CONCERNS

New probe contract tells host overrides to return None for any unreadable binding, dropping the "only if classifiable as expected" qualifier the fence depends on.

Watch

  • The docstring added to protected_member_session_for_pid says overrides "must return None when a binding is missing or unreadable (for example a Seatbelt deny)". A host kit implemented to that wording maps every EPERM — including one the fenced process induced by chmoding its own binding — to None, so from_env() falls through to KIROCREW_SESSION_KEY and the process re-identifies as the ambient session: exactly the attack the PR's own comments and tests defend against. The description's version ("return None only when they can classify a missing or expected unreadable binding") is the correct contract, but that "only" never made it into the diff.
    Clears when: the docstring is reworded to require the override to positively classify the denial as expected (e.g. its own sandbox profile) before returning None, otherwise raise or return "".
  • The title and "now maps every exception" claim describe from_env() fail-closed behavior that is byte-identical in base (src/kiro_crew/mcp_caller.py:290-291 already read except Exception: protected = ""); this PR only adds a warning, docs, and pinning tests. A future reader auditing this security fence will misattribute when the behavior landed.
    Clears when: the description/commit body states the fail-closed mapping pre-existed and this PR pins and documents it; the only behavioral fix is the foreground logging soft-fail.

[DESIGN-REVIEWED] dac4ee1

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5, fork) — 🟡 CONCERNS

Premise-level review of dac4ee1581799f18e39ea2d61adb8b6283bc27ca via the fork AI-review pipeline — 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.

First-Principles-Verdict: CONCERNS

The identity "fix" ships no behavior change — base already fails closed at mcp_caller.py:290; only the logging soft-fail is new behavior.

Not justified as shipped

  • Item 4 — symptom-level framing risk: "What changed" claims from_env() "now maps every exception … instead of falling through to token, environment, or pid-file identity", but the base line the diff touches already reads except Exception: protected = "" — no fallthrough existed, and the diff adds only a warning and comments. The title's "fail closed when protected session probes raise" describes behavior the base commit already had; the diff's identity half is tests and docs pinning it, which is fine work mislabeled as a fix. A reader (or changelog) would record a security hole closed that was never open.
    Clears when: the title/description present the identity half as pinning existing fail-closed behavior, or evidence (a base commit or issue) shows the fallthrough ever existed.

What this change ships

Inventory (7 items) — 6 justified

Intent: FIX — keep sandboxed MCP children bootable when gateway.log is denied, without weakening the protected-identity fence.

  1. Foreground commands boot with console-only logging when gateway.log can't open — justified
  2. That soft-fail path still installs Bearer/JWT redaction for long-lived commands — justified
  3. Detached gateway still refuses to start without persistent logging (newly test-pinned) — justified
  4. A warning now logs when the protected-identity probe raises — symptom-level: described as a behavior change the base already had; see above
  5. Probe contract documented: None for classified absence, "" for revoked — justified
  6. Tests pin fail-closed identity in both consumers (from_env, tool policy) — justified
  7. test_cli_logging.py black-formatted and removed from baseline — undeclared, rides along (mandated by the ratchet gate once formatted)

[FIRST-PRINCIPLES-REVIEWED] dac4ee1

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5 Review (fork) — ✅ no blocking findings

Reviewed dac4ee1581799f18e39ea2d61adb8b6283bc27ca via the fork AI-review pipeline; updated in place on each push.

Review details

Nothing blocks; one advisory — the soft-fail's own notice is suppressed by the logger level this function set 40 lines earlier.

FINDING — src/kiro_crew/cli.py:1128 — with agent.log_level: "ERROR" (a first-class enum value, config/sections.py:1677) and no -v (exactly how mcp-core is spawned — agent._kirocrew_mcp_invocation adds no verbose flag), line 1086 has already run logging.getLogger("kiro_crew").setLevel(logging.ERROR), so logging.getLogger("kiro_crew").warning( is dropped by Logger.isEnabledFor before any handler is consulted (the repo pins this exact suppression in test/test_cli_logging.py:311); the process then boots with no persistent log and emits nothing at all, falsifying the new comment's justification that "the warning lands somewhere a human reads" — and for a foreground gateway the boot rotation at cli.py:1098-1104 has already moved the old log to .prev, so kirocrew logs reports "No gateway logs found" or tails stale content → Fix: bypass the logger for this notice, e.g. print(f"persistent log file {log_file} unavailable ({exc}); continuing with console logging only", file=sys.stderr).

[OPUS-REVIEWED] dac4ee1

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review (fork) — ✅ no blocking findings

Reviewed dac4ee1581799f18e39ea2d61adb8b6283bc27ca via the fork AI-review pipeline; updated in place on each push.

Review details

FINDING -- src/kiro_crew/member_memory_auth.py:153 -- "or unreadable" contradicts the fail-closed caller contract, directing induced unreadable bindings to ambient identity fallthrough -> Fix: limit None to missing bindings or classified expected sandbox denials.
[GPT-REVIEWED] dac4ee1

@pstarkgit
pstarkgit force-pushed the fix/seatbelt-session-key-shadowing branch from ef4cef8 to 0b1af92 Compare September 23, 2026 06:06
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Sep 23, 2026
@pstarkgit
pstarkgit force-pushed the fix/seatbelt-session-key-shadowing branch from 0b1af92 to 9ad9cc3 Compare September 23, 2026 06:43
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Sep 23, 2026
@pstarkgit
pstarkgit force-pushed the fix/seatbelt-session-key-shadowing branch from 9ad9cc3 to ec9d22a Compare September 23, 2026 16:53
@github-actions github-actions Bot removed merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: action required A blocking check or review needs attention labels Sep 23, 2026
@pstarkgit
pstarkgit force-pushed the fix/seatbelt-session-key-shadowing branch from 620dc98 to 3edf79b Compare September 23, 2026 23:39
@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 23, 2026
@pstarkgit
pstarkgit force-pushed the fix/seatbelt-session-key-shadowing branch from 3edf79b to bc8b9c1 Compare September 24, 2026 03:04
@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 24, 2026
@pstarkgit
pstarkgit force-pushed the fix/seatbelt-session-key-shadowing branch from bc8b9c1 to df8db0a Compare September 24, 2026 03:35
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 24, 2026
…nding fence

Under Seatbelt a sandboxed MCP child (Pi `spawn_run` -> `kirocrew mcp-core`)
could not start: opening the `gateway.log` rotating file handler raised OSError
and aborted CLI logging setup before the MCP handshake.

Soft-fail that open for CONSOLE-backed setups only. `basicConfig` has already
installed a console handler there, so the warning lands somewhere a human reads
and the child still boots. A DETACHED process keeps failing loudly: it
deliberately skips `basicConfig` (a console echo would double-write into the log
stderr already points at), so swallowing the error would boot a long-lived
gateway with no persistent log and no destination for the warning saying so.
Bearer/JWT console redaction is installed before the early return, which the
normal call site would otherwise skip.

The member-binding probe keeps its fail-closed classification. An earlier
revision of this change caught `PermissionError` from
`protected_member_session_for_pid` in `mcp_caller.py` / `mcp_shared.py` and
treated it as absence so token/env identity could win. That reopens the
fallthrough the binding exists to fence: `KIROCREW_SESSION_KEY`, the signed
token and the pid file are all writable by the same uid, and `chmod` needs only
ownership, so a fenced process could make its OWN binding record unreadable and
come back as the ambient session with that session's tool-policy exclusions and
`spawn_run` identity. A core resolver cannot tell that induced EPERM from a
sandbox deny; a host override can. So the distinction is documented as a
contract on `protected_member_session_for_pid` instead: overrides return `None`
for a binding that is missing OR unreadable, and `""` only for an explicitly
invalid/revoked record.

Tests pin both halves: a raising probe refuses across PermissionError, OSError
and RuntimeError rather than reaching the env key; an EPERM probe is
`resolution_failed` and asks the gateway for no policy; a detached unopenable
`gateway.log` raises while a foreground one does not. Drop
test/test_cli_logging.py from the black baseline now that it is clean.
@pstarkgit pstarkgit changed the title fix(mcp): don't let Seatbelt probe failures shadow session keys fix(mcp): fail closed when protected session probes raise Sep 28, 2026
@pstarkgit
pstarkgit force-pushed the fix/seatbelt-session-key-shadowing branch from df8db0a to dac4ee1 Compare September 28, 2026 18:18
@pstarkgit
pstarkgit requested a review from a team as a code owner September 28, 2026 18:18
@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 merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 28, 2026

@bolichen97 bolichen97 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.

Reviewed on behalf of the maintainers (sweep 2026-09-28): readiness passed, required checks green on dac4ee1, no blocking review threads, security posture unchanged. Approving and merging.

@bolichen97
bolichen97 merged commit f64e5ee into kirodotdev:main Sep 28, 2026
100 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants