Angular 元标记中不允许使用 Content-Security-Policy-Report-Only

Angular Content-Security-Policy-Report-Only disallowed in meta tag

我正在尝试为 Angular 应用程序启用仅报告模式的内容安全策略,但是当我尝试通过元标记注入它时,我在 Chrome 中收到一条不允许的消息(v64).

<meta http-equiv="Content-Security-Policy-Report-Only" content="default-src 'self'" />

The report-only Content Security Policy 'default-src 'self'' was delivered via a element, which is disallowed. The policy has been ignored.

我也尝试过使用 Edge (v41) 和 Firefox (v59),它们没有错误,但也没有控制台输出。我错过了配置步骤还是有解决方法?

Content-Security-Policy-Report-Only 策略只能用 HTTP 响应指定 header。 CSP spec explicitly disallows if from from being specified with a meta element:

Note: The Content-Security-Policy-Report-Only header is not supported inside a meta element. Neither are the report-uri, frame-ancestors, and sandbox directives.

除了使用 HTTP 响应 header 指定它之外,没有其他解决方法。

旧线程但是,在 Angular 8 中,添加元内容安全策略确实会向 Chrome 调试器工具中的控制台报告任何错误。我将它添加到 index.html 文件中。