diff options
Diffstat (limited to 'lib/app.ts')
| -rw-r--r-- | lib/app.ts | 50 |
1 files changed, 48 insertions, 2 deletions
@@ -13,6 +13,13 @@ import dns from "node:dns"; import * as path from "path"; import { jwtStrategy } from "$lib/auth"; +import { adminIpAllowed, clientIp } from "$lib/adminip"; + +declare module "fastify" { + interface FastifyRequest { + isTor: boolean; + } +} const app = fastify({ logger: true, @@ -80,6 +87,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,14 +97,43 @@ 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:/, ""); - 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:/, ""); + // SSR forwards the client IP in cf-connecting-ip without x-forwarded-for. + // Onion pages never pass Cloudflare, so the UI has no client IP to forward + // for them; it does forward the page's Host as x-forwarded-host. Only the + // ui peer is believed on that, and clearnet pages always reach ui with a + // coinos.io Host. + const forwarded = req.headers["cf-connecting-ip"]; + const fwdHost = String(req.headers["x-forwarded-host"] || ""); + const viaUi = + uiPeers.has(peer) && + ((typeof forwarded === "string" && untrusted.has(forwarded)) || + fwdHost.endsWith(".onion")); + const viaTor = untrusted.has(peer) || (hop && untrusted.has(hop)) || viaUi; + if (!viaTor) return; + req.isTor = true; + req.headers["cf-connecting-ip"] = untrusted.has(peer) + ? peer + : hop && untrusted.has(hop) + ? hop + : [...untrusted][0]; delete req.headers["x-forwarded-for"]; delete req.headers["rate-limit-by"]; }); @@ -210,4 +247,13 @@ app.setNotFoundHandler((_, reply) => reply.code(404).type("text/html").send("Not Found"), ); +// Admin sessions only from config.adminIps (lib/adminip.ts). preHandler runs +// after each route's auth, so req.user is set for every authenticated route. +app.addHook("preHandler", async (req: any, reply) => { + if (req.user?.username && req.user.username === config.admin && !adminIpAllowed(req)) { + req.log?.warn?.({ ip: clientIp(req), url: req.url }, "admin session refused: address not allowed"); + return reply.code(403).send("forbidden"); + } +}); + export default app; |
