Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Content Security Policy forward compatible?

If script-src: hash-source is used in a browser that does not understand hash-source, will the browser ignore all of script-src:, or even all of the CSP? Or will it only ignore the hash-source part?

More generally, do browsers implement CSP in forward compatible manner?

like image 766
Jonas H. Avatar asked Dec 08 '16 08:12

Jonas H.


People also ask

Does CSP prevent XSS?

Content Security Policy (CSP) is a W3C standard introduced to prevent Cross-Site Scripting (XSS), clickjacking and other attacks as the result of code injection in a web page. It is a computer security standard recommended by W3C Working Group which is supported by almost all major modern web browsers.

Is CSP server side or client side?

It can be enabled on either client- or server-side, and is unobtrusive in unsupported browsers — that is, browsers that don't support CSP will ignore its directives and load the page as normal. CSP is supported by the current versions of all modern desktop browsers: Safari, Chrome, Firefox, and IE Edge.

How does a Content-Security-Policy work?

The Content Security Policy (CSP) is a protection standard that helps secure websites and applications against various attacks, including data injection, clickjacking, and cross-site scripting attacks. CSP implements the same-origin policy, ensuring that the browser only executes code from valid sources.

How do I know if CSP is enabled?

Once the page source is shown, find out whether a CSP is present in a meta tag. Conduct a find (Ctrl-F on Windows, Cmd-F on Mac) and search for the term “Content-Security-Policy”. If “Content-Security-Policy” is found, the CSP will be the code that comes after that term.


2 Answers

What oreoshake stated about backward compatibility is accurate. The process of determining an element match is described in section 6.6.2.2 of the CSP draft standard: In the presence of hash-source or nonce-source, unsafe-inline is ignored by conforming user agents:

A source list allows all inline behavior of a given type if it contains the keyword-source expression 'unsafe-inline', and does not override that expression as described in the following algorithm:

[...]

If expression matches the nonce-source or hash-source grammar, return "Does Not Allow".

Furthermore, CSP 2 specifies the process of parsing a source list with unknown tokens as follows:

For each token returned by splitting source list on spaces, if the token matches the grammar for source-expression, add the token to the set of source expressions.

Otherwise, it should be ignored. So clearly the authors intended at least a certain level of forward compatibility.

like image 83
Niklas B. Avatar answered Oct 26 '22 18:10

Niklas B.


Browsers that do not understand hash source elements may emit a warning in the console, but they may not as well. The recommended approach is to use user agent sniffing to detect support or send both 'unsafe-inline' with your hash source values.

User agents that understand hash sources will ignore the 'unsafe-inline' and those that do not will fallback to the 'unsafe-inline'. So it's backwards compatible.

like image 25
oreoshake Avatar answered Oct 26 '22 17:10

oreoshake