How to test a Content Security Policy before enforcing it
A Content Security Policy (CSP) is a set of browser rules your site sends in an HTTP response header. It can limit where scripts, images and other resources load from. Start in Report-Only mode to see what the proposed policy would block while those resources can still load.
This walkthrough uses a fictional storefront at https://shop.example. Its scripts and styles are files served by the shop, while product photos come from https://images.example. The first draft misses that image host.
1. Generate a Report-Only header
- Open the Content-Security-Policy Generator. Keep the opening
default-srcrow with'self'as its source. That limits several kinds of resource requests to the shop’s own origin unless you give them a more specific rule. - Use Add directive for each additional row in the example. Set
object-srcandframe-ancestorsto'none'because this shop needs neither plugin content nor embedding on other pages. Setbase-urito'self'to restrict where a page’s base URL can point. - Set Delivery mode to Report-Only. Leave the optional report destination blank for this browser-console check. Use the output for your server framework to send the header from a staging copy of the site. Keep any existing enforced policy in place during this check.
The staging page’s response should include this header.
Content-Security-Policy-Report-Only: default-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'2. Read the image violation
Open your browser’s developer tools before you reload a product page. In Chrome, open Network, select the page’s document request and look under Response Headers for the Report-Only header. Then open Console and look for a message like this one.
[Report Only] Loading https://images.example/mug.webp would violate default-src 'self'.The URL tells you which resource triggered the warning. The message names default-src because the draft has no img-src rule, so image requests fall back to default-src. The photo can still appear because the candidate policy is in Report-Only mode.
Confirm that the host belongs to an integration you use. The shop deliberately serves its photos from images.example, so that host belongs in the image rule. If the request came from an unused widget, remove the widget and check that the request is gone.
3. Allow the image host and test again
In the Generator, use Add directive and select img-src. Enter 'self' https://images.example in its sources field. Keep 'self' to allow images served by the shop itself.
img-src 'self' https://images.exampleOnce present, img-src supplies the image rules in place of the default-src fallback. Update the staging header and reload the same product page. The photos should still display, and the warning about the image host should disappear. Continue through the rest of the site. Open the cart, try sign-in on staging and check any pages that load different images, fonts or embedded content.
4. Check the policy your site actually sends
Copy the value from the staging page’s Response Headers and paste it into the Content-Security-Policy Analyzer. Paste the part starting with default-src. In the results, look for img-src with both expected sources and review any flagged directives. The Analyzer checks the pasted text for common issues. The browser checks whether your actual pages work under it.
If a photo is already blocked in Report-Only mode, check for an existing Content-Security-Policy response header or a CSP <meta> element. Those policies still apply. If several enforced policies are present, the request has to satisfy all of them.
5. Try enforcement on staging
Switch the Generator’s Delivery mode to Enforce and use the output to update the enforced Content-Security-Policy header on staging. Confirm the header name in the document response and repeat the same browser checks. In the example, product photos and the shop’s own images should load. A photo from an unlisted origin should be blocked.
When you roll out the enforced policy to your live site, keep the previous configuration ready to restore. Recheck the live response headers and the same user flows after the rollout, since staging and production can load different resources.