From 963150f537a64a6fd1d79342bc553568c8e7430d Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 30 Sep 2026 18:56:35 +0200 Subject: [PATCH] Audit events never carry a completion code Soul pass 5, S5-3: auth_completion_created stored completion.code, which the unauthenticated redeem route turns into the session until it expires (300 s by default), so the audit trail could redeem a live sign-in. The two auth_completion_redeemed events (the HTTP redeem route and the private command plane's redeem by attempt) stored the spent code as well. All three payloads drop it. Nothing reads audit payloads back: the store has no audit read, and no repository indexed by VoidBot consumes these events. The event's sessionId already names the completion (one per callback), so no replacement key is added. This matches Heimdall's own witness contract, which says codes are redacted from audit exports. Tests drive a sign-in through the HTTP redeem route and through the plane, pin each payload exactly, and check the code appears in no audit event. Co-Authored-By: Claude Opus 5.5 --- src/app.ts | 5 +-- src/private-command-plane.ts | 1 - tests/app.test.ts | 27 ++++++++++++++++ tests/private-command-diagnostics.test.ts | 39 +++++++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/app.ts b/src/app.ts index eaaef7d..9621856 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1304,10 +1304,12 @@ export async function buildApp(options: BuildAppOptions = {}): Promise { expect(secondRedeemResponse.statusCode).toBe(410); }); + it("keeps the completion code out of the audit trail, live or redeemed", async () => { + const store = new InMemoryStore(); + const app = await buildApp({ config: createTestConfig(), store, oauthRuntimes: { discord: createMockDiscordRuntime() } }); + apps.push(app); + const auditEvents = () => + [...(store as unknown as { auditEvents: Map }).auditEvents.values()]; + + const stateToken = await startDiscordSignIn(app); + const callback = await app.inject({ + method: "GET", + url: `/v1/oauth/discord/callback?code=test-code&state=${encodeURIComponent(stateToken)}`, + }); + const { code, expiresAt } = callback.json().completion as { code: string; expiresAt: string }; + const created = auditEvents().filter((event) => event.eventType === "auth_completion_created"); + expect(created.map((event) => event.eventPayloadJson)).toEqual([{ provider: "discord", mode: "sign_in", expiresAt }]); + expect(created[0]!.sessionId).toEqual(expect.any(String)); + // While the code is live, the audit trail must not be a way to redeem it. + expect(JSON.stringify(auditEvents())).not.toContain(code); + + const redeem = await app.inject({ method: "POST", url: "/v1/apps/repixelizer/auth-completions/redeem", payload: { completionCode: code } }); + expect(redeem.statusCode).toBe(201); + const redeemed = auditEvents().filter((event) => event.eventType === "auth_completion_redeemed"); + expect(redeemed.map((event) => event.eventPayloadJson)).toEqual([{ provider: "discord", mode: "sign_in" }]); + expect(redeemed[0]!.sessionId).toBe(created[0]!.sessionId); + expect(JSON.stringify(auditEvents())).not.toContain(code); + }); + it("refreshes a Repixelizer app claim from a Heimdall refresh token without provider OAuth", async () => { const store = new InMemoryStore(); let exchangeCalls = 0; diff --git a/tests/private-command-diagnostics.test.ts b/tests/private-command-diagnostics.test.ts index 3204e10..4f7e965 100644 --- a/tests/private-command-diagnostics.test.ts +++ b/tests/private-command-diagnostics.test.ts @@ -109,3 +109,42 @@ it("keeps a provider's refusal body out of every plaintext part of the reply", a expect(reply.diagnostics).toEqual(["Heimdall denied the private command (ProviderResponseError, provider_status)."]); expect(plaintext(reply)).not.toContain("CANARY"); }); + +it("keeps the completion code out of the audit trail when the plane redeems by attempt", async () => { + vi.stubGlobal("fetch", async (url: string | URL) => { + const u = String(url); + if (u.endsWith("/oauth2/token")) { + return new Response( + JSON.stringify({ access_token: "a", refresh_token: "r", token_type: "Bearer", expires_in: 60, scope: "identify" }), + { status: 200 } + ); + } + if (u.endsWith("/users/@me")) return new Response(JSON.stringify({ id: "u1", username: "u1" }), { status: 200 }); + if (u.includes("/member")) return new Response(JSON.stringify({ roles: ["r"] }), { status: 200 }); + return new Response("{}", { status: 404 }); + }); + const cfg = config(); + const store = new InMemoryStore(); + const app = await buildApp({ config: cfg, store }); + resources.push(app); + const plane = await startHeimdallPrivateCommandPlane(app, cfg); + resources.push(plane); + const policy = { kind: "discord_role_access", guildId: "g", allowedRoleIds: ["r"] }; + const begin = open(await command(plane.endpoint, "heimdall.auth.begin", "heimdall.auth_begin_command.v1", "k1", + { provider: "discord", mode: "sign_in", returnTo: "https://yggdrasil.gamecult.org/ghostlight/", entitlementPolicy: policy })); + const state = new URL(String((begin.navigation as { url: string }).url)).searchParams.get("state")!; + const callback = await app.inject({ + method: "GET", + url: `/v1/oauth/discord/callback?code=c1&state=${encodeURIComponent(state)}`, + headers: { accept: "application/json" }, + }); + const code = (callback.json().completion as { code: string }).code; + const done = open(await command(plane.endpoint, "heimdall.auth.complete", "heimdall.auth_complete_command.v1", "k2", { handle: begin.handle })); + expect(done.status).toBe("authenticated"); + + const events = [...(store as unknown as { auditEvents: Map }).auditEvents.values()]; + expect(events.filter((event) => event.eventType === "auth_completion_redeemed").map((event) => event.eventPayloadJson)).toEqual([ + { provider: "discord", mode: "sign_in" }, + ]); + expect(JSON.stringify(events)).not.toContain(code); +});