SPB Git

Repository Public

proxy.ts 1.6 KB 43 lines typescript

1import { NextResponse, type NextRequest } from "next/server";23// Per-request Content-Security-Policy with a nonce (Next applies the nonce to its own scripts).4// No inline style attribute is allowed: components position/size through CSS classes, SVG attributes or the CSSOM.5export function buildCsp(nonce: string, dev: boolean): string {6  const script = [`'self'`, `'nonce-${nonce}'`, `'strict-dynamic'`];7  if (dev) script.push(`'unsafe-eval'`); // React refresh / Turbopack dev runtime only8  return [9    `default-src 'self'`,10    `script-src ${script.join(" ")}`,11    `style-src 'self' 'nonce-${nonce}'`,12    `img-src 'self' data:`,13    `font-src 'self'`,14    `connect-src 'self'`,15    `object-src 'none'`,16    `frame-ancestors 'none'`,17    `base-uri 'none'`,18    `form-action 'self'`,19    `manifest-src 'self'`,20    `worker-src 'self'`,21  ].join("; ");22}2324export function proxy(request: NextRequest) {25  const bytes = new Uint8Array(18);26  crypto.getRandomValues(bytes);27  const nonce = btoa(String.fromCharCode(...bytes));28  const csp = buildCsp(nonce, process.env.NODE_ENV !== "production");29  const headers = new Headers(request.headers);30  headers.set("x-nonce", nonce);31  headers.set("content-security-policy", csp);32  const response = NextResponse.next({ request: { headers } });33  response.headers.set("content-security-policy", csp);34  response.headers.set("referrer-policy", "no-referrer");35  response.headers.set("x-content-type-options", "nosniff");36  response.headers.set("cache-control", "no-store");37  return response;38}3940export const config = {41  matcher: [{ source: "/((?!_next/static|_next/image|favicon.ico|icon.svg).*)" }],42};43
Files main
Menu

Theme