CSP 错误:拒绝构建,因为祖先违反了以下内容安全策略指令

问题描述 投票:0回答:1

我在这里看到了很多这样的问题。但没有一个答案对我有用。

这是我的设置:

我有一个使用 oidc 在 https://localhost:4200 上运行的 Angular 17 前端项目。 我有一个在 https://localhost:7001 上运行的 C# VS .NET 8 API 项目 我有一个在 https://localhost:6001 上运行的 C# VS .NET 8 Identity 项目

当我查看附加到 https://localhost:4200 的 Chrome 控制台日志时,我不断看到此错误。

拒绝构建“https://localhost:6001/”,因为祖先违反了以下内容安全策略指令:“frame-ancestors https://localhost:6001/”

我没有使用 iframe,但这显然是基于 URL 的 OIDC 内部的东西。

问题是,我在我的 CSP 政策中允许这样做。我得到了 CSP,但我只是不明白我错过了什么。 这是我在 https://localhost:6001 上的 CSP 策略

    var csp = "default-src 'self'; object-src 'self'; frame-ancestors https://localhost:6001/; allow-forms allow-same-origin allow-scripts; base-uri 'self';";

    if (!context.HttpContext.Response.Headers.ContainsKey("Content-Security-Policy"))
    {
        context.HttpContext.Response.Headers.Append("Content-Security-Policy", csp);
    }
    if (!context.HttpContext.Response.Headers.ContainsKey("X-Content-Security-Policy"))
    {
        context.HttpContext.Response.Headers.Append("X-Content-Security-Policy", csp);
    }

这对我来说确实是正确的,如果这是显而易见的,我很抱歉,我只是不明白为什么它不起作用?

c# visual-studio content-security-policy .net-8.0
1个回答
0
投票

Frame-ancestors 是关于谁可以嵌入页面。当您通过 localhost:7001 嵌入 localhost:6001 时,您必须在 localhost:6001 上设置 'frame-ancestors localhost:7001'。

还要检查您是否没有提供多项政策,因为您需要通过所有政策。

关于 X-Content-Security-Policy:除非您需要保护 IE 用户,否则您不需要这个,但无论如何,他们现在不能真正期望任何安全性。

© www.soinside.com 2019 - 2024. All rights reserved.