diff options
| author | Adam Soltys <[email protected]> | 2026-12-02 13:45:44 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-12-02 13:45:44 -0700 |
| commit | 5b7881f3195d9c1479cfffc207014540a9db4d9a (patch) | |
| tree | 1316b495e207db65001c120c0ef3ff48c26c59f4 /test | |
| parent | 796999cb8a6fc7abbfb9dc2643e372795fed9882 (diff) | |
| download | coinos-server-5b7881f3195d9c1479cfffc207014540a9db4d9a.tar.gz coinos-server-5b7881f3195d9c1479cfffc207014540a9db4d9a.zip | |
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 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VMan6SSfVopM1ZzXKZmLYU
Diffstat (limited to 'test')
| -rw-r--r-- | test/registration-limit.test.ts | 50 |
1 files changed, 50 insertions, 0 deletions
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); +}); |
