Content Security Policy (CSP) is a
computer security
Computer security (also cybersecurity, digital security, or information technology (IT) security) is a subdiscipline within the field of information security. It consists of the protection of computer software, systems and computer network, n ...
standard introduced to prevent
cross-site scripting
Cross-site scripting (XSS) is a type of security vulnerability that can be found in some web applications. XSS attacks enable attackers to inject client-side scripts into web pages viewed by other users. A cross-site scripting vulnerability may be ...
(XSS),
clickjacking
Clickjacking (classified as a user interface redress attack or UI redressing) is a Malware, malicious technique of tricking a User (computing), user into clicking on something different from what the user perceives, thus potentially revealing Con ...
and other
code injection
Code injection is a computer security exploit where a program fails to correctly process external data, such as user input, causing it to interpret the data as executable commands. An attacker using this method "injects" code into the program whi ...
attacks resulting from execution of malicious content in the trusted
web page
A web page (or webpage) is a World Wide Web, Web document that is accessed in a web browser. A website typically consists of many web pages hyperlink, linked together under a common domain name. The term "web page" is therefore a metaphor of pap ...
context.
It is a Candidate Recommendation of the
W3C
The World Wide Web Consortium (W3C) is the main international standards organization for the World Wide Web. Founded in 1994 by Tim Berners-Lee, the consortium is made up of member organizations that maintain full-time staff working together in ...
working group on Web Application Security,
widely supported by modern
web browser
A web browser, often shortened to browser, is an application for accessing websites. When a user requests a web page from a particular website, the browser retrieves its files from a web server and then displays the page on the user's scr ...
s.
CSP provides a standard method for website owners to declare approved origins of content that browsers should be allowed to load on that website—covered types are
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
,
CSS,
HTML frames,
web worker
A web worker, as defined by the World Wide Web Consortium (W3C) and the Web Hypertext Application Technology Working Group (WHATWG), is a JavaScript script executed from an HTML page that runs in the background, independently of scripts that m ...
s,
fonts
In movable type, metal typesetting, a font is a particular #Characteristics, size, weight and style of a ''typeface'', defined as the set of fonts that share an overall design.
For instance, the typeface Bauer Bodoni (shown in the figure) inclu ...
, images, embeddable objects such as
Java applet
Java applets were applet, small applications written in the Java (programming language), Java programming language, or another programming language that Compiled language, compiles to Java bytecode, and delivered to users in the form of Ja ...
s,
ActiveX
ActiveX is a deprecated software framework created by Microsoft that adapts its earlier Component Object Model (COM) and Object Linking and Embedding (OLE) technologies for content downloaded from a network, particularly from the World Wide W ...
, audio and video files, and other
HTML5
HTML5 (Hypertext Markup Language 5) is a markup language used for structuring and presenting hypertext documents on the World Wide Web. It was the fifth and final major HTML version that is now a retired World Wide Web Consortium (W3C) recommend ...
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:
*
Content-Security-Policy
– standard header name proposed by the W3C document.
Google Chrome
Google Chrome is a web browser developed by Google. It was first released in 2008 for Microsoft Windows, built with free software components from Apple WebKit and Mozilla Firefox. Versions were later released for Linux, macOS, iOS, iPadOS, an ...
supports this as of version 25. Firefox supports this as of version 23, released on 6 August 2013.
WebKit
WebKit is a browser engine primarily used in Apple's Safari web browser, as well as all web browsers on iOS and iPadOS. WebKit is also used by the PlayStation consoles starting with the PS3, the Tizen mobile operating systems, the Amazon K ...
supports this as of version 528 (nightly build). Chromium-based
Microsoft Edge
Microsoft Edge is a Proprietary Software, proprietary cross-platform software, cross-platform web browser created by Microsoft and based on the Chromium (web browser), Chromium open-source project, superseding Edge Legacy. In Windows 11, Edge ...
support is similar to Chrome's.
*
X-WebKit-CSP
– deprecated, experimental header introduced into
Google Chrome
Google Chrome is a web browser developed by Google. It was first released in 2008 for Microsoft Windows, built with free software components from Apple WebKit and Mozilla Firefox. Versions were later released for Linux, macOS, iOS, iPadOS, an ...
,
Safari
A safari (; originally ) is an overland journey to observe wildlife, wild animals, especially in East Africa. The so-called big five game, "Big Five" game animals of Africa – lion, African leopard, leopard, rhinoceros, African elephant, elep ...
and other WebKit-based web browsers in 2011.
*
X-Content-Security-Policy
– 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
meta tag
Meta most commonly refers to:
* Meta (prefix), a common affix and word in English ( in Greek)
* Meta Platforms, an American multinational technology conglomerate (formerly ''Facebook, Inc.'')
Meta or META may also refer to:
Businesses
* Meta (ac ...
, although in this case its effectiveness will be limited.
Internet Explorer 10
Internet Explorer 10 (IE10) is the tenth version of the Internet Explorer web browser and the successor to Internet Explorer 9, released by Microsoft on September 4, 2012. It is the default browser on Windows 8 and Windows Server 2012, and wa ...
and
Internet Explorer 11
Internet Explorer 11 (IE11) is the eleventh and final version of the Internet Explorer web browser, by now retired. It was initially included in the release of Windows 8.1, Windows RT, Windows RT 8.1 and Windows Server 2012 R2 on October 17, 2013 ...
also support CSP, but only sandbox directive, using the experimental
X-Content-Security-Policy
header.
A number of web application frameworks support CSP, for example
AngularJS
AngularJS (also known as Angular 1) is a discontinued free and open-source JavaScript-based web framework for developing single-page applications. It was maintained mainly by Google and a community of individuals and corporations. It aimed to si ...
(natively) and
Django (middleware). Instructions for
Ruby on Rails
Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web pa ...
have been posted by
GitHub
GitHub () is a Proprietary software, proprietary developer platform that allows developers to create, store, manage, and share their code. It uses Git to provide distributed version control and GitHub itself provides access control, bug trackin ...
. Web framework support is however only required if the CSP contents somehow depend on the web application's state—such as usage of the
nonce
origin. Otherwise, the CSP is rather static and can be delivered from
web application tiers above the application, for example on
load balancer
In computing, load balancing is the process of distributing a set of tasks over a set of resources
''Resource'' refers to all the materials available in our environment which are Technology, technologically accessible, Economics, economically ...
or
web server
A web server is computer software and underlying Computer hardware, hardware that accepts requests via Hypertext Transfer Protocol, HTTP (the network protocol created to distribute web content) or its secure variant HTTPS. A user agent, co ...
.
Bypasses
In December 2015 and December 2016, a few methods of bypassing
'nonce'
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
Content-Security-Policy
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
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
code
**
blocks,
**
DOM event handlers as HTML attributes (e.g.
onclick
)
** The
javascript:
links
* Inline
CSS statements
**
block
**
style
attributed to HTML elements
* Dynamic
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
code evaluation
**
eval()
** string arguments for
setTimeout
and
setInterval
functions
**
new Function()
constructor
* Dynamic
CSS statements
**
CSSStyleSheet.insertRule()
method
While using CSP in a new application may be quite straightforward, especially with CSP-compatible
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
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
JSON (JavaScript Object Notation, pronounced or ) is an open standard file format and electronic data interchange, data interchange format that uses Human-readable medium and data, human-readable text to store and transmit data objects consi ...
instead of evaluating it and use
EventTarget.addEventListener()
to set event handlers.
Notes
Reporting
Any time a requested resource or script execution violates the policy, the browser will fire a
POST
request to the value specified in
report-uri
or
report-to
containing details of the violation.
CSP reports are standard
JSON
JSON (JavaScript Object Notation, pronounced or ) is an open standard file format and electronic data interchange, data interchange format that uses Human-readable medium and data, human-readable text to store and transmit data objects consi ...
structures and can be captured either by application's own
API
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
or public CSP report receivers.
In 2018 security researchers showed how to send false positive reports to the designated receiver specified in
report-uri
. 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
A bookmarklet is a bookmark stored in a web browser that contains JavaScript commands that add new features to the browser. They are stored as the URL of a bookmark in a web browser or as a hyperlink on a web page. Bookmarklets are usually small ...
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
The World Wide Web Consortium (W3C) is the main international standards organization for the World Wide Web. Founded in 1994 by Tim Berners-Lee, the consortium is made up of member organizations that maintain full-time staff working together in ...
Web Application Security Working Group considers such script to be part of the
Trusted Computing Base
The trusted computing base (TCB) of a computer system is the set of all hardware, firmware, and/or software components that are critical to its security, in the sense that bugs or vulnerabilities occurring inside the TCB might jeopardize the ...
implemented by the browser; however, it has been argued to the working group by a representative of
Cox Communications
Cox Communications, Inc. (also known as Cox Cable and formerly Cox Broadcasting Corporation, Dimension Cable Services and Times-Mirror Cable), is an American digital cable television provider, telecommunications and home automation services comp ...
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
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
,
CSS) are loaded from third-party servers (typically
CDNs)
* Mixed Content, to clarify the intended browser's policy on pages loaded over
HTTPS
Hypertext Transfer Protocol Secure (HTTPS) is an extension of the Hypertext Transfer Protocol (HTTP). It uses encryption for secure communication over a computer network, and is widely used on the Internet. In HTTPS, the communication protoc ...
and linking content over plaintext
HTTP
HTTP (Hypertext Transfer Protocol) is an application layer protocol in the Internet protocol suite model for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web, wher ...
* Upgrade Insecure Requests, hinting browsers on how to handle legacy links on pages migrated to
HTTPS
Hypertext Transfer Protocol Secure (HTTPS) is an extension of the Hypertext Transfer Protocol (HTTP). It uses encryption for secure communication over a computer network, and is widely used on the Internet. In HTTPS, the communication protoc ...
* Credential Management, a unified
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
API
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
to access user's credentials to facilitate complex login schemes,
* Referrer Policy, CSP extension to hint the browser on generation of the
Referer headers.
See also
*
Same-origin policy
In computing, the same-origin policy (SOP) is a concept in the web application security model. Under the policy, a web browser permits scripts contained in a first web page to access data in a second web page, but only if both web pages have the sa ...
*
NoScript
NoScript (or NoScript Security Suite) is a free and open-source extension for Firefox- and Chromium-based web browsers, written and maintained by Giorgio Maone, a software developer and member of the Mozilla Security Group.
Features
Active ...
– anti-XSS protection and Application Boundaries Enforcer (ABE), extension for
Firefox
Mozilla Firefox, or simply Firefox, is a free and open-source web browser developed by the Mozilla Foundation and its subsidiary, the Mozilla Corporation. It uses the Gecko rendering engine to display web pages, which implements curr ...
*
HTTP Switchboard – user defined CSP rules, extension for
Google Chrome
Google Chrome is a web browser developed by Google. It was first released in 2008 for Microsoft Windows, built with free software components from Apple WebKit and Mozilla Firefox. Versions were later released for Linux, macOS, iOS, iPadOS, an ...
and
Opera
Opera is a form of History of theatre#European theatre, Western theatre in which music is a fundamental component and dramatic roles are taken by Singing, singers. Such a "work" (the literal translation of the Italian word "opera") is typically ...
*
HTTP Strict Transport Security
HTTP Strict Transport Security (HSTS) is a policy mechanism that helps to protect websites against man-in-the-middle attacks such as protocol downgrade attacks and cookie hijacking. It allows web servers to declare that web browsers (or other c ...
*
HTTP Public Key Pinning
References
{{Reflist, 30em
External links
Content Security Policy W3C Working DraftContent Security Policy (CSP)on
MDN Web Docs
MDN Web Docs, previously Mozilla Developer Network and formerly Mozilla Developer Center, is a documentation repository and learning resource for web developers. It was started by Mozilla in 2005 as a unified place for documentation about open ...
Computer security standards
Hacking (computer security)
Hypertext Transfer Protocol headers
Web applications
Web security exploits