Skip to content
IT glossary
SecurityUpdated December 3, 2026

Content Security Policy

Content Security Policy (CSP) lets a website tell the browser which resources it may load and which scripts it may run. These restrictions help limit the impact of cross-site scripting (XSS).

Also known as: CSP

What CSP does

When a browser processes a page, it normally loads resources referenced by the page and runs its JavaScript. Content Security Policy lets the site narrow those permissions. For example, a page can load its own JavaScript files while forbidding scripts written directly into the HTML. That distinction matters for cross-site scripting (XSS): even if unwanted markup makes it into the page, the browser can refuse to run a script that violates the policy. CSP does not remove the unwanted markup or fix the flaw that let it in.

How the browser applies it

The server typically sends a Content-Security-Policy header with the HTML response. The header is a semicolon-separated list of rules called directives. The browser checks script-src for scripts, img-src for images, and connect-src for connections made through APIs such as fetch() and WebSocket. If img-src is omitted, default-src can supply the rule for images. Not every directive has that fallback: frame-ancestors, which says who can embed the page in a frame, must be set explicitly if you want to impose that restriction through CSP.

Example: separate rules for scripts and images

Content-Security-Policy: default-src 'self'; script-src 'self'; img-src 'self' https://img.example.com; frame-ancestors 'none'

A script loaded from /app.js on the page's own origin is allowed, but an inline <script> block or an onclick attribute is not. Images can come from either the page's origin or https://img.example.com. The last directive prevents other pages, including pages on the same origin, from embedding this document in a frame.

Rolling out a policy

On an existing site, try a candidate policy in Content-Security-Policy-Report-Only first. It lets you observe what would violate that policy without blocking it. Configure a reporting endpoint if you want to collect browser reports centrally, and test actual user journeys before enforcing the rules: a homepage may look fine while a checkout widget or an API request fails under the new policy.

If the page genuinely needs an inline script, avoid granting blanket permission with 'unsafe-inline'. A hash can authorize a specific script by its contents. A nonce is an unpredictable value generated for each response: the server puts it in both the policy and the nonce attribute of the script it intends to allow. Neither approach replaces context-aware output escaping or sanitization of user-supplied HTML.

What interviewers ask

You may be asked to diagnose why a script or API call stopped working after a CSP change. Start by checking the HTML response headers and the browser console for the directive that was violated. Be ready to distinguish CSP from CORS: the page's CSP can restrict where it makes a fetch() request, while the API's CORS headers govern whether the browser lets JavaScript on another origin read the response.