Skip to content

Commit

Permalink
csrf, switch from token per request to token per session. solves issu…
Browse files Browse the repository at this point in the history
…es when using multiple tabs.
  • Loading branch information
AdSchellevis committed Feb 2, 2017
1 parent 895e30d commit f20640d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function beforeExecuteRoute($dispatcher)
}

// check for valid csrf on post requests
if ($this->request->isPost() && !$this->security->checkToken()) {
if ($this->request->isPost() && !$this->security->checkToken(null, null, false)) {
// post without csrf, exit.
$this->response->setStatusCode(403, "Forbidden");
return false;
Expand All @@ -195,10 +195,9 @@ public function beforeExecuteRoute($dispatcher)
}

// include csrf for volt view rendering.
$this->view->setVars([
'csrf_tokenKey' => $this->security->getTokenKey(),
'csrf_token' => $this->security->getToken()
]);
$csrf_token = $this->session->get('$PHALCON/CSRF$');
$csrf_tokenKey = $this->session->get('$PHALCON/CSRF/KEY$');
$this->view->setVars(['csrf_tokenKey' => $csrf_tokenKey,'csrf_token' => $csrf_token]);

// link menu system to view, append /ui in uri because of rewrite
$menu = new Menu\MenuSystem();
Expand Down
25 changes: 11 additions & 14 deletions src/www/csrf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

class LegacyCSRF
{
private $securityToken = null;
private $securityTokenKey = null;
private $di = null;
private $security = null;
private $session = null;
Expand Down Expand Up @@ -58,16 +56,13 @@ class LegacyCSRF
{
$result = false; // default, not valid
$this->Session();
// do not destroy token after successfull validation, some pages use ajax type requests
$this->securityTokenKey = $_SESSION['$PHALCON/CSRF/KEY$'];
$this->securityToken = !empty($_POST[$this->securityTokenKey]) ? $_POST[$this->securityTokenKey] : "";
if (empty($this->securityToken)) {
$securityTokenKey = $_SESSION['$PHALCON/CSRF/KEY$'];
if (empty($_POST[$securityTokenKey])) {
if (!empty($_SERVER['HTTP_X_CSRFTOKEN'])) {
$this->securityToken = $_SERVER['HTTP_X_CSRFTOKEN'];
$result = $this->security->checkToken(null, $this->securityToken, false);
$result = $this->security->checkToken(null, $_SERVER['HTTP_X_CSRFTOKEN'], false);
}
} else {
$result = $this->security->checkToken($this->securityTokenKey, $this->securityToken, false);
$result = $this->security->checkToken($securityTokenKey, $_POST[$securityTokenKey], false);
}
// close session after validation
session_write_close();
Expand All @@ -77,12 +72,14 @@ class LegacyCSRF
private function newToken()
{
$this->Session();
// only request new token when checkToken() hasn't saved one
if ($this->securityToken == null) {
$this->securityToken = $this->security->getToken();
$this->securityTokenKey = $this->security->getTokenKey();
// only request new token when session has none
$securityTokenKey = $_SESSION['$PHALCON/CSRF/KEY$'];
$securityToken = $_SESSION['$PHALCON/CSRF$'];
if (empty($securityToken) || empty($securityTokenKey)) {
$securityToken = $this->security->getToken();
$securityTokenKey = $this->security->getTokenKey();
}
return array('token'=>$this->securityToken, 'key' => $this->securityTokenKey);
return array('token'=>$securityToken, 'key' => $securityTokenKey);
}

public function csrfRewriteHandler($buffer)
Expand Down

1 comment on commit f20640d

@fraenki
Copy link
Member

@fraenki fraenki commented on f20640d Feb 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this fix, Ad! :)

Please sign in to comment.