【发布时间】:2009-12-03 01:36:28
【问题描述】:
我正在创建一些 IFrameable 内容。我们希望用户能够 IFrame 此页面,但只能从一组域列表中。
有什么我们可以检查的,看看父页面的域名是什么?
if (top != self) { top.location.replace(self.location.href); }
【问题讨论】:
标签: php javascript iframe whitelist
我正在创建一些 IFrameable 内容。我们希望用户能够 IFrame 此页面,但只能从一组域列表中。
有什么我们可以检查的,看看父页面的域名是什么?
if (top != self) { top.location.replace(self.location.href); }
【问题讨论】:
标签: php javascript iframe whitelist
不,如果父页面不在您的安全上下文中(同源策略),则父页面的 location 不可见。您当然可以查看自己框架的document.referrer,但这并不完全防水......客户端的引荐来源检查比服务器端的用处稍小,但仍然可以通过以下方式规避类似于框架中的刷新转发器。
Content Security Policy 中的 frame-ancestors 限制可能有一天会允许这样做。
【讨论】:
正如 bobince 所说,document.referrer 看起来是您最好的选择。您将在 iFrame 的 src 页面中检查这一点。但是,HTTP 引用者信息很容易被欺骗,因此这种方法不是很安全。
本文展示了如何使用 PHP:How to bypass the REFERER security check
【讨论】:
document.referrer 将完成工作。
我正在使用它来检查页面是否从白名单域加载。我确信有办法解决这个问题,但它似乎有效。
var currentUrl = document.referrer;
var okayUrl = "http://good-domain.com";
var okayUrl2 = "http://another-good-domain.com";
//check if page is loaded in iframe
if ( window.location !== window.parent.location ) {
//check if parent is a whitelisted domain
if (currentUrl == okayUrl || currentUrl == okayUrl2)
{
//if it is a good domain, then just log the parent url or something
console.log(currentUrl);
} else {
//if it is a bad domain, then do something about it
alert ("Woah buddy. Can't touch this!");
window.location = "http://en.wikipedia.org/wiki/Rickrolling";
}
}
【讨论】: