2009-12-03 35 views
0

我正在創建一些IFrameable內容。我們希望用戶能夠將此頁面IFrame,但只能從一組域列表中獲取。如何防止IFraming帶白名單

有什麼我們可以檢查看看父頁的域名是什麼?

if (top != self) { top.location.replace(self.location.href); } 

回答

2

不,如果該頁面是不是在你的安全上下文(同源策略)父頁面的location是不可見的。你當然可以看看你自己的框架的document.referrer,但這不是完全防水的...在客戶端的引用檢查比在服務器端稍微沒​​有用處,但它仍然可以被類似的東西繞過該框架中的刷新轉發器。

Content Security Policyframe-ancestors限制可能有一天會允許這樣做。

2

正如bobince所說,document.referrer看起來是你最好的選擇。您可以在頁面中檢查這將是iFrame的src。但是,HTTP引用者信息很容易被欺騙,所以這種方法不是很安全。

本文介紹如何使用PHP來做到這一點:How to bypass the REFERER security check

+0

我打賭大多數人都沒有足夠的技巧來建立一個iframe到他們的網站,並通過有效的引薦。目標是讓公衆不要在其他人的網站上看到iframe。謝謝(你的)信息。 – Slashterix 2009-12-03 07:28:34

+1

這就是我的想法,所以在這種情況下,'document.referrer'將完成工作。 – sachleen 2009-12-04 01:36:53

1

我使用這個檢查,如果該頁面是從白名單域加載。我確信有這方面的解決方法,但它似乎工作。

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"; 
    } 
}