import { describe, it, expect } from "vitest"; import { NextRequest } from "next/server"; import { middleware } from "./middleware"; import { sealSession } from "@/app/lib/sessionSeal"; // portal-script-nonce-csp (F30): every page response carries a per-request nonce script policy, and the same // policy is passed on the request so Next.js puts the nonce on its own scripts. const PUBLIC_PAGE = "/login"; const PROTECTED_PAGE = "/dashboard"; function get(path: string): NextRequest { return new NextRequest(new URL(path, "https://portal.test"), { method: "GET" }); } function nonceOf(policy: string | null): string | undefined { return policy?.match(/'nonce-([^']+)'/)?.[1]; } describe("middleware: script policy", () => { it("sends a nonce script policy on a page", async () => { const res = await middleware(get(PUBLIC_PAGE)); const policy = res.headers.get("content-security-policy"); expect(policy).toMatch(/script-src 'self' 'nonce-[A-Za-z0-9+/=]+' 'strict-dynamic'/); expect(policy).toContain("frame-ancestors 'self'"); expect(policy).toContain("object-src 'none'"); expect(policy).toContain("base-uri 'self'"); }); it("passes the same policy on the request for Next.js to read the nonce", async () => { const res = await middleware(get(PUBLIC_PAGE)); const forwarded = res.headers.get("x-middleware-request-content-security-policy"); expect(forwarded).toBe(res.headers.get("content-security-policy")); expect(res.headers.get("x-middleware-override-headers")).toContain("content-security-policy"); }); it("uses a new nonce for every request", async () => { const a = nonceOf((await middleware(get(PUBLIC_PAGE))).headers.get("content-security-policy")); const b = nonceOf((await middleware(get(PUBLIC_PAGE))).headers.get("content-security-policy")); expect(a).toBeDefined(); expect(a).not.toBe(b); }); it("adds no script policy to API routes", async () => { const res = await middleware(get("/api/anything")); expect(res.headers.get("content-security-policy")).toBeNull(); }); it("still redirects a signed-out visitor on a protected page", async () => { const res = await middleware(get(PROTECTED_PAGE)); expect(res.status).toBeGreaterThanOrEqual(300); expect(res.status).toBeLessThan(400); expect(res.headers.get("location")).toContain("/login"); }); it("sends the policy on a protected page for a signed-in visitor", async () => { const cookie = "__Host-colite_admin=" + (await sealSession({ token: "jwt", resetPasswordRequired: false, absoluteExpiry: 4102444800 })); const res = await middleware(new NextRequest(new URL("/dashboard", "https://portal.test"), { headers: { cookie } })); expect(res.headers.get("x-middleware-next")).toBe("1"); expect(res.headers.get("content-security-policy")).toMatch(/script-src 'self' 'nonce-[^']+' 'strict-dynamic'/); }); it("still sends a reset-required visitor to the reset page", async () => { const cookie = "__Host-colite_admin=" + (await sealSession({ token: "jwt", resetPasswordRequired: true, absoluteExpiry: 4102444800 })); const res = await middleware(new NextRequest(new URL("/dashboard", "https://portal.test"), { headers: { cookie } })); expect(res.headers.get("location")).toContain("/reset-password"); }); });