Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to set a Content Security Policy on response to a non-HTML request?

I understand that I might set a CSP header for the main page of my site, say https://example.com, to restrict from where I can load scripts and other resources. But, if there is no CSP on the main page, how will the browser treat a policy on a subsequent request?

This is two examples in one:

  1. I browse to https://example.com and get back some HTML (no CSP header)
  2. The HTML includes a script from https://example.com/script.js [*]
  3. This script requests some data from the API by issuing a GET to https://api.example.com [*]
  4. The same script then opens a websocket to the same domain, https://api.example.com
  5. The same script then makes another request, POSTing externally to https://api.analytics.com

[*] In steps 2 and 3, what would happen if the script.js or the JSON response came back with a restrictve CSP, like Content-Security-Policy: default-src 'none'? Would any downstream requests be changed? Does the browser do anything to prevent any of the script's requests? Or are the allowed because the original page load did not include any CSP?

Thanks!

like image 419
lordbyron Avatar asked Jun 14 '16 18:06

lordbyron


People also ask

What is Content-Security-Policy in HTML?

Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS) and data injection attacks. These attacks are used for everything from data theft, to site defacement, to malware distribution.

Where do I put Content-Security-Policy in HTML?

To add this custom meta tag, you can go to www.yourStore.com/Admin/Setting/GeneralCommon and find Custom <head> tag and add this as shown in the image below. Content Security Policy protects against Cross Site Scripting (XSS) and other forms of attacks such as ClickJacking.

How do I disable Content-Security-Policy in HTML?

Click the extension icon to disable Content-Security-Policy header for the tab. Click the extension icon again to re-enable Content-Security-Policy header. Use this only as a last resort. Disabling Content-Security-Policy means disabling features designed to protect you from cross-site scripting.

Should I use Content-Security-Policy?

Why use the Content Security Policy? The primary benefit of CSP is preventing the exploitation of cross-site scripting vulnerabilities. When an application uses a strict policy, an attacker who finds an XSS bug will no longer be able to force the browser to execute malicious scripts on the page.


1 Answers

The current CSP spec (version 2) says (emphasis mine):

3.6. Policy applicability

Policies are associated with an protected resource, and enforced or monitored for that resource. If a resource does not create a new execution context (for example, when including a script, image, or stylesheet into a document), then any policies delivered with that resource are discarded without effect. Its execution is subject to the policy or policies of the including context.

CSPs only apply to resources that create a new execution context (i.e., a Web page), which includes only top-level documents, embedded objects like <iframe>s, and scripts loaded as Web Workers. If a resource is served over HTTP and that reource is not used in a way that creates a new execution context, the CSP has no effect.

Therefore, your scripts will behave identically whether or not they are served with a Content-Security-Policy header.

like image 111
apsillers Avatar answered Sep 20 '22 12:09

apsillers