mobile-safari - 内容安全策略在台式机上按预期工作,但在移动设备上不起作用

标签 mobile-safari content-security-policy

在自己的网站上制定内容安全策略是在自己的网站上提供额外安全层的好方法。

我有一个内容安全策略,可以在桌面上按预期工作,但它破坏了移动设备(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/

相关文章:

css - 如何使CSS中的按钮在所有设备上看起来相同?

javascript - Symfony 3 - 内容安全政策

ios - 如何在 ios safari 上禁用支持通用链接的页面的 "open in app"横幅?

iphone - 移动Safari : Disable scrolling pages "out of screen"

html - 在 HTML 数字输入中显示尾随的小数零

angularjs - 拒绝在 Ionic Framework 中加载字体 '' 数据 :font/woff;base64, ...."

Azure 创建拒绝但更新允许

content-security-policy - 违反 CSP 规则的本地托管字体

amazon-s3 - 内容安全策略标准是否支持通配符路径?如果没有,为什么不呢?

ios - CSS3 - 通过 JavaScript 为 margin-left 属性设置动画