只允许少数网站嵌入我的 iFrame
我有一个小部件。我只希望少数网站通过 iFrame 嵌入我的小部件。
_________________
| Friend |
| __________ |
| | Widget | |
| |_________| |
|________________|
“简单!”,你说。只需将其放在小部件代码的开头即可:
if (!/^http:\/\/([a-z0-9]+\.)?friend\.com)/.test(document.referrer)) {
alert('hacker detected');
return;
}
但是,如果黑客这样做,此代码将无法保护我们:
_____________________
| Hacker |
| _________________ |
| | Friend | |
| | __________ | |
| | | Widget | | |
| | |_________| | |
| |________________| |
|____________________|
通过巧妙的 CSS,黑客可以砍掉 Friend 的布局并仅显示小部件。没有人会怀疑正在发生任何形式的黑客攻击。所以你再说一遍,“简单,让我们这样做:”
if (!/^http:\/\/([a-z0-9]+\.)?friend\.com)/.test(top.location.href)) {
alert('hacker detected');
return;
}
但这仍然不起作用。读取 top.location.href
时出现权限被拒绝错误。黑客、朋友和小部件都有不同的域,因此这是一个跨站点脚本错误。
在你笑着说“没有黑客会经历这么多麻烦来做这件事”之前,我想说我已经发现有人这样做了。有问题的小部件包含敏感数据,法律上只允许我在某些网站上显示这些数据。
I have a small widget. I want only a few websites to embed my widget through an iFrame.
_________________
| Friend |
| __________ |
| | Widget | |
| |_________| |
|________________|
"Easy!", you say. Just put this in the beginning of your widget code:
if (!/^http:\/\/([a-z0-9]+\.)?friend\.com)/.test(document.referrer)) {
alert('hacker detected');
return;
}
But this code will fail to protect us if a hacker does this:
_____________________
| Hacker |
| _________________ |
| | Friend | |
| | __________ | |
| | | Widget | | |
| | |_________| | |
| |________________| |
|____________________|
Through clever CSS, the hacker can chop off Friend's layout and only show the widget. No one would suspect any kind of hacking was going on. So you say again, "Easy, lets do this:"
if (!/^http:\/\/([a-z0-9]+\.)?friend\.com)/.test(top.location.href)) {
alert('hacker detected');
return;
}
But this still doesn't work. You get a permission denied error when reading top.location.href
. Hacker, Friend, and Widget all have different domains so this is a cross-site scripting error.
Before you laugh and say "no hacker is going to go through all this trouble doing this", I want to say that I've already found people doing this. The widget in question has sensitive data that I'm only legally allowed to show on certain websites.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须要求您的朋友实现代码,以防止他的网站被用作 iframe。
You gotta ask your friend to implement code that will keep his site from being used as an iframe.