import { describe, it, expect } from "vitest"; import { readdirSync, readFileSync } from "node:fs"; import { join, relative, resolve } from "node:path"; // portal-query-allowlist (F38): no BFF route may forward the client's raw query string, or interpolate a raw // query value, into a mobile-api path. Routes build their upstream query with upstreamQuery() (allow-listed, // validated, encoded) and encode any query value they put into a path. const PORTAL_ROOT = resolve(__dirname, "../../.."); const API_DIR = join(PORTAL_ROOT, "app/api"); /** * Routes left unconverted because an existing test pins the old raw forwarding. Never add to this list to get * a new route past the sweep; remove an entry the moment its route is converted (the test fails if a listed * route is clean). */ const KNOWN_CONFLICTS: readonly string[] = []; function routeFiles(dir: string): string[] { return readdirSync(dir, { withFileTypes: true }).flatMap(e => { const p = join(dir, e.name); if (e.isDirectory()) return routeFiles(p); return e.name === "route.ts" ? [p] : []; }); } const escape = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); /** The reasons a route source forwards a raw query, or [] when it does not. */ function rawQueryOffences(src: string): string[] { const out: string[] = []; if (/\bsearchParams\.toString\(\)/.test(src)) out.push("searchParams.toString()"); if (/\bnextUrl\.search\b/.test(src)) out.push("nextUrl.search"); if (/\bsearchParams\.entries\(\)/.test(src)) out.push("searchParams.entries()"); if (/\$\{\s*[^}]*searchParams\.get\(/.test(src)) out.push("${searchParams.get(...)} interpolated"); // Locals bound to the request's searchParams: `const x = new URL(req.url).searchParams` / `req.nextUrl.searchParams`. const bound = [...src.matchAll(/const\s+(\w+)\s*=\s*[^;\n]*\.searchParams\s*;/g)].map(m => m[1]); for (const name of bound) { const n = escape(name); if (new RegExp(`\\b${n}\\.toString\\(\\)`).test(src)) out.push(`${name}.toString()`); if (new RegExp(`\\b${n}\\.entries\\(\\)`).test(src)) out.push(`${name}.entries()`); if (new RegExp(`\\bof\\s+${n}\\b`).test(src)) out.push(`for…of ${name}`); if (new RegExp(`\\$\\{\\s*${n}\\.get\\(`).test(src)) out.push(`\${${name}.get(...)} interpolated`); } // Locals read from a query param and then interpolated bare (not through encodeURIComponent) into a template. const fromQuery = [...src.matchAll(/const\s+(\w+)\s*=\s*[^;\n]*\.get\(\s*"[^"]+"\s*\)[^;\n]*;/g)].map(m => m[1]); for (const name of fromQuery) { if (new RegExp(`\\$\\{\\s*${escape(name)}\\s*\\}`).test(src)) out.push(`\${${name}} interpolated raw`); } return out; } describe("BFF routes never forward a raw query to mobile-api (F38)", () => { const files = routeFiles(API_DIR).map(f => ({ rel: relative(PORTAL_ROOT, f), src: readFileSync(f, "utf8") })); it("finds the route files", () => { expect(files.length).toBeGreaterThan(10); }); it("has no route outside KNOWN_CONFLICTS that forwards a raw query", () => { const offenders = files .filter(f => !KNOWN_CONFLICTS.includes(f.rel)) .map(f => ({ file: f.rel, why: rawQueryOffences(f.src) })) .filter(o => o.why.length > 0) .map(o => `${o.file}: ${o.why.join(", ")}`); expect(offenders).toEqual([]); }); it("lists no KNOWN_CONFLICTS route that is already clean", () => { const clean = KNOWN_CONFLICTS.filter(rel => { const f = files.find(x => x.rel === rel); return !f || rawQueryOffences(f.src).length === 0; }); expect(clean).toEqual([]); }); }); describe("rawQueryOffences", () => { it("flags each raw-forwarding shape", () => { expect(rawQueryOffences("const qs = new URL(req.url).searchParams.toString();")).not.toEqual([]); expect(rawQueryOffences("const s = new URL(req.url).searchParams;\ncall(`/x?${s.toString()}`);")).not.toEqual([]); expect(rawQueryOffences("const s = new URL(req.url).searchParams;\ncall(`/x?page=${s.get(\"page\")}`);")).not.toEqual([]); expect(rawQueryOffences("const level = new URL(req.url).searchParams.get(\"level\") ?? \"99\";\ncall(`/x?level=${level}`);")).not.toEqual([]); expect(rawQueryOffences("call(`/x${req.nextUrl.search}`);")).not.toEqual([]); }); it("accepts upstreamQuery and encoded values", () => { expect(rawQueryOffences("const s = new URL(req.url).searchParams;\ncall(`/x${upstreamQuery(s, RULES)}`);")).toEqual([]); expect(rawQueryOffences("const id = new URL(req.url).searchParams.get(\"id\");\ncall(`/x?id=${encodeURIComponent(id)}`);")).toEqual([]); }); });