JavaScriptでリンク先URLがhttp/httpsか確認する方法


JavaScriptDOM-based XSShttphttps
    var div, elm; 
    // 変数 url は攻撃者がコントロール可能な文字列
    if( url.match( /^https?:\/\// ) ){
        div = document.getElementById( "info" );
        elm = document.createElement( "a" );
        elm.setAttribute( "href", url );
        elm.appendChild( document.createTextNode( url ) );
        div.appendChild( elm );
    }

urlhttp://example.jphttps://example.jpAURLjavascript:alert(1)vbscript:MsgBox 1DOM-based XSS
urlhttp://https:///foo <a href="/foo"> URL
malaelm.href = "javascript:"; elm.protocol AprotocolURLhttpelm.protocol:URLelm.protocol:URLDOM-based XSS
    var div, elm; 
    // 変数 url は攻撃者がコントロール可能な文字列
    div = document.getElementById( "info" );
    elm = document.createElement( "a" );
    elm.setAttribute( "href", url );
    if( elm.protocol.match( /^https?:$/ ) || elm.protocol === ":" || elm.protocol === "" ){
        elm.appendChild( document.createTextNode( url ) );
        div.appendChild( elm );
    }

AprotocolhosthostnameChromeFirefox new URL( url, location.href ); IE



DOM (by mala)
 

URLAprotocol http://utf-8.jp/test/a-elm-protocol.html 

2014-11-10: : IEa使 URL : Days on the Moon