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); +});