import type { Database } from "bun:sqlite"; import { createHmac, randomBytes, timingSafeEqual } from "node:crypto"; import type { Context } from "hono"; import { getCookie, setCookie } from "hono/cookie"; import { setting } from "../server/db.ts"; // §23's spam defenses, all server-side: nothing here trusts a clock or a value from the browser. export const MIN_SECONDS_ON_PAGE = 3; const STAMP_MAX_AGE = 86400; export const FILINGS_PER_HOUR = 5; export const MAX_CHARS = 20_000; const FORM_COOKIE = "kepr_form"; const HEX = /^[0-9a-f]{32,64}$/; /** One random key, made on first use and kept in kepr.db, for the IP hash and the form stamps. */ function secret(db: Database): string { db.query("INSERT OR IGNORE INTO settings (key, value) VALUES ('issue_secret', ?)").run( randomBytes(32).toString("hex"), ); return setting(db, "issue_secret") as string; } function mac(db: Database, message: string): string { return createHmac("sha256", secret(db)).update(message).digest("hex"); } /** * The originating address, as Kepr 2 read it: the first `X-Forwarded-For` entry. This trusts the * reverse proxy (Caddy) to set the header itself; Caddy replaces a client's header with the * connecting address unless `trusted_proxies` says otherwise. Exposed directly, a client could * forge it and dodge the rate limit, so without the header the socket address is used. */ function clientIp(c: Context): string { const forwarded = c.req.header("x-forwarded-for")?.split(",")[0]?.trim(); if (forwarded) return forwarded; // Bun passes its server as the env; Hono's test `request()` has none. const server = c.env as { requestIP?: (request: Request) => { address: string } | null }; return server?.requestIP?.(c.req.raw)?.address ?? ""; } /** A keyed hash, so the stored value can't be reversed by hashing every IPv4 address. */ export function ipHash(db: Database, c: Context): string { return mac(db, `ip:${clientIp(c)}`); } /** * The same-site token (§25) and the time-on-page clock in one: a random cookie, SameSite=Strict so a * cross-site post arrives without it, and a form stamp `.`. */ export function formStamp(db: Database, c: Context, now: number): string { let cookie = getCookie(c, FORM_COOKIE); if (!cookie || !HEX.test(cookie)) { cookie = randomBytes(16).toString("hex"); setCookie(c, FORM_COOKIE, cookie, { httpOnly: true, secure: true, sameSite: "Strict", path: "/", }); } return `${now}.${mac(db, `form:${now}:${cookie}`)}`; } export type StampCheck = "ok" | "invalid" | "too-fast"; export function checkStamp(db: Database, c: Context, stamp: string, now: number): StampCheck { const cookie = getCookie(c, FORM_COOKIE); const [time, signature] = stamp.split("."); if (!cookie || !time || !signature || !/^\d{1,12}$/.test(time)) return "invalid"; const expected = Buffer.from(mac(db, `form:${time}:${cookie}`)); const given = Buffer.from(signature); if (given.length !== expected.length || !timingSafeEqual(given, expected)) return "invalid"; const age = now - Number(time); if (age > STAMP_MAX_AGE || age < 0) return "invalid"; return age < MIN_SECONDS_ON_PAGE ? "too-fast" : "ok"; }