Consent review fixes: ask-only cookie, fixed-expiry grants links, wording carried to the receipt, one revocation per grant

The ask sets its own cookie and no launch binding; Continue sets the binding
and carries the exact text shown into it; the receipt stores that text and its
hash. Grants-page form tokens keep the link's expiry. Revocations are unique
per grant and revoke reports only what it wrote. access_denied's attempt
denial code is pinned for Bifrost and Repixelizer.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Heimdall 2026-11-08 13:30:39 +00:00
commit 71ce9c16e4
11 changed files with 413 additions and 71 deletions

View file

@ -1,4 +1,5 @@
import { createHmac, randomUUID } from "node:crypto";
import { deflateRawSync, inflateRawSync } from "node:zlib";
import cookie from "@fastify/cookie";
import cors from "@fastify/cors";
import Fastify, { type FastifyError, type FastifyInstance, type FastifyReply, type FastifyRequest } from "fastify";
@ -26,7 +27,7 @@ import {
import { issueAccessClaim, type IssueAccessClaimInput } from "./claims.js";
import { builtInAppProfiles, getAppProfile, isAllowedReturnOrigin, serializeAppProfile, admitsProviderForMode, callbackMint } from "./app-profiles.js";
import { renderBrowserHandoffPage } from "./browser-handoff.js";
import { consentDigest, grantViews, GRANTS_PATH, renderConsentPage, renderGrantsPage, type ConsentAsk } from "./consent.js";
import { consentText, digestOfText, grantViews, GRANTS_PATH, renderConsentPage, renderGrantsPage, type ConsentAsk } from "./consent.js";
import { type HeimdallConfig, loadConfig } from "./config.js";
import { createTokenCustody, type TokenCustody } from "./custody.js";
import { grantFact } from "./facts.js";
@ -252,14 +253,74 @@ function oauthBindingCookieName(jti: string): string {
function signOAuthBinding(
config: HeimdallConfig,
keys: ReturnType<typeof createRuntimeKeyMaterial>,
state: OAuthStatePayload
state: OAuthStatePayload,
shownText?: string
): string {
return signJwt(
{ typ: oauthBindingType, iss: config.issuer, jti: state.jti, iat: nowEpochSeconds(), exp: state.exp },
{
typ: oauthBindingType,
iss: config.issuer,
jti: state.jti,
iat: nowEpochSeconds(),
exp: state.exp,
// The wording the person agreed to, carried from the ask to the receipt.
...(shownText === undefined ? {} : { ct: deflateRawSync(shownText).toString("base64url") }),
},
keys
);
}
const consentAskType = "heimdall_consent_ask";
function consentAskCookieName(jti: string): string {
return `__Host-heimdall-ask-${jti}`;
}
/**
* The ask's own cookie. It proves a browser was shown the ask and carries the
* exact text shown; only Continue and No accept it. It is not the launch
* binding: that is set when Continue claims the launch, so that merely opening
* the launch URL never makes a browser the state's owner.
*/
function signConsentAsk(config: HeimdallConfig, keys: ReturnType<typeof createRuntimeKeyMaterial>, state: OAuthStatePayload, shownText: string): string {
return signJwt(
{ typ: consentAskType, iss: config.issuer, jti: state.jti, iat: nowEpochSeconds(), exp: state.exp, ct: deflateRawSync(shownText).toString("base64url") },
keys
);
}
/** The text a held cookie of the given type carries, or null when the cookie is absent, foreign, expired or carries none. */
function cookieConsentText(
config: HeimdallConfig,
keys: ReturnType<typeof createRuntimeKeyMaterial>,
token: string | undefined,
type: string,
state: OAuthStatePayload
): string | null {
if (!token) return null;
const verification = verifyJwt(token, keys.publicKey);
if (!verification.valid) return null;
const { typ, iss, jti, exp, ct } = verification.payload;
if (typ !== type || iss !== config.issuer || jti !== state.jti || typeof exp !== "number" || exp <= nowEpochSeconds() || typeof ct !== "string") {
return null;
}
try {
return inflateRawSync(Buffer.from(ct, "base64url")).toString("utf8");
} catch {
return null;
}
}
function setConsentAskCookie(reply: FastifyReply, state: OAuthStatePayload, value: string, maxAgeSeconds: number): void {
reply.setCookie(consentAskCookieName(state.jti), value, {
path: "/",
secure: true,
httpOnly: true,
sameSite: "lax",
maxAge: Math.max(0, maxAgeSeconds),
});
}
function oauthBindingHolds(
config: HeimdallConfig,
keys: ReturnType<typeof createRuntimeKeyMaterial>,
@ -827,18 +888,27 @@ const grantsPageTokenType = "heimdall_grants_page";
const grantsPageTokenTtlSeconds = 600;
/** A short, purpose-bound proof of a signed-in account, so the grants page can be opened and its forms posted by a browser that holds no session. */
function signGrantsPageToken(config: HeimdallConfig, keys: ReturnType<typeof createRuntimeKeyMaterial>, accountId: string): string {
const iat = nowEpochSeconds();
return signJwt({ typ: grantsPageTokenType, iss: config.issuer, account_id: accountId, iat, exp: iat + grantsPageTokenTtlSeconds }, keys);
function signGrantsPageToken(
config: HeimdallConfig,
keys: ReturnType<typeof createRuntimeKeyMaterial>,
accountId: string,
exp: number = nowEpochSeconds() + grantsPageTokenTtlSeconds
): string {
return signJwt({ typ: grantsPageTokenType, iss: config.issuer, account_id: accountId, iat: nowEpochSeconds(), exp }, keys);
}
function grantsPageTokenAccount(config: HeimdallConfig, keys: ReturnType<typeof createRuntimeKeyMaterial>, token: unknown): string | null {
/** The account and expiry a page token proves. A page's own forms are signed to this same expiry, so a link is never renewed by being used. */
function grantsPageTokenAccount(
config: HeimdallConfig,
keys: ReturnType<typeof createRuntimeKeyMaterial>,
token: unknown
): { accountId: string; exp: number } | null {
if (typeof token !== "string") return null;
const verification = verifyJwt(token, keys.publicKey);
if (!verification.valid) return null;
const { typ, iss, account_id: accountId, exp } = verification.payload;
return typ === grantsPageTokenType && iss === config.issuer && typeof accountId === "string" && typeof exp === "number" && exp > nowEpochSeconds()
? accountId
? { accountId, exp }
: null;
}
@ -1239,7 +1309,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
};
/** Claim the launch use, bind the browser, and send it to the provider. */
const beginProviderRedirect = async (request: FastifyRequest, reply: FastifyReply, state: OAuthStatePayload, rawState: string) => {
const beginProviderRedirect = async (request: FastifyRequest, reply: FastifyReply, state: OAuthStatePayload, rawState: string, shownText?: string) => {
const clientId = config.providers[state.provider].clientId;
if (!clientId) {
return providerNotConfigured(reply, state);
@ -1256,7 +1326,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
}
const oidc = providerCatalog[state.provider].oidc ? deriveOidcSecrets(state.jti, keys.privateKey) : undefined;
setOAuthBindingCookie(reply, state, signOAuthBinding(config, keys, state), state.exp - nowEpochSeconds());
setOAuthBindingCookie(reply, state, signOAuthBinding(config, keys, state, shownText), state.exp - nowEpochSeconds());
reply.header("Cache-Control", "no-store");
reply.header("Referrer-Policy", "no-referrer");
return reply.redirect(
@ -1310,10 +1380,12 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
return providerNotConfigured(reply, state);
}
// Heimdall is about to hold a credential for the app: ask first. Viewing
// the ask claims nothing; only Continue spends the launch use. The
// binding cookie rides on the page so that the form's answer can only
// come from the browser that was shown it.
setOAuthBindingCookie(reply, state, signOAuthBinding(config, keys, state), state.exp - nowEpochSeconds());
// the ask claims nothing and binds nothing; only Continue spends the
// launch use and sets the launch binding. The ask's own cookie rides on
// the page so that the form's answer can only come from the browser
// that was shown it, and carries the text it was shown.
const ask = consentAskFor(state);
setConsentAskCookie(reply, state, signConsentAsk(config, keys, state, consentText(ask)), state.exp - nowEpochSeconds());
reply.header("Cache-Control", "no-store");
reply.header("Referrer-Policy", "no-referrer");
reply.header("X-Frame-Options", "DENY");
@ -1322,7 +1394,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
`default-src 'none'; style-src 'unsafe-inline'; frame-ancestors 'none'; base-uri 'none'; form-action 'self' ${new URL(providerCatalog[state.provider].authorizationEndpoint).origin}`
);
reply.type("text/html; charset=utf-8");
return reply.send(renderConsentPage(consentAskFor(state), request.query.state));
return reply.send(renderConsentPage(ask, request.query.state));
}
return beginProviderRedirect(request, reply, state, request.query.state);
}
@ -1365,11 +1437,13 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
return { error: "consent_not_required" };
}
// Either answer must come from the browser the ask was shown to.
if (!oauthBindingHolds(config, keys, request.cookies[oauthBindingCookieName(state.jti)], state)) {
const shownText = cookieConsentText(config, keys, request.cookies[consentAskCookieName(state.jti)], consentAskType, state);
if (shownText === null) {
return refuseVerifiedState(request, reply, state, "state_not_bound");
}
setConsentAskCookie(reply, state, "", 0);
if (request.body.decision === "continue") {
return beginProviderRedirect(request, reply, state, request.body.state);
return beginProviderRedirect(request, reply, state, request.body.state, shownText);
}
// A decline spends both uses of the state: it can neither launch nor complete afterwards.
const usedAt = new Date().toISOString();
@ -1710,7 +1784,13 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
let consentReceiptId: string | undefined;
if (mint === "custody_only") {
const ask = consentAskFor(statePayload);
const receipt = await store.createConsentReceipt({
// The text the person was shown rode in the launch binding. A custody
// flow that did not pass through the ask has none and completes nothing.
const shownText = cookieConsentText(config, keys, request.cookies[oauthBindingCookieName(statePayload.jti)], oauthBindingType, statePayload);
if (shownText === null) {
throw new Error("A managed connection completed without the consent text the person was shown.");
}
const { receipt } = await store.createConsentReceipt({
kind: "grant",
accountId: account.id,
appSlug: statePayload.app_slug,
@ -1718,7 +1798,8 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
createdAt: nowIso,
scopesAsked: [...ask.scopes],
scopesGranted: tokenSet.scope,
consentPageDigest: consentDigest(ask),
consentPageDigest: digestOfText(shownText),
consentText: shownText,
});
consentReceiptId = receipt.id;
}
@ -2504,9 +2585,12 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
}
);
const grantsAccount = async (request: FastifyRequest, pageToken?: unknown): Promise<string | null> =>
(await resolveAccessAccount({ config, keys, store }, request.headers.authorization)) ??
grantsPageTokenAccount(config, keys, pageToken);
/** The signed-in account, and the expiry the page's own forms get: a bearer starts a fresh window, a page link only passes on the one it has. */
const grantsAccount = async (request: FastifyRequest, pageToken?: unknown): Promise<{ accountId: string; exp: number } | null> => {
const bearerAccount = await resolveAccessAccount({ config, keys, store }, request.headers.authorization);
if (bearerAccount) return { accountId: bearerAccount, exp: nowEpochSeconds() + grantsPageTokenTtlSeconds };
return grantsPageTokenAccount(config, keys, pageToken);
};
const grantsReply = (reply: FastifyReply) => {
reply.header("Cache-Control", "no-store");
@ -2526,13 +2610,14 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
GRANTS_PATH,
{ schema: { querystring: { type: "object", additionalProperties: false, properties: { t: { type: "string" } } } } },
async (request, reply) => {
const accountId = await grantsAccount(request, request.query.t);
if (!accountId) return signInRequired(reply);
const caller = await grantsAccount(request, request.query.t);
if (!caller) return signInRequired(reply);
const { accountId } = caller;
grantsReply(reply);
const receipts = await store.listConsentReceipts(accountId);
if (prefersHtml(request.headers.accept)) {
reply.type("text/html; charset=utf-8");
return reply.send(renderGrantsPage(grantViews(receipts), signGrantsPageToken(config, keys, accountId)));
return reply.send(renderGrantsPage(grantViews(receipts), signGrantsPageToken(config, keys, accountId, caller.exp)));
}
return { grants: grantViews(receipts), receipts };
}
@ -2559,8 +2644,9 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
},
},
async (request, reply) => {
const accountId = await grantsAccount(request, request.body?.t);
if (!accountId) return signInRequired(reply);
const caller = await grantsAccount(request, request.body?.t);
if (!caller) return signInRequired(reply);
const { accountId } = caller;
grantsReply(reply);
const receipts = await store.listConsentReceipts(accountId);
const grant = grantViews(receipts).find((view) => view.id === request.params.grantId);
@ -2580,7 +2666,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
await store.updateLinkedIdentityCredential(identity.id, async () => ({ kind: "revoke", updatedAt: at }));
}
for (const live of grantViews(receipts).filter((view) => view.provider === grant.provider && view.status === "active")) {
await store.createConsentReceipt({
const { created } = await store.createConsentReceipt({
kind: "revocation",
accountId,
appSlug: live.appSlug,
@ -2588,19 +2674,22 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
createdAt: at,
revokesReceiptId: live.id,
});
revoked.push(live.id);
// A concurrent revoke may have written it first; only the writer reports it.
if (created) revoked.push(live.id);
}
if (revoked.length) {
await store.createAuditEvent({
accountId,
eventType: "consent_revoked",
eventPayloadJson: { provider: grant.provider, grantIds: revoked },
createdAt: at,
});
}
await store.createAuditEvent({
accountId,
eventType: "consent_revoked",
eventPayloadJson: { provider: grant.provider, grantIds: revoked },
createdAt: at,
});
}
if (prefersHtml(request.headers.accept) || request.headers["content-type"]?.startsWith("application/x-www-form-urlencoded")) {
reply.type("text/html; charset=utf-8");
const views = grantViews(await store.listConsentReceipts(accountId));
return reply.send(renderGrantsPage(views, signGrantsPageToken(config, keys, accountId), revoked.length ? "Taken back." : "Already taken back."));
return reply.send(renderGrantsPage(views, signGrantsPageToken(config, keys, accountId, caller.exp), revoked.length ? "Taken back." : "Already taken back."));
}
return { revoked, grants: grantViews(await store.listConsentReceipts(accountId)) };
}

View file

@ -43,8 +43,27 @@ export function consentLines(ask: ConsentAsk): string[] {
];
}
/** The labels on the two buttons. They are wording the person agrees to, so they are part of the text. */
export function consentButtons(ask: ConsentAsk): { go: string; no: string } {
return { go: `Continue to ${providerCatalog[ask.provider].displayName}`, no: "No, return to the app" };
}
/**
* Everything the person reads before answering, as one text: the lines and the
* button labels. It is built once, when the ask is rendered, and that exact
* text travels with the flow to the receipt; nothing rebuilds it later.
*/
export function consentText(ask: ConsentAsk): string {
const { go, no } = consentButtons(ask);
return [...consentLines(ask), go, no].join("\n");
}
export function digestOfText(text: string): string {
return createHash("sha256").update(text, "utf8").digest("hex");
}
export function consentDigest(ask: ConsentAsk): string {
return createHash("sha256").update(consentLines(ask).join("\n"), "utf8").digest("hex");
return digestOfText(consentText(ask));
}
export function escapeHtml(value: string): string {
@ -98,13 +117,14 @@ ${rest.map((line) => ` <li>${escapeHtml(line)}</li>`).join("\n")}
</ul>
<form method="post" action="/v1/oauth/${ask.provider}/consent">
<input type="hidden" name="state" value="${escapeHtml(state)}">
${form("continue", `Continue to ${provider}`)}
${form("decline", "No, return to the app")}
${form("continue", consentButtons(ask).go)}
${form("decline", consentButtons(ask).no)}
</form>`
);
}
export interface GrantView {
consentText: string;
id: string;
appSlug: AppSlug;
provider: Provider;
@ -135,6 +155,7 @@ export function grantViews(receipts: readonly StoredConsentReceipt[]): GrantView
scopesGranted: receipt.scopesGranted,
grantedAt: receipt.createdAt,
consentPageDigest: receipt.consentPageDigest,
consentText: receipt.consentText,
status: revocation ? "revoked" : "active",
...(revocation ? { revokedAt: revocation.createdAt, revocationReceiptId: revocation.id } : {}),
});

View file

@ -486,10 +486,14 @@ export class InMemoryStore implements HeimdallStore {
this.auditEvents.set(event.id ?? randomUUID(), event);
}
async createConsentReceipt(input: CreateConsentReceiptInput): Promise<StoredConsentReceipt> {
async createConsentReceipt(input: CreateConsentReceiptInput): Promise<{ receipt: StoredConsentReceipt; created: boolean }> {
if (input.kind === "revocation") {
const existing = this.consentReceipts.find((r) => r.kind === "revocation" && r.revokesReceiptId === input.revokesReceiptId);
if (existing) return { receipt: clone(existing), created: false };
}
const receipt = { id: randomUUID(), ...clone(input) } as StoredConsentReceipt;
this.consentReceipts.push(clone(receipt));
return clone(receipt);
return { receipt: clone(receipt), created: true };
}
async listConsentReceipts(accountId: string): Promise<StoredConsentReceipt[]> {

View file

@ -260,6 +260,7 @@ interface ConsentReceiptRow {
scopes_asked: string | null;
scopes_granted: string | null;
consent_page_digest: string | null;
consent_text: string | null;
revokes_receipt_id: string | null;
}
@ -280,6 +281,7 @@ function mapConsentReceiptRow(row: ConsentReceiptRow): StoredConsentReceipt {
scopesAsked: parseScopes(row.scopes_asked ?? ""),
scopesGranted: parseScopes(row.scopes_granted ?? ""),
consentPageDigest: row.consent_page_digest ?? "",
consentText: row.consent_text ?? "",
};
}
@ -1232,15 +1234,18 @@ export class PostgresStore implements HeimdallStore {
);
}
async createConsentReceipt(input: CreateConsentReceiptInput): Promise<StoredConsentReceipt> {
async createConsentReceipt(input: CreateConsentReceiptInput): Promise<{ receipt: StoredConsentReceipt; created: boolean }> {
const grant = input.kind === "grant" ? input : null;
// No conflict target and the partial unique index decides: a second
// revocation of one grant inserts nothing and the first is read back.
const result = await this.pool.query<ConsentReceiptRow>(
`
INSERT INTO consent_receipts (
id, kind, account_id, app_slug, provider, created_at,
scopes_asked, scopes_granted, consent_page_digest, revokes_receipt_id
scopes_asked, scopes_granted, consent_page_digest, consent_text, revokes_receipt_id
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
ON CONFLICT DO NOTHING
RETURNING *
`,
[
@ -1253,10 +1258,18 @@ export class PostgresStore implements HeimdallStore {
grant ? grant.scopesAsked.join(" ") : null,
grant ? grant.scopesGranted.join(" ") : null,
grant ? grant.consentPageDigest : null,
grant ? grant.consentText : null,
input.kind === "revocation" ? input.revokesReceiptId : null,
]
);
return mapConsentReceiptRow(expectRow(result.rows[0], "createConsentReceipt"));
if (result.rowCount) {
return { receipt: mapConsentReceiptRow(expectRow(result.rows[0], "createConsentReceipt")), created: true };
}
const existing = await this.pool.query<ConsentReceiptRow>(
`SELECT * FROM consent_receipts WHERE revokes_receipt_id = $1`,
[input.kind === "revocation" ? input.revokesReceiptId : null]
);
return { receipt: mapConsentReceiptRow(expectRow(existing.rows[0], "createConsentReceipt")), created: false };
}
async listConsentReceipts(accountId: string): Promise<StoredConsentReceipt[]> {

View file

@ -192,9 +192,15 @@ CREATE TABLE IF NOT EXISTS consent_receipts (
scopes_asked TEXT,
scopes_granted TEXT,
consent_page_digest TEXT,
consent_text TEXT,
revokes_receipt_id TEXT
);
-- A grant is taken back once: the second revocation naming it conflicts.
CREATE UNIQUE INDEX IF NOT EXISTS consent_receipts_revokes_unique_idx
ON consent_receipts(revokes_receipt_id)
WHERE revokes_receipt_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS consent_receipts_account_idx
ON consent_receipts(account_id, created_at);

View file

@ -257,7 +257,9 @@ export type CreatePrivateCommandReceiptInput = StoredPrivateCommandReceipt;
* edited, and a revocation is a second row naming it, so a grant is live
* exactly when no revocation names it. `scopesAsked` is what Heimdall sent the
* provider, `scopesGranted` what the provider returned, `consentPageDigest`
* the hash of the wording the person was shown.
* the hash of the wording the person was shown and `consentText` that wording
* itself. A grant is revoked at most once: a second revocation naming the same
* grant is not written, and the store answers with the first.
*/
export type StoredConsentReceipt = {
id: string;
@ -266,7 +268,7 @@ export type StoredConsentReceipt = {
provider: Provider;
createdAt: string;
} & (
| { kind: "grant"; scopesAsked: string[]; scopesGranted: string[]; consentPageDigest: string }
| { kind: "grant"; scopesAsked: string[]; scopesGranted: string[]; consentPageDigest: string; consentText: string }
| { kind: "revocation"; revokesReceiptId: string }
);
@ -350,7 +352,8 @@ export interface HeimdallStore {
*/
claimOAuthStateUse(input: ClaimOAuthStateUseInput): Promise<boolean>;
createAuditEvent(input: CreateAuditEventInput): Promise<void>;
createConsentReceipt(input: CreateConsentReceiptInput): Promise<StoredConsentReceipt>;
/** `created` is false when a revocation for that grant already existed and is what comes back. */
createConsentReceipt(input: CreateConsentReceiptInput): Promise<{ receipt: StoredConsentReceipt; created: boolean }>;
/** Every receipt the account holds, oldest first. */
listConsentReceipts(accountId: string): Promise<StoredConsentReceipt[]>;
}