gpt4 book ai didi

content-security-policy - 在 CSP 样式-src 中不尊重 Sha 哈希

转载 作者:行者123 更新时间:2023-12-04 11:41:14 28 4
gpt4 key购买 nike

我有一个使用 asp-validation-summary 的 ASP.NET Web 应用程序.因此,内联 style被添加到我页面中的 HTML 元素中。

当我运行我的应用程序时,这在控制台(Chrome v78.0.3904.108)中给了我以下错误:

Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self' https://fonts.googleapis.com". Either the 'unsafe-inline' keyword, a hash ('sha256-aqNNdDLnnrDOnTNdkJpYlAxKVJtLt9CtFLklmInuUAE='), or a nonce ('nonce-...') is required to enable inline execution.



所以我将哈希添加到我的 CSP 中,现在看起来像这样:
style-src 'self' https://fonts.googleapis.com 'sha256-aqNNdDLnnrDOnTNdkJpYlAxKVJtLt9CtFLklmInuUAE=';

当我加载我的页面时,我仍然收到类似的错误:

Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self' https://fonts.googleapis.com 'sha256-aqNNdDLnnrDOnTNdkJpYlAxKVJtLt9CtFLklmInuUAE='". Either the 'unsafe-inline' keyword, a hash ('sha256-aqNNdDLnnrDOnTNdkJpYlAxKVJtLt9CtFLklmInuUAE='), or a nonce ('nonce-...') is required to enable inline execution.



这是整个 CSP header :
X-Content-Security-Policy: default-src 'self'; object-src 'none'; frame-ancestors 'none'; sandbox allow-forms allow-same-origin allow-scripts; base-uri 'self'; upgrade-insecure-requests; style-src 'self' https://fonts.googleapis.com 'sha256-aqNNdDLnnrDOnTNdkJpYlAxKVJtLt9CtFLklmInuUAE='; font-src 'self' https://fonts.gstatic.com;

正如你所看到的,我已经添加了哈希,正如错误中所暗示的那样。它似乎也是一个 valid header .

但为什么这不起作用?

最佳答案

您必须添加 unsafe-hashes .unsafe-hashes允许启用特定的内联事件处理程序。如果您只需要允许内联事件处理程序而不是内联元素或 javascript: URL,则与使用 unsafe-inline 表达式相比,这是一种更安全的方法。
来源:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src

关于content-security-policy - 在 CSP 样式-src 中不尊重 Sha 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59057957/

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