我正在尝试在 .NET api 项目中配置内容安全策略,当我运行
dotnet watch
时,我在控制台中收到错误。
我有 .NET 7.0 ASP.NET Web API 和 React 客户端。
我已经阅读了很多与此相关的主题和问题,但似乎仍然没有取得任何成功。
错误信息:
[Report Only] 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-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='), or a nonce
('nonce-...') is required to enable inline execution.
我已将所有内联样式移至 CSS 文件中,但仍然出现相同的错误。 当我在 VSCode 中搜索样式时,没有找到内联样式代码的结果。
我对 cloudinary 和 Accuweather 也有类似的错误,但在我将它们添加到例外中后,它们立即消失了。
这是我在启动类中的代码:
app.UseCspReportOnly(opt => opt
.BlockAllMixedContent()
.StyleSources(s => s.Self().CustomSources(
"https://fonts.googleapis.com"
))
.FontSources(s => s.Self().CustomSources(
"https://fonts.gstatic.com",
"https://maps.gstatic.com",
"data:"
))
.FormActions(s => s.Self())
.FrameAncestors(s => s.Self())
.ImageSources(s => s.Self().CustomSources(
"https://res.cloudinary.com",
"https://developer.accuweather.com",
"data:"
))
.ScriptSources(s => s.Self().CustomSources(
"https://connect.facebook.net",
"https://maps.googleapis.com"
))
);
这是我的html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description"
content="Web site created using vite" />
<link rel="icon" href="/logo.ico" />
<title>Твоята риболовна изстория</title>
<script type="module" crossorigin src="/assets/index-49eab6e8.js"></script>
<link rel="stylesheet" href="/assets/index-959f16a9.css">
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
我尝试修改manifest.json文件或向html文件添加元标记,正如其他答案中所述,但这些都没有帮助。
也许我错过了一些东西。寻求有关我缺少什么以及在哪里寻找问题的建议。如果需要更多信息,请发表评论。
很可能存在修改元素样式的脚本,这将被视为内联样式。检查错误消息中的代码链接以查看它是哪个脚本。如果是您自己的脚本,您应该修改 css 类,而不是直接修改样式。如果这是具有静态修改的第三方脚本,您可以使用哈希值。如果是第三方动态修改,你将替换/重写/接受较弱的CSP。