Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
68 lines
3.2 KiB
TypeScript
68 lines
3.2 KiB
TypeScript
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-bb_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-bb_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");
|
|
});
|
|
});
|