防止頁面被iframe惡意嵌套,iframe惡意嵌套

來源:互聯網
上載者:User

防止頁面被iframe惡意嵌套,iframe惡意嵌套

新blog地址:http://hengyunabc.github.io/prevent-iframe-stealing/

緣起

在看資料時,看到這樣的防止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的文法:

http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax

在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的方法

http://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

用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());    }
參考

http://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript

http://stackoverflow.com/questions/5522097/prevent-iframe-stealing

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.