import { describe, it, expect, beforeEach, afterEach } from "vitest"; import { sealSession, openSession } from "./sessionSeal"; import { encodeSession } from "./sessionCodec"; // sign-session-cookies (F32): the session cookie is HMAC-signed; anything edited, unsigned or forged reads as no session. const SAMPLE = { token: "jwt-abc", resetPasswordRequired: true, absoluteExpiry: 1900000000 }; let saved: { secret?: string; key?: string }; beforeEach(() => { saved = { secret: process.env.SESSION_SECRET, key: process.env.MOBILE_API_KEY }; process.env.SESSION_SECRET = "unit-test-secret"; }); afterEach(() => { process.env.SESSION_SECRET = saved.secret; process.env.MOBILE_API_KEY = saved.key; if (saved.secret === undefined) delete process.env.SESSION_SECRET; if (saved.key === undefined) delete process.env.MOBILE_API_KEY; }); function edit(raw: string, change: (o: Record) => void): string { const obj = JSON.parse(decodeURIComponent(raw)); change(obj); return encodeURIComponent(JSON.stringify(obj)); } describe("sealSession / openSession", () => { it("round-trips the session", async () => { expect(await openSession(await sealSession(SAMPLE))).toEqual(SAMPLE); }); it("keeps the cookie readable JSON with the signature as an extra field", async () => { const obj = JSON.parse(decodeURIComponent(await sealSession(SAMPLE))); expect(typeof obj.sig).toBe("string"); expect(obj.token).toBe(SAMPLE.token); }); it("rejects an edited flag", async () => { const raw = edit(await sealSession(SAMPLE), o => { o.resetPasswordRequired = !o.resetPasswordRequired; }); expect(await openSession(raw)).toBeNull(); }); it("rejects an edited token", async () => { const raw = edit(await sealSession(SAMPLE), o => { o.token = "someone-else"; }); expect(await openSession(raw)).toBeNull(); }); it("rejects a stripped or forged signature", async () => { const sealed = await sealSession(SAMPLE); expect(await openSession(edit(sealed, o => { delete o.sig; }))).toBeNull(); expect(await openSession(edit(sealed, o => { o.sig = "AAAA"; }))).toBeNull(); }); it("rejects an unsigned cookie written before this change", async () => { expect(await openSession(encodeSession(SAMPLE))).toBeNull(); }); it("rejects a cookie signed with another key", async () => { const sealed = await sealSession(SAMPLE); process.env.SESSION_SECRET = "another-secret"; expect(await openSession(sealed)).toBeNull(); }); it("rejects garbage and empty input", async () => { expect(await openSession("not-json")).toBeNull(); expect(await openSession("")).toBeNull(); expect(await openSession(undefined)).toBeNull(); }); it("derives the key from MOBILE_API_KEY when SESSION_SECRET is unset", async () => { delete process.env.SESSION_SECRET; process.env.MOBILE_API_KEY = "server-api-key"; const sealed = await sealSession(SAMPLE); expect(await openSession(sealed)).toEqual(SAMPLE); process.env.MOBILE_API_KEY = "rotated-key"; expect(await openSession(sealed)).toBeNull(); }); it("fails closed with no key at all", async () => { const sealed = await sealSession(SAMPLE); delete process.env.SESSION_SECRET; delete process.env.MOBILE_API_KEY; await expect(sealSession(SAMPLE)).rejects.toThrow(); expect(await openSession(sealed)).toBeNull(); }); }); // Review follow-up: the key carries a per-portal label, so a cookie signed for the other portal (or with the bare // secret) never verifies here, even when both portals share SESSION_SECRET or MOBILE_API_KEY. describe("per-portal key", () => { it("rejects a cookie signed with the bare secret or another portal's label", async () => { process.env.SESSION_SECRET = "shared-secret"; const sealed = JSON.parse(decodeURIComponent(await sealSession(SAMPLE))); delete sealed.sig; const payload = JSON.stringify(sealed); for (const keyText of ["shared-secret", "bb-session-v1:shared-secret", "bb-member-session-v1:shared-secret"]) { const key = await crypto.subtle.importKey("raw", new TextEncoder().encode(keyText), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]); const sig = new Uint8Array(await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(payload))); const forged = encodeURIComponent(JSON.stringify({ ...sealed, sig: btoa(String.fromCharCode(...sig)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "") })); expect(await openSession(forged)).toBeNull(); } }); });