Content Security Policy (CSP) Editor & Builder
A Content Security Policy is the one browser-native control that decides where your page may load scripts, styles, fonts and network requests from. This builder turns that policy into checkboxes and shows the exact header to copy, so you never have to guess the directive grammar again.
Last reviewed: 2026-11-26
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests;
Aucun problème détecté dans la politique actuelle.
What a Content Security Policy actually does
CSP is an HTTP response header. The browser reads it before the page starts executing and cancels any request that does not match the declared source lists. A script tag pointing at a domain you never allowed simply never runs, even if an attacker managed to inject it into your HTML.
That is why CSP is described as a second line of defence against XSS rather than a replacement for escaping output. It does not sanitise anything. It removes the payoff of a successful injection by refusing to execute the payload. The same header also closes clickjacking through frame-ancestors, base-tag hijacking through base-uri, and legacy plugin injection through object-src.
A policy is a list of directives separated by semicolons, and each directive ends with a source list. When a directive is missing, the browser falls back to default-src. That single fallback is why default-src 'self' already blocks most third-party loads without you naming a single host.
Building a policy in four steps
- Start from default-src 'self'. Everything that is not explicitly allowed stays blocked, which is the direction you want to fail in.
- Open DevTools, switch to the Console, and reload the real page. Every blocked resource prints a violation message containing the exact URL that needs to be allowed.
- Allow only the origins the console complains about, one directive at a time. Do not paste a list you found online: your CDN, analytics and font domains are specific to your stack.
- Ship the policy as Content-Security-Policy-Report-Only for a week, review the collected reports, then switch to the enforcing header and copy the final value from the output panel.
Directives that carry the most weight
Most of the security benefit comes from a handful of directives. The rest are refinements. The table below lists the ones worth reviewing before you ship.
| Directive | What it controls | Safer starting point |
|---|---|---|
| default-src | Fallback for every fetch directive that is not listed separately | 'self' |
| script-src | Executable JavaScript, including inline blocks, eval and workers | 'self' plus nonces |
| style-src | Stylesheets and the style attribute | 'self' |
| img-src | Images, favicons and CSS background images | 'self' data: |
| connect-src | fetch(), XMLHttpRequest, WebSocket and EventSource targets | 'self' |
| font-src | Web fonts loaded through @font-face | 'self' |
| frame-ancestors | Which pages may embed this page in a frame (clickjacking) | 'none' |
| base-uri | What the base tag may point at (relative-URL hijacking) | 'none' |
| form-action | Where form submissions may be sent | 'self' |
| object-src | Legacy embedders such as Flash, Java and PDF plugins | 'none' |
- Three directives are ignored when the policy arrives in a meta tag: frame-ancestors, report-uri and sandbox. If clickjacking protection matters, the header has to come from the server.
- The report-to directive is the CSP3 replacement for report-uri, but browser support is still uneven. Sending both during a rollout is the safe option.
Strict versus balanced: picking a preset
The strict preset allows no inline code and sets object-src, base-uri and frame-ancestors to their locked-down values. Aim for this shape on any application where you control every line of JavaScript that ships.
The balanced preset keeps 'unsafe-inline' in style-src and adds the data: and blob: sources that most modern pages need. It is the pragmatic choice when a CMS, a third-party widget or a CSS-in-JS runtime injects styles you cannot annotate with a nonce.
- Never put 'unsafe-inline' in script-src. It disables nonce and hash handling for that directive, so the protection you were trying to gain silently disappears.
- Add 'strict-dynamic' only together with a nonce or a hash. It tells the browser to trust scripts loaded by your own trusted scripts, which is what bundlers with dynamic imports need.
- If you are tempted by 'unsafe-eval', look for new Function(), string-based setTimeout, or a framework build that compiles templates at runtime.
- A policy that is never enforced still has value as documentation, but do not mistake Report-Only for protection.
Shipping the header in production
The header value is always the same; the place you paste it depends on where the response is generated. Copy the string from the output panel and put it in the layer that owns your HTTP headers. If several layers add the header, the browser receives multiple policies and enforces all of them, which is rarely what you want.
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; object-src 'none'; base-uri 'none'; frame-ancestors 'none'" always;const csp =
"default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'";
export default {
async headers() {
return [
{
source: "/:path*",
headers: [{ key: "Content-Security-Policy", value: csp }],
},
];
},
};/*
Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'
X-Content-Type-Options: nosniffapp.use(
helmet({
contentSecurityPolicy: {
directives: {
"default-src": ["'self'"],
"object-src": ["'none'"],
"base-uri": ["'none'"],
"frame-ancestors": ["'none'"],
},
},
})
);- Nginx drops headers on error responses unless you append the always flag, so a 404 page would otherwise ship without any policy.
- On static hosts, remember that the header must be added by the platform. A meta tag inside the exported HTML cannot cover frame-ancestors.
Rolling out safely with Report-Only
Content-Security-Policy-Report-Only never blocks anything. The browser evaluates the policy, lets the page load normally, and posts a JSON report to the endpoint you name. That lets you discover the resources you forgot without breaking the site for real users.
Run it against production traffic for at least a week, then replace the Report-Only header with the enforcing one. Keep the collector alive afterwards, because extensions and unusual browsers will keep producing reports your own testing never generated.
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self'; report-uri /csp-report{
"csp-report": {
"document-uri": "https://example.com/checkout",
"violated-directive": "script-src",
"blocked-uri": "https://cdn.example.net/analytics.js",
"disposition": "report"
}
}- Deduplicate reports by blocked-uri before you act on them, otherwise one broken third-party script can bury the real findings.
- Relax the policy only for the specific origin that reported. Widening a whole directive to https: because of one host is how CSP slowly becomes decorative.
Reading the violations you will actually hit
- Refused to load the script 'https://cdn.example.com/x.js' — the host is missing from script-src. Add the origin, not the full path; source lists match origins, not files.
- Refused to execute inline script — an inline script or an onclick attribute has no nonce or hash. Move the code into a file, or emit a per-request nonce and put the matching nonce-{value} on the tag.
- Refused to apply inline style — a style attribute or a CSS-in-JS runtime is injecting rules. Either allow 'unsafe-inline' in style-src or switch to a style solution that understands nonces.
- Refused to frame 'https://app.example.com' because an ancestor violates frame-ancestors 'none' — you are embedding your own page. Loosen frame-ancestors for the domains that legitimately frame you.
- The source list contains an invalid source: 'strict-dynamic' — strict-dynamic is only valid inside script-src and requires a nonce or hash next to it.
- Blocked by connect-src — an API call or WebSocket target is not allowed. WebSocket origins need the ws:// or wss:// scheme written out explicitly.
Where CSP stops
CSP does not stop stored XSS that runs with a valid nonce, does not stop server-side template injection, and does not stop data exfiltration through a domain you deliberately allowlisted. It raises the cost of an exploit; it does not remove the class of bug.
It also does not cover native mobile clients, HTML email (clients ignore the header) or browsers old enough to misinterpret the directives they receive. Treat it as one layer of a defence that still needs output encoding, HttpOnly cookies and a sensible SameSite setting.
Frequently asked questions
- Does a Content Security Policy slow down my site?
- No measurable amount. The browser parses one extra header and matches request URLs against a short list of origins. The cost is microseconds per request, far below the cost of a single blocked third-party script.
- Can I set CSP with a meta tag instead of a header?
- Partially. A meta http-equiv="Content-Security-Policy" tag works for fetch directives, but frame-ancestors, report-uri and sandbox are ignored there. Any policy that needs clickjacking protection has to be sent as a real HTTP header.
- Should I use nonces or hashes for inline scripts?
- Nonces for dynamic pages: generate a fresh random value per response and put the same value in the header and on each script tag. Hashes for static HTML where the script content never changes. Nonces cannot be cached, so fully static hosts usually prefer hashes.
- Why does style-src still need 'unsafe-inline'?
- Because the style attribute and most CSS-in-JS libraries inject rules at runtime, and CSP has no way to attach a nonce to a style attribute. If every style lives in an external file, you can safely drop 'unsafe-inline' from style-src.
- Does CSP prevent all XSS attacks?
- No. It removes the most common execution paths, but bypasses still exist, for example JSONP endpoints on allowlisted domains and DOM sinks that run before the policy applies. Output encoding and input validation remain mandatory.
- What is the difference between 'none' and 'self'?
- 'self' allows the page's own origin. 'none' allows nothing at all. Use 'none' for directives that should never load anything in your application, typically object-src, base-uri and frame-ancestors.
- Is Report-Only enough for production?
- It is a rollout tool, not protection. Report-Only blocks nothing, so once the violation reports are clean it must be replaced by the enforcing header.
- What happens to a resource that CSP blocks?
- The request is cancelled inside the browser before it reaches the network in most cases, and a violation event fires. The console shows the blocked URI, report-uri delivers it to your collector, the script never executes and a blocked image renders as broken.