diff options
| author | Adam Soltys <[email protected]> | 2026-11-13 16:11:02 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-11-13 16:11:02 -0700 |
| commit | 67e0a77d967ed1ea3cfb1aae0a09431dd27014aa (patch) | |
| tree | 4578d2a68eb40231069272b38d8b90b6e64ed98b /lib | |
| parent | b8b409910f71976959135b336b409c4923cf4c87 (diff) | |
| download | coinos-server-67e0a77d967ed1ea3cfb1aae0a09431dd27014aa.tar.gz coinos-server-67e0a77d967ed1ea3cfb1aae0a09431dd27014aa.zip | |
app: recognise onion requests that arrive through lb
The onion service points at lb rather than the app, so the socket peer is
lb and the tor check never matched; the client's own cf-connecting-ip and
x-forwarded-for headers were trusted. Also check the last x-forwarded-for
hop (which lb overwrites with its own peer) and pin the client IP to it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/app.ts | 12 |
1 files changed, 10 insertions, 2 deletions
@@ -95,8 +95,16 @@ setInterval(resolveUntrusted, 30000).unref(); app.addHook("onRequest", async (req) => { const peer = (req.socket.remoteAddress || "").replace(/^::ffff:/, ""); - if (!untrusted.has(peer)) return; - req.headers["cf-connecting-ip"] = peer; + // The onion service points at lb, not at us, so the peer is usually lb. + // lb (Caddy, no trusted_proxies) overwrites x-forwarded-for with its own + // peer, so a tor address there means the request came in over the onion. + const xff = req.headers["x-forwarded-for"]; + const hop = (Array.isArray(xff) ? xff.at(-1) : xff?.split(",").at(-1)) + ?.trim() + .replace(/^::ffff:/, ""); + const viaTor = untrusted.has(peer) || (hop && untrusted.has(hop)); + if (!viaTor) return; + req.headers["cf-connecting-ip"] = untrusted.has(peer) ? peer : hop; delete req.headers["x-forwarded-for"]; delete req.headers["rate-limit-by"]; }); |
