// PCH-02-007 regression tests — the HIBP k-anonymity range request must ask // for response padding, and the parser must stay correct on padded responses. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; // SHA-1("password") = 5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8 const PREFIX = "5BAA6"; const SUFFIX = "1E4C9B93F3F0682250B6CF8331B7EE68FD8"; describe("PCH-02-007 — HIBP Add-Padding", () => { let fetchMock: ReturnType; beforeEach(() => { vi.resetModules(); const chromeMock = { storage: { local: { get: vi.fn(async () => ({})), set: vi.fn(async () => undefined), remove: vi.fn(async () => undefined), }, }, alarms: { clear: vi.fn(), create: vi.fn(), onAlarm: { addListener: vi.fn() }, }, idle: { setDetectionInterval: vi.fn(), onStateChanged: { addListener: vi.fn() }, }, } as unknown as typeof chrome; (globalThis as { chrome?: typeof chrome }).chrome = chromeMock; fetchMock = vi.fn(async () => ({ ok: true, text: async () => [ "0018A45C4D1DEF81644B54AB7F969B88D65:1", `${SUFFIX}:3303003`, "FFFFFFAAAAAABBBBBBCCCCCCDDDDDDEEEEE:0", ].join("\r\n"), })); vi.stubGlobal("fetch", fetchMock); }); afterEach(() => { vi.useRealTimers(); vi.unstubAllGlobals(); }); it("sends Add-Padding: true on the range request", async () => { const vault = await import("./vault"); await vault.checkBreach("password"); expect(fetchMock).toHaveBeenCalledWith( `https://api.pwnedpasswords.com/range/${PREFIX}`, expect.objectContaining({ headers: expect.objectContaining({ "Add-Padding": "true" }), }), ); }); it("still parses the real breach count out of a padded response", async () => { const vault = await import("./vault"); const count = await vault.checkBreach("password"); expect(count).toBe(3303003); }); it("reports 0 when the response holds only padding lines", async () => { fetchMock.mockImplementationOnce(async () => ({ ok: true, text: async () => [ "FFFFFFAAAAAABBBBBBCCCCCCDDDDDDEEEEE:0", "EEEEEEAAAAAABBBBBBCCCCCCDDDDDDFFFFF:0", ].join("\r\n"), })); const vault = await import("./vault"); const count = await vault.checkBreach("password"); expect(count).toBe(0); }); it.each(["", "not a range", `${SUFFIX}:7junk`, `${SUFFIX}:9007199254740992`, `${SUFFIX}A:7`, `${SUFFIX}:-1`, `${SUFFIX}:NaN`, `${SUFFIX}:7\ninvalid`])("reports unknown for malformed range %s", async (body) => { fetchMock.mockResolvedValueOnce({ ok: true, text: async () => body }); const vault = await import("./vault"); expect(await vault.checkBreach("password")).toBeNull(); }); it.each(["http", "network", "body"])("reports unknown on %s failure", async (failure) => { fetchMock.mockImplementationOnce(async () => { if (failure === "network") throw new Error("offline"); return { ok: failure !== "http", text: async () => { throw new Error("body failed"); } }; }); const vault = await import("./vault"); expect(await vault.checkBreach("password")).toBeNull(); }); it.each(["fetch", "body"])("bounds a stalled %s even if transport ignores abort", async (stage) => { vi.useFakeTimers(); const stalled = new Promise(() => {}); fetchMock.mockImplementationOnce(() => stage === "fetch" ? stalled : Promise.resolve({ ok: true, text: () => stalled })); const vault = await import("./vault"); const lookup = vault.checkBreach("password"); await vi.waitFor(() => expect(fetchMock).toHaveBeenCalled()); await vi.advanceTimersByTimeAsync(10_000); expect(await lookup).toBeNull(); expect(fetchMock.mock.calls[0][1].signal.aborted).toBe(true); }); it("accepts LF ranges and a trailing newline", async () => { fetchMock.mockResolvedValueOnce({ ok: true, text: async () => `${SUFFIX}:9\n${"A".repeat(35)}:0\n` }); const vault = await import("./vault"); expect(await vault.checkBreach("password")).toBe(9); }); });