Content Security Policy Without Breaking Your Website
A useful CSP starts with an inventory and a report-only rollout. Replace wildcards with deliberate trust boundaries while keeping payments, embeds and legitimate scripts working.

Content Security Policy is easy to copy and harder to operate. A header that blocks half the site is not a security success. Neither is a policy containing enough wildcards and unsafe exceptions to permit almost anything.
CSP lets a page restrict where scripts and other resources may come from and which embedding behaviours are allowed. It can reduce the impact of some injection attacks, but it does not replace escaping, sanitisation, dependency review or authorization. Treat it as another layer, not a certificate that your templates are safe.
Inventory what the page loads
Open representative pages and list script, style, image, font, frame and network destinations. Include checkout, search, analytics, chat and logged-in pages. The homepage is rarely the whole application.
Ask whether each third-party dependency is necessary. An old widget that nobody uses is easier to remove than to accommodate indefinitely. For essential services, check the vendor's current CSP documentation and test their complete workflow, including authentication popups and payment challenges.
Separate loading a script from allowing that script to send network requests. script-src and connect-src control different actions. Copying the script host into every directive grants permissions without an operational reason.
Begin in report-only mode
Content-Security-Policy-Report-Only evaluates a policy without enforcing most resource restrictions. It gives you evidence before you cause a customer-facing outage. Reports can be noisy or forged, and reporting delivery is not guaranteed, so combine them with browser testing.
This is an illustrative starting point for a self-hosted page, not a policy to paste onto an arbitrary application:
Content-Security-Policy-Report-Only: default-src class="text-code-string">'self'; script-src class="text-code-string">'self' class="text-code-string">'nonce-PER_RESPONSE_VALUE'; style-src class="text-code-string">'self'; img-src class="text-code-string">'self'; font-src class="text-code-string">'self'; connect-src class="text-code-string">'self'; object-src class="text-code-string">'none'; base-uri class="text-code-string">'self'; form-action class="text-code-string">'self'; frame-ancestors class="text-code-string">'none'Replace the nonce placeholder with an unpredictable value generated for each response and put the identical value on permitted inline script elements. Add only the resource destinations your inventory justifies. If your framework injects inline styles or hydration scripts, plan for them explicitly instead of reflexively adding unsafe-inline everywhere.
The reporting directives and endpoint configuration depend on browser support and your collection setup. Review both modern Reporting API support and legacy reporting where relevant. Never assume report-only testing proves every directive will work: frame-ancestors in particular needs enforcement-mode verification.
Choose nonces or hashes deliberately
A nonce works well when the server generates HTML for each request. It must be fresh and sufficiently random, not a user ID or a fixed environment value. Never reflect a request-supplied nonce into the policy.
Hashes suit stable inline code. The hash must match the exact script bytes, including whitespace. A release changing an inline bootstrap requires updating the policy. Build tooling can automate that, but you still need to test the generated output.
With a nonce or hash-based policy, strict-dynamic can delegate script loading to a trusted script in supporting browsers. That is powerful: a trusted tag manager becomes a major trust boundary. Understand fallback behaviour and older browser requirements before adding broad host allowances alongside it.
Remove the awkward inline behaviours
Inline onclick handlers and javascript: URLs complicate a strong policy. Replace them with event listeners in approved scripts and real navigation links. Avoid eval and Function constructors unless there is a documented, reviewed reason you cannot remove them.
Styles need separate attention. A nonce on a style element does not automatically permit arbitrary style attributes. Libraries that write inline styles may require configuration changes or a consciously scoped exception. Test the actual compiled application, not only your authored source.
If your build needs unsafe-eval only during local development, keep that exception out of the production policy. Development conveniences should not silently define the published security boundary.
Think about caching
A per-response nonce in HTML must match the policy header delivered with that HTML. Independently caching either part can produce mismatches. A shared cache serving the same nonce-bearing response repeatedly can also undermine the intended per-response freshness.
Choose a deployment strategy that generates nonce and HTML together, or use a hash-based policy for truly static output. Test through the actual CDN because the origin's headers are not necessarily the headers a visitor receives.
Multiple enforcing CSP headers combine restrictively; a second broad policy does not cancel an earlier restrictive one. When a proxy adds a policy, inspect the final response before blaming the application.
Review the trust boundaries
| Directive | Question to ask | |---|---| | script-src | Which code may execute? | | connect-src | Where may scripts send requests? | | frame-src | Which frames may this page load? | | frame-ancestors | Which sites may embed this page? | | form-action | Where may forms submit? | | base-uri | Can an injected base element rewrite relative URLs? |
Frame-ancestors is not inherited from default-src and belongs in a response header, not a meta policy. A payment provider may need frame-src permission without being allowed to embed your whole application. These are separate decisions.
Enforce in a controlled release
After reviewing reports, enforce on a limited set of pages or a controlled rollout. Test navigation, uploads, login, logout, payment, error pages and language changes. Watch for violations tied to a specific browser or third-party step.
Keep a rollback path and a person responsible for reviewing policy changes. Report endpoints should limit request sizes and avoid retaining sensitive URL data unnecessarily. Browser extensions can generate irrelevant reports; do not whitelist unknown domains merely to make the report count smaller.
The policy should get easier to explain over time. Each allowed source needs a reason and an owner. If the only explanation is that the site once broke without it, that is a request for another investigation, not a permanent permission.
Paj Digital Labs
The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.
Related reading

International Websites: Locale URLs, Hreflang and Formatting Without Guesswork
Language, country and formatting are related but different choices. Give translated pages stable URLs, preserve user selection and keep search signals aligned with real equivalents.

Progressive Enhancement for Article Sites: The Page Should Outlive Its Scripts
Core reading and navigation should remain available when hydration or a third-party script fails. Start with useful HTML and layer interactions without turning every link into an application dependency.

Website Search That Finds Answers: Ranking, Typos and Empty Results
Search quality depends on the documents and ranking rules, not the size of the input box. Index useful fields, handle imperfect queries and give readers a way forward when nothing matches.