Rolling out a new policy
A Content-Security-Policy that blocks something your site needs breaks it for real visitors. The safe way to roll one out is to watch first and enforce later:
report-only → review → tighten → enforce
1. Start in report-only mode
Send your policy as Content-Security-Policy-Report-Only, with reporting to your endpoint (see Setting up your CSP headers):
Content-Security-Policy-Report-Only: default-src 'self'; report-uri https://csp-report-to.com/r/YOUR_SITE_KEY; report-to csp
Reporting-Endpoints: csp="https://csp-report-to.com/r/YOUR_SITE_KEY"
Nothing is blocked, so this is safe on your live site. default-src 'self' reports everything that doesn't come from your own domain, which is a good way to find out what your pages actually load. If you already know some of what you need, start with a fuller policy instead.
Tip: add 'report-sample' to script-src and style-src (for example script-src 'self' 'report-sample'). Browsers then include the first 40 characters of blocked inline code in their reports, which you can see in the violation's samples.
2. Let real traffic run, then review
Leave it running long enough for visitors to reach every part of your site: a few days, or a week for a site with pages that are rarely visited, such as checkout, account settings or the admin area. Click through those pages yourself too.
Then open your site's violations list and tick Group by directive. The list becomes one section per directive, and each section lists exactly what that directive would need to allow. For example:
| Directive | Blocked | What it means |
|---|---|---|
script-src-elem |
https://www.googletagmanager.com |
A <script src> from that host. Allow the host in script-src. |
img-src |
data: |
An image from a data: URL. Add data: to img-src. |
script-src-elem |
inline |
An inline <script> block. Move it to a file, or allow it with a nonce or hash. |
script-src |
eval |
eval() or new Function(). Remove it, or allow 'unsafe-eval' if you must. |
connect-src |
https://api.example.com |
A fetch(), XHR or WebSocket to that host. Allow it in connect-src. |
Browsers often report the more specific directives script-src-elem, script-src-attr, style-src-elem and style-src-attr. If your policy doesn't use those, add the source to script-src or style-src, which covers both kinds.
For each violation, decide whether it's something your site needs (allow it) or something it shouldn't be loading (remove it from your site, or leave it blocked). Open a violation to see which pages it happens on and the latest raw reports, including the full blocked URL. Troubleshooting and FAQ explains inline, eval, data: and blob:.
3. Tighten the policy
Update the report-only policy with what you decided to allow, and deploy it. Then mark the violations you've dealt with:
- Resolve the ones you allowed or fixed. If one comes back, it reopens and you get an alert, so you'll know if a fix didn't work.
- Mute the ones you've decided to keep blocked, such as a script injected by something you don't control. They stay out of your list and never alert again.
Repeat until the Open list stays quiet for a while under normal traffic.
4. Enforce
Rename the header from Content-Security-Policy-Report-Only to Content-Security-Policy:
Content-Security-Policy: default-src 'self'; report-uri https://csp-report-to.com/r/YOUR_SITE_KEY; report-to csp
Reporting-Endpoints: csp="https://csp-report-to.com/r/YOUR_SITE_KEY"
Browsers now block what the policy doesn't allow.
Keep reporting on after you enforce. Keep report-uri and report-to in the enforced policy. Your site changes, third-party scripts change, and a new violation now means something is really broken for visitors. Reporting is how you find out, from the alert email, before someone tells you.
Changing a policy that's already enforced
Don't edit the enforced policy directly. Run the new version side by side in report-only mode first:
Content-Security-Policy: <your current policy>; report-uri https://csp-report-to.com/r/YOUR_SITE_KEY; report-to csp
Content-Security-Policy-Report-Only: <your new policy>; report-uri https://csp-report-to.com/r/YOUR_SITE_KEY; report-to csp
Reporting-Endpoints: csp="https://csp-report-to.com/r/YOUR_SITE_KEY"
Browsers apply both policies independently: the current one keeps protecting your site, while the new one only reports. Both report to the same endpoint, so their violations appear in the same list. To tell them apart, open a violation and check the samples' disposition: report came from the report-only policy, enforce from the enforced one. (If a browser leaves the disposition out of its report, it shows as enforce.)
When the new policy's reports are quiet, make it the enforced one and remove the report-only header.