Content Security Policy

Content Security Policy (CSP) is a computer security standard introduced to prevent cross-site scripting (XSS), clickjacking and other code injection attacks resulting from execution of malicious content in the trusted web page context. It is a Candidate Recommendation of the W3C working group on Web Application Security, widely supported by modern web browsers. CSP provides a standard method for website owners to declare approved origins of content that browsers should be allowed to load on that website&mdash;covered types are JavaScript, CSS, HTML frames, web workers, fonts, images, embeddable objects such as Java applets, ActiveX, audio and video files, and other HTML5 features.

Status
The standard, originally named Content Restrictions, was proposed by Robert Hansen in 2004, first implemented in Firefox 4 and quickly picked up by other browsers. Version 1 of the standard was published in 2012 as W3C candidate recommendation and quickly with further versions (Level 2) published in 2014. , the draft of Level 3 is being developed with the new features being quickly adopted by the web browsers.

The following header names are in use as part of experimental CSP implementations:


 * – standard header name proposed by the W3C document. Google Chrome supports this as of version 25. Firefox supports this as of version 23, released on 6 August 2013. WebKit supports this as of version 528 (nightly build). Chromium-based Microsoft Edge support is similar to Chrome's.
 * – deprecated, experimental header introduced into Google Chrome, Safari and other WebKit-based web browsers in 2011.
 * – deprecated, experimental header introduced in Gecko 2 based browsers (Firefox 4 to Firefox 22, Thunderbird 3.3,  SeaMonkey 2.1).

A website can declare multiple CSP headers, also mixing enforcement and report-only ones. Each header will be processed separately by the browser.

CSP can also be delivered within the HTML code using a HTML META tag, although in this case its effectiveness will be limited.

Internet Explorer 10 and Internet Explorer 11 also support CSP, but only sandbox directive, using the experimental  header.

A number of web application frameworks support CSP, for example AngularJS (natively) and Django (middleware). Instructions for Ruby on Rails have been posted by GitHub. Web framework support is however only required if the CSP contents somehow depend on the web application's state—such as usage of the  origin. Otherwise, the CSP is rather static and can be delivered from web application tiers above the application, for example on load balancer or web server.

Bypasses
In December 2015 and December 2016, a few methods of bypassing   allowlisting origins were published. In January 2016, another method was published, which leverages server-wide CSP allowlisting to exploit old and vulnerable versions of JavaScript libraries hosted at the same server (frequent case with CDN servers). In May 2017 one more method was published to bypass CSP using web application frameworks code.

Mode of operation


If the  header is present in the server response, a compliant client enforces the declarative allowlist policy. One example goal of a policy is a stricter execution mode for JavaScript in order to prevent certain cross-site scripting attacks. In practice this means that a number of features are disabled by default:
 * Inline JavaScript code
 * blocks,
 * DOM event handlers as HTML attributes (e.g. )
 * The  links
 * Inline CSS statements
 * block
 * attributed to HTML elements
 * Dynamic JavaScript code evaluation
 * string arguments for  and   functions
 * constructor
 * Dynamic CSS statements
 * method
 * method

While using CSP in a new application may be quite straightforward, especially with CSP-compatible JavaScript framework, existing applications may require some refactoring—or relaxing the policy. Recommended coding practice for CSP-compatible web applications is to load code from external source files, parse JSON instead of evaluating it and use  to set event handlers.

Reporting
Any time a requested resource or script execution violates the policy, the browser will fire a  request to the value specified in  or   containing details of the violation.

CSP reports are standard JSON structures and can be captured either by application's own API or public CSP report receivers.

In 2018 security researchers showed how to send false positive reports to the designated receiver specified in. This allows potential attackers to arbitrarily trigger those alarms and might render them less useful in case of a real attack. This behaviour is intended and cannot be fixed, as the browser (client) is sending the reports.

Browser add-ons and extensions exemption
According to the original CSP (1.0) Processing Model (2012–2013), CSP should not interfere with the operation of browser add-ons or extensions installed by the user. This feature of CSP would have effectively allowed any add-on, extension, or Bookmarklet to inject script into web sites, regardless of the origin of that script, and thus be exempt from CSP policies.

However, this policy has since been modified (as of CSP 1.1 ) with the following wording. Note the use of the word "may" instead of the prior absolute "should (not)" wording:"Note: User agents may allow users to modify or bypass policy enforcement through user preferences, bookmarklets, third-party additions to the user agent, and other such mechanisms."The absolute "should" wording was being used by browser users to request/demand adherence to the policy and have changes installed in popular browsers (Firefox, Chrome, Safari) to support it. This was particularly contentious when sites like Twitter and GitHub started using strong CSP policies, which 'broke' the use of Bookmarklets.

The W3C Web Application Security Working Group considers such script to be part of the Trusted Computing Base implemented by the browser; however, it has been argued to the working group by a representative of Cox Communications that this exemption is a potential security hole that could be exploited by malicious or compromised add-ons or extensions.

Complementary measures
a number of new browser security standards are being proposed by W3C, most of them complementary to CSP:
 * Subresource Integrity (SRI), to ensure only known, trusted resource files (typically JavaScript, CSS) are loaded from third-party servers (typically CDNs)
 * Mixed Content, to clarify the intended browser's policy on pages loaded over HTTPS and linking content over plaintext HTTP
 * Upgrade Insecure Requests, hinting browsers on how to handle legacy links on pages migrated to HTTPS
 * Credential Management, a unified JavaScript API to access user's credentials to facilitate complex login schemes,
 * Referrer Policy, CSP extension to hint the browser on generation of the Referer headers.