【发布时间】:2021-11-23 06:05:28
【问题描述】:
我已将 script-src CSP 指令添加到我的 AWS CloudFront 分配中,我的 javascript 将无法运行。在浏览器控制台中,我可以看到以下错误:
Refused to execute inline script because it violates the following Content Security
Policy directive: "default-src 'self' <<LIST OF DOMAINS>>
Either the 'unsafe-inline' keyword, a hash ('sha256-123abc='), or a nonce ('nonce-...') is required
to enable inline execution. Note also that 'script-src' was not explicitly set, so 'default-src' is used
as a fallback.
但是,我的 CSP 中包含以下代码:
; script-src 'sha256-123abc=' "
当我删除哈希并使用“self”时,会选择 script-src 指令,但这样我的脚本就无法运行……有人知道问题可能是什么吗?
【问题讨论】:
-
(1) Chrome 诊断有时会误导(在最新版本中已修复)。内联脚本可以是
<script>标签或<tag onclick='...'>或<a href='javascript:...'>。最后两个不能通过使用 ``hash-value'` 来解决,但是 Chrome 提供了通过哈希来允许这些。 (2)'sha256-123abc='和'self'可以同时使用,为什么在添加'hash'的时候要去掉'self'?你如何定义script-src指令“拾取”与否?
标签: amazon-cloudfront content-security-policy