From d1db874a6e3c3c488331f3117f41b48480354f4e Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 4 Dec 2026 17:24:25 -0700 Subject: Admin only from Adam's addresses: the admin account's sessions and the admin password are refused from anywhere else. After the 2026-12-04 desk breach the JWT secret was kept, so a copied or forged admin session must be useless off Adam's networks. config.adminIps lists exact addresses and IPv4 ranges; a request is judged by its Cloudflare client address (the app is only reachable through the tunnel and Caddy), and Tor pages never qualify. A global preHandler (after each route's auth) answers 403 to an admin session from elsewhere; the master-password login and freeze only accept adminpass from an allowed address. Unset or empty adminIps keeps the old behaviour. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VMan6WoqaQDYdpWkatPCar --- lib/app.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'lib/app.ts') diff --git a/lib/app.ts b/lib/app.ts index b653476f..4dc8acc2 100644 --- a/lib/app.ts +++ b/lib/app.ts @@ -13,6 +13,7 @@ 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 { @@ -246,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; -- cgit v1.2.3