我正在创建一些IFrameable内容。我们希望用户能够将此页面IFrame,但只能从一组域列表中获取。如何防止IFraming带白名单
有什么我们可以检查看看父页的域名是什么?
if (top != self) { top.location.replace(self.location.href); }
我正在创建一些IFrameable内容。我们希望用户能够将此页面IFrame,但只能从一组域列表中获取。如何防止IFraming带白名单
有什么我们可以检查看看父页的域名是什么?
if (top != self) { top.location.replace(self.location.href); }
不,如果该页面是不是在你的安全上下文(同源策略)父页面的location
是不可见的。你当然可以看看你自己的框架的document.referrer
,但这不是完全防水的...在客户端的引用检查比在服务器端稍微没有用处,但它仍然可以被类似的东西绕过该框架中的刷新转发器。
在Content Security Policy的frame-ancestors
限制可能有一天会允许这样做。
正如bobince所说,document.referrer
看起来是你最好的选择。您可以在页面中检查这将是iFrame的src。但是,HTTP引用者信息很容易被欺骗,所以这种方法不是很安全。
本文介绍如何使用PHP来做到这一点:How to bypass the REFERER security check
我使用这个检查,如果该页面是从白名单域加载。我确信有这方面的解决方法,但它似乎工作。
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";
}
}
我打赌大多数人都没有足够的技巧来建立一个iframe到他们的网站,并通过有效的引荐。目标是让公众不要在其他人的网站上看到iframe。谢谢(你的)信息。 – Slashterix 2009-12-03 07:28:34
这就是我的想法,所以在这种情况下,'document.referrer'将完成工作。 – sachleen 2009-12-04 01:36:53