我们经常使用 HTML target="_blank"
或 window.open()
在新窗口中打开页面。
// in html
<a href="www.google.com" target="_blank">open google</a>
// in javascript
window.open("www.google.com")
复制代码
但是,当新打开的页面指向一个我们不知道的网站时,我们就会被暴露在钓鱼网站的漏洞中。新页面通过 window.opener
对象获得了对链接页面的一些部分访问权限。
例如,可以使用 window.opener.location
将初始页面的用户指向一个假的钓鱼网站,该网站模仿原始网站的外观并做各种恶心的事情。鉴于用户信任已经打开的页面,这可能是非常有效的。
为了防止这种情况,我们可以:
在 HTML 中使用 rel="noopener
和 target="_blank"
。
<a href="someLink.com" target="_blank" rel="noopener noreferrer">
open securely in a new tab
</a>
在Javascript中,一定要重置 opener
属性:
const newWindow = window.open("someLink.com");
newWindow.opener = null;
后续:现在看来,noreferrer 是多余的,所以
noopener` 对于HTML的使用应该是足够的。
原文:掘金