Content Security Policy

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

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.[1] It is a Candidate Recommendation of the W3C working group on Web Application Security,[2] widely supported by the modern web browsers.[3] 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, 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,[4] 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[5] and quickly with further versions (Level 2) published in 2014. As of 2015 draft of Level 3 is being developed with the new features being quickly adopted by the web browsers.[6]

The following header names are in use as part of an experimental CSP implementations:[3]

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

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 be also delivered within the HTML code using a HTML META tag, although in this case its effectiveness will be limited.[13]

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

A number of web application frameworks support CSP, for example AngularJS[15] (natively) and Django (middleware).[16] Instructions for Ruby on Rails have been posted by GitHub.[17] 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 or web server.

As of 2015 a number of new browser security standards are being proposed by W3C, most of them complementary to CSP:[18]

  • Sub-Resource 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, an 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.[18]

In December 2015 a method of bypassing 'nonce' whitelisting origins was published.[19] Another method leverages server-wide CSP whitelisting to exploit old and vulnerable versions of JavaScript libraries hosted at the same server (frequent case with CDN servers).[20]

Mode of operation

Mapping between HTML5 and JavaScript features and Content Security Policy controls

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 code[moo 1]
    • <script> blocks,[moo 2]
    • DOM event handlers as HTML attributes (e.g. onclick)
    • The javascript: links
  • Inline CSS statements
    • <style> block[moo 2]
    • style attributed to HTML elements
  • Dynamic JavaScript code evaluation[moo 3]
    • 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 framework,[moo 4] 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 (<script src>), parse JSON instead of evaluating it and use EventTarget.addEventListener() to set event handlers.[21]

Notes

  1. This behavior can be disabled globally by a special 'unsafe-inline' statement
  2. 2.0 2.1 Trusted inline <script> and <style> blocks can individually whitelisted in the CSP using nonce or hash statements.
  3. This behavior can be disabled globally by a special 'unsafe-eval' statement
  4. For example, AngularJS requires only one initialization flag to be switched into the CSP-compatible mode — <html ng-app ng-csp>

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[22] containing details of the violation.

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

Browser add-ons and extensions exemption

According to the CSP Processing Model,[26] 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.[27][28]

See also

References

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.
  3. 3.0 3.1 Lua error in package.lua at line 80: module 'strict' not found.
  4. Lua error in package.lua at line 80: module 'strict' not found.
  5. Lua error in package.lua at line 80: module 'strict' not found.
  6. Lua error in package.lua at line 80: module 'strict' not found.
  7. Lua error in package.lua at line 80: module 'strict' not found.
  8. Lua error in package.lua at line 80: module 'strict' not found.
  9. Lua error in package.lua at line 80: module 'strict' not found.
  10. Lua error in package.lua at line 80: module 'strict' not found.
  11. Lua error in package.lua at line 80: module 'strict' not found.
  12. Lua error in package.lua at line 80: module 'strict' not found.
  13. Lua error in package.lua at line 80: module 'strict' not found.
  14. Lua error in package.lua at line 80: module 'strict' not found.
  15. Lua error in package.lua at line 80: module 'strict' not found.
  16. Lua error in package.lua at line 80: module 'strict' not found.
  17. Lua error in package.lua at line 80: module 'strict' not found.
  18. 18.0 18.1 Lua error in package.lua at line 80: module 'strict' not found.
  19. Lua error in package.lua at line 80: module 'strict' not found.
  20. Lua error in package.lua at line 80: module 'strict' not found.
  21. Lua error in package.lua at line 80: module 'strict' not found.
  22. http://www.w3.org/TR/CSP/#example-violation-report
  23. For example in Django a CSP receiver is available in django-security module.
  24. Lua error in package.lua at line 80: module 'strict' not found.
  25. Lua error in package.lua at line 80: module 'strict' not found.
  26. Lua error in package.lua at line 80: module 'strict' not found.
  27. Lua error in package.lua at line 80: module 'strict' not found.
  28. Lua error in package.lua at line 80: module 'strict' not found.
  29. https://addons.mozilla.org/en-US/firefox/addon/noscript/
  30. http://noscript.net/
  31. https://chrome.google.com/webstore/detail/http-switchboard/mghdpehejfekicfjcdbfofhcmnjhgaag
  32. https://addons.opera.com/en/extensions/details/http-switchboard/

External links