How this portfolio scores A+ on securityheaders.com
Moving from an A to an A+ meant removing 'unsafe-inline' from the Content-Security-Policy. Here is what that took in Next.js 16 — and the mistake I made along the way.
- Next.js
- CSP
- Web security
A portfolio for security work should be secure itself. When I first scanned this site on securityheaders.com it already scored an A, but the grade was capped with one warning: the Content-Security-Policy allowed 'unsafe-inline' in script-src. This post walks through how I removed it.
Step 1: the easy headers
Most security headers are static, so they live in next.config.ts and apply to every route. HSTS forces HTTPS, X-Frame-Options blocks clickjacking, nosniff stops MIME sniffing, and Permissions-Policy switches off features the site never needs.
const securityHeaders = [
{ key: "Strict-Transport-Security", value: "max-age=63072000; includeSubDomains; preload" },
{ key: "X-Content-Type-Options", value: "nosniff" },
{ key: "X-Frame-Options", value: "DENY" },
{ key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
{ key: "Permissions-Policy", value: "camera=(), microphone=(), geolocation=()" },
{ key: "Cross-Origin-Opener-Policy", value: "same-origin" },
{ key: "Cross-Origin-Resource-Policy", value: "same-origin" },
];That alone is worth an A. The last step is the hard one: a Content-Security-Policy that doesn't trust inline scripts.
Step 2: why 'unsafe-inline' was there
Next.js injects small inline scripts to hydrate the page. Without a nonce or hash, the only way to let them run is 'unsafe-inline' — which also lets any injected script run, defeating much of the point of a CSP.
Step 3: a nonce per request
A nonce is a random value generated for every response. Only scripts carrying it are allowed to run, so an attacker who injects markup can't guess it. In Next.js 16 the place to create it is proxy.ts (formerly middleware): generate the nonce, put it in the CSP header, and Next.js reads it back and stamps it on its own scripts.
export function proxy(request: NextRequest) {
const nonce = Buffer.from(crypto.randomUUID()).toString("base64");
const csp = [
"default-src 'self'",
`script-src 'self' 'nonce-${nonce}' 'strict-dynamic'`,
"object-src 'none'",
"base-uri 'self'",
"frame-ancestors 'none'",
].join("; ");
const headers = new Headers(request.headers);
headers.set("Content-Security-Policy", csp);
const response = NextResponse.next({ request: { headers } });
response.headers.set("Content-Security-Policy", csp);
return response;
}'strict-dynamic' lets scripts that the trusted ones load — like the analytics script — run too, without having to list every source.
The trade-offs
- Pages have to render per request, because a static page built ahead of time can't contain a fresh nonce. For a portfolio, that costs a fraction of a second.
- style-src still allows 'unsafe-inline', because animation libraries write inline style attributes that nonces can't cover. Scanners only grade script-src, and inline styles are far less dangerous than inline scripts.
- The Open Graph image gets Cross-Origin-Resource-Policy: cross-origin, so LinkedIn and WhatsApp can still load link previews.
The mistake I made
To show the protection working, I first printed this visit's nonce on the home page. An automated security review flagged it straight away: browsers deliberately hide nonce values from the page, and printing one as text undoes that. The fix was to show a short SHA-256 fingerprint instead — it still changes on every visit, but the real value is never exposed.
Lesson: proving a control works must never weaken the control. Review your own showcases like you would a client's code.
Result
The site now scores A+ with no capped-grade warning. You can check it yourself on securityheaders.com — and if you want the same for your product, that's exactly the kind of work I take on.
Want this for your product?
I harden web apps and set up security headers, CSP and monitoring — as a freelance project or on your team.