Content Security Policy

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Content Security Policy (CSP) is a computer security concept applied to prevent cross-site scripting (XSS) attacks and other related attacks.[1] It is a Candidate Recommendation of the W3C working group on Web Application Security.[2] CSP provides a standard HTTP header that allows website owners to declare approved sources of content that browsers should be allowed to load on that page — covered types are JavaScript, CSS, HTML frames, fonts, images and embeddable objects such as Java applets, ActiveX, audio and video files.


CSP was originally developed by the Mozilla Foundation and was first implemented in Firefox 4. As of 2012 the CSP is a W3C candidate.[3] The following header names are in use as part of an experimental CSP implementations:[4]

  • Content-Security-Policy — standard header name proposed by the W3C document. Google Chrome supports this as of version 25.[5] Firefox supports this as of version 23,[6] released on 6 August 2013.[7] WebKit supports this as of version 528 (nightly build).[8]
  • X-WebKit-CSP — experimental header introduced into Google Chrome and other WebKit-based browsers (Safari) in 2011.[9]
  • X-Content-Security-Policy — experimental header introduced in Gecko 2 based browsers (Firefox 4 to Firefox 22, Thunderbird 3.3, SeaMonkey 2.1).[10]

Support for the sandbox directive is also available in Internet Explorer 10 and Internet Explorer 11 using the experimental X-Content-Security-Policy header.[11]

New CSP 1.1 specification is being developed by W3C.[12]

There's initial support for CSP in some web frameworks such as AngularJS[13] and Django.[14] Instructions for Ruby on Rails have been posted by GitHub.[15]

Mode of operation[edit]

If the Content-Security-Policy header is present in the server response, a compliant client enforces the declarative whitelist 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 (e.g. <script></script>, DOM event attributes like onclick, and anchor tags with an href value that starts with "javascript:") is blocked - all script code must reside in separate files, served from a whitelisted domain (can be enabled by unsafe-inline),
  • Dynamic code evaluation (via eval() and string arguments for both setTimeout and setInterval) are blocked (can be enabled by unsafe-eval)

Recommended coding practice for CSP-compatible web applications is to load code from external source files (<script src>), parse JSON instead of evaluating it and use inline functions for other statements.[16]

In addition to restricting execution of JavaScript, a policy can specify from where resources can be loaded for a given page. This includes CSS, JavaScript, images, frames, applets, Ajax, etc.[17]


Anytime a requested resource or script execution violates the policy, the browser will fire a POST request to the value specified in report-uri[18] containing details of the violation.

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

Browser add-ons and extensions exemption[edit]

According to the CSP Processing Model,[22] CSP should not interfere with the operation of browser add-ons or extensions installed by the user. This feature of CSP effectively allows any add-on or extension to inject script into web sites, regardless of the origin of that script, and thus be exempt to CSP policies. 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.[23][24]

See also[edit]


  1. ^ Sid Stamm (2009-03-11). "Security/CSP/Spec - MozillaWiki". Retrieved 2011-06-29. Content Security Policy is intended to help web designers or server administrators specify how content interacts on their web sites. It helps mitigate and detect types of attacks such as XSS and data injection. 
  2. ^ "State of the draft". 2011-11-30. Retrieved 2011-12-30. 
  3. ^ "Content Security Policy 1.0". W3C Candidate Recommendation. 15 November 2012. Retrieved February 22, 2013. 
  4. ^ "Can I use Content Security Policy?". Fyrd. Retrieved February 22, 2013. 
  5. ^ "Chrome 25 Beta: Content Security Policy and Shadow DOM". Google. January 14, 2013. Retrieved February 22, 2013. 
  6. ^ "Content Security Policy 1.0 lands in Firefox Aurora". Mozilla Foundation. May 29, 2013. Retrieved June 16, 2013. 
  7. ^ "RapidRelease/Calendar". Mozilla Foundation. May 29, 2013. Retrieved June 16, 2013. 
  8. ^ "Bug 96765 - Implement the "Content-Security-Policy" header". WebKit. October 31, 2012. Retrieved August 7, 2015. 
  9. ^ "New Chromium security features, June 2011". Google. June 14, 2011. Retrieved February 22, 2013. 
  10. ^ "Introducing Content Security Policy". Mozilla Foundation. Retrieved February 22, 2013. 
  11. ^ "Defense in Depth: Locking Down Mash-Ups with HTML5 Sandbox". Windows Internet Explorer Engineering Team. Retrieved April 13, 2014. 
  12. ^ "Proposals for Version 1.1". W3C. Retrieved March 22, 2013. 
  13. ^ "ngCsp directive". AngularJS. 
  14. ^ "django-security". 
  15. ^ "Content security policy". GitHub. 
  16. ^ West, Mike (June 15, 2012). "An Introduction to Content Security Policy". HTML5 Rocks. Retrieved February 22, 2013. 
  17. ^
  18. ^
  19. ^ For example in Django a CSP receiver is available in django-security module.
  20. ^ "Content Security Policy Builder". 
  21. ^ "Content Security Policy Reporting". Scott Helme. 
  22. ^ "CSP Processing Model". 2012-11-15. Retrieved 2013-10-06. 
  23. ^ "Subverting CSP policies for browser add-ons (extensions).". 2013-09-25. Retrieved 2013-10-06. 
  24. ^ "Re: [CSP] Request to amend bookmarklet/extensions sentence in CSP1.1". 2014-08-03. Retrieved 2015-10-08. 
  25. ^
  26. ^
  27. ^
  28. ^

External links[edit]