A Content Security Policy, or CSP, is a set of instructions a website sends to browsers about which resources and behaviors a page may use. It can reduce the impact of certain script injection attacks, restrict embedding, and control resource loading. The browser can block actions that violate an enforced policy, but a policy that does not match the application can also stop required features from working.

Start with a limited group of pages whose behavior you can test. Identify the scripts, styles, images, fonts, frames, network calls, and inline code that the application needs. Send a report-only policy, inspect violations, refine the rules, and then enforce a tested policy. Continue to encode or sanitize untrusted content and maintain dependencies; CSP is defense in depth rather than a replacement for those controls.

Inventory Resources and Page Behaviors

Group pages by function and list the resources each group loads, including resources added after user interaction. A checkout page may use a payment frame that an article page does not. A web application may fetch API data from another origin, meaning a different scheme, hostname, or port. Third-party scripts may load additional scripts. Record the owner and reason for each dependency so that a policy can allow what is needed without broadly trusting an entire collection of unrelated hosts.

Look for inline script and style elements, event handler attributes such as onclick, javascript: links, and code that evaluates strings as JavaScript. Restrictive script or style policies can block these uses, depending on the directive involved. Remove unnecessary inline handlers or unsafe evaluation where feasible. Adding 'unsafe-inline' to permit inline JavaScript broadly removes an important defense against injected scripts.

Decide which pages need each policy and where the response headers will be set. Content-Security-Policy is the HTTP response header used for enforcement. A content delivery network (CDN) or reverse proxy may add or replace headers, so inspect the response that reaches the browser. Include dynamic pages, error pages, and relevant hostnames in the inventory. A policy seen on one response does not prove coverage everywhere.

Start with Report-Only Mode

Send a proposed policy through Content-Security-Policy-Report-Only. The browser checks actions against the proposed rules without blocking those actions because of that policy. The report-only policy must be delivered as an HTTP header, not an HTML meta element. An existing enforced policy can coexist with a separate report-only candidate.

For example, the following headers test a policy that, when enforced, allows resources from the page's own origin, disables object and embed resources, and restricts the document's base URL to the same origin. The reporting name and URL are illustrative; replace them with a working report collector that you control.

Content-Security-Policy-Report-Only: default-src 'self'; object-src 'none'; base-uri 'self'; report-to csp
Reporting-Endpoints: csp="https://example.com/security/csp-reports"

The report-to csp directive selects the destination named in Reporting-Endpoints. The collector must accept the browser's JSON reports sent by HTTP POST; declaring a URL does not create that service. Older browsers may need the deprecated report-uri directive as a fallback, so check the reporting support of the browsers you need to serve.

Exercise the different page states through normal traffic and functional testing, but do not treat every violation report as a reason to widen the policy. Some reports may come from browser extensions, injected content, old cached pages, or an unexpected third party. Confirm the actual dependency before allowing it. Reports can contain page addresses, so restrict who can read them and set an appropriate retention period.

Report-only is not a guarantee of full coverage. A browser may not send every report, and testing may miss rarely used functions. Pair reports with manual checks of login, forms, payments, media, embedded content, and administrative pages. Include representative browsers because support for some directives differs.

Refine Sources, Nonces, and Hashes

CSP directives assign rules to different resource types. Use script-src for scripts, style-src for stylesheets, img-src for images, and connect-src for connections made by scripts. The default-src rule supplies a fallback for fetch directives when no more specific rule applies. It does not set every CSP restriction: base URL controls and page embedding require separate directives. Use base-uri for the base URL and frame-ancestors to limit which sites may embed the page.

Write directives as names followed by values, separating directives with semicolons. Source expressions within a directive are separated by spaces. The keyword 'self' permits resources from the page's origin, while an explicit resource directive replaces the default allowance for that category. The initial report-only example is a way to find required exceptions, not a finished script policy for every application.

Choose Script Nonces or Hashes

A script policy using nonces or hashes can provide stronger protection than a permissive host list. For a nonce policy, generate an unpredictable value with a cryptographically secure random generator for each response. Put that value in the CSP and in the nonce attribute of the approved script elements. A static nonce can be learned and reused by an attacker, so the value must change between responses.

In script-src 'nonce-VALUE', VALUE represents the newly generated value; it is not a literal setting to reuse. The corresponding script attribute is nonce="VALUE", without the nonce- prefix. Add the attribute only to scripts the application intends to authorize, not to arbitrary markup inserted into the response.

For an inline script hash, calculate a SHA-256, SHA-384, or SHA-512 digest of the script content, encode the digest in Base64, and add the matching lowercase prefix, such as sha256-. Place the resulting expression in single quotes in script-src. The browser authorizes content that matches that digest. Hashes suit stable script content, but changing the script requires a new hash in the policy. Choose the approach for each group of pages and test the required scripts, styles, and embedded content.

Inspect Violations before Enforcement

Group reports by directive, page, and resource. A reported payment frame, image, or API request may reveal a required source missing from the policy. An unknown script may instead be something the policy should block. Confirm which resource caused the violation and why the application needs it before adding a narrowly defined exception. Remove unused resources where possible.

Check for policies that are syntactically valid but ineffective. A script-src rule that trusts many hosts may allow an attacker to supply code through one of those hosts. A policy sent only on the homepage does not protect other documents. A report-only policy never blocks on its own. Browser tools can show whether the response carried the expected header, whether a resource was blocked, and which directive made that decision.

Enforce and Monitor the Policy

After reviewing the candidate in report-only mode, enforce it through the Content-Security-Policy header on a limited group of representative pages. Test essential tasks and watch errors and violation reports. Expand coverage gradually, and keep the previous configuration available in case a new restriction breaks a required function. Keep a newer candidate in report-only mode when testing revisions to an existing enforced policy.

Revisit CSP when scripts, templates, CDN settings, integrations, or frameworks change. A new dependency can fail under an old policy, and a broad new exception can weaken protection. Record why each source is allowed and where approved scripts receive their nonces. When a dependency is removed, check whether its old allowance can be removed too.