我正在尝试在内容安全策略中使用哈希...
以下是我的控制台中出现的两个错误示例:
Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' apis.google.com cdn.iubenda.com cdnjs.cloudflare.com www.googletagmanager.com". Either the 'unsafe-inline' keyword, a hash ('sha256-oKmCrr+GWRARSXYeVJshOWETr0oqOtt73CNO8efpujQ='), or a nonce ('nonce-...') is required to enable inline execution.
Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' apis.google.com cdn.iubenda.com cdnjs.cloudflare.com www.googletagmanager.com". Either the 'unsafe-inline' keyword, a hash ('sha256-pS4Uy3ilo+JLn8IadtJGfyO9z7jqIrGUONfEUDLxoPk='), or a nonce ('nonce-...') is required to enable inline execution.
这是相应的内容安全策略指令:
add_header Content-Security-Policy "default-src 'self'; script-src 'self' apis.google.com cdn.iubenda.com cdnjs.cloudflare.com www.googletagmanager.com; style-src 'self' fonts.googleapis.com; img-src 'self' cdn.shortpixel.ai secure.gravatar.com; font-src 'self' fonts.googleapis.com fonts.gstatic.com";
特别是在这个例子中:
script-src 'self' apis.google.com cdn.iubenda.com cdnjs.cloudflare.com www.googletagmanager.com;
根据我阅读的哈希方式指南,我应该能够像在控制台中一样将哈希值添加到指令中...
最简单的生成哈希值的方法是打开开发者工具控制台,它会输出您的脚本的期望哈希值。
但是,如果我修改指令以包含哈希(如下所示),我在控制台中仍然会收到相同的错误(显然哈希值不同)。
script-src 'self' apis.google.com cdn.iubenda.com cdnjs.cloudflare.com www.googletagmanager.com 'sha256-oKmCrr+GWRARSXYeVJshOWETr0oqOtt73CNO8efpujQ=';
正确的CSP指令哈希方式是什么?同一指令为何会有多个错误,这是否基本上为每个指定域名都有一个错误?应该使用一个哈希覆盖所有指定的域名吗?
我不太确定自己该如何处理这个问题。