files/en-us/web/security/practical_implementation_guides/csp/index.md
The Content-Security-Policy HTTP header provides fine-grained control over the code that can be loaded on a site, and what it is allowed to do.
The main problem this article focuses on is cross-site scripting ({{Glossary("Cross-site_scripting", "XSS")}}) attacks. These are generally due to a lack of control and awareness of the sources from which site resources are loaded. This problem gets more difficult to manage as sites become larger and more complex and increasingly rely on third-party resources such as JavaScript libraries.
[!NOTE] CSP is one part of a complete strategy for protecting against XSS attacks. There are other factors involved, such as output encoding and sanitization, which are also important.
CSP can also help to fix other problems, which are covered in other articles:
frame-ancestors directive.upgrade-insecure-requests directive. See Upgrading insecure requests.Implementing a strict CSP is the best way to mitigate XSS vulnerabilities with CSP. This uses nonce- or hash-based fetch directives to ensure that only scripts and/or styles that include the correct {{Glossary("Nonce", "nonce")}} or hash will be executed. JavaScript inserted by a hacker will simply not run.
Strict CSPs also:
onclick. This prevents improperly-escaped user inputs from being interpreted by the web browser as JavaScript.eval(), which is another effect of the script-src directive.object-src 'none'.<base> element to set a base URI via base-uri 'none';.Strict CSPs are preferred over location-based policies, also called allowlist policies, where you specify which domains scripts can be run from. This is because allowlist policies often end up allowing unsafe domains, which defeats the entire point of having a CSP, and they can get very large and unwieldy, especially if you are trying to permit services that require many third party scripts to function.
Implement a strict CSP, then start to pinpoint resources that are failing to load as a result of the policy, taking steps to work around these issues.
[!NOTE] Before implementing any actual CSP with the
Content-Security-Policyheader, you are advised to first test it out using theContent-Security-Policy-Report-OnlyHTTP header; see Report-only CSPs below.
nonce attributes by the server. If you are instead using hashes, external scripts should have the correct hash inserted into integrity attributes.strict-dynamic directive, which gives scripts loaded by the first script the same level of trust without being explicitly given a nonce or hash.eval(). For example, replace inline event handlers with addEventListener() calls inside scripts.object-src 'none'.eval(), you can add the unsafe-eval keyword to your strict CSP to allow them, although this makes the CSP significantly weaker.unsafe-hashes keyword to your strict CSP to allow them. This is somewhat unsafe, but much safer than allowing all inline JavaScript.If you are unable to get a strict CSP to work, an allowlist-based CSP is much better than none, and a CSP like default-src https: still provides some protection, disabling unsafe inline/eval() and only allowing loading of resources (images, fonts, scripts, etc.) over HTTPS.
[!WARNING] If at all possible, avoid including unsafe sources inside your CSP. Examples include:
unsafe-inline.data:URIs insidescript-src,object-src, ordefault-src.- Overly broad sources or form submission targets.
If you are unable to use the Content-Security-Policy header, pages can instead include a <meta http-equiv="Content-Security-Policy" content="…"> element. This should be the first {{htmlelement("meta")}} element that appears inside the document {{htmlelement("head")}}.
Before implementing any actual CSP with the Content-Security-Policy header, you are advised to first test it out using the Content-Security-Policy-Report-Only HTTP header. This allows you to see if any violations would have occurred with that policy.
Sites should use the report-to and report-uri reporting directives. These cause the browser to POST JSON reports about CSP violations to endpoints (specified in the {{httpheader("Reporting-Endpoints")}} header in the case of report-to). This allows CSP violations to be caught and repaired quickly.
[!NOTE] The
report-todirective is preferred over the deprecatedreport-uridirective. However, both are still needed becausereport-todoes not yet have full cross-browser support.