From 673713a015789b02e3ad91baa422f5e1ede33837 Mon Sep 17 00:00:00 2001 From: Heimdall Date: Thu, 12 Nov 2026 23:21:12 +0000 Subject: [PATCH] Step-up: build the authorize URL before claiming the launch (a refused step-up no longer burns the state); pin the strict step_up parse with a signed forgery; document backend_callback and mixed-version limits Co-Authored-By: Claude Sonnet 5.5 --- docs/service-contract.md | 4 +++ src/app.ts | 26 ++++++++--------- tests/step-up.test.ts | 61 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/docs/service-contract.md b/docs/service-contract.md index eb0b35f..0469186 100644 --- a/docs/service-contract.md +++ b/docs/service-contract.md @@ -220,6 +220,10 @@ Step-up (`stepUp: true`, sign_in only, off by default): - A provider with no such parameter (Discord, Patreon, GitHub, Spotify, Joystick) is `400 step_up_not_supported`, with the provider named, before any state is signed. The URL builder throws rather than omit the step. +- A `backend_callback` handoff delivery does not carry `stepUpRequested`; only the redeemed completion does. +- A state signed by a build with step-up and launched by an older build loses the step + silently (the older parser ignores `step_up`): deploy every Heimdall instance on one + release, with no mixed window. - Heimdall requests the step; it cannot observe that the provider honoured it. `stepUpRequested` says "asked", not "verified". diff --git a/src/app.ts b/src/app.ts index 12ace03..e3c4931 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1365,6 +1365,18 @@ export async function buildApp(options: BuildAppOptions = {}): Promise { await Promise.all(apps.splice(0).map((app) => app.close())); }); -async function rig() { +async function rig(config = createTestConfig()) { const app = await buildApp({ - config: createTestConfig(), + config, store: new InMemoryStore(), oauthRuntimes: { twitch: runtime("twitch", "t-1"), youtube: runtime("youtube", "y-1") }, }); @@ -150,3 +153,55 @@ describe("the flag is the signed state's, not the redeem side's", () => { } }); }); + +describe("a forged or odd state cannot carry a step-up the start would not have signed", () => { + // A real signing key on disk, so the test can re-sign a state as only Heimdall's key holder could. + const dir = mkdtempSync(join(tmpdir(), "heimdall-stepup-")); + const config = { ...createTestConfig(), signingPrivateKeyPath: join(dir, "signing.pem"), bootstrapSigningPrivateKeyOnMissing: true }; + // The app creates the key on first boot; read it only after the rig exists. + const loadKeys = (c: typeof config) => createRuntimeKeyMaterial({ ...c, bootstrapSigningPrivateKeyOnMissing: false }); + + async function forgedState(app: App, provider: string, headers: Record, stepUp: unknown, appSlug = "streampixels", url = returnTo) { + const started = await start(app, provider, {}, headers, appSlug, url); + const real = new URL(started.json().authorizationUrl).searchParams.get("state") ?? ""; + const keys = loadKeys(config); + const verified = verifyJwt(real, keys.publicKey) as { valid: boolean; payload: Record }; + expect(verified.valid, "the test key signs what the app verifies").toBe(true); + return { real, forged: signJwt({ ...verified.payload, step_up: stepUp }, keys) }; + } + const launch = (app: App, provider: string, state: string) => app.inject({ method: "GET", url: `/v1/oauth/${provider}/launch?state=${encodeURIComponent(state)}` }); + + it("rejects any step_up that is not null or true as invalid_state at the launch hop", async () => { + const app = await rig(config); + expect((await launch(app, "twitch", (await forgedState(app, "twitch", {}, true)).forged)).statusCode, "a well-formed true still launches").toBe(302); + for (const odd of ["true", "yes", 1, 0, false, {}, []]) { + const res = await launch(app, "twitch", (await forgedState(app, "twitch", {}, odd)).forged); + expect([res.statusCode, res.json().error], JSON.stringify(odd)).toEqual([400, "invalid_state"]); + } + rmSync(dir, { recursive: true, force: true }); + }); + + it("a step-up state for a provider with no re-authentication parameter fails closed and does not burn the state", async () => { + const dir2 = mkdtempSync(join(tmpdir(), "heimdall-stepup-")); + const config2 = { ...createTestConfig(), signingPrivateKeyPath: join(dir2, "signing.pem"), bootstrapSigningPrivateKeyOnMissing: true }; + try { + const app = await rig(config2); + const bifrost = { "x-heimdall-app-secret": "bifrost-secret" }; + const started = await start(app, "discord", {}, bifrost, "bifrost", "https://bifrost.gamecult.org/auth/heimdall/return"); + const real = new URL(started.json().authorizationUrl).searchParams.get("state") ?? ""; + const keys2 = loadKeys(config2); + const verified = verifyJwt(real, keys2.publicKey) as { valid: boolean; payload: Record }; + expect(verified.valid).toBe(true); + const forged = signJwt({ ...verified.payload, step_up: true }, keys2); + for (let attempt = 0; attempt < 2; attempt += 1) { + const res = await launch(app, "discord", forged); + expect(res.statusCode, "never a redirect without the step").toBe(500); + expect(res.headers.location).toBeUndefined(); + } + // Not "state_already_launched": the refused launches did not claim the state. + expect((await launch(app, "discord", real)).statusCode).toBe(302); + } finally { + rmSync(dir2, { recursive: true, force: true }); + } + }); +});