Heimdall/src/store/schema.ts
Meta Vens ea804ddbbe 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>
2026-09-08 14:43:33 +02:00

174 lines
5.7 KiB
TypeScript

export const CREATE_SCHEMA_SQL = `
CREATE TABLE IF NOT EXISTS registered_apps (
slug TEXT PRIMARY KEY,
display_name TEXT NOT NULL,
profile_version TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
identity_providers JSONB NOT NULL,
entitlement_sources JSONB NOT NULL,
managed_connection_providers JSONB NOT NULL,
capabilities_json JSONB NOT NULL,
redirect_uris JSONB NOT NULL
);
-- Runtime app registration (POST /v1/apps) minted a client_secret_hash that
-- nothing ever verified. Deleted with the caller-identity cut; drop the
-- column on any store built before this schema too.
ALTER TABLE registered_apps DROP COLUMN IF EXISTS client_secret_hash;
CREATE TABLE IF NOT EXISTS accounts (
id TEXT PRIMARY KEY,
created_at TIMESTAMPTZ NOT NULL,
last_seen_at TIMESTAMPTZ NOT NULL,
display_name TEXT,
primary_email TEXT
);
CREATE TABLE IF NOT EXISTS linked_identities (
id TEXT PRIMARY KEY,
account_id TEXT NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
provider TEXT NOT NULL,
provider_user_id TEXT NOT NULL,
username TEXT,
display_name TEXT,
primary_email TEXT,
access_token_encrypted TEXT,
refresh_token_encrypted TEXT,
token_expires_at TIMESTAMPTZ,
scopes TEXT NOT NULL DEFAULT '',
profile_json JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
UNIQUE(provider, provider_user_id)
);
CREATE INDEX IF NOT EXISTS linked_identities_account_id_idx
ON linked_identities(account_id);
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
account_id TEXT NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
app_slug TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
last_seen_at TIMESTAMPTZ NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
claims_json JSONB NOT NULL,
access_revision INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS sessions_account_id_idx
ON sessions(account_id);
CREATE TABLE IF NOT EXISTS auth_attempts (
handle TEXT PRIMARY KEY,
app_slug TEXT NOT NULL,
provider TEXT NOT NULL,
mode TEXT NOT NULL,
return_to TEXT NOT NULL,
status TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
completed_at TIMESTAMPTZ,
consumed_at TIMESTAMPTZ,
denial_code TEXT
);
CREATE INDEX IF NOT EXISTS auth_attempts_lookup_idx
ON auth_attempts(app_slug, expires_at, status);
CREATE TABLE IF NOT EXISTS private_command_receipts (
app_slug TEXT NOT NULL,
idempotency_key TEXT NOT NULL,
request_fingerprint TEXT NOT NULL,
status TEXT NOT NULL,
content_schema TEXT NOT NULL,
envelope_base64 TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY (app_slug, idempotency_key)
);
CREATE INDEX IF NOT EXISTS private_command_receipts_expiry_idx
ON private_command_receipts(expires_at);
CREATE TABLE IF NOT EXISTS auth_completions (
code TEXT PRIMARY KEY,
app_slug TEXT NOT NULL,
provider TEXT NOT NULL,
mode TEXT NOT NULL,
account_id TEXT NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
session_id TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
return_to TEXT NOT NULL,
payload_json JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
consumed_at TIMESTAMPTZ
);
CREATE INDEX IF NOT EXISTS auth_completions_lookup_idx
ON auth_completions(app_slug, expires_at, consumed_at);
-- Correlation handle for the browser_completion handoff. Before the
-- caller-identity cut, the completion code itself was the caller-chosen
-- attemptId (an unauthenticated account-takeover primitive); the code is now
-- always minted by the store, and attempt_id lets the private command plane
-- redeem the completion it created without ever needing the code.
ALTER TABLE auth_completions ADD COLUMN IF NOT EXISTS attempt_id TEXT;
-- One attempt handle may bind at most one unconsumed completion (R21.1): a
-- second completion for the same handle would let the by-attempt UPDATE in
-- consumeAuthCompletionByAttempt match more than one row, silently consuming
-- and discarding all but the one it returns. The predicate is partial rather
-- than a plain UNIQUE(app_slug, attempt_id) because a NULL attempt_id (no
-- correlation handle) and a *consumed*, already-settled completion must not
-- collide with a later one.
DROP INDEX IF EXISTS auth_completions_attempt_idx;
CREATE UNIQUE INDEX IF NOT EXISTS auth_completions_attempt_unconsumed_unique_idx
ON auth_completions(app_slug, attempt_id)
WHERE attempt_id IS NOT NULL AND consumed_at IS NULL;
CREATE TABLE IF NOT EXISTS capability_grants (
id TEXT PRIMARY KEY,
account_id TEXT NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
scope_type TEXT NOT NULL,
scope_id TEXT,
capability TEXT NOT NULL,
source TEXT NOT NULL,
status TEXT NOT NULL,
expires_at TIMESTAMPTZ,
note TEXT,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX IF NOT EXISTS capability_grants_lookup_idx
ON capability_grants(account_id, scope_type, scope_id, status);
CREATE TABLE IF NOT EXISTS entitlement_snapshots (
id TEXT PRIMARY KEY,
account_id TEXT NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
provider TEXT NOT NULL,
scope TEXT NOT NULL,
evaluated_at TIMESTAMPTZ NOT NULL,
is_allowed BOOLEAN NOT NULL,
reason_code TEXT NOT NULL,
reason_detail TEXT,
raw_summary_json JSONB NOT NULL,
UNIQUE(account_id, provider, scope)
);
CREATE TABLE IF NOT EXISTS audit_events (
id TEXT PRIMARY KEY,
account_id TEXT,
session_id TEXT,
app_slug TEXT,
event_type TEXT NOT NULL,
event_payload_json JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX IF NOT EXISTS audit_events_lookup_idx
ON audit_events(account_id, session_id, app_slug, created_at);
`;