Code guides

Build a CSP header for a static site

·

A static catalog uses scripts and styles from its own site and serves some images from a separate host. Build a policy that describes these sources and test it with the pages visitors use.

Content Security Policy Builder

Build a policy for a static site

Choose Load example and keep Enforce selected. The policy uses 'self' as the default resource fallback, and explicitly keeps scripts same-origin. The img-src input includes 'self' and https://images.example.com.

Object sources and framing by other sites are denied with 'none'. The base URL and form submission destinations are limited to 'self'. Select Build CSP header to see every emitted directive.

W3C Content Security Policy Level 3 directives

Test the HTTP response header

Copy the full header and configure its name and value as an HTTP response header on a staging page. Replace the example image host with the actual host used by your catalog.

Open the catalog in a browser and verify that the same-origin script and both intended image sources still load. Open the browser console and inspect any CSP violations alongside resource requests.

W3C Content Security Policy Level 3 delivery

Check report-only before enforcing

Select Report only and rebuild to get a Content-Security-Policy-Report-Only header for evaluation. Compatible browsers can report violations in developer tools while loading resources. This output leaves reporting endpoints unconfigured.

When you switch to Enforce, test the same pages again with the enforcing header. Review existing policies on the response because browsers can apply more than one policy.

W3C Content Security Policy Level 3 delivery

Common questions

How do I allow a CDN for scripts or images?

Add its HTTPS host to the directive for the affected resource. Include 'self' if the page still loads those resources from the same origin.

Why can I not combine 'none' with another source?

'none' expresses an empty allowlist and belongs on its own. Use the specific sources you want to allow when the list should contain entries.

Sources

Put it into practice.

Related guides