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

标签 .htaccess content-security-policy response-headers

我想要达到的目标:两者都将页面内的所有 HTTP 请求升级到 HTTPS 并报告/记录事件。

服务器同时支持 HTTP 和 HTTPS。

测试 HTML 页面。注意 <img> 中的硬编码 HTTP 协议(protocol)

<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 所有混合内容,它明确不会发送报告。来自 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.

关于.htaccess - 内容安全策略 header - 升级不安全请求和报告事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35748905/

相关文章:

javascript - Angular-Cli 的子资源完整性

javascript - YouTube 播放器无法在生产版本中工作

.htaccess - 部分 301 重定向和 SEO 策略

apache - htaccess文件以某种方式被缓存?

phantomjs - 在 PhantomJS 中禁用内容安全策略

rest - Spring Rest API 拦截器在每个/每个请求上添加响应 header

apache - 请求 header 与响应 header

aws-lambda - 如何在VTL模板中附加响应头

javascript - Emberjs 应用程序刷新索引以外的路由会出现 404 错误

php - 在 PHP 中使用 URL 的最后一部分作为查询字符串,而不是目录路径