Codeigniter ajax CSRF problem

前端 未结 7 2116
醉话见心
醉话见心 2020-11-27 06:08

I\'ve made a simple autoload function that loads content when you scroll down on a website. However, there seems to be a few problems when i enable CSRF protection in Codeig

7条回答
  •  孤城傲影
    2020-11-27 06:35

    Having reviewed my situation I believe the best option is to use CSRF but reset the token on each attempt. Otherwise the ideas expressed earlier about re-using the cookie token would allow an attacker to resubmit data hundreds of times using the same token which defeats the object of the point.

    As such I have created the following function:

    public function resetCSRF(){    
    
        $this->security = null;
    
        $_COOKIE[$this->config->item('csrf_cookie_name')] = null;
    
        load_class('Security', 'core');
    
        $this->security->csrf_set_cookie();
    
    return $this->security->get_csrf_hash();
    }
    

    If for example an ajax based login form fails - call this function in your PHP and then on the javascript side that receives the failure (this solution uses Jquery and a getCookie function from w3schools) would then simply call:

    $('input[name="csrf_test_name"]').val(getCookie('csrf_cookie_name'));

提交回复
热议问题