diff options
| author | Adam Soltys <[email protected]> | 2026-12-02 13:45:56 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-12-02 13:45:56 -0700 |
| commit | c3e2e37e9b0186814a5d9cc04b9c728427e4f0cd (patch) | |
| tree | 5bce88760fac62b2b0d486e3a8537d73f3128d14 /test/pin-limit.test.ts | |
| parent | a07e326bc9dae4a3df02ce8f08cb4f71f094392e (diff) | |
| download | coinos-server-c3e2e37e9b0186814a5d9cc04b9c728427e4f0cd.tar.gz coinos-server-c3e2e37e9b0186814a5d9cc04b9c728427e4f0cd.zip | |
auth: lock PIN checks after ten wrong PINs in an hour
Every PIN check answered right or wrong with no per-account limit (POST
/pin, the sends that require a PIN, and the settings update), so anyone
holding a stolen session could try every 6-digit PIN from rotating IPs.
checkPin() now counts wrong PINs per account in pinfail:<id>; after ten in
an hour every PIN check fails, the right PIN included, until the hour is
up. A missing PIN doesn't count, and the right PIN clears the count.
POST /pin answers 429 while locked.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VMan6SSfVopM1ZzXKZmLYU
Diffstat (limited to 'test/pin-limit.test.ts')
| -rw-r--r-- | test/pin-limit.test.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/test/pin-limit.test.ts b/test/pin-limit.test.ts new file mode 100644 index 00000000..f8702c0b --- /dev/null +++ b/test/pin-limit.test.ts @@ -0,0 +1,40 @@ +import { afterAll, expect, test } from "bun:test"; +import { db } from "../lib/db"; +import { checkPin, requirePin } from "../lib/auth"; + +// Run inside app, where the configured DB resolves. Uses a throwaway user id, +// so only the pinfail:<id> key it creates is touched. +const user = { id: `test-${crypto.randomUUID()}`, pin: "123456" }; +const key = `pinfail:${user.id}`; +afterAll(async () => { await db.del(key); }); + +test("no PIN on the account: anything passes and nothing is counted", async () => { + expect(await checkPin({ id: user.id }, "000000")).toBe(true); + expect(await db.exists(key)).toBe(0); +}); + +test("a missing PIN is not a guess", async () => { + expect(await checkPin(user, undefined)).toBe(false); + expect(await checkPin(user, "")).toBe(false); + expect(await db.exists(key)).toBe(0); +}); + +test("the right PIN clears earlier wrong guesses", async () => { + expect(await checkPin(user, "000001")).toBe(false); + expect(Number(await db.get(key))).toBe(1); + expect(await db.ttl(key)).toBeGreaterThan(0); + expect(await checkPin(user, "123456")).toBe(true); + expect(await db.exists(key)).toBe(0); +}); + +test("ten wrong PINs lock every PIN check, the right one included", async () => { + for (let i = 0; i < 10; i++) expect(await checkPin(user, `9999${i}0`)).toBe(false); + await expect(checkPin(user, "123456")).rejects.toThrow("Too many wrong PINs"); + await expect(requirePin({ body: { pin: "123456" }, user })).rejects.toThrow("Too many wrong PINs"); +}); + +test("requirePin still refuses a wrong PIN and allows the right one", async () => { + await db.del(key); + await expect(requirePin({ body: { pin: "111111" }, user })).rejects.toThrow("Invalid pin"); + await requirePin({ body: { pin: "123456" }, user }); +}); |
