diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/app.ts | 20 | ||||
| -rw-r--r-- | lib/registration-limit.ts | 19 |
2 files changed, 37 insertions, 2 deletions
@@ -14,6 +14,12 @@ import * as path from "path"; import { jwtStrategy } from "$lib/auth"; +declare module "fastify" { + interface FastifyRequest { + isTor: boolean; + } +} + const app = fastify({ logger: true, logController: new fastify.LogController({ disableRequestLogging: true }), @@ -80,6 +86,7 @@ const redactHeaders = (headers: any) => { // rate limiter so its keyGenerator sees the rewritten header. const UNTRUSTED_PEERS = ["tor"]; let untrusted = new Set<string>(); +let uiPeers = new Set<string>(); const resolveUntrusted = async () => { const next = new Set<string>(); for (const host of UNTRUSTED_PEERS) { @@ -89,10 +96,15 @@ const resolveUntrusted = async () => { } catch {} } if (next.size) untrusted = next; + try { + uiPeers = new Set((await dns.promises.lookup("ui", { all: true })) + .map(({ address }) => address)); + } catch {} }; await resolveUntrusted(); setInterval(resolveUntrusted, 30000).unref(); +app.decorateRequest("isTor", false); app.addHook("onRequest", async (req) => { const peer = (req.socket.remoteAddress || "").replace(/^::ffff:/, ""); // The onion service points at lb, not at us, so the peer is usually lb. @@ -102,9 +114,13 @@ app.addHook("onRequest", async (req) => { const hop = (Array.isArray(xff) ? xff.at(-1) : xff?.split(",").at(-1)) ?.trim() .replace(/^::ffff:/, ""); - const viaTor = untrusted.has(peer) || (hop && untrusted.has(hop)); + // SSR forwards the client IP in cf-connecting-ip without x-forwarded-for. + const forwarded = req.headers["cf-connecting-ip"]; + const viaUi = uiPeers.has(peer) && typeof forwarded === "string" && untrusted.has(forwarded); + const viaTor = untrusted.has(peer) || (hop && untrusted.has(hop)) || viaUi; if (!viaTor) return; - req.headers["cf-connecting-ip"] = untrusted.has(peer) ? peer : hop; + req.isTor = true; + req.headers["cf-connecting-ip"] = untrusted.has(peer) ? peer : viaUi ? forwarded : hop; delete req.headers["x-forwarded-for"]; delete req.headers["rate-limit-by"]; }); 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; +}; |
