R21.1: bind the browser_completion attempt handle to the private plane

The public /v1/oauth/:provider/start route read handoff.attemptId
straight out of the request body and folded it into the signed state
token unconditionally. Since the private command plane's beginAuth
mints an attempt handle server-side and later redeems by that handle
(consumeAuthCompletionByAttempt), anyone who learned a pending handle
could carry it through their own OAuth flow and bind their own
identity to it -- the app's completeAuth(H) would then authenticate
the victim's session as the attacker. Same family as the takeover
chain closed in the prior pass: a caller-chosen identifier trusted as
authority.

normalizeOAuthHandoff now always strips attemptId from a
browser_completion handoff; a public caller has no attempt to
correlate and gets its completion code directly. The only path an
attempt handle can reach the state token is startOAuthFlow's new
trustedBrowserAttemptId option, which is a TypeScript function
argument set by beginAuth from the handle it just minted -- never a
value parsed out of a request body.

Also adds the uniqueness constraint the by-attempt redemption needed:
a partial unique index on (app_slug, attempt_id) WHERE attempt_id IS
NOT NULL AND consumed_at IS NULL, mirrored in InMemoryStore. Without
it, consumeAuthCompletionByAttempt's UPDATE could match more than one
row and silently discard all but the one it returned.

Flips Soul's attempt-handle-binding it.fails to a passing it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-08 14:43:33 +02:00
commit ea804ddbbe
5 changed files with 74 additions and 25 deletions

View file

@ -247,14 +247,22 @@ function prefersHtml(acceptHeader: string | undefined): boolean {
return acceptHeader.includes("text/html") || acceptHeader.includes("application/xhtml+xml");
}
/**
* `browser_completion.attemptId` is never accepted from a request body. It
* exists only to correlate a completion with the attempt the private command
* plane's `beginAuth` created server-side (see `startOAuthFlow`'s
* `trustedBrowserAttemptId` parameter) — a handle a public caller could name
* here would let it complete someone else's pending attempt (R21.1). A public
* `browser_completion` caller has no attempt to correlate: it gets the
* completion code directly in the callback response or handoff page.
*/
function normalizeOAuthHandoff(value: OAuthStartRequest["handoff"]): OAuthHandoff {
if (!value) {
return { kind: "browser_completion" };
}
if (value.kind === "browser_completion") {
const attemptId = value.attemptId?.trim();
return attemptId ? { kind: "browser_completion", attemptId } : { kind: "browser_completion" };
return { kind: "browser_completion" };
}
if (
@ -507,7 +515,20 @@ export async function startOAuthFlow(
ctx: Pick<HeimdallRuntimeContext, "config" | "keys" | "store">,
provider: Provider,
caller: AppCaller | null,
input: OAuthStartRequest
input: OAuthStartRequest,
options?: {
/**
* The attempt handle to bind into the state token's `browser_completion`
* handoff. Set only by `beginAuth` (private-command-plane.ts), which
* mints the handle itself via `store.createAuthAttempt` before calling
* here — it is a TypeScript function argument, never a value parsed out
* of `input`/the request body, so a public HTTP caller has no way to
* populate it (R21.1). `normalizeOAuthHandoff` already strips any
* body-supplied `attemptId` for this reason; this is the only path by
* which one reaches the signed state token.
*/
trustedBrowserAttemptId?: string;
}
): Promise<HandlerResult> {
const { config, keys, store } = ctx;
const profile = await resolveAppProfile(store, input.appSlug);
@ -562,6 +583,9 @@ export async function startOAuthFlow(
},
};
}
if (options?.trustedBrowserAttemptId && handoff.kind === "browser_completion") {
handoff = { kind: "browser_completion", attemptId: options.trustedBrowserAttemptId };
}
if (entitlementPolicy && (!caller || caller.appSlug !== profile.slug)) {
return { statusCode: 401, body: { error: "app_auth_required" } };
}