From 5b7881f3195d9c1479cfffc207014540a9db4d9a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 2 Dec 2026 13:45:44 -0700 Subject: register: cap account creation over Tor at five an hour Onion clients all arrive from the Tor gateway's address, so the per-IP and per-range signup caps either let them create unlimited accounts or would count unrelated internal traffic. Mark requests that came through Tor (including ones the UI's server-side rendering relays, which forward the client address in cf-connecting-ip) with req.isTor, and give them one shared quota of five new accounts an hour for /register and first-time nostr logins. The recaptcha bypass for onion users now uses the same flag instead of trusting the Host header. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VMan6SSfVopM1ZzXKZmLYU --- test/registration-limit.test.ts | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 test/registration-limit.test.ts (limited to 'test') diff --git a/test/registration-limit.test.ts b/test/registration-limit.test.ts new file mode 100644 index 00000000..2e75e512 --- /dev/null +++ b/test/registration-limit.test.ts @@ -0,0 +1,50 @@ +import { afterAll, expect, test } from "bun:test"; +import { db } from "../lib/db"; +import { allowTorRegistration } from "../lib/registration-limit"; + +// Exercise the actual atomic Redis script under an isolated key; never consume +// the production signup quota. Run inside app, where the configured DB resolves. +const key = `test:register:tor:${crypto.randomUUID()}`; +const store = { + eval: (script, options) => db.eval(script, { ...options, keys: [key] }), +} as typeof db; +const reply = () => ({ + status: 200, + headers: {}, + body: null, + header(name, value) { this.headers[name] = value; return this; }, + code(value) { this.status = value; return this; }, + send(value) { this.body = value; return this; }, +}); + +afterAll(async () => { await db.del(key); }); + +test("clearnet requests do not consume the shared Tor quota", async () => { + expect(await allowTorRegistration({ isTor: false }, reply(), store)).toBe(true); + expect(await db.exists(key)).toBe(0); +}); + +test("concurrent onion signups allow five and reject the rest regardless of headers", async () => { + const responses = Array.from({ length: 12 }, reply); + const results = await Promise.all(responses.map((res, i) => + allowTorRegistration({ isTor: true, headers: { + "cf-connecting-ip": `198.51.100.${i}`, + "user-agent": `rotating-${i}`, + "rate-limit-by": "ua", + } }, res, store), + )); + expect(results.filter(Boolean)).toHaveLength(5); + expect(results.filter((allowed) => !allowed)).toHaveLength(7); + for (const res of responses.filter((r) => r.status === 429)) { + expect(res.headers["Retry-After"]).toBeGreaterThan(0); + expect(res.headers["Retry-After"]).toBeLessThanOrEqual(3600); + } + expect(await db.ttl(key)).toBeGreaterThan(0); +}); + +test("quota permits signups again after expiry", async () => { + await db.del(key); + expect(await allowTorRegistration({ isTor: true }, reply(), store)).toBe(true); + expect(await db.get(key)).toBe("1"); + expect(await db.ttl(key)).toBeGreaterThan(0); +}); -- cgit v1.2.3