window.open()和target= blank存在安全漏洞

6,298 阅读1分钟

作者:Daniel 译者:前端小智 来源:js-craft

我们经常使用 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="noopenertarget="_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的使用应该是足够的。

原文:www.js-caft.io/blog/window…

交流

有梦想,有干货,微信搜索 【大迁世界】 关注这个在凌晨还在刷碗的刷碗智。

本文 GitHub github.com/qq449245884… 已收录,有一线大厂面试完整考点、资料以及我的系列文章。