Skip to content

P5: session identity isolation — per-request subject, no ambient credential #239

Closed

Description

@cwiklik

Split out of #220. This is the session identity isolation slice: per-request subject inflow, removal of the ambient credential fallbacks, and a startup scrub.

Source of truth is the P5 spec and ADR-0032, not #220's severity table. #220's table was written from reading the code rather than tracing it and is wrong on four of five rows; the corrected tracing is in the spec's §2.1.

Why this is the prerequisite, not the density win

#220 framed the blocker as five process-global mutables. Only one is real, and it is not a concurrency bug: ANTHROPIC_API_KEY is seeded write-once-if-absent at harness/src/run-turn.ts:310-312, so session A's token sticks process-wide and sessions B..N authenticate as A. That is an identity leak, not mutual corruption — and the actual missing prerequisite is that no per-session credential enters the harness at all.

Three ordered steps (spec §3.2 — the order is forced by §2.3)

  1. buildConfig() → buildConfig(req) in packages/knative-server/src/server.ts:66-73 (call sites :111, :174, :411, :415).
    • Subject arrives on a dedicated X-SH-Subject header — not Authorization (reserved for caller auth; ADR-0011 lock-down implies it is coming) and not the request body (the body is logged and persisted to Redis).
    • Placeholder derived by a pure, non-secret mapping from mounted config.
    • Missing subject → 401 with no upstream request made.
  2. Delete the ambient reads in harness/src/run-turn.ts::applyModelGateway: the process.env.ANTHROPIC_API_KEY seed (:310-312) and the || process.env.ANTHROPIC_AUTH_TOKEN fallback (:306). Signature unchanged — only what it trusts. Safe only after step 1.
  3. Startup scrub at the server entrypoint only: set ANTHROPIC_API_KEY to the §3.3 sentinel (e.g. sh-unused-see-authorization-header), and delete ANTHROPIC_OAUTH_TOKEN and ANTHROPIC_AUTH_TOKEN.
    • Not in run-turn, not in leaf-job — both paths share applyModelGateway, and scrubbing there would break leaf mode.
    • The sentinel is required, not cosmetic: pi resolves the request key by provider name, so with ANTHROPIC_API_KEY simply absent, _getRequiredRequestAuth throws No API key found for "anthropic" and gateway mode breaks outright. Step 2 must not land without step 3.

Not in scope

  • Any pi-fork change. Pi's credential path is already per-session (AgentSession._getRequiredRequestAuth). Threading a SessionContext through pi was considered and rejected — four refactors that would add fork divergence and fix nothing.
  • harness/src/cli.ts:9-14 and packages/knative-server/src/leaf-job.ts:15-18 stay ambient on purpose.
  • Caller authentication — that is ADR-0011's lock-down work, not new scope here. X-SH-Subject says whose work this is, not who may ask.
  • Per-tenant sandbox or data isolation.

Placeholder isolation is in scope at the same strictness as key isolation (spec §3.4)

Under lock-down the harness holds no secret, so a leaked placeholder looks cosmetic. It is not. A placeholder is an identity assertion. If session B inherits tenant A's placeholder, the injector faithfully swaps in A's real credential — so A's budget is spent on B's work, and the injector's audit trail records the request as legitimately A's. The one mechanism that would otherwise catch the error instead certifies it.

The four inert globals get reachability guards

stdoutTakeoverState, sessionResourceCleanups, fileMutationQueues, commandResultCache (spec §4). #220 called these blockers; they are inert in server mode. Each guard exists to make that stay true, not to fix anything.

cwd is the one item that can change the verdict. It is process-wide, unlisted by #220, and expected inert under the FS-free two-tier model (P1) — but not proven. A failure here upgrades scope rather than being papered over.

Tests

Use spec §5's six rows as written. Three of them look over-specified until you know why, so the rationale is recorded here:

  • Precedence. The Sentinel identity row asserts ANTHROPIC_OAUTH_TOKEN and ANTHROPIC_AUTH_TOKEN are absent, not just that ANTHROPIC_API_KEY holds the sentinel. pi-fork packages/ai/src/env-api-keys.ts:96-99 returns ["ANTHROPIC_OAUTH_TOKEN", "ANTHROPIC_API_KEY"] — OAuth outranks the API key — so pinning one variable leaves the assertion green while a real credential wins.
  • The leaf survives the deletions, for two independent reasons (spec §3.2 step 2). This is the dual-path check most likely to be re-derived from scratch: applyModelGateway is shared with runLeaf, and deploy/knative/leaf-scaledjob.yaml:65-72 mounts ANTHROPIC_API_KEY from llm-credentials independently.
  • Interleaving must occur at await boundaries, not sequentially. A sequential two-session test passes while the bug remains, which is worse than no test.
  • The ambient-absence test must set the env var deliberately and assert failure anyway.

Known gap, out of scope: deploy/knative/service.yaml:45-49 and :56-60 still mount the real api-key and auth-token from llm-credentials. Spec §3.6 scopes the lock-down row to in-process precisely because closing this "cannot be done by editing service.yaml" — it needs an injector in the harness's egress path (ADR-0011 · Z2). Tracked separately.

Interactions the MU1 control-plane spec records against this work

#238 (multi-user control plane, MU1) is a separate track that composes with this one. Its §3.6 names three things a P5 implementation should know, so they do not get discovered late:

  1. The Bearer payload is one-or-the-other. P5's target is an inert placeholder rewritten by an injector; MU1's interim mode puts the real control-plane-resolved token in the same field. A P5 implementation that unconditionally sets the placeholder would silently overwrite MU1's token. Hence TurnConfig should carry a tagged credential ({mode: 'placeholder'|'direct', value}), not a bare string.
  2. Direct mode diverges from §5's in-process invariant. Scope the lock-down assertion to the environment (which MU1 never writes) rather than the whole process, or gate it on direct mode being disabled — otherwise the assertion contradicts an accepted ADR-0033 cost.
  3. Inbound X-SH-Subject becomes conditional. Do not pin "inbound X-SH-Subject is always honoured": on a token-bearing path the subject is token.sub and an inbound header is ignored. Operator and leaf paths keep the header behaviour.

Naming to confirm before implementation

The X-SH-Subject header and the placeholder-config env SH_SUBJECT_PLACEHOLDERS. Both were pinned by the spec as proposals awaiting confirmation.

Mechanics (spec §8)

  • Worktree order: git submodule update --init --recursive, then cd pi-fork && npm ci && npm run build, then root pnpm install.
  • Tests need the sh-test-redis container on :6379. ~10 ECONNREFUSED across 4 files means it is stopped, not a regression.
  • make lint skips untracked files — stage new files first.
  • git commit -s (DCO is a required check).

Relationships

Activity

  1. added
    enhancement
    on Sep 8, 2026
  2. pdettori commented on Sep 17, 2026

    @pdettori
    Member

    Epic: rossoctl/rossoctl#2546 (Stream 1 — multi-tenancy/session isolation)

    Status update (2026-09-17)

    Still unimplemented. MU1 landed in #249 and is explicitly
    additive — its PR description states P5's env fallbacks and the write-once
    ANTHROPIC_API_KEY seed are untouched, "removing them is P5's to do." So this issue's
    three ordered steps (spec §3.2) have not started.

    This is now the single blocker on:

    Assisted-By: Claude (Anthropic AI) [email protected]

  3. pdettori commented on Oct 15, 2026

    @pdettori
    Member

    Status check, 2026-10-14. The 2026-09-17 note above ("still unimplemented") looks out of date.

    What I found on main:

    • Step 1 is in place: buildConfig(auth?: TurnAuth | null) in packages/knative-server/src/server.ts:82, called with the per-request auth on the turn paths.
    • The write-once ANTHROPIC_API_KEY seed is now a constant sentinel (AMBIENT_KEY_SENTINEL, harness/src/run-turn.ts:611), so session A's token can no longer stick process-wide.
    • MI1 (docs/specs/2026-09-28-moca-multi-user-isolation-design.md) amends P5 §3.2, and D8 refuses direct mode (real inference keys in the worker) under MOCA_TENANCY=multi.

    I have not traced steps 2 and 3 (removal of every ambient fallback, and the startup scrub) end to end. The MI1 spec still lists the P5 scrub and sentinel among the "designed only" items, so that list may predate the code.

    Proposal: someone confirms steps 2 and 3 against MI1 (R2 and the scrub), and if they hold, close this as completed by #350. Anything left over becomes a small issue of its own. The claim boundary in #241 stays as it is: multi-tenant safety is not claimable until cortex#905 lands.

  4. pdettori commented on Oct 15, 2026

    @pdettori
    Member

    Keeping this open until the remaining steps are verified. Checklist for whoever picks it up (details in my status comment above):

    • Step 2: every ambient credential fallback is gone under MOCA_TENANCY=multi, not only the ANTHROPIC_API_KEY seed (harness/src/run-turn.ts; MI1 R2 and D8).
    • Step 3: the startup scrub exists and a test plants an ambient key and asserts a turn cannot use it.
    • The "designed only" line about the P5 scrub in docs/specs/2026-09-28-moca-multi-user-isolation-design.md is either true or updated.

    If all three hold, close as completed by #350. Anything that does not hold becomes its own issue. The claim boundary in #241 is unchanged: multi-tenant safety is not claimable until cortex#905 lands.

  5. pdettori commented on Nov 16, 2026

    @pdettori
    Member

    Closing as completed by #350 (MI1 S1).

    • Step 1: an authenticated turn builds its config from its own identity (buildConfig(auth), packages/server/src/server.ts) and never receives the ambient ANTHROPIC_AUTH_TOKEN. turn-auth-route.test.ts plants that token and asserts it is absent.
    • Step 3: under MOCA_TENANCY=multi, scrubAmbientCredentials (packages/server/src/server-process.ts) deletes every provider-credential env var at boot, for both the server and the P6 worker, and sets the sentinel. server-process.test.ts covers this.
    • Step 2: the env fallback in harness/src/run-turn.ts is still in the code. It can't be reached under multi because the scrub deletes the variable, and under single it is deliberate (ADR-0033 direct mode).

    Loose end: line 151 of docs/specs/2026-09-28-moca-multi-user-isolation-design.md still lists "P5's scrub and sentinel" as "Designed only". That will be fixed in the pre-v0.11.0 docs pass.

  6. added this to the v0.11.0 milestone on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions