aboutsummaryrefslogtreecommitdiffstats
path: root/lib/app.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/app.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/app.ts')
-rw-r--r--lib/app.ts20
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/app.ts b/lib/app.ts
index e349f1ee..b4595fc9 100644
--- a/lib/app.ts
+++ b/lib/app.ts
@@ -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"];
});