16
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AjaxにおけるCSRF対策

Last updated at Posted at 2013-10-05

##手法
・HTTPヘッダのX-requested-withの値をチェックし、ajaxであることを担保する。

validate_sample.php
    private function validateCSRF() 
    {
        if( isset($_SERVER['HTTP_X_REQUESTED_WITH'])
            && strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])==='xmlhttprequest'
        ){
            return true; // not csrf
        }
            return false; // invalid request
    }

##理論的背景
SameOriginPolicyより、Ajaxであることはすなわり不正な外部ドメインからのアクセスではない。

上記から、X-requested-withの確認のみでCSRF対策が可能となる。

##考慮事項

・X-requested-withは操作することも可能
・Ajax level2ではクロスドメイン間の通信が可能であるためこの対策は無効

→定石通りワンタイムトークンを用いるのがベターか。

##参考
http://madaolog.blogspot.jp/2012/12/ajaxwebcsrf.html

16
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?