aboutsummaryrefslogtreecommitdiffstats
path: root/lib/registration-limit.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-12-02 13:45:44 -0700
committerAdam Soltys <[email protected]>2026-12-02 13:45:44 -0700
commit5b7881f3195d9c1479cfffc207014540a9db4d9a (patch)
tree1316b495e207db65001c120c0ef3ff48c26c59f4 /lib/registration-limit.ts
parent796999cb8a6fc7abbfb9dc2643e372795fed9882 (diff)
downloadcoinos-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 'lib/registration-limit.ts')
-rw-r--r--lib/registration-limit.ts19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/registration-limit.ts b/lib/registration-limit.ts
new file mode 100644
index 00000000..2550e37f
--- /dev/null
+++ b/lib/registration-limit.ts
@@ -0,0 +1,19 @@
+import { db } from "$lib/db";
+
+// Onion clients share the Tor gateway's private IP. Keep a separate quota:
+// exempting private IPs lets onion clients create unlimited accounts, while
+// applying the public /16 quota would also count unrelated internal traffic.
+export const allowTorRegistration = async (req, res, store = db) => {
+ if (!req.isTor) return true;
+ const [count, ttl] = (await store.eval(
+ `local n = redis.call('INCR', KEYS[1])
+ if n == 1 then redis.call('EXPIRE', KEYS[1], 3600) end
+ return {n, redis.call('TTL', KEYS[1])}`,
+ { keys: ["register:tor:hour"], arguments: [] },
+ )) as number[];
+ if (count <= 5) return true;
+ res.header("Retry-After", Math.max(1, ttl)).code(429).send({
+ message: "Too many accounts created over Tor, try again later",
+ });
+ return false;
+};