aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/auth.ts25
-rw-r--r--routes/users.ts10
-rw-r--r--test/pin-limit.test.ts40
3 files changed, 71 insertions, 4 deletions
diff --git a/lib/auth.ts b/lib/auth.ts
index 107c44f4..af5de3fd 100644
--- a/lib/auth.ts
+++ b/lib/auth.ts
@@ -22,8 +22,31 @@ export const auth = {
}),
};
+// Wrong PINs per account. Without a limit, anyone holding a stolen session
+// can try all 10^6 PINs from rotating IPs, since every PIN check answers
+// right/wrong. After PIN_MAX wrong guesses in PIN_WINDOW seconds, every PIN
+// check fails, the right PIN included, until the window runs out.
+const PIN_MAX = 10;
+const PIN_WINDOW = 3600;
+
+// true if the account has no PIN or `pin` is right. A missing pin is not a
+// guess and doesn't count; a wrong one does.
+export const checkPin = async (user, pin) => {
+ if (!user?.pin) return true;
+ const k = `pinfail:${user.id}`;
+ const n = Number(await db.get(k)) || 0;
+ if (n >= PIN_MAX) fail("Too many wrong PINs, try again in an hour");
+ if (pin === undefined || pin === null || pin === "") return false;
+ if (pin === user.pin) {
+ if (n) await db.del(k);
+ return true;
+ }
+ if ((await db.incr(k)) === 1) await db.expire(k, PIN_WINDOW);
+ return false;
+};
+
export const requirePin = async ({ body, user }) => {
- if (!user || (user.pin && user.pin !== body.pin)) fail("Invalid pin");
+ if (!user || !(await checkPin(user, body?.pin))) fail("Invalid pin");
};
// A caller-supplied `aid` decides which account's node wallet composes and
diff --git a/routes/users.ts b/routes/users.ts
index 51eb14be..a4c8f3da 100644
--- a/routes/users.ts
+++ b/routes/users.ts
@@ -2,7 +2,7 @@ import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import config from "$config";
-import { requirePin } from "$lib/auth";
+import { checkPin, requirePin } from "$lib/auth";
import { db, g, gf, s } from "$lib/db";
import { err, l, warn } from "$lib/logging";
import { isDefaultName } from "$lib/default-names";
@@ -441,7 +441,7 @@ export default {
user.nsec = undefined;
}
- if (user.pin && !(pin === user.pin)) fail("Pin required");
+ if (!(await checkPin(user, pin))) fail("Pin required");
if (typeof newpin !== "undefined" && newpin.length === 6)
user.pin = newpin;
if (user.pin === "delete") user.pin = undefined;
@@ -811,7 +811,11 @@ export default {
body: { pin },
user,
} = req;
- res.send(!user.pin || user.pin === pin);
+ try {
+ res.send(await checkPin(user, pin));
+ } catch (e) {
+ res.code(429).send(e.message);
+ }
},
async otpsecret(req, res) {
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 });
+});