// HTTP routes: / → app/index.html (no token), static assets, /api/ping, /api/upload, /attachments/* import { createServer, type IncomingMessage, type ServerResponse, type Server } from "node:http"; import { readFile, stat } from "node:fs/promises"; import { existsSync } from "node:fs"; import { join, normalize, extname } from "node:path"; import { checkHost, classifyOrigin, tokenEqual } from "./security.js"; import type { Config } from "./config.js"; import type { AttachmentStore } from "./store/attachments.js"; import { log } from "./log.js"; import { tr } from "./util/i18n.js"; const MIME: Record = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".svg": "image/svg+xml", ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp", ".woff2": "font/woff2", ".map": "application/json", ".json": "application/json; charset=utf-8", }; const MAX_UPLOAD = 50 * 1024 * 1024; // 50MB const MAX_MSG = 200 * 1024; // Ordinary messages: 200KB export type HttpHooks = { onUpgrade?: (req: IncomingMessage, socket: import("node:net").Socket, head: Buffer) => void; }; // A malformed %xx must not become a 500 function safeDecode(s: string): string | null { try { return decodeURIComponent(s); } catch { return null; } } function send(res: ServerResponse, code: number, body: string | Buffer, headers: Record = {}): void { res.writeHead(code, headers); res.end(body); } function sendJson(res: ServerResponse, code: number, obj: unknown, headers: Record = {}): void { send(res, code, JSON.stringify(obj), { "content-type": "application/json; charset=utf-8", "cache-control": "no-store", ...headers }); } // Common headers for static responses: no caching (the page has no token, but don't let old versions linger), no sniffing, no Referer const STATIC_HEADERS: Record = { "cache-control": "no-store", "x-content-type-options": "nosniff", "referrer-policy": "no-referrer", }; export function createHttpServer(cfg: Config, appDir: string, attachments: AttachmentStore, hooks: HttpHooks = {}): Server { const server = createServer(async (req, res) => { try { await route(req, res); } catch (e) { log("error", "HTTP 处理异常", { url: req.url, err: String(e) }); if (!res.headersSent) sendJson(res, 500, { ok: false, e: { code: "internal", message: tr("http.internal") } }); else res.end(); } }); // Origin policy per request (see classifyOrigin in security.ts): // foreign (another site) → always 403, even with a token // opaque (Origin: null) → may only "present" the token on token routes (/api/*, /attachments/*); // it confers no trust by itself. Static routes are always refused. // none / self → static routes allowed (static files contain no token); token routes still need the token. // The CORS header is only given (as null) on responses that carry a valid token + Origin: null; preflight only for /api/upload. async function route(req: IncomingMessage, res: ServerResponse): Promise { // Host must be loopback + this process's actual port: blocks DNS rebinding (a rebound page's GET carries no Origin) const hostCheck = checkHost(req.headers.host, cfg.port); if (!hostCheck.ok) { sendJson(res, hostCheck.code, { ok: false, e: { code: "bad_host", message: hostCheck.message } }); return; } const origin = classifyOrigin(req.headers.origin, cfg.port); if (origin === "foreign") { sendJson(res, 403, { ok: false, e: { code: "bad_origin", message: tr("http.badOrigin") } }); return; } const url = new URL(req.url ?? "/", `http://127.0.0.1:${cfg.port}`); const pathname = url.pathname; const isApi = pathname.startsWith("/api/"); const isAttachment = pathname.startsWith("/attachments/"); // OPTIONS preflight: browsers don't send custom headers on a preflight, so there is no token here. // Only allow /api/upload, the one cross-origin POST the frontend really makes, and only for null / our own origin; // the preflight itself returns no data, and the real request still has to pass the token check. if (req.method === "OPTIONS") { if (pathname !== "/api/upload" || origin === "none") { sendJson(res, 403, { ok: false, e: { code: "bad_preflight", message: tr("http.badPreflight") } }); return; } send(res, 204, "", { "access-control-allow-origin": origin === "opaque" ? "null" : `http://127.0.0.1:${cfg.port}`, "access-control-allow-headers": "x-agent-console, content-type", "access-control-allow-methods": "GET, POST, OPTIONS", "access-control-max-age": "600", vary: "origin", }); return; } // Token routes: the token is the only authorization if (isApi || isAttachment) { const headerToken = req.headers["x-agent-console"]; // Attachments accept ?t= ( can't send custom headers); the API only accepts the header const queryToken = isAttachment ? url.searchParams.get("t") : null; const validToken = (typeof headerToken === "string" && tokenEqual(headerToken, cfg.token)) || (typeof queryToken === "string" && tokenEqual(queryToken, cfg.token)); if (!validToken) { sendJson(res, 403, { ok: false, e: { code: "bad_token", message: tr("http.badToken") } }); return; } // In a sandbox iframe (no allow-same-origin) the page's origin is null, so even a same-address fetch is cross-origin: // the response needs allow-origin: null to be readable. It's only sent after the token is verified, // so any Origin: null page without the token can read nothing. if (origin === "opaque") { res.setHeader("access-control-allow-origin", "null"); res.setHeader("vary", "origin"); } if (isApi) { await routeApi(req, res, pathname, url); return; } const id = safeDecode(pathname.slice("/attachments/".length)); const a = id === null ? null : await attachments.get(id); if (!a) { sendJson(res, 404, { ok: false, e: { code: "not_found", message: tr("http.noAttachment") } }); return; } const data = await attachments.read(a.id); send(res, 200, data ?? Buffer.alloc(0), { ...STATIC_HEADERS, "content-type": a.mime }); return; } // Static assets: refuse Origin: null. Ordinary navigations and