# How do I add a Content Security Policy to a Next.js app?

By Mark Kashef, founder of Lockpick. Published 2026-11-27, updated 2026-11-27.

Add a Content Security Policy to a Next.js app with a fresh nonce per request, step by step, plus how to check it and what a scan can't tell you.

To add a Content Security Policy to a Next.js app, generate a fresh nonce for every request in a proxy and send it in the Content-Security-Policy header, as long as your pages are dynamically rendered. Static pages can't carry a nonce, because they are generated at build time when no request exists.

## What is a Content Security Policy and why does it matter?

A Content Security Policy (CSP) is a series of instructions from your site to the browser, telling it to restrict what the code on your site is allowed to do. The Next.js docs describe it as a guard against cross-site scripting (XSS), clickjacking and other code injection attacks. With it you can say which origins may supply scripts, stylesheets, images, fonts, objects, media, iframes and more.

A nonce is a unique, random string created for one-time use, and paired with a CSP it lets selected inline scripts or styles run. An attacker who wanted to load a script into your page would need to guess the nonce, which is why it must be unpredictable and unique for every request.

Policies that list allowed domains are hard to get right and often allow unsafe domains by accident, so they don't protect well against XSS. MDN cites one write-up in which a developer was asked to add 187 Google domains to the allowlist just to integrate Google Analytics.

:::callout warning
Don't read a missing Content-Security-Policy header as proof that your site was attacked or exploited. A missing header leaves your app without that guard against cross-site scripting, clickjacking and other code injection attacks, which makes those attacks easier.
:::

## How do I check?

A CSP should arrive in the Content-Security-Policy response header, set on all responses to all requests, not only the main document. This command prints the response headers for the Atlas homepage, so you can see what Atlas sends.

```bash
curl -sI https://atlas.example
```

Run it on another Atlas page too, and run it twice on the same page so you can compare the nonces. Then look for these:

- If you only see Content-Security-Policy-Report-Only, that policy generates reports but is not enforced.
- If the nonce is the same on two loads of the same page, fix it, because a nonce must be unpredictable and unique for every request.
- If script-src includes 'unsafe-eval' on your live site, remove it, because neither React nor Next.js uses eval in production by default.
- If object-src is missing, add object-src 'none' when your app doesn't need embedded objects.
- If you see X-Content-Security-Policy or X-WebKit-CSP, drop them, because OWASP calls their implementations obsolete, limited, inconsistent and buggy.

Lockpick checks your site from the outside, the way a stranger or an attacker would find it. A scan reads your homepage, the files it loads and your security headers. A Pulse sends at most 12 requests to your site. For what the finding means, see the [Content Security Policy check](/checks/csp).

[Check your site](https://www.lockpick.app/check)

## How do I fix it?

Make these changes in order, then check again.

1. **Make a nonce in a proxy.** In Next.js, a proxy can add headers and generate nonces before the page renders. Use a random value, never a counter or a fixed string.
2. **Put the nonce in two headers.** Your proxy adds the nonce to the Content-Security-Policy header and also sets it in a custom x-nonce header.
3. **Let Next.js attach it.** During rendering, Next.js parses the Content-Security-Policy header and extracts the nonce using the 'nonce-{value}' pattern. It then applies the nonce automatically.
4. **Render pages dynamically.** When you use nonces in your CSP, all pages must be dynamically rendered. Partial Prerendering (PPR) doesn't work with a nonce-based CSP, since static shell scripts won't have access to the nonce.
5. **Keep 'unsafe-eval' in development only.** React needs 'unsafe-eval' in development to give better debugging information, but it is not required for production.
6. **Rewrite what a nonce can't cover.** Nonces let you keep inline script tags, but you still need to remove inline event handlers, javascript: URLs and uses of eval(). Inline event handlers should usually be replaced with calls to addEventListener().
7. **Try it in report-only mode first.** Browsers support sending Content-Security-Policy and Content-Security-Policy-Report-Only together, so you can run a strict report-only policy while a looser policy stays enforced. MDN recommends reporting violations through the Reporting API, with endpoints declared in Reporting-Endpoints and named in the policy's report-to directive.
8. **Switch to enforcing and check again.** Once the report-only policy stops flagging things you need, send it as Content-Security-Policy and run the curl command again.

On Atlas, the enforced header looks like this once the proxy is in place, with a new nonce on every response.

```http
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-NewValueOnEveryRequest'; object-src 'none'; frame-ancestors 'none'; form-action 'self'
```

If your pages need to stay static, Next.js offers experimental support for a hash-based CSP using Subresource Integrity, which keeps static generation while still giving you a strict CSP. Hash-based policies suit static pages because the hashes stay the same, so both the policy and the content can be static. If your app doesn't need nonces at all, you can set the CSP header directly in next.config.js.

### Fix it with your AI

Paste the prompt for your builder. Each one sets up the same policy as the Atlas example above.

:::fix-prompt v0
Add a nonce-based Content Security Policy to this Next.js app.

1. In the Next.js proxy, create a new unpredictable random nonce for every request.
2. Build this policy with the nonce: default-src 'self'; script-src 'self' 'nonce-{nonce}'; object-src 'none'; frame-ancestors 'none'; form-action 'self'. Add 'unsafe-eval' to script-src only when NODE_ENV is development.
3. Set the policy in the Content-Security-Policy header and put the nonce in an x-nonce header, so Next.js can read it during rendering.
4. Make every page that needs the nonce render dynamically, and turn off Partial Prerendering for those pages.
5. Replace inline event handlers and javascript: URLs with addEventListener calls.
6. Send the policy as Content-Security-Policy-Report-Only first, list every violation it reports, fix them, then switch the header to Content-Security-Policy.

Never add * or 'unsafe-eval' to the production policy.
:::

:::fix-prompt cursor
Add a nonce-based Content Security Policy to this Next.js app. Show me the plan before editing files.

1. In the Next.js proxy, generate a new unpredictable random nonce on every request.
2. Build the policy default-src 'self'; script-src 'self' 'nonce-{nonce}'; object-src 'none'; frame-ancestors 'none'; form-action 'self'. Include 'unsafe-eval' only when NODE_ENV is development.
3. Set it in the Content-Security-Policy header and set the nonce in an x-nonce header.
4. Find every page that is statically generated or uses Partial Prerendering and tell me which ones must become dynamic for the nonce to work. If a page must stay static, propose the hash-based Subresource Integrity option instead.
5. Search the codebase for inline event handlers, javascript: URLs and eval() and replace them.
6. Ship the policy as Content-Security-Policy-Report-Only first, then switch to Content-Security-Policy once the reports are clean.

Do not use * or 'unsafe-eval' in the production policy.
:::

:::fix-prompt claude-code
Add a nonce-based Content Security Policy to this Next.js app.

1. In the Next.js proxy, generate a new unpredictable random nonce for every request.
2. Send Content-Security-Policy with default-src 'self'; script-src 'self' 'nonce-{nonce}'; object-src 'none'; frame-ancestors 'none'; form-action 'self'. Add 'unsafe-eval' to script-src only in development.
3. Put the same nonce in an x-nonce header so Next.js can apply it during rendering.
4. Make the pages that need the nonce render dynamically and do not use Partial Prerendering on them.
5. Replace inline event handlers, javascript: URLs and eval() calls.
6. Start in Content-Security-Policy-Report-Only mode, fix what it reports, then enforce it.
7. Start the production build locally, request the same page twice with curl -sI and confirm the header is present and the nonce changes each time.

Never put * or 'unsafe-eval' in the production policy.
:::

## What a check cannot tell you

A Lockpick report is not a certificate and not a full penetration test. It covers what that scan could see on that day. Anything a scan couldn't check is marked as not checked, never as passed.

Lockpick never signs in to your site and never looks at anything behind a login. If your dashboard renders differently from your homepage, look at its response headers yourself in your browser's developer tools.

Lockpick also never sends attack payloads. Read a CSP finding as a description of the policy your site sends, and review the domains it allows yourself, since allowlist policies often allow unsafe domains by accident.

## Questions people ask

### Does Next.js need 'unsafe-eval' in the CSP?

Keep it out of your production policy. In development, 'unsafe-eval' is required because React uses eval to give better debugging information, such as rebuilding server-side error stacks in the browser. Neither React nor Next.js uses eval in production by default.

### Can I use a nonce with statically generated pages?

No. Next.js applies nonces during server-side rendering, and static pages are generated at build time when no request or response headers exist, so no nonce can be injected. For static pages, Next.js offers experimental hash-based CSP support using Subresource Integrity.

### Can I set the CSP in a meta tag instead of a header?

OWASP lists three ways to deliver a policy, and a meta tag is one of them. It's meant for cases where you can't use the header, such as HTML files deployed on a CDN where the headers are out of your control. If you can set headers, send the Content-Security-Policy response header.

### Does a missing CSP mean my site was hacked?

No. Don't read a missing header as proof that anyone attacked or exploited your site. It leaves out a guard the Next.js docs describe against cross-site scripting, clickjacking and other code injection attacks, so those attacks get easier.

### How do I test a strict policy without breaking my app?

Send it as Content-Security-Policy-Report-Only first. That policy generates reports but is not enforced. Browsers support using it alongside an enforced Content-Security-Policy, so a strict report-only policy can run next to a looser enforced one.

## Sources

- [Content Security Policy - OWASP Cheat Sheet Series](https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html), OWASP Cheat Sheet Series, read on 2026-10-16
- [Content Security Policy (CSP) - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP), MDN Web Docs, read on 2026-10-16
- [How to set a Content Security Policy (CSP) for your Next.js application](https://nextjs.org/docs/app/guides/content-security-policy), Next.js docs, read on 2026-10-16
- [How we check · Docs · Lockpick](https://docs.lockpick.app/security/how-we-check), Lockpick docs, read on 2026-11-04
- [What we never do · Docs · Lockpick](https://docs.lockpick.app/security/what-we-never-do), Lockpick docs, read on 2026-10-29
