Check passwords against breaches
For: an app developer who wants to refuse a password that a breach exposed.
What it does: core sends 5 characters of the password’s SHA-1 to the range API, and refuses a password that the answer lists.
Before you start
Section titled “Before you start”- A working password sign-in from Sign in with a password.
- Add
@authtempo/breach-checkto the API. - Set
password.breachChecktocreateBreachCheck(). - Set
timeoutMswhen 2 seconds is too short for your network. It takes 500 to 10000.
import { createBreachCheck } from "@authtempo/breach-check";import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
export function createBreachCheckAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string;}) { return createAuth({ baseURL: "https://api.example.com", appURL: "https://app.example.com", trustedOrigins: ["https://app.example.com"], secret: deps.secret, storage: deps.storage, mail: deps.mail, password: { // The HIBP range API: only 5 characters of the SHA-1 leave the server. // No answer in 2 seconds refuses the password with breach_check_unavailable. breachCheck: createBreachCheck({ timeoutMs: 2000 }), }, });}import { afterEach, describe, expect, it, vi } from "vitest";import { call, harness } from "../support/harness.ts";import { createBreachCheckAuth } from "./breach-check.ts";
/** A range API stub that gives one answer and counts the calls. No test calls the network. */function stubRange(answer: () => Response): { calls: number } { const seen = { calls: 0 }; vi.stubGlobal("fetch", async () => { seen.calls++; return answer(); }); return seen;}
describe("a breach check on the password", () => { afterEach(() => { vi.unstubAllGlobals(); });
it("runs the policy first, so a short password costs no call to the range API", async () => { const range = stubRange(() => new Response("0000000000000000000000000000000000A:0\n")); const auth = createBreachCheckAuth(harness()); const short = await call(auth, "POST", "/auth/password/sign-up", { email: "ada@example.com", password: "short", }); expect(await short.json()).toEqual({ error: "password_policy" }); expect(range.calls).toBe(0); });
it("fails closed: an outage or a page that is not a range refuses the password with 503", async () => { for (const answer of [ () => new Response("down", { status: 503 }), () => new Response("<html>sign in to the network</html>", { status: 200 }), ]) { const range = stubRange(answer); const auth = createBreachCheckAuth(harness()); const res = await call(auth, "POST", "/auth/password/sign-up", { email: "ada@example.com", password: "correct horse battery", }); expect(res.status).toBe(503); expect(await res.json()).toEqual({ error: "breach_check_unavailable" }); expect(range.calls).toBe(1); } });});How it behaves
Section titled “How it behaves”- Only the first 5 characters of the SHA-1 leave the server. The range API answers every suffix that starts with them.
- Each request asks for padding. A line with a count of 0 is padding, and it never refuses a password.
- The policy runs first. A password that the policy refuses costs no call.
- The check fails closed. A late answer, a network error, a status other than 200, or a body over 512 KB refuses the password.
- A 200 answer with no range line refuses the password too. An empty body or the HTML page of a proxy is such an answer.
- On 2026-10-15, real range answers held 76 to 105 KB. The cap of 512 KB holds about five times the largest.
- A reset link stays usable after that refusal. The person tries again with the same link.
- A reset by email code spends its code on each refused password. After an outage, the person asks for a new code.
- Your own function can answer
"unavailable"too. It sets its own deadline, because core sets none. A throw answers 500. - The limit of each password route bounds the calls. The limits page lists them.
- The package has one live test. It runs only with
AUTHTEMPO_LIVE_HIBP=1, so a normal test run sends no call.
If it fails
Section titled “If it fails”password_breached: the range API lists the password. Ask for another one.breach_check_unavailable: the range API gave no usable answer in time. Try again in a few minutes.