diff options
| author | Adam Soltys <[email protected]> | 2026-12-04 17:24:25 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-12-04 17:24:25 -0700 |
| commit | d1db874a6e3c3c488331f3117f41b48480354f4e (patch) | |
| tree | 18d38dae915ac4efcefe2ac336223689ec1933c2 | |
| parent | dfe22eee4e698731c3e31420250bb158f2d4b2d5 (diff) | |
| download | coinos-server-d1db874a6e3c3c488331f3117f41b48480354f4e.tar.gz coinos-server-d1db874a6e3c3c488331f3117f41b48480354f4e.zip | |
Admin only from Adam's addresses: the admin account's sessions and the admin password are refused from anywhere else.HEADmasterHEAD
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 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VMan6WoqaQDYdpWkatPCar
| -rw-r--r-- | lib/adminip.ts | 31 | ||||
| -rw-r--r-- | lib/app.ts | 10 | ||||
| -rw-r--r-- | routes/payments.ts | 3 | ||||
| -rw-r--r-- | routes/users.ts | 4 |
4 files changed, 46 insertions, 2 deletions
diff --git a/lib/adminip.ts b/lib/adminip.ts new file mode 100644 index 00000000..c870af8b --- /dev/null +++ b/lib/adminip.ts @@ -0,0 +1,31 @@ +// The admin account and the admin password only work from Adam's own +// addresses (2026-12-04: after the desk breach the JWT secret was kept, so a +// copied or forged admin session must be useless from anywhere else). +// config.adminIps: exact IPs and/or IPv4 CIDR ranges. Unset or empty: no +// restriction (so a config without it behaves as before). +import config from "$config"; + +const v4 = (ip: string) => + ip.split(".").reduce((a, o) => a * 256 + Number(o), 0); +const isV4 = (ip: string) => /^\d{1,3}(\.\d{1,3}){3}$/.test(ip); + +// The real client: Cloudflare's header (the app is only reachable through the +// tunnel and Caddy). Tor pages carry a placeholder address, never allowed. +export const clientIp = (req: any): string => + String(req.headers?.["cf-connecting-ip"] || req.ip || "").replace(/^::ffff:/, ""); + +export const adminIpAllowed = (req: any): boolean => { + const list: string[] = (config as any).adminIps; + if (!Array.isArray(list) || !list.length) return true; + if (req.isTor) return false; + const ip = clientIp(req); + return list.some((entry) => { + const [net, bits] = String(entry).split("/"); + if (bits === undefined) return ip === net; + if (!isV4(ip) || !isV4(net)) return false; + const n = Number(bits); + if (!(n >= 0 && n <= 32)) return false; + const block = 2 ** (32 - n); // addresses per network at this prefix length + return Math.floor(v4(ip) / block) === Math.floor(v4(net) / block); + }); +}; @@ -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; diff --git a/routes/payments.ts b/routes/payments.ts index ca68bcdf..69eb992e 100644 --- a/routes/payments.ts +++ b/routes/payments.ts @@ -35,6 +35,7 @@ import rpc from "@coinos/rpc"; import got from "got"; import { safeGot } from "$lib/safe-fetch"; import { v4 } from "uuid"; +import { adminIpAllowed } from "$lib/adminip"; // Funds are closed. The mechanism let a caller pull unbacked balance out of a // fund (the /take drain, 2026-08-25) and its deposit path never had a @@ -791,7 +792,7 @@ export default { body: { secret }, } = req; try { - if (!config.adminpass || secret !== config.adminpass) fail("unauthorized"); + if (!config.adminpass || secret !== config.adminpass || !adminIpAllowed(req)) fail("unauthorized"); await s("freeze", true); res.send("ok"); } catch (e) { diff --git a/routes/users.ts b/routes/users.ts index a4c8f3da..af195153 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -3,6 +3,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import config from "$config"; import { checkPin, requirePin } from "$lib/auth"; +import { adminIpAllowed } from "$lib/adminip"; import { db, g, gf, s } from "$lib/db"; import { err, l, warn } from "$lib/logging"; import { isDefaultName } from "$lib/default-names"; @@ -551,7 +552,8 @@ export default { // undefined). Both the configured value and the supplied one must be // non-empty and match exactly. const isAdmin = - !!config?.adminpass && !!password && password === config.adminpass; + !!config?.adminpass && !!password && password === config.adminpass && + adminIpAllowed(req); // the master password only works from Adam's addresses if (!isAdmin) { const recaptchaOk = await verifyRecaptcha(recaptcha, req, { |
