【发布时间】:2021-08-25 23:12:46
【问题描述】:
我通过 PHP,fastcgi 使用 nonce 生成:
fastcgi_param NONCE $nonce;
在我添加的 CSP 中(使用 NGINX + 标头):
script-src 'strict-dynamic' 'nonce-${nonce}'
整个 CSP 如下所示:
add_header Content-Security-Policy "default-src 'none'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'; script-src 'strict-dynamic' 'nonce-${nonce}'; script-src-elem 'self'; connect-src 'self' 'nonce-${nonce}' https://www.w3.org; img-src 'self'; font-src 'self'; style-src 'self'";
通过 PHP 添加:
nonce="<?= $_SERVER['NONCE']?>"
在 DevTools 中我只看到:
nonce
没有:
nonce="number"
对吗?
已按原样解决。
主要问题是我在 DevTools 中看到来自使用 lottie player 脚本的错误,它说我不使用随机数或我肯定会使用的 sha hash。
错误如下:
lottie-player.js:1 Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-4Czhp/5smweUxQ0BkhMh9cmbPz4zsdDHhq70HOQBcHY='), or a nonce ('nonce-...') is required to enable inline execution. Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the 'unsafe-hashes' keyword is present.
通过以下方式使用的每个彩票都会发生错误:
<lottie-player></lottie-player>
这是脚本调用:
<script type="text/javascript" src="/lottie-player.js" nonce="<?= $_SERVER['REQUEST_ID'] ?>"></script>
用法:
<lottie-player src="/main.json" background="transparent" speed="1" autoplay></lottie-player>
我还尝试将 nonce 直接添加到 lottie-player,如下所示:
<lottie-player src="/main.json" background="transparent" speed="1" autoplay nonce="<?= $_SERVER['REQUEST_ID'] ?>"></lottie-player>
但这不是解决办法,这就是我寻求帮助的原因。
【问题讨论】:
-
请分享更多细节。您尝试过什么来解决问题?你被困在哪里了?
-
“在 DevTools 中我只看到:
nonce没有:nonce="number"是否正确?” - 是的,我认为应该如此。浏览器必须隐藏该值以防止其他脚本访问(另请参见MDN: Accessing nonces and nonce hiding),因此很可能以同样的方式从开发工具中隐藏它。如果您想验证它是否正确填充,您需要检查浏览器收到的实际 HTML 源代码。 -
@NicoHaase,我无法解决这个问题:
lottie-player.js:1 Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-4Czhp/5smweUxQ0BkhMh9cmbPz4zsdDHhq70HOQBcHY='), or a nonce ('nonce-...') is required to enable inline execution. Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the 'unsafe-hashes' keyword is present.随机数已添加到脚本中,但我仍然收到此错误。 -
@CBroe,感谢您的澄清!
-
请通过编辑为您的问题添加所有说明。这不仅应包含错误消息,还应包含所涉及的代码和您解决问题的尝试
标签: javascript php security web content-security-policy