/** * oracle — the door. The security floor, the body caps and the table of * paths live here; the mind is in `oracle.ts`, the ledger in `db.ts` and * every page in `pages.ts`. * * The ledger is a SQLite file under FORGE_APP_DATA. Production and each * preview get their own directory, so a preview starts with a copy of * production's verdicts and never writes back to them. */ import { join } from "node:path"; import * as ledger from "./db.ts"; import { isOutcome } from "./oracle.ts"; import { homePage, ledgerPage, notFoundPage, verdictPage } from "./pages.ts"; const db = ledger.open(); const chrome = { env: process.env.FORGE_APP_ENV ?? "local", pull: process.env.FORGE_APP_PULL, commit: (process.env.FORGE_APP_COMMIT ?? "").slice(0, 7), ssh: process.env.FORGE_APP_SSH, }; // The security floor every response stands on. Scripts and styles come from // this origin (inline styles allowed, inline scripts not), nothing frames us, // browsers never guess a content type, and referrers stay on this site. // The app's public address is what the forge told us, never the socket's. const publicOrigin = new URL(process.env.FORGE_APP_URL ?? "http://localhost").origin; const SECURITY = { "content-security-policy": "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; frame-ancestors 'none'; base-uri 'none'; form-action 'self'", "x-content-type-options": "nosniff", "referrer-policy": "strict-origin-when-cross-origin", "permissions-policy": "camera=(), microphone=(), geolocation=()", // Once served over https, always: browsers remember for a year. ...(publicOrigin.startsWith("https:") ? { "strict-transport-security": "max-age=31536000" } : {}), }; const html = (body: string, status = 200): Response => new Response(body, { status, headers: { "content-type": "text/html; charset=utf-8", ...SECURITY } }); // A browser on another site can make a person's browser POST here; it cannot // make that request look same-origin. Browsers say where a request came from // (Sec-Fetch-Site, or Origin); a request that names another site is refused. const sameOrigin = (request: Request): boolean => { const site = request.headers.get("sec-fetch-site"); if (site !== null) return site === "same-origin" || site === "none"; const origin = request.headers.get("origin"); return origin === null || origin === publicOrigin; }; // A request body is read up to a cap and no further, counting the bytes as // they arrive. A declared Content-Length is a claim: a chunked request has // none, and a lie is free. Undefined means "too large" — refuse it. Past the // cap the rest is drained and dropped so the connection stays usable for // the next request; past eight times the cap it is cut off instead. const BODY_MAX = 64 * 1024; async function readBody(request: Request, max = BODY_MAX): Promise { const reader = request.body?.getReader(); if (reader === undefined) return new Uint8Array(); const parts: Uint8Array[] = []; let size = 0; for (;;) { const { done, value } = await reader.read(); if (done) break; size += value.byteLength; if (size > max) { for (;;) { const next = await reader.read(); if (next.done) return undefined; size += next.value.byteLength; if (size > max * 8) { await reader.cancel(); return undefined; } } } parts.push(value); } const body = new Uint8Array(size); let at = 0; for (const part of parts) { body.set(part, at); at += part.byteLength; } return body; } // The form fields of a capped body, decoded the way the browser sent them // (a plain form or a multipart one), by handing the bytes back to a Request. async function readForm(request: Request): Promise { const body = await readBody(request); if (body === undefined) return undefined; return new Request(request.url, { method: "POST", headers: { "content-type": request.headers.get("content-type") ?? "" }, body, }).formData(); } export const server = Bun.serve({ port: Number(process.env.PORT ?? 8080), hostname: process.env.HOST ?? "127.0.0.1", async fetch(request) { const url = new URL(request.url); if (request.method === "POST" && !sameOrigin(request)) return new Response("forbidden", { status: 403 }); // Ask. An empty question is not a question; the same question is not a // new one, which `ledger.ask` enforces in one statement. if (request.method === "POST" && url.pathname === "/ask") { const form = await readForm(request); if (form === undefined) return new Response("too large", { status: 413, headers: SECURITY }); const question = String(form.get("question") ?? "").trim(); if (question === "") return Response.redirect("/", 303); const row = ledger.ask(db, question); return Response.redirect(`/v/${row.id}`, 303); } // Score a verdict. Only the three words count; anything else is a // client inventing states for the ledger to hold. const judging = url.pathname.match(/^\/v\/(\d{1,15})\/judge$/); if (request.method === "POST" && judging) { const form = await readForm(request); if (form === undefined) return new Response("too large", { status: 413, headers: SECURITY }); const outcome = String(form.get("outcome") ?? ""); const id = Number(judging[1]); if (!isOutcome(outcome) || ledger.get(db, id) === null) return html(notFoundPage(chrome), 404); ledger.judge(db, id, outcome); return Response.redirect(`/v/${id}`, 303); } const reading = request.method === "GET" || request.method === "HEAD"; if (reading && url.pathname === "/") { const full = html(homePage(chrome, ledger.recent(db), ledger.scored(db))); return request.method === "HEAD" ? new Response(null, { status: full.status, headers: full.headers }) : full; } const permalink = url.pathname.match(/^\/v\/(\d{1,15})$/); if (reading && permalink) { const row = ledger.get(db, Number(permalink[1])); return row === null ? html(notFoundPage(chrome), 404) : html(verdictPage(chrome, row)); } if (reading && url.pathname === "/ledger") { const show = url.searchParams.get("show") ?? "all"; const filter = ledger.isFilter(show) ? show : "all"; return html(ledgerPage(chrome, ledger.ledger(db, filter), filter)); } // Files under ./static are served as they are, cached for a year: name // them by content (app.3f2a9c.js) and a change is a new name, never a // stale cache. The path is confined to the directory. if (reading && url.pathname.startsWith("/static/") && !url.pathname.includes("..")) { const file = Bun.file(join(import.meta.dir, "..", url.pathname.slice(1))); if (await file.exists()) return new Response(file, { headers: { "cache-control": "public, max-age=31536000, immutable", ...SECURITY }, }); } return html(notFoundPage(chrome), 404); }, // A bug answers with the floor and nothing else: never the runtime's own // error page, which carries source and paths, and never without the // headers. The detail goes to the log, where it belongs. error(error) { console.error(error); return html("

Something went wrong

", 500); }, }); console.log( `the oracle is listening on http://${server.hostname}:${server.port} (${chrome.env})`, );