gpt4 book ai didi

content-security-policy - 正确使用散列与内容安全策略 (CSP)

转载 作者:行者123 更新时间:2023-12-05 06:56:31 24 4
gpt4 key购买 nike

我正尝试在我的内容安全策略中使用哈希...

以下是我的控制台中的两个示例错误:

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;

根据我阅读 CSP guide on hashes 的内容,我应该能够根据我的控制台将哈希添加到指令中......

The easiest way to generate it is to just open the developer tools console and it will output what the expected hash of your script was in the console error message.

但是如果我修改我的指令以包含散列(下面的示例),我仍然会在控制台中遇到相同的错误(显然是不同的散列)。

script-src 'self' apis.google.com cdn.iubenda.com cdnjs.cloudflare.com www.googletagmanager.com 'sha256-oKmCrr+GWRARSXYeVJshOWETr0oqOtt73CNO8efpujQ=';

散列 CSP 指令的正确方法究竟是怎样的?为什么同一个指令有多个错误,这基本上是针对每个指定的域吗?一个哈希是否应该覆盖所有指定的域?

不太确定我应该怎么做。

最佳答案

From what I gathered from reading thecontent-security-policy.com/hash/ CSP guide on hashes, I should beable to add the hash as per my console to the directive...

是的,它只是“理论上”有效,“实践”更难。是的,Google Chrome 计算哈希值,但您需要仔细阅读错误消息以确定真正被阻止的内容:inline script , javascript: navigationinline event handler .因为每一个都有自己的修复方法。
- 内联脚本只能由“sha256-VALUE” token 允许。
- 要允许 javascript:导航和内联事件处理程序,您需要使用带有 “unsafe-hashes” 的“sha256-VALUE”标记。并不是所有的浏览器都支持 javascript 的 'unsafe-hashes':目前的导航。

But if I modify my directive to include the hash (example below), Istill get the same error in console (obviously with a different hash).

你为什么停下来?我看到你使用 www.googletagmanager.com (GTM),你认为 GTM 只有一个内联脚本吗?您允许了父脚本,它开始加载子脚本,所以您需要两个的哈希值。
您可以使用父脚本哈希 + 'strict-dynamic' token 来允许所有子脚本,但它目前在 Safari 中不起作用。

在最后,您将获得所有内联脚本的大量哈希值。不好的是 GTM 和其他人可能会不时更改内联脚本的内容,因此您必须添加新的散列并删除过时的。但是您不知道哪个脚本属于哪个哈希。

因此,更好的方法是对任何内联脚本使用'nonce-value',自从 GTM distributes 'nonce' to all inline scripts 以来更是如此。除了自定义 HTML 标签。对于自定义 HTML 标记(如果使用),您可以使用哈希,因为这些脚本在您的控制之下。

最好先手动调查所有内联脚本,然后再决定如何更轻松可靠地允许它们。

PS:GTM 是 CSP 的难点,因为 GTM 可用于注入(inject)内联/外部脚本的开放列表。如果使用自定义 JavaScript 变量名称用于 «Custom HTML tag»,则需要允许 'unsafe-eval'。
你可以test your GTM它加载了哪些额外脚本的 ID 以及哪个 CSP 就足够了。

关于content-security-policy - 正确使用散列与内容安全策略 (CSP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65106785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com