Content Security Policy in Depth for Enterprises

Content Security Policy in Depth for Enterprises

CSP is a control many three-year-old sites either lack or carry permissively. The modern policy shape, a report-only-first live-site migration, and the legacy framing-header move.

Content Security Policy is a control many three-year-old sites either lack entirely or carry in a permissive form that defends nothing — it is the mitigation that contains the script injection your application could not prevent, which makes its absence a load-bearing gap rather than a hardening nice-to-have. This article gives you the modern policy shape, a lower-risk way to migrate a live site onto it that starts in report-only mode, and the legacy framing-header move that comes with it; it shows the shape with one minimal illustrative block and defers the production policy itself to engineering, because a pasted CSP is the part that breaks and the posture is the part that lasts.

Why CSP is a control legacy sites often lack

Walk the response headers of a typical site that has not been rebuilt in three years and the content policy is often in one of two states, both of which are the same state functionally: absent, or present but written so permissively — broad allowlists, an unsafe-inline that was added to make a feature work and never removed — that it permits exactly what it exists to stop. HTTP Archive's 2025 Web Almanac, which measures the web as a whole rather than by site age, found a Content-Security-Policy header on 21.9% of the sites it measured, up from 18.5% in 2024, and reported that the unsafe-inline and unsafe-eval keywords are used very often in script-src. A permissive policy is not a weaker version of protection. It is the appearance of protection with none of the effect, which is worse than absence because it satisfies a checkbox while the gap stays open.

The reason this gap is load-bearing and not cosmetic is what the control actually does. The first-line client-side defenses try to prevent an injection from existing — input handling, output encoding, framework escaping. Those are necessary and they are also never complete; a single template or a third-party script is enough for an injected script to reach the page. The content policy is a control that assumes that failure already happened and limits it anyway. It is not the only one — cookie attributes such as HttpOnly, which keeps page script from reading a cookie (though the browser still attaches it to requests that script makes), can also limit the impact of an XSS attack — but what it adds is control over which resources the page can fetch or execute. It does not stop the script from being injected; it limits what the injected script can do. An injected inline script that lacks the policy's nonce or hash is blocked from running, and a policy that sets default-src and allowlists the servers the page may communicate with can also mitigate exfiltration and beaconing, while the script rules limit where a second stage can load from. The CSP Level 3 draft describes the control as defense-in-depth that reduces the harm a malicious injection can cause, not as a replacement for careful input validation and output encoding. It is the containment layer, and a site without a real one is a site betting that prevention is perfect, which it never is.

The distinction between no policy and a permissive one is worth holding onto because the two fail differently, and the permissive case is the one that costs more. A site with no policy fails an automated check and a competent reviewer both — the gap is at least visible. A site with a broad, unsafe-inline policy can pass the checkbox in a customer security review while presenting an attacker exactly the same open surface. The review and the attacker disagree about whether the site is protected, and the permissive policy is, in effect, engineered to satisfy only the first audience. That is the worst position to be in: the control reports green to the people deciding whether to trust you and reports nothing to the people testing whether they can. A CTO reading a clean CSP line in an internal report should ask not "is it present" but "does it deny anything," because a policy that denies nothing is not a weaker control, it is a louder absence.

This sits next to a boundary worth drawing explicitly, because it looks like a neighboring topic's territory and is not. The choice of rendering model and application framework — server-rendered, single-page, the hydration approach — is its own decision space owned elsewhere. This topic does not weigh in on which to pick. It owns the consequence of whichever was picked: a single-page application's runtime injection surface, the trust assumptions a hydration step makes, the way a rendering choice widens or narrows what an injected script can reach. The framework decision is one conversation; the client-side attack surface that decision creates is this one, and the content policy is the control that bounds it regardless of which framework produced it.

A modern policy shape

The target shape of a modern policy is not an allowlist of trusted domains. Allowlist-only policies fail in practice for a structural reason: an enterprise site's set of legitimately loaded origins is large, changes constantly, and includes third-parties that themselves load further origins, so the allowlist is either too tight to ship or so broad it readmits the injection it was meant to exclude. The maintained version of that policy can drift toward permissive over time when each breakage is fixed by widening it, and a policy that only widens is a policy decaying toward off.

The modern shape replaces the allowlist with a per-response nonce or hash, adds strict-dynamic where a trusted script needs to load further scripts without each being separately enumerated, and removes unsafe-inline rather than accommodating it. The reference for this shape is the W3C CSP Level 3 specification, the per-directive documentation is MDN's CSP reference, and the rationale for nonce-and-strict-dynamic over allowlists is laid out in web.dev's strict CSP guidance. strict-dynamic is a trade-off rather than a free default. web.dev includes it in both the nonce-based and the hash-based strict policies its guide sets out. The CSP Level 3 draft notes that with it, scripts created at runtime are allowed to execute, so if an attacker can control the location of such a script, the policy will allow arbitrary scripts to load. The draft says that developers who use strict-dynamic should audit the uses of non-parser-inserted APIs and ensure they are not invoked with potentially untrusted data, including in applications or frameworks that tend to determine script locations at runtime, and its Strict CSP section adds that, while strict-dynamic eases deployment, it should be avoided when possible. MDN's CSP guide likewise says it makes a policy less secure, because if the included scripts create script elements based on potential sources of XSS, the policy will not protect them. The shape — explicitly a shape, not a policy to paste into production, and wrapped across lines here only for readability (send each header value on a single line, since HTTP/1.1 deprecates line folding outside the message/http media type) — looks like this:

code
Reporting-Endpoints: csp-endpoint="https://example.com/csp-reports"

Content-Security-Policy:
  default-src 'self';
  script-src 'nonce-<per-response-random>' 'strict-dynamic';
  object-src 'none';
  base-uri 'none';
  frame-ancestors 'self';
  report-to csp-endpoint;
  report-uri https://example.com/csp-reports;

Two details in that block are deliberate. The report-to directive only names an endpoint; the server has to define that name separately in the Reporting-Endpoints response header (MDN notes it can also be the name of a group defined in the deprecated Report-To header), or the reports go nowhere. The deprecated report-uri directive is kept alongside it because MDN's report-to reference notes that you can specify both directives until report-to is broadly supported, the CSP Level 3 draft suggests specifying both for backwards compatibility, and Firefox added report-to support only in Firefox 149, released in March 2026 (as of Oct 2026, per MDN's browser compatibility data); browsers that support report-to ignore report-uri, so the pair does not double-report.

The block is illustrative of the structure, not a production configuration. A real policy carries the directives a specific application needs, a real reporting destination, and the source rules its actual dependencies require, and arriving at it is engineering work on the running system, not a copy from this page. The point of showing the shape is to make one thing concrete: the modern target is driven by nonces or hashes, with strict-dynamic where it is needed, not a list of allowed hostnames, because the list is the part that decays and the nonce is the part that does not.

Migrating an existing site without breaking it

A strict policy applied by enforcement to a live site that was not built with one can break something as soon as it is enforced, and what breaks is hard to predict from a reading of the codebase. The lower-risk path is report-only first: deploy the policy in the mode that reports what it would have blocked without blocking it, collect the reports, read them, fix the real conflicts, and only then enforce. web.dev's strict CSP guide starts with confirming the policy in a local development environment before deploying to staging and then production, and lists report-only mode as an optional step that is handy for testing a potentially breaking change in production before enforcing; for a live site that was not built with a policy, this article treats that step as the default. Enforcing on such a site without that evidence risks shipping an outage with a security rationale attached.

The reports do not report theory. They report the actual breakage surface, and on a legacy site it can be sorted into a small number of recognizable classes worth naming so the team knows what it is looking at.

Breakage classWhat the report-only run revealsPossible sources
Inline handlersInline event attributes and inline script blocks the policy blocksOlder templates and hand-written pages predating the policy
Injected third-partyA vendor script that is not itself nonced, writes scripts in through `document.write`, injects inline code, or sends requests to origins the policy does not allowTag managers, analytics, marketing embeds added through a console
Build outputGenerated inline bootstrap or style the build emits that the policy does not nonceA build pipeline that was never made policy-aware
Dynamic eval patternsCode paths that construct and execute strings the policy disallowsOlder libraries and patterns the application still depends on

Reading these from evidence rather than guessing is the entire value of the report-only phase. Each class has a different fix and a different owner — an inline handler is a template change, an injected third-party is a vendor conversation or a removal decision, build output is a pipeline change, a dynamic pattern is a dependency decision — and a team that learns the distribution before enforcing makes the enforcement switch a non-event. There is also an organizational reason to keep the report-only phase, and it is larger than the immediate outage it prevents. The first time a strict-enforce-first attempt takes down a visible page — a campaign landing page, a checkout step — the lesson the organization learns is not "we enforced too early." It is "security broke the site." That lesson is durable and expensive: it makes the next hardening proposal politically harder, gives every team a precedent to cite when they want to defer a control, and reframes the security function as a source of incidents rather than a reducer of them. Report-only protects the program, not only the page. It lets the security work accumulate a track record of non-events, which is the only track record that keeps the next decision easy.

Enforcement is then an evidence-backed decision: the team enforces because the reports stopped showing conflicts, not because a date arrived. The OWASP Content Security Policy Cheat Sheet describes report-only delivery as often used as a precursor to blocking mode and strongly recommends following Google's instructions for creating a strict CSP, and web.dev's guide lists report-only deployment as an optional step before enforcing. The sequence this article recommends — report, read, fix, enforce — is the part that does not change even though the directives do.

Framing and the legacy header move

The content policy migration is also when a legacy header steps down to a supporting role. Clickjacking protection used to live in a standalone framing header (X-Frame-Options); the modern home for it is the content policy's framing directive (frame-ancestors), which, when enforced, takes precedence over the older header and expresses the same intent more precisely. When an enforced policy includes frame-ancestors, browsers that support it ignore X-Frame-Options; the CSP Level 3 draft ties this to a policy whose disposition is "enforce", so a frame-ancestors directive in a report-only policy does not switch the legacy header off. MDN's clickjacking guidance notes that also setting X-Frame-Options can prevent embedding in browsers that don't support frame-ancestors, while adding that, as browser support for frame-ancestors is very good, this is not a major concern. This article still recommends keeping the legacy header, but only as a fallback that must say the same thing as the policy, not as a second source of truth; during the report-only phase, it is also the framing control that is actually enforced. A three-year-old site that still carries only the legacy framing header is not protected and modern; it is protected by a control the modern policy is supposed to own, and making frame-ancestors the authoritative control during the CSP migration is the natural moment to do it rather than a separate project.

The reporting endpoint is the other thing that outlives the launch. It is tempting to treat the report destination as a migration artifact — useful during the report-only phase, removed once enforcement is on. That is the wrong model. The endpoint is an ongoing signal: after enforcement, it is the channel that tells you when a new third-party, a new route, or a build change introduced something the policy now blocks in production, which is the early warning that the policy has drifted out of step with the site again. A policy with no live reporting destination is a policy you will only learn has broken when a user does.

This is the same failure the broader posture warns about, observed at the level of a single control. A frozen site does not become unsafe by being edited; it becomes unsafe because the world around it moves and nothing tells the team it moved. A policy is a frozen artifact in exactly that sense — correct on the day it shipped, silently diverging from the site as third-parties, routes, and build output change around it. The reporting endpoint is the one instrument that turns that silent divergence into a signal before a user encounters it. A policy without a live endpoint is a control that has adopted the precise failure mode the rest of this topic exists to prevent: it looks fine, serves traffic, and is drifting out of the safe set with nothing watching the distance grow.

The deliverable for engineering is the policy shape, the migration sequence, and the framing consolidation as a posture, not the final production policy. The exact directives, the nonce wiring into the rendering layer, the per-route differences, and the build-pipeline changes are engineering's to own and deliberately not specified here, because those are the parts that depend on the specific application and rot when the application changes. What does not rot is the shape — nonces or hashes (with strict-dynamic where needed) over allowlists, report-only before enforcement, frame-ancestors as the authoritative framing control with the legacy header kept only as an aligned fallback, and a reporting endpoint that stays — and that shape is the posture this article hands over.

Project Inquiry