阻止来自潜在可信来源的混合内容(127.0.0.0/8) [英] Blocking mixed content from potentially trustworthy origins (127.0.0.0/8)

查看:71
本文介绍了阻止来自潜在可信来源的混合内容(127.0.0.0/8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未被阻止://stackoverflow.com/a/60487457/3664487>潜在可信赖的来源,其中包括从127.0.0.0到127.255.255.255的IP地址.可以将浏览器配置为阻止此类地址的混合内容吗?这样可以简化本地测试.

解决方案

我发现没有浏览器设置可将可能信任的域视为不信任,但是这是使127.0.0.1和不信任的几种方法域的行为相同,或生成通常会生成警告的项目报告.

XHR

对于XHR,将条目添加到您的 hosts 文件中就足够了(在Firefox 73.0.1和Chrome 80.0.3987中进行了测试).

 #/etc/hosts127.0.0.1 example.com 

https://example.com XHR请求"rel ="nofollow noreferrer"> http://example.com 将被混合内容"规则阻止.请注意,XHR仍是CORS的主体,并且可能还会被CORS政策阻止.

这也适用于WebSockets和几个混合内容示例).

如果您希望127.0.0.1像常规域名一样运行,则有两种选择:

  • 使用内容安全策略(CSP)完全阻止混合内容(这甚至可以帮助您确保网站的未来安全性)
  • 让浏览器生成可能会生成警告的元素报告

阻止混合内容

添加此CSP指令以仅允许HTTPS图像.

  Content-Security-Policy:image-src https: 

使用 default-src 而不是 image-src 来仅允许所有其他连接类型使用HTTPS.其他连接类型的列表和他们的指令.

生成报告

添加此CSP指令可使浏览器对可能被阻止的资源发布JSON报告.

  Content-Security-Policy-Report-Only:default-src https :;report-uri/您的端点 

这里有一些Express代码可以做到这一点.

 让cspCounter = 1;const CSP_VIOLATION_REPORT_ENDPOINT ='/csp-violation-report-endpoint';app.use((req,res,next)=> {res.set('Content-Security-Policy-Report-Only','default-src https :; report-uri $ {CSP_VIOLATION_REPORT_ENDPOINT}`);下一个();});app.post(CSP_VIOLATION_REPORT_ENDPOINT,(req,res)=> {const reportFile =`/tmp/csp-report-$ {cspCounter ++}.json`;req.pipe(fs.createWriteStream(reportFile));req.on('end',()=> res.send('ok'));fs.readFile(reportFile,(err,data)=> debug('csp-report')(err || JSON.parse(data.toString())));}); 

可通过 https://github.com/codebling/mixed-content获得测试服务器-测试

Mixed content isn't blocked for potentially trustworthy origins, including IP addresses from 127.0.0.0 to 127.255.255.255. Can browsers be configured to block mixed content for such addresses? This would make local testing easier.

解决方案

I have found no browser settings to treat potentially trusted domains as untrusted, BUT here are several options to make 127.0.0.1 and untrusted domains behave the same, or to generate a report of items that would normally generate a warning.

XHR

For XHR, adding an entry to your hosts file is enough (tested in Firefox 73.0.1 & Chrome 80.0.3987).

# /etc/hosts
127.0.0.1 example.com

XHR requests from https://example.com to http://example.com will be blocked by the Mixed Content rules. Note that XHR is still subject CORS and may additionally be blocked by the CORS policy.

This also applies to WebSockets and several other connection types.

<img> and other non-XHR

I have found no way to generate only a warning for images or other connection types (you can see a nearly-exhaustive list with examples at Mixed Content Examples).

There are two options if you wish 127.0.0.1 to behave as if it were a regular domain:

  • Block Mixed Content entirely (this may even help future-proof your site) using a Content Security Policy (CSP)
  • Get the browser to generate a report of elements which would have generated a warning

Blocking Mixed Content

Add this CSP directive to allow only HTTPS images.

Content-Security-Policy: image-src https:

Use default-src instead of image-src to allow only HTTPS for all other connection types. List of other connection types and their directives.

Generating a report

Add this CSP directive to get the browser to POST a JSON report of resources that would have been blocked.

Content-Security-Policy-Report-Only: default-src https:; report-uri /your-endpoint

Here's some Express code to do that.

let cspCounter = 1;
const CSP_VIOLATION_REPORT_ENDPOINT = '/csp-violation-report-endpoint';
app.use( (req, res, next) => {
  res.set('Content-Security-Policy-Report-Only', `default-src https:; report-uri ${CSP_VIOLATION_REPORT_ENDPOINT}`);
  next();
});
app.post(CSP_VIOLATION_REPORT_ENDPOINT, (req, res) => {
  const reportFile = `/tmp/csp-report-${cspCounter++}.json`;
  req.pipe(fs.createWriteStream(reportFile));
  req.on('end', () => res.send('ok'));  
  fs.readFile(reportFile, (err, data) => debug('csp-report')(err || JSON.parse(data.toString())) );
});

A test server is available at https://github.com/codebling/mixed-content-test

这篇关于阻止来自潜在可信来源的混合内容(127.0.0.0/8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆