3

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

 2 years ago
source link: https://segmentfault.com/a/1190000040809019
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

发布于 10 月 21 日

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

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

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

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


代码部署后可能存在的BUG没法实时知道,事后为了解决这些BUG,花了大量的时间进行log 调试,这边顺便给大家推荐一个好用的BUG监控工具 Fundebug

原文:http://www.js-caft.io/blog/wi...

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

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK