XMLHttpRequestã使ã£ãCSRF対ç
åããã¦èªãã§ãã ããï¼Flashã¨ç¹å®ãã©ã¦ã¶ã®çµã¿åããã§cross originã§ã«ã¹ã¿ã ãããä»ä¸ãåºæ¥ã¦ãã¾ãåé¡ãæªã ã«ç´ã£ã¦ããªã話 (2014-02/07)
XMLHttpRequestã使ããã¨ã§ãCookieããªãã¡ã©ãhiddenå ã®ãã¼ã¯ã³ã使ç¨ããã«ã·ã³ãã«ã«CSRF対çãè¡ãããPOSTããJavaScriptã¯ä»¥ä¸ã®éãã(2013/03/04:ã³ã¼ãä¸é¨ä¿®æ£)
function post(){ var s = "mail=" + encodeURIComponent( document.getElementById("mail").value ) + "&msg=" + encodeURIComponent( document.getElementById("msg").value ); var xhr = new XMLHttpRequest(); xhr.open( "POST", "/inquiry", true ); xhr.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" ); xhr.setRequestHeader( "X-From", location.href ); xhr.onreadystatechange = function(){ /* ... */}; xhr.send( s ); return false; }
ããé常ã®XHRã使ã£ãPOSTã ããsetRequestHeaderã«ãã X-From: ãªã¯ã¨ã¹ããããã«èªèº«ã®URLãè¨å®ãã¦ããã
ãµã¼ãå´ã§ã¯éããã¦ããå
容ã«ã¤ãã¦ã以ä¸ãæ¤æ»ããã
- Host: ãªã¯ã¨ã¹ãããããèªèº«ã®ãã¹ãåãæãã¦ããã㨠(2013/03/04追è¨ããããè¦ã¦ãããªãã¨DNS Rebindingã§çªç ´ãããå¯è½æ§ãããã¨kanatokoããããææãã)
- X-From: ãªã¯ã¨ã¹ãããããä»ä¸ããã¦ãããã¨ã
- X-From: ãªã¯ã¨ã¹ããããã®å¤ããæ³å®ãã¦ããHTMLãã¼ã¸ã®ã¢ãã¬ã¹ã§ãããã¨ã(2013/03/03追è¨:ãã®ç¢ºèªã¯å¿ é ãããªã)
- Origin: ãªã¯ã¨ã¹ããããã以ä¸ã®ããããã§ãããã¨:
以ä¸ã®æ¡ä»¶ãæºããå ´åãCSRFã§ã¯ãªãæ£è¦ã®ãªã¯ã¨ã¹ãã¨ãã¦å¦çãç¶è¡ãã¦ããã
CSRFã®ããã«æ»æè
ãç½ ãã¼ã¸ãç¨æããç½ ãã¼ã¸å
ããformã®submitã«ãã£ã¦ãªã¯ã¨ã¹ããçºè¡ããå ´åã«ã¯ X-From:ããããä»ä¸ãããã¨ã¯ã§ããªããã¾ããç½ ãã¼ã¸å
ããXHRãçµç±ãã¦ãªã¯ã¨ã¹ããçºè¡ããå ´åã«ã¯ãOrigin:ããããç½ ãµã¤ãã®ãªãªã¸ã³ã示ãããããã«ããããªã¯ã¨ã¹ããæ£è¦ã®æç¶ããçµã¦çºè¡ããããã®ããç½ ãã¼ã¸ããçºè¡ããããã®ãããµã¼ãå´ã§ã¯å¤æã§ããã¨ããä»çµã¿ã§ããã
ãã®æ¹å¼ã«ããã¡ãªããã¯
- Cookieãhiddenã«ãããã¼ã¯ã³ã使ç¨ããããµã¼ãå´ã§ã»ãã·ã§ã³æ©æ§ãä¸è¦
- Captchaããã¹ã¯ã¼ãåå ¥åã®ãããªãã¦ã¼ã¶ã¼ã«ã¨ã£ã¦ã®é¢åãããæç¶ããä¸è¦
- CookieãCaptchaãhiddenã«ãããã¼ã¯ã³ã®ãããªãç§å¯ã®æ å ±ãã使ç¨ããªãã®ã§ããããã¯ã¼ã¯ä¸ã§çè´ããã¦ãCSRFããããã¨ããªã*2
- Cookieã使ç¨ããªãã®ã§ãã¯ããã¼ã¢ã³ã¹ã¿ã¼ã®å½±é¿ãåããªãã
- ãªãã¡ã©ã使ç¨ããªãã®ã§ããªãã¡ã©ãéä¿¡ããªãç°å¢ã§ãå©ç¨å¯è½ã
ãªã©ããããä¸æ¹ã§ãã¡ãªããã¨ãã¦ã¯ãJavaScriptãå¿
é ã¨ãããã¨ã§ããã
ã¾ããIE6-IE9ãè¦æ¨ã¦ãã®ã§ããã°ããã©ã¼ã ã®HTMLãè¨ç½®ãããµã¤ãã¨POSTå
ãµã¤ããå¥ã®ãªãªã¸ã³ã«é
ç½®ãããã®å ´åã«ãCookieãã»ãã·ã§ã³ã®å¼ãç¶ããªã©ãä¸è¦ã¨ããã¡ãªãããããã(2013/03/03追è¨:ãã¡ãããµã¼ãå´ã¯preflightãªã¯ã¨ã¹ãã«å¯¾å¿ããå¿
è¦ãããã2013/03/05ä¿®æ£:IE8->IE9)
ãã ããããããå®è£ ãå®éã«ã¯è¦ããã¨ããªãã®ã§ãç§ãæ³å®ã§ãã¦ããªããããªè½ã¨ãç©´ãããã®ãããããªãï¼ããæ¸ãã¦ãä»ããããç ãããï¼ã
*1:Google Chromeã§ã¯åä¸ãªãªã¸ã³ã§ãPOSTã§ã¯Origin:ããããä»ä¸ããã
*2:ãããã¯ã¼ã¯ä¸ã§æ»æè ãæ¹ããã§ããå ´åã¯ãã¡ããCSRFãããå¯è½æ§ãããã