--- url: "https://web.dev/articles/csp" titulek: "Content security policy | Articles | web.dev" h1: "Content security policy Stay organized with collections Save and categorize content based on your preferences." popis: "Content Security Policy can significantly reduce the risk and impact of cross-site scripting attacks in modern browsers." jazyk: "en" kanonicka: "https://web.dev/articles/csp" drobecky: ["Articles", "Content security policy"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 02:42:25" znaku: 20978 slov: 3132 kodovani: "iso-8859-2" sha256: "3e265990a5ac24c97295b5b2f2f1fc234a9a17210b99277ab8cda12c98dabab4" --- Content security policy Stay organized with collections Save and categorize content based on your preferences. Content Security Policy can significantly reduce the risk and impact of cross-site scripting attacks in modern browsers. Joe Medley Mike West Browser Support 25 14 23 7 [Source](https://developer.mozilla.org/docs/Web/HTTP/Reference/Headers/Content-Security-Policy) The web's security model is based on a [*same-origin policy*](//en.wikipedia.org/wiki/Same-origin_policy). For example, code from `https://mybank.com` must have access to only `https://mybank.com`'s data, and `https://evil.example.com` must never be allowed access. Each origin is, in theory, kept isolated from the rest of the web, giving developers a safe sandbox to build in. In practice, however, attackers have found several ways to subvert the system. [Cross-site scripting (XSS)](//en.wikipedia.org/wiki/Cross-site_scripting) attacks, for example, bypass the same-origin policy by tricking a site into delivering malicious code along with the intended content. This is a huge problem, as browsers trust all of the code that shows up on a page as being legitimately part of that page's security origin. The [XSS Cheat Sheet](https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet) is an old but representative cross-section of the methods an attacker might use to violate this trust by injecting malicious code. If an attacker successfully injects *any* code at all, they've compromised the user session and gained access to private information. This page outlines Content Security Policy (CSP) as a strategy for reducing the risk and impact of XSS attacks in modern browsers. Components of CSP To implement an effective CSP, take the following steps: Use allowlists to tell the client what's allowed and what isn't. Learn what directives are available. Learn the keywords they take. Restrict the use of inline code and `eval()`. Report policy violations to your server before enforcing them. Source allowlists XSS attacks exploit the browser's inability to distinguish between script that's part of your application and script that's been maliciously injected by a third party. For example, the Google +1 button at the bottom of this page loads and executes code from `https://apis.google.com/js/plusone.js` in the context of this page's origin. We trust that code, but we can't expect the browser to figure out on its own that code from `apis.google.com` is safe to run, while code from `apis.evil.example.com` probably isn't. The browser happily downloads and executes any code a page requests, regardless of source. CSP's `Content-Security-Policy` HTTP header lets you create an allowlist of sources of trusted content, and tells the browser to execute or render only resources from those sources. Even if an attacker can find a hole to inject a script through, the script won't match the allowlist, and therefore won't be executed. We trust `apis.google.com` to deliver valid code, and we trust ourselves to do the same. Here's an example policy that allows scripts to execute only when they come from one of those two sources: `Content-Security-Policy: script-src 'self' https://apis.google.com` `script-src` is a directive that controls a set of script-related privileges for a page. This header `'self'` as one valid source of script, and `https://apis.google.com` as another. The browser can now download and execute JavaScript from `apis.google.com` over HTTPS, as well as from the current page's origin, but not from any other origin. If an attacker injects code into your site, the browser throws an error and doesn't run the injected script. The console shows an error when a script tries to run from an origin not on the allowlist. Policy applies to a wide variety of resources CSP provides a set of policy directives that enable granular control over the resources a page is allowed to load, including `script-src` from the previous example. The following list outlines the rest of the resource directives as of level 2. A [level 3 specification](https://www.w3.org/TR/CSP3/) has been drafted, but it's [largely unimplemented](https://www.chromestatus.com/features#csp3) in the major browsers. `base-uri` Restricts the URLs that can appear in a page's `` element. `child-src` Lists the URLs for workers and embedded frame contents. For example, `child-src https://youtube.com` enables embedding videos from YouTube but not from other origins. `connect-src` Limits the origins that you can connect to using XHR, WebSockets, and EventSource. `font-src` Specifies the origins that can serve web fonts. For example, you can allow Google's web fonts using `font-src https://themes.googleusercontent.com`. `form-action` Lists valid endpoints for submission from `
` tags. `frame-ancestors` Specifies the sources that can embed the current page. This directive applies to ``, `