import { describe, expect, it } from "vitest"; import { readFileSync, readdirSync, statSync } from "node:fs"; import { join, relative } from "node:path"; /** * F20 (security assessment): admin-portal API routes are served from the origin that holds the admin session. A route * that streams an upstream body must never echo the upstream Content-Type unchecked (a stored text/html or SVG would * render as script on this origin) and must forbid MIME sniffing. Every route that reads an upstream content-type has * to allow-list it (safeContentType or an explicit type set) and send X-Content-Type-Options: nosniff. */ const API_ROOT = join(__dirname, "..", "..", "api"); function routeFiles(dir: string): string[] { return readdirSync(dir).flatMap((name) => { const path = join(dir, name); if (statSync(path).isDirectory()) return routeFiles(path); return name === "route.ts" ? [path] : []; }); } describe("binary proxy routes", () => { const readsUpstreamType = routeFiles(API_ROOT).filter((f) => /\.headers\.get\(\s*["']content-type["']\s*\)/i.test(readFileSync(f, "utf8")), ); it("finds the proxy routes (the sweep is not vacuous)", () => { expect(readsUpstreamType.length).toBeGreaterThanOrEqual(5); }); it.each(readsUpstreamType.map((f) => [relative(API_ROOT, f), f]))("%s allow-lists the type and forbids sniffing", (_name, file) => { const src = readFileSync(file, "utf8"); expect(src, "echoes the upstream content-type unchecked").not.toMatch( /["']Content-Type["']\s*:\s*\w+\.headers\.get\(\s*["']content-type["']/i, ); expect(src).toMatch(/["']X-Content-Type-Options["']\s*:\s*["']nosniff["']|SAFE_BINARY_HEADERS/); }); });