内容安全策略是否向前兼容?

Is Content Security Policy forward compatible?

如果在不理解hash-source的浏览器中使用script-src: hash-source,浏览器会忽略所有script-src:,甚至忽略所有CSP吗?还是只会忽略 hash-source 部分?

更一般地说,浏览器是否以向前兼容的方式实现 CSP?

不理解哈希源元素的浏览器可能会在控制台中发出警告,但也可能不会。推荐的方法是使用用户代理嗅探来检测支持或将 'unsafe-inline' 与您的哈希源值一起发送。

理解哈希源的用户代理将忽略 'unsafe-inline',而那些不理解的用户代理将回退到 'unsafe-inline'。所以它 向后 兼容。

oreoshake 所说的向后兼容性是准确的。 section 6.6.2.2 of the CSP draft standard 中描述了确定元素匹配的过程:在 hash-sourcenonce-source 存在的情况下,符合用户代理会忽略 unsafe-inline

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".

此外,CSP 2指定parsing a source list with unknown tokens的过程如下:

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.

否则应该忽略。很明显,作者打算至少实现一定程度的向前兼容性。