内容安全策略 header - 升级不安全请求并报告事件

Content security policy header - both upgrade insecure requests and report the events

我想要实现的目标:同时将页面内的所有 HTTP 请求升级到 HTTPS 和 report/log 事件。

服务器同时支持 HTTP 和 HTTPS。

测试HTML页面。请注意 <img>

中的硬编码 HTTP 协议
<html>
<head>
</head>
<body>
    <img src="http://example.com/testimage.png" />
</body>
</html>

根据 W3C documentation,我这样设置我的 .htaccess:

Header set Content-Security-Policy "upgrade-insecure-requests; default-src https:"
Header set Content-Security-Policy-Report-Only "default-src https:; report-uri https://report-uri.io/report/..."

当我调用 https://example.com/testpage.html 时,它通过 HTTPS 加载图像但不报告事件。

我错过了什么?

我不确定 upgrade-insecure-requestsblock-all-mixed-content 是否会生成事件,因为它们首先阻止了不良事件的发生。

我的推理可能是错误的,但我知道 block-all-mixed-content 不会产生违规行为。

对于 block-all-mixed-content,它明确不会发送报告。来自 https://www.w3.org/TR/mixed-content/#strict-opt-in:

This directive has no effect when monitored. This directive’s only effect is to set a policy flag on the protected resource; it will therefore never be violated, and has no reporting requirements.