前端记录一则window.open带来的安全漏洞

1,213 阅读1分钟

转载自前端小智

我们经常打开新标签页使用的办法

\\ in html
<a href="www.google.com" target="_blank">open google</a>
\\in js
window.open("www.google.com")

但是,当新打开的页面指向一个我们不知道的网站时,我们就会被暴露在钓鱼网站的漏洞中。新打开的页面通过 window.opener对象获得了对链接页面的一些部分访问权限。

为了防止这种情况,我们可以: 在 HTML 中使用 rel="noopener 和 target="_blank"

<a href="someLink.com" target="_blank" rel="noreferrer"> open securely in a new tab </a>

在Javascript中,一定要重置 opener 属性:

const newWindow = window.open("someLink.com"); 
newWindow.opener = null;