这种应用内容安全策略的方法可以吗?

Is this method of applying content security policy ok?

我正在尝试将 CSP 添加到我的 Web 应用程序,我在我的索引页面中添加了以下元标记:

<meta http-equiv="Content-Security-Policy" content="img-src 'self' data:;default-src *;style-src 'self' http://* 'unsafe-inline';script-src 'self' http://* 'unsafe-inline' 'unsafe-eval';" />

还有以下我在 IIS 上的 web.config 文件:

<customHeaders>
     <add name="Content-Security-Policy" value="default-src 'self' 'unsafe-inline' http://*.domain.com;
                                                img-src 'self' http://*.domain.com data:"  />

      </customHeaders>

是否需要同时添加元标记和额外的 header?或者其中之一就足够了?

元标记政策是否覆盖自定义 header?

这个 script-src 'self' http://* 'unsafe-inline' 'unsafe-eval' 是否意味着我可以在我的代码中编写内联 JavaScript 并使用 eval 函数?此规则是否覆盖 headers 设置的策略? (因为据我所知,在 header 中我禁止使用内联 JavaScript 和 eval 函数)

我的最后一个问题是,如果我使用这些设置,我应该在我的 html 中使用 ng-csp 还是它的其他变体 ng-csp="no-unsafe-eval"

Is it necessary to add both meta tag and additional headers? or One of them is sufficient?

一个就够了。如果您可以让您的服务器在响应 header 中发送策略,那就更好了。您也不需要在 meta 元素中指定任何内容,这没有任何优势。

Does the meta tag policy override the custom header?

如果 meta 政策更严格,meta 政策只会覆盖 header 政策。看到这个答案:

What is happening when I have two CSP (Content Security Policies) policies - header & meta?

…引用了 CSP 的一部分,“向要执行的策略列表添加额外的策略只能进一步限制受保护资源的功能”

Does this script-src 'self' http://* 'unsafe-inline' 'unsafe-eval' mean that I can write inline JavaScript and use eval function inside my code?

如果您的 header 也为 script-src 指定了这些值,它就会这样做。但是您的 header 政策没有。所以浏览器使用最严格的策略,不管它在哪里指定。

Does this rule override the policy set by headers? (because as far as I know in headers I have prohibited usage of inline JavaScript and eval function)

不,正如 What is happening when I have two CSP (Content Security Policies) policies - header & meta? 答案所解释的那样,您不能通过在别处指定 less-strict 策略来覆盖严格策略。

因此,您最好只在一个地方指定所有政策值,即 header(而不是 meta)。