// Host / Origin / token checks (timingSafeEqual). // // Security model (after the PR #535 review): // - Host is checked on every HTTP request and WS upgrade and must be 127.0.0.1: or localhost:, // to block DNS rebinding (requests from a rebound page carry no Origin, so only Host identifies them). // - Origin: null (opaque origin) is not trusted. Any website can send Origin: null from a sandboxed iframe, a data: or a file: page, // so null by itself means nothing. The token is the only authorization: // * token routes (/api/*, /attachments/*, WS hello) let a null origin "present" the token, // because Magpie's plugin page is a sandbox="allow-scripts" iframe and can only request as null; // * static routes (/, app.js etc.) always 403 for null and send no CORS headers. // - Any other website's Origin is refused, even with the correct token. import { timingSafeEqual, createHash } from "node:crypto"; export type CheckResult = { ok: true } | { ok: false; code: number; message: string }; // none: no Origin (ordinary navigation,