php - CSRF token in Yii timing out? -
my csrf token seems timing out. there way prevent this?
i have single page app frontend written in angularjs. part, csrf token's validate fine , can confirm send token in through every request. however, see errors server saying "the csrf token not verified". not understand internals of how csrf token verified, there reason why happening?
our application international, because of our server infrastructure there slowdown in geographical areas. i've noticed (just ad-hoc) if, example, user uploading file , request takes longer usual, chances of csrf token being invalid higher.
would love hear ideas this! in advance!
i think yii validing csrf against cookie, try extending chttprequest base class , validate against session. leads varying session time out against csrf validity.
class httprequest extends chttprequest { public function getcsrftoken() { if ($this->_csrftoken === null) { $session = yii::app()->session; $csrftoken = $session->itemat($this->csrftokenname); if ($csrftoken === null) { $csrftoken = sha1(uniqid(mt_rand(), true)); $session->add($this->csrftokenname, $csrftoken); } $this->_csrftoken = $csrftoken; } return $this->_csrftoken; } public function validatecsrftoken($event) { if ($this->getispostrequest()) { // validate post requests $session = yii::app()->session; $headers = common::getallheaders(); if ($session->contains($this->csrftokenname) && (isset($_post[$this->csrftokenname]) || isset($headers['x-csrf-token']))) { $tokenfromsession = $session->itemat($this->csrftokenname); $tokenfrompost = isset($_post[$this->csrftokenname]) ? $_post[$this->csrftokenname] : $headers['x-csrf-token']; $valid = $tokenfromsession === $tokenfrompost; } else { $valid = false; } if (!$valid) { header("location: " . yii::app()->createabsoluteurl(yii::app()->request->url)); } } } } and use in config file:
'request' => array( 'enablecsrfvalidation' => true, 'class' => 'httprequest', 'csrfcookie' => array( 'httponly' => true, 'secure' => true ), note: donot copy , paste content. modify per need. start. hope understand problem.
Comments
Post a Comment