从 Javascript 检测损坏的锁图标(混合安全/不安全内容)
我正在努力使网站在 HTTPS 下功能齐全。作为其中的一部分,我想确保我们永远不会“打破锁定”。也就是说,我们永远不应该在 SSL 页面上加载非 SSL 内容,这可能会根据浏览器触发警告或其他指示器。为了验证情况是否如此,我想做两件事:
- 编写 Selenium 测试来验证各种操作不会破坏锁定。
- 在 JS 中编写日志记录代码,在用户会话期间检查锁是否被破坏,如果是则记录回服务器。
有没有可以在JS中使用的方法来检查浏览器的HTTPS锁定图标的损坏/未损坏状态?或者等效地,当前页面内容的混合/非混合状态?
I'm working on making a site fully functional under HTTPS. As part of this, I'd like to ensure we never "break the lock." That is, we should never load non-SSL content on an SSL page, which can trigger a warning or other indicator depending on the browser. To verify this is the case, I'd like to do two things:
- Write Selenium tests that verify various actions don't break the lock.
- Write logging code in JS that checks during a user session whether the lock is broken, and logs back to the server if it is.
Is there any method that can be used in JS to check the broken / not broken state of the browser's HTTPS lock icon? Or equivalently, the mixed / non-mixed state of the current page's content?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法从 JavaScript 本身检测到这一点,但您可以使用 内容-Security-Policy (CSP) HTTP 标头指示浏览器将混合内容报告发送到您的服务器或第三方聚合服务。
以下是向第三方服务 report-uri.io 报告混合内容的 CSP 标头示例:
内容安全策略报告仅:默认 src https:;报告 uri https://report-uri.io/report/
这篇文章由 Report URI 的维护者撰写更详细地了解其工作原理。如果您愿意,还可以配置 CSP 标头以向您自己的 URL 报告。
You can't detect this from JavaScript itself, but you can use the Content-Security-Policy (CSP) HTTP header to instruct the browser to send reports of mixed content to either your server, or a third-party aggregation service.
Here's an example of a CSP header that reports mixed content to a third-party service, report-uri.io:
Content-Security-Policy-Report-Only: default-src https:; report-uri https://report-uri.io/report/<YOUR_NAME_HERE>
This article by the maintainer of Report URI goes into more detail about how this works. You can also configure your CSP header to report to your own URL if you prefer.
您可以遍历整个 DOM 并检查所有链接以确保它们是
https://
。You could iterate though the entire DOM and check all links to make sure they are
https://
.您可以使用 混合内容扫描,这是我编写的 PHP CLI 脚本,用于扫描您的网站是否存在混合内容内容。
从 CLI 运行此脚本,如下:
脚本本身将开始扫描并在运行时提供反馈。找到混合内容时,导致混合内容警告的 URL 将显示在屏幕上:
还可以传入包含要扫描的 URL 列表的文件,并将输出更改为 JSON。还支持忽略模式。
You can use Mixed Content Scan, a PHP CLI scripts I wrote which scans your site for Mixed Content.
Run this script from the CLI, a such:
The script itself will start scanning and give feedback whilst running. When Mixed Content is found, the URLs causing Mixed Content warnings will be shown on screen:
It's also possible to pass in a file containing a list of URLs to scan, and to change the output to JSON. Ignore patterns are also supported.