在自己的网站上制定内容安全策略是在自己的网站上提供额外安全层的好方法。
我有一个内容安全策略,可以在桌面上按预期工作,但它破坏了移动设备(safari)上的网站。内容安全策略在元标记内。我正在使用随机数和哈希。在移动设备上,我收到错误消息,指出它拒绝执行内联脚本,因为它违反了包含哈希和随机数的内容安全策略指令。该错误还指出我需要代码中的哈希或随机数来执行代码,但它们已经存在于那里,这就是它在桌面上运行良好的方式。问题在于,在移动设备上,它表现得好像散列和随机数不存在一样。任何提示表示赞赏。
最佳答案
在 CSP 中,如果您包含 script-src
的随机数或 style-src
, unsafe-inline
如果浏览器理解随机数,将被忽略。因此,为了兼容不支持 CSP2 的旧版浏览器(例如,iOS 9 和更早版本的 Safari),请同时包含您的 nonce 和 unsafe-inline
.
较新的浏览器将遵循随机数并忽略 unsafe-inline
.较旧的浏览器无法理解 nonce,因此会退回到 unsafe-inline
。 .
见 https://csp.withgoogle.com/docs/strict-csp.html
script-src nonce-{random} 'unsafe-inline'
The nonce directive means that elements will be allowed to execute only if they contain a nonce attribute matching the randomly-generated value which appears in the policy.
Note: In the presence of a CSP nonce the unsafe-inline directive will be ignored by modern browsers. Older browsers, which don't support nonces, will see unsafe-inline and allow inline scripts to execute.
关于mobile-safari - 内容安全策略在台式机上按预期工作,但在移动设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50185456/