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 { clientIp } from "../server/client.ts"; 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 REPLIES_PER_HOUR = 20; 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"); } /** 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:${limitKey(clientIp(c))}`); } /** * What a limit counts: IPv4 as it stands, IPv6 as its /64. One machine can walk every address in * its prefix, so a limit keyed on the exact address is no limit at all. */ export function limitKey(address: string): string { const bare = address.split("%")[0].toLowerCase(); const mapped = bare.startsWith("::ffff:") ? bare.slice(7) : ""; if (mapped.includes(".")) return mapped; // Two `::` is not an address; anything else is expanded so its first four groups are real. if (!bare.includes(":") || bare.split("::").length > 2) return bare; const [head = "", tail = ""] = bare.split("::"); const left = head === "" ? [] : head.split(":"); const right = tail === "" ? [] : tail.split(":"); const fill = 8 - left.length - right.length; if (fill < 0) return bare; const groups = [...left, ...Array(fill).fill("0"), ...right]; if (!groups.every((group) => /^[0-9a-f]{1,4}$/.test(group))) return bare; // Unpadded, so `0db8` and `db8` are the same prefix, as an address may be written either way. return groups .slice(0, 4) .map((group) => Number.parseInt(group, 16).toString(16)) .join(":"); } /** * A windowed count per key, in memory, with an optional ceiling for the whole site per UTC day. * Entries are pruned by time, not position — a clock that stepped backwards outside this process * (a test's fake timer, an NTP correction) leaves an old entry in the middle, not the front — and * a check reads that key's own handful of entries, never a scan of everything remembered. */ export function attemptLimiter( windowMs: number, perKey: number, perDay = Number.POSITIVE_INFINITY, ) { const seen = new Map(); let day = -1; let today = 0; const roll = (now: number) => { const utcDay = Math.floor(now / 86_400_000); if (utcDay === day) return; day = utcDay; today = 0; }; return { /** Whether the key may try now; nothing is counted until `note`. */ allowed(key: string, now: number): boolean { roll(now); const recent = (seen.get(key) ?? []).filter((at) => at > now - windowMs); if (recent.length === 0) seen.delete(key); else seen.set(key, recent); return recent.length < perKey && today < perDay; }, /** Records an attempt: a caller that only learns it failed does so afterwards. */ note(key: string, now: number): void { roll(now); today++; seen.set(key, [...(seen.get(key) ?? []), now]); }, }; } /** * What a follower's confirm and stop links carry (§23): only their inbox ever sees it. The * address is covered too, so a link whose row is gone cannot reach a later follower that reuses * the id — `issue_followers.id` is a rowid, and SQLite hands a deleted row's id back out. */ export function followToken(db: Database, followerId: number, email: string): string { return mac(db, `follow:${followerId}:${email.toLowerCase()}`); } export function followTokenMatches( db: Database, followerId: number, email: string, token: string, ): boolean { const expected = Buffer.from(followToken(db, followerId, email)); const given = Buffer.from(token); return given.length === expected.length && timingSafeEqual(given, expected); } /** * 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"; }