fix(kas): keep persisted MCP sign-ins per authenticated user

Closed
KY
14d ago
+1653-120

Problem / Motivation

Goal: A remote MCP sign-in that Kiro Crew keeps across kas sessions is only ever offered back to the same authenticated user, never to another user's session, subagent or cron.

With agent.kas_persist_mcp_sign_in on, the grant vault at <data_home>/kas/mcp-grants/ was gateway-wide and keyed only by the MCP server's config digest. On a gateway with more than one dashboard user (a Telegram, Teams or Webex user given a dashboard link), a second user's session reused the first user's lennysdata, Notion or Figma sign-in. @bolichen97 raised this on #23143 (review).

Why it matters

Token bytes never leave the vault, but authorization crossed user boundaries: user B could act in a third-party service as user A. The setting cannot become a default, or be recommended on any shared gateway, until grants are partitioned by a trusted user identity.

Not a goal

  • Turning agent.kas_persist_mcp_sign_in on by default. It stays off.
  • Recording which user owns a session durably, or adding per-session access control. Who may type into a session is unchanged.
  • Persisting sign-ins for channel, cron, app, crew-member, subagent-process or background sessions. They keep sign-ins in the engine's memory.
  • Migrating grants from the old gateway-wide vault.

What changed (motivation → approach → change)

Security-relevant change. This decides which user's OAuth tokens an engine process may read.

The engine's _kiro/secret/* requests name no session and no user, and one KAS process can host several sessions (a parent and its subagents). So the vault is chosen per process, from the one user the gateway can prove drove the session that process was spawned for.

PieceWhat it does
mcp_grant_principals.py (outside acp/, so the dashboard can reach it without crossing the agent-SDK boundary)In-memory map from session key to principal. A second, different principal conflicts the key for the rest of the gateway process; the binding never moves to the latest caller. Bounded, and a conflict is never evicted while a plain binding can go instead.
dashboard/mcp_grant_binding.pyBinds from the signed token subject before the session's engine can spawn: POST /api/chat, POST /api/chat/slots, every non-read call on a per-slot route through the existing per-slot checkpoint (slot_ownership.py; continue, regenerate, edit-resend, reload, agent switch and the rest), and the owner's resume prefetch on tab focus (moved with its function to ws_focus.py, which keeps ws.py under the line cap). The owner (any owner subject, including local-app on a single-user local gateway) is owner; any other subject is user:<subject>. An app, crew member or cron caller, or a channel-linked, app or agent-created slot, conflicts the key instead.
session_parts/links.pyLinking a session to a Slack thread or a two-way channel mirror conflicts its key; the binding re-reads both persisted links, so a restart cannot clear that.
kas_secret_storage.pyMcpGrantStore requires a principal and lives at kas/mcp-grants/principals/<sha256 of principal>/. SecretStorageServer re-checks its binding before and after the key lock, after each vault read and right before the answer is written. Once the binding is lost (a conflict or an eviction) the process falls back to its own memory for good, even if the key is re-bound.
runtime_spawn_launch.py, runtime_class.py, acp_runtime_start.pyThe provider passes its own session key as mcp_grant_key; the handshake declares secretStorage only when that key has exactly one principal. runtime_host_requests.py re-checks the answer with release just before writing it. Shared, pooled, background and companion runtimes pass none.

Existing gateway-wide grants are ignored, not migrated. The old kas/mcp-grants/.vault/ names no user, so nothing reads it and each user signs in again. It is left on disk.

Known limit: a session two users both type into shares its live MCP connections, persisted or not. The second user conflicts the key, so the vault stops being used, but tokens the engine already loaded stay in that process's memory.

The setting stays off by default, and the loader already reads only a real true as on (_safe_bool), with an unreadable file giving the field default. #23143's extra fail-closed parser was needed only for its on default, so it is not carried; tests pin that a non-boolean value and an unreadable config read as off.

Specs (acp-client.md, config.md), the user config doc (docs/configuration.md) and the setting's help text (config/sections_agent.py, regenerated into config-baseline.json) describe the per-user behaviour.

Backwards compatibility

Compatible: the setting is still off by default. With it on, sign-ins stored by the earlier build are no longer read, so those users sign in once more.

Removes nothing: the old vault directory stays on disk untouched; no key, route or public function is removed.

Tests

  • test_kas_mcp_secret_storage.py: end to end through a KAS-shaped stub, A stores a grant, B's process reads nothing and must authorize itself, and A's later process reads A's grant. Unbound keys (empty, a Slack key, a cron key) declare no storage. A second user on A's session stops vault use. A conflicted key spawns without storage. The old gateway-wide vault is never served. Eviction cannot clear a conflict. Non-boolean values and an unreadable config read as off.
  • test_mcp_grant_binding.py: the owner and non-owner subject mapping; app, member and cron callers; channel, app and agent-created slots, Slack and two-way mirror links conflicting the key; a second user on any mutating per-slot route conflicting it while reads bind nothing; the owner focus prefetch binding the owner.
  • test_runtime_bindings.py: the AcpRuntime.__init__ signature pin for mcp_grant_key.

Proof: not provable -- other: prove.py reported BASELINE_RED because the local interpreter is Python 3.13, where test_runtime_bindings' type-hint pin fails on main too (pathlib._local repr); CI runs 3.12

Local run: the four changed test files plus the config pipeline, eager-spawn, chat-send, slot-create, project-relaunch and facade suites pass (1,261 tests), except that 3.13-only type-hint pin.

Adversarial check: a fresh subagent given only the diff tried to make B receive A's grant across binding races, eviction, restart, rekeyed and pooled runtimes, app, member and cron callers, symlinks and the old vault. It found no exploit. Its eviction and non-person-caller findings are fixed in this diff.

Manual verification

N/A: no live multi-user gateway. The stub engine tests drive the real handshake and reader loop.

Related Issues

Closes #24643

Replaces #23143.

Sibling sweep

Swept: git grep -nE "_run_chat\(|sessions\.get_or_create\(" -- src/kiro_crew/dashboard
Sibling: src/kiro_crew/dashboard/openai_compat.py -- changed here: runs a turn on a caller-named slot's own key, now binds first
Sibling: src/kiro_crew/dashboard/chat_threads.py -- not affected -- thread replies run on their own thread_session_key, which nothing binds, so they persist nothing
Sibling: src/kiro_crew/dashboard/handlers/side.py -- not affected -- side turns run on their own side key, never bound
Sibling: src/kiro_crew/dashboard/handlers/hooks.py -- not affected -- hook turns run on hook: keys, never bound

Pattern harvest

Rule candidate: review-prompt
Pattern: a store answering requests that name no user must be keyed by a principal bound at spawn, never shared gateway-wide.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • 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