1. 程式人生 > >防止頁面被iframe惡意巢狀

防止頁面被iframe惡意巢狀

緣起

在看資料時,看到這樣的防止iframe巢狀的程式碼:

try {
    if (window.top != window.self) {
        var ref = document.referer;
        if (ref.substring(0, 2) === '//') {
            ref = 'http:' + ref;
        } else if (ref.split('://').length === 1) {
            ref = 'http://' + ref;
        }
        var url = ref.split('/'
); var _l = {auth: ''}; var host = url[2].split('@'); if (host.length === 1) { host = host[0].split(':'); } else { _l.auth = host[0]; host = host[1].split(':'); } var parentHostName = host[0]; if (parentHostName.indexOf("test.com"
) == -1 && parentHostName.indexOf("test2.com") == -1) { top.location.href = "http://www.test.com"; } } } catch (e) { }

假定test.com,test2.com是自己的域名,當其它網站惡意巢狀本站的頁面時,跳轉回本站的首頁。

上面的程式碼有兩個問題:

  • referer拼寫錯誤,實際上應該是referrer
  • 解析referrer的程式碼太複雜,還不一定正確

無論在任何語言裡,都不建議手工寫程式碼處理URL。因為url的複雜度超出一般人的想像。很多安全的問題就是因為解析URL不當引起的。比如防止CSRF時判斷referrer。

URI的語法:

在javascript裡解析url最好的辦法

在javascript裡解析url的最好辦法是利用瀏覽器的js引擎,通過建立一個a標籤:

var getLocation = function(href) {
    var l = document.createElement("a");
    l.href = href;
    return l;
};
var l = getLocation("http://example.com/path");
console.debug(l.hostname)

簡潔防iframe惡意巢狀的方法

下面給出一個簡潔的防止iframe惡意巢狀的判斷方法:

if(window.top != window && document.referrer){
  var a = document.createElement("a");
  a.href = document.referrer;
  var host = a.hostname;

  var endsWith = function (str, suffix) {
      return str.indexOf(suffix, str.length - suffix.length) !== -1;
  }

  if(!endsWith(host, '.test.com') || !endsWith(host, '.test2.com')){
    top.location.href = "http://www.test.com";
  }
}

java裡處理URL的方法

用contain, indexOf, endWitch這些函式時都要小心。

 public static void main(String[] args) throws Exception {

        URL aURL = new URL("http://example.com:80/docs/books/tutorial"
                           + "/index.html?name=networking#DOWNLOADING");

        System.out.println("protocol = " + aURL.getProtocol());
        System.out.println("authority = " + aURL.getAuthority());
        System.out.println("host = " + aURL.getHost());
        System.out.println("port = " + aURL.getPort());
        System.out.println("path = " + aURL.getPath());
        System.out.println("query = " + aURL.getQuery());
        System.out.println("filename = " + aURL.getFile());
        System.out.println("ref = " + aURL.getRef());
    }

參考