Check the shown text before any code exchange; pin link expiry with a clock

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Heimdall 2026-11-08 13:31:43 +00:00
commit d4f7dce098
2 changed files with 15 additions and 8 deletions

View file

@ -1619,6 +1619,16 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
if (mint === "refused") {
throw new Error("The signed state names a mode the provider has no role for in this app.");
}
// The text the person was shown rode in the launch binding. A custody
// flow that did not pass through the ask has none: it exchanges no code
// and stores no credential.
const shownText =
mint === "custody_only"
? cookieConsentText(config, keys, request.cookies[oauthBindingCookieName(statePayload.jti)], oauthBindingType, statePayload)
: null;
if (mint === "custody_only" && shownText === null) {
throw new Error("A managed connection completed without the consent text the person was shown.");
}
const callbackDescriptor = providerCatalog[request.params.provider];
const oidc = callbackDescriptor.oidc ? deriveOidcSecrets(statePayload.jti, keys.privateKey) : undefined;
const tokenSet = await runtime.exchangeAuthorizationCode({
@ -1782,14 +1792,8 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
// Heimdall now holds a credential the person was asked about: the
// receipt is the record of that agreement, written with the credential.
let consentReceiptId: string | undefined;
if (mint === "custody_only") {
if (mint === "custody_only" && shownText !== null) {
const ask = consentAskFor(statePayload);
// 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,

View file

@ -14,6 +14,7 @@ import { createTestConfig, setCookieOf, stateOf } from "./oauth-flow.js";
const apps: FastifyInstance[] = [];
afterEach(async () => {
vi.useRealTimers();
vi.restoreAllMocks();
while (apps.length) await apps.pop()?.close();
});
@ -206,7 +207,7 @@ describe("the ask", () => {
const done = await callback(app, state, `__Host-heimdall-oauth-${jti}=${bare}`, "code=code-a");
expect(done.statusCode).not.toBe(201);
expect(done.json()).not.toHaveProperty("consentReceiptId");
expect(await store.listConsentReceipts((await store.findStoredLinkedIdentity("joystick", "joy-a"))?.accountId ?? "none")).toEqual([]);
expect(await store.findStoredLinkedIdentity("joystick", "joy-a"), "no credential is kept").toBeNull();
});
it("the consent route refuses a flow that has no ask", async () => {
@ -488,6 +489,8 @@ describe("my grants", () => {
const link = await app.inject({ method: "POST", url: "/v1/me/grants/link", headers: bearer(a.accessToken) });
const url = new URL(link.json().url as string);
const linkToken = url.searchParams.get("t")!;
// Two minutes on, so a form token minted afresh would differ from the link's.
vi.useFakeTimers({ toFake: ["Date"], now: Date.now() + 120_000 });
const page = await app.inject({ method: "GET", url: `${url.pathname}${url.search}`, headers: { accept: "text/html" } });
const formToken = /name="t" value="([^"]+)"/.exec(page.body)![1]!;
expect(decodeJwt(formToken).payload.exp).toBe(decodeJwt(linkToken).payload.exp);