只允许少数网站嵌入我的 iFrame

发布于 2024-11-07 00:15:04 字数 1019 浏览 0 评论 0原文

我有一个小部件。我只希望少数网站通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以往的大感动 2024-11-14 00:15:04

您必须要求您的朋友实现代码,以防止他的网站被用作 iframe。

if(top != self){
   alert('Hacker Attack');
}

You gotta ask your friend to implement code that will keep his site from being used as an iframe.

if(top != self){
   alert('Hacker Attack');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文