Content security policy
What a new app's response headers already refuse, the opt-in report-only policy for scripts, images and frames, and the hosts to add before enforcing it.
A new app refuses framing, plugins and a foreign <base> on every response. It says nothing about which scripts, styles, images or frames a page may load, because that depends on your analytics, sign-in providers and embeds. contentSecurityPolicy writes that part as a report-only header, so you can see what it would block before it blocks anything.
What the scaffold sends
headers() in next.config.mjs sends these on every response:
| Header | Value |
|---|---|
Content-Security-Policy |
frame-ancestors 'none'; object-src 'none'; base-uri 'self' |
Permissions-Policy |
camera=(), geolocation=(), payment=(), usb=(), microphone=(self) |
Cross-Origin-Opener-Policy |
same-origin-allow-popups |
X-Frame-Options |
DENY |
X-Content-Type-Options |
nosniff |
Referrer-Policy |
strict-origin-when-cross-origin |
Strict-Transport-Security |
max-age=63072000; includeSubDomains |
The microphone stays on for the page itself because the composer’s voice input uses it. Add any other feature a page of yours uses to the Permissions-Policy.
Turn on the report-only policy
contentSecurityPolicy from @intelligo-dev/next/csp returns one { key, value } pair, the shape headers() takes. Its key is Content-Security-Policy-Report-Only: the browser reports each violation in its console, and to reportUri when you give one, and blocks nothing.
import { contentSecurityPolicy } from "@intelligo-dev/next/csp";
const reported = contentSecurityPolicy({
development: process.env.NODE_ENV !== "production",
img: ["https://lh3.googleusercontent.com"],
reportUri: "/api/csp-report",
});
// In headers(), beside the headers the scaffold already sends:
// headers: [reported, { key: "Content-Security-Policy", value: "…" }, …]With no options the policy lets a page load from the app itself and nothing else:
| Directive | Allows |
|---|---|
default-src |
The app |
script-src |
The app and inline scripts, which Next.js hydrates with |
style-src |
The app and inline styles, which the components write |
img-src, media-src |
The app, data: and blob: |
font-src |
The app and data: |
connect-src |
The app: the chat stream and every action are same-origin |
frame-src |
The app |
worker-src |
The app and blob: |
form-action |
The app, and Stripe’s checkout and billing portal |
object-src, base-uri, frame-ancestors |
Nothing, the app, and nothing |
Hosts to add
The framework’s own pages load nothing from another host. What you configure does:
| You use | Add |
|---|---|
| Sign-in with Google or GitHub | img: the provider’s avatar host, such as https://lh3.googleusercontent.com or https://avatars.githubusercontent.com |
| Stored attachments with signed URLs | img: your bucket’s host |
imageHosts in lib/chat-config.tsx |
img: the same hosts |
| An analytics tag | script and connect: its hosts |
| A local payment provider’s hosted page | formAction: its host |
| An embedded video or widget | frame: its host |
A source is a scheme (https:) or a host with its scheme, with an optional wildcard label, port and path. Anything else throws when the config loads, so a typo cannot end a directive early.
Before you enforce it
Run with the report-only header until a normal session reports nothing, then send the same value under Content-Security-Policy, joined to the three directives the scaffold already has.
Two things to check first:
- Inline scripts. Without a nonce the policy allows every inline script, which is what an injected script would be. Passing
noncereplaces that with'nonce-…' 'strict-dynamic'. A nonce is per request, so it is set inproxy.ts, every page becomes dynamically rendered, and Next.js must see the header on the request to stamp its own scripts: follow the Next.js content security policy guide for that wiring. - The HTML preview. An artifact’s HTML or SVG preview is a sandboxed
srcdocframe, and asrcdocframe inherits the policy of the page around it. Under an enforced nonce policy the preview’s own inline scripts carry no nonce and do not run. Open a preview while the policy is still report-only and read what it reports before you enforce a nonce.