前阵子我碰到了interesting security hole
<a href="http://someurl.here" target="_blank">Link</a>
看起来足够无害,但是有一个漏洞,因为默认情况下,正在打开的页面允许已打开的页面通过
window.opener
对其进行回调。有一些限制,可以跨域使用,但仍然可以做一些恶作剧window.opener.location = 'http://gotcha.badstuff';
现在,HTML有一种解决方法
<a href="http://someurl.here" target="_blank" rel="noopener noreferrer">Link</a>
这样可以防止新窗口传递
window.opener
。这对于HTML来说很好,而且很好,但是如果您使用window.open
怎么办?<button type="button" onclick="window.open('http://someurl.here', '_blank');">
Click Me
</button>
您如何阻止此处传递的
window.opener
的使用? 最佳答案
现在,window.open()
调用supports功能“noopener”。
因此,调用window.open('https://www.your.url','_blank','noopener')
应该使用window.opener
为空打开新窗口/标签。
我在查找支持的浏览器(和版本)的可靠列表时遇到了麻烦-MDN指出here
从我的实验中,我看到它适用于:
但不适用于:
(在运行Windows 10的PC上的所有测试...)
为了向后兼容,最好将它与t3__rry's answer结合使用。