Skip to content

Commit a7c25f9

Browse files
committed
Adds secureCookie configurable option
1 parent 7344ed9 commit a7c25f9

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

libs/config.sample.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"jsPath" => "../js/csrfprotector.js",
2020
"jsUrl" => "",
2121
"tokenLength" => 10,
22+
"secureCookie" => false,
2223
"disabledJavascriptMessage" => "This site attempts to protect users against <a href=\"https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29\">
2324
Cross-Site Request Forgeries </a> attacks. In order to do so, you must have JavaScript enabled in your web browser otherwise this site will fail to work correctly for you.
2425
See details of your web browser for how to enable JavaScript.",

libs/csrf/csrfprotector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,10 @@ public static function refreshToken()
325325
//set token to cookie for client side processing
326326
setcookie(self::$config['CSRFP_TOKEN'],
327327
$token,
328-
time() + self::$cookieExpiryTime);
328+
time() + self::$cookieExpiryTime,
329+
'',
330+
'',
331+
(array_key_exists('secureCookie', self::$config) ? (bool)self::$config['secureCookie'] : false));
329332
}
330333

331334
/*

test/csrfprotector_test.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public static function checkHeader($needle)
2424
}
2525
return false;
2626
}
27+
28+
public static function getHeaderValue($needle)
29+
{
30+
$haystack = xdebug_get_headers();
31+
foreach ($haystack as $key => $value) {
32+
if (strpos($value, $needle) === 0) {
33+
// Deliberately overwrite to accept the last rather than first match
34+
// as xdebug_get_headers() will accumulate all set headers
35+
list(,$hvalue) = explode(':', $value, 2);
36+
}
37+
}
38+
return $hvalue;
39+
}
2740
}
2841

2942

@@ -44,6 +57,7 @@ public function setUp()
4457
{
4558
csrfprotector::$config['jsPath'] = '../js/csrfprotector.js';
4659
csrfprotector::$config['CSRFP_TOKEN'] = 'csrfp_token';
60+
csrfprotector::$config['secureCookie'] = false;
4761

4862

4963

@@ -54,6 +68,7 @@ public function setUp()
5468
$_POST[csrfprotector::$config['CSRFP_TOKEN']] = $_GET[csrfprotector::$config['CSRFP_TOKEN']] = '123';
5569
$_SESSION[csrfprotector::$config['CSRFP_TOKEN']] = array('abc'); //token mismatch - leading to failed validation
5670
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
71+
$_SERVER['HTTPS'] = null;
5772

5873
$this->config = include(__DIR__ .'/../libs/config.sample.php');
5974

@@ -90,6 +105,15 @@ public function testRefreshToken()
90105
$this->assertTrue(csrfp_wrapper::checkHeader($_SESSION[csrfprotector::$config['CSRFP_TOKEN']][1]));
91106
}
92107

108+
public function testSecureCookie()
109+
{
110+
$_SERVER['REQUEST_METHOD'] = 'POST';
111+
$_SESSION[csrfprotector::$config['CSRFP_TOKEN']] = array('123abcd');
112+
113+
csrfprotector::$config['secureCookie'] = true;
114+
csrfprotector::refreshToken(); //will create new session and cookies
115+
$this->assertRegExp('/; secure/', csrfp_wrapper::getHeaderValue('Set-Cookie'));
116+
}
93117

94118
/**
95119
* test authorise post -> log directory exception

0 commit comments

Comments
 (0)