import { NextResponse, type NextRequest } from "next/server"; import { bodyRefusal, writeRefusal } from "@/lib/report/writeGuard.mjs"; // EVERY API WRITE IS REFUSED CROSS-SITE (umtool redesign E4): a POST/PUT/PATCH/DELETE under /api/ from another // site's page, a sibling site's, or a page whose name is not this machine (DNS rebinding) is answered 403 here, and // its route handler never runs (Next has already buffered the body, up to proxyClientMaxBodySize: next.config.ts). A // request no browser sent (curl, the CLI, an agent, the editor's server) passes, and so does every read. The rule is // in lib/report/writeGuard.mjs, and its test sweeps every route file that exports a write. A write declaring a body // over the cap is answered 413 with its sentence (bodyRefusal), not handed on cut. The matcher keeps the // proxy off pages, static assets and the uploads (EXEMPT, which refuse cross-site themselves). It must stay the // literal GUARD_MATCHER, because a matcher is read at build time and cannot be imported. export function proxy(request: NextRequest) { const refused = writeRefusal(request.method, request.nextUrl.pathname, request.headers); if (refused) return NextResponse.json({ error: refused }, { status: 403, headers: { "cache-control": "no-store" } }); // A body over the cap would reach its route cut at it (next.config.ts proxyClientMaxBodySize): said here, 413. const tooBig = bodyRefusal(request.method, request.nextUrl.pathname, request.headers); if (tooBig) return NextResponse.json({ error: tooBig }, { status: 413, headers: { "cache-control": "no-store" } }); return NextResponse.next(); } export const config = { matcher: "/api/((?!report/voices$|report/post-narration$|report/post-narration/record$).*)", };