Skip to content

Commit

Permalink
SEC-7244: Setting new cookie i.e expiry time along with csrf token. (#6)
Browse files Browse the repository at this point in the history
* Adding new cookie i.r expiry time along with token

* Update test cases.
  • Loading branch information
bhavinrshah authored Apr 3, 2023
1 parent 87b133d commit e1bdd99
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
29 changes: 24 additions & 5 deletions libs/csrf/csrfprotector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

// name of HTTP POST variable for authentication
define("CSRFP_TOKEN","csrfp_token");
define("CSRFP_TOKEN_EXPIRY","csrfp_token_expiry");

// We insert token name and list of url patterns for which
// GET requests are validated against CSRF as hidden input fields
Expand Down Expand Up @@ -158,6 +159,9 @@ public static function init($length = null, $action = null, $logger = null)
if (self::$config['CSRFP_TOKEN'] == '')
self::$config['CSRFP_TOKEN'] = CSRFP_TOKEN;

if (self::$config['CSRFP_TOKEN_EXPIRY'] == '')
self::$config['CSRFP_TOKEN_EXPIRY'] = CSRFP_TOKEN_EXPIRY;

self::$tokenHeaderKey = 'HTTP_' .strtoupper(self::$config['CSRFP_TOKEN']);
self::$tokenHeaderKey = str_replace('-', '_', self::$tokenHeaderKey);

Expand Down Expand Up @@ -297,13 +301,18 @@ private static function getTokenFromRequest() {
private static function isValidToken($token) {
if (!isset($_SESSION[self::$config['CSRFP_TOKEN']])) return false;
if (!is_array($_SESSION[self::$config['CSRFP_TOKEN']])) return false;
// Clear match token from the session
foreach ($_SESSION[self::$config['CSRFP_TOKEN']] as $_key => $_value) {
if ($_value == $token) {
unset($_SESSION[self::$config['CSRFP_TOKEN']][$_key]);
foreach ($_SESSION[self::$config['CSRFP_TOKEN']] as $key => $value) {
if ($value == $token) {

// Clear all older tokens assuming they have been consumed
foreach ($_SESSION[self::$config['CSRFP_TOKEN']] as $_key => $_value) {
if ($_value == $token) break;
array_shift($_SESSION[self::$config['CSRFP_TOKEN']]);
}
return true;
}
}

return false;
}

Expand Down Expand Up @@ -393,10 +402,20 @@ public static function refreshToken()
self::$cookieConfig = new csrfpCookieConfig(self::$config['cookieConfig']);
}

$expiryTime = time() + self::$cookieConfig->expire;

setcookie(
self::$config['CSRFP_TOKEN'],
$token,
time() + self::$cookieConfig->expire,
$expiryTime,
self::$cookieConfig->path,
self::$cookieConfig->domain,
(bool) self::$cookieConfig->secure);

setcookie(
self::$config['CSRFP_TOKEN_EXPIRY'],
$expiryTime,
$expiryTime,
self::$cookieConfig->path,
self::$cookieConfig->domain,
(bool) self::$cookieConfig->secure);
Expand Down
1 change: 1 addition & 0 deletions test/config.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
return array(
"CSRFP_TOKEN" => "csrfp_token",
"CSRFP_TOKEN_EXPIRY" => "csrfp_token_expiry",
"logDirectory" => "../log",
"failedAuthAction" => array(
"GET" => 0,
Expand Down
1 change: 1 addition & 0 deletions test/config.testInit_incompleteConfigurationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
return array(
"CSRFP_TOKEN" => "csrfp_token",
"CSRFP_TOKEN_EXPIRY" => "csrfp_token_expiry",
// "logDirectory" => "../log",
// "failedAuthAction" => array(
// "GET" => 0,
Expand Down
1 change: 1 addition & 0 deletions test/config.testInit_withoutInjectedCSRFGuardScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
return array(
"CSRFP_TOKEN" => "csrfp_token",
"CSRFP_TOKEN_EXPIRY" => "csrfp_token_expiry",
"logDirectory" => "../log",
"failedAuthAction" => array(
"GET" => 0,
Expand Down
9 changes: 5 additions & 4 deletions test/csrfprotector_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function setUp()
$this->logDir = __DIR__ .'/logs';

csrfprotector::$config['CSRFP_TOKEN'] = 'csrfp_token';
csrfprotector::$config['CSRFP_TOKEN_EXPIRY'] = 'csrfp_token_expiry';
csrfprotector::$config['cookieConfig'] = array('secure' => false);
csrfprotector::$config['logDirectory'] = '../test/logs';

Expand Down Expand Up @@ -396,7 +397,7 @@ public function testAuthorisePost_success()
$temp = $_SESSION[csrfprotector::$config['CSRFP_TOKEN']];

csrfprotector::authorizePost(); //will create new session and cookies
$this->assertTrue(!isset($_SESSION[csrfprotector::$config['CSRFP_TOKEN']][0]));
$this->assertFalse($temp == $_SESSION[csrfprotector::$config['CSRFP_TOKEN']][0]);
$this->assertTrue(csrfp_wrapper::checkHeader('Set-Cookie'));
$this->assertTrue(csrfp_wrapper::checkHeader('csrfp_token'));
// $this->assertTrue(csrfp_wrapper::checkHeader($_SESSION[csrfprotector::$config['CSRFP_TOKEN']][0])); // Combine these 3 later
Expand All @@ -406,7 +407,7 @@ public function testAuthorisePost_success()
csrfp_wrapper::changeRequestType('GET');
$_POST[csrfprotector::$config['CSRFP_TOKEN']]
= $_GET[csrfprotector::$config['CSRFP_TOKEN']]
= $_SESSION[csrfprotector::$config['CSRFP_TOKEN']][1];
= $_SESSION[csrfprotector::$config['CSRFP_TOKEN']][0];
$temp = $_SESSION[csrfprotector::$config['CSRFP_TOKEN']];

csrfprotector::authorizePost(); //will create new session and cookies
Expand Down Expand Up @@ -437,7 +438,7 @@ public function testAuthorisePost_success_2()
$temp = $_SESSION[csrfprotector::$config['CSRFP_TOKEN']];

csrfprotector::authorizePost(); //will create new session and cookies
$this->assertTrue(!isset($_SESSION[csrfprotector::$config['CSRFP_TOKEN']][0]));
$this->assertFalse($temp == $_SESSION[csrfprotector::$config['CSRFP_TOKEN']][0]);
$this->assertTrue(csrfp_wrapper::checkHeader('Set-Cookie'));
$this->assertTrue(csrfp_wrapper::checkHeader('csrfp_token'));
// $this->assertTrue(csrfp_wrapper::checkHeader($_SESSION[csrfprotector::$config['CSRFP_TOKEN']][0])); // Combine these 3 later
Expand Down Expand Up @@ -733,7 +734,7 @@ public function testMultipleInitializeException()
$_SERVER['REQUEST_METHOD'] = 'GET';
csrfProtector::init();

$this->assertTrue(count(csrfProtector::$config) == 10);
$this->assertTrue(count(csrfProtector::$config) == 11);
try {
csrfProtector::init();
$this->fail("alreadyInitializedException not raised");
Expand Down
1 change: 1 addition & 0 deletions test/csrfprotector_test_customlogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class csrfp_test_customLogger extends PHPUnit_Framework_TestCase

public function setUp() {
csrfprotector::$config['CSRFP_TOKEN'] = 'csrfp_token';
csrfprotector::$config['CSRFP_TOKEN_EXPIRY'] = 'csrfp_token_expiry';
csrfprotector::$config['cookieConfig'] = array('secure' => false);
csrfprotector::$config['logDirectory'] = '../test/logs';
$_SERVER['REQUEST_URI'] = 'temp'; // For logging
Expand Down

0 comments on commit e1bdd99

Please sign in to comment.