Why is the generated CSRF security token not saved and used through SESSION, as suggested here ? Currently, in CI2, the CSRF protection mechanism (in the security class) is as follows:
1. Create a unique value for the CSRF token in the _csrf_set_hash () function:
$this->csrf_hash = md5(uniqid(rand(), TRUE));
2. Paste this token into the hidden form field (using the form_open helper)
3. The user submits the form and the server receives the token through POST. CI checks the token in the function "_sanitize_globals ()" in the input class:
$this->security->csrf_verify();
4. The function "csrf_verify" only for checking the security class is POST ['token'] set and POST ['token'] equal to COOKIE ['token'];
public function csrf_verify(){ // If no POST data exists we will set the CSRF cookie if (count($_POST) == 0) { return $this->csrf_set_cookie(); } // Do the tokens exist in both the _POST and _COOKIE arrays? if ( ! isset($_POST[$this->_csrf_token_name]) OR ! isset($_COOKIE[$this->_csrf_cookie_name])) { $this->csrf_show_error(); } // Do the tokens match? if ($_POST[$this->_csrf_token_name] != $_COOKIE[$this->_csrf_cookie_name]) { $this->csrf_show_error(); } // We kill this since we're done and we don't want to // polute the _POST array unset($_POST[$this->_csrf_token_name]); // Nothing should last forever unset($_COOKIE[$this->_csrf_cookie_name]); $this->_csrf_set_hash(); $this->csrf_set_cookie(); log_message('debug', "CSRF token verified "); return $this; }
Why not store the token in the session? IMHO just checks POST ['token'] is not empty and equal to COOKIE ['token'] is not enough, because both can be sent by an evil site.
source share