1import type { Database } from "bun:sqlite";2import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";3import type { Context } from "hono";4import { getCookie, setCookie } from "hono/cookie";5import { setting } from "../server/db.ts";678export const MIN_SECONDS_ON_PAGE = 3;9const STAMP_MAX_AGE = 86400;10export const FILINGS_PER_HOUR = 5;11export const MAX_CHARS = 20_000;12const FORM_COOKIE = "kepr_form";13const HEX = /^[0-9a-f]{32,64}$/;141516function secret(db: Database): string {17 db.query("INSERT OR IGNORE INTO settings (key, value) VALUES ('issue_secret', ?)").run(18 randomBytes(32).toString("hex"),19 );20 return setting(db, "issue_secret") as string;21}2223function mac(db: Database, message: string): string {24 return createHmac("sha256", secret(db)).update(message).digest("hex");25}2627282930313233function clientIp(c: Context): string {34 const forwarded = c.req.header("x-forwarded-for")?.split(",")[0]?.trim();35 if (forwarded) return forwarded;36 37 const server = c.env as { requestIP?: (request: Request) => { address: string } | null };38 return server?.requestIP?.(c.req.raw)?.address ?? "";39}404142export function ipHash(db: Database, c: Context): string {43 return mac(db, `ip:${clientIp(c)}`);44}454647484950export function formStamp(db: Database, c: Context, now: number): string {51 let cookie = getCookie(c, FORM_COOKIE);52 if (!cookie || !HEX.test(cookie)) {53 cookie = randomBytes(16).toString("hex");54 setCookie(c, FORM_COOKIE, cookie, {55 httpOnly: true,56 secure: true,57 sameSite: "Strict",58 path: "/",59 });60 }61 return `${now}.${mac(db, `form:${now}:${cookie}`)}`;62}6364export type StampCheck = "ok" | "invalid" | "too-fast";6566export function checkStamp(db: Database, c: Context, stamp: string, now: number): StampCheck {67 const cookie = getCookie(c, FORM_COOKIE);68 const [time, signature] = stamp.split(".");69 if (!cookie || !time || !signature || !/^\d{1,12}$/.test(time)) return "invalid";70 const expected = Buffer.from(mac(db, `form:${time}:${cookie}`));71 const given = Buffer.from(signature);72 if (given.length !== expected.length || !timingSafeEqual(given, expected)) return "invalid";73 const age = now - Number(time);74 if (age > STAMP_MAX_AGE || age < 0) return "invalid";75 return age < MIN_SECONDS_ON_PAGE ? "too-fast" : "ok";76}