Skip to content

Commit 6f12262

Browse files
authored
Merge pull request #55 from mberkowski/secure-cookie
secureCookie configuration option for cookie secure flag
2 parents 536b42f + 5007eb1 commit 6f12262

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

libs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ CSRFProtector configuration
1616
- `jsPath`: location of the js file **relative** to `config.php`. <br>**Default:** `../js/csrfprotector.js`
1717
- `jsUrl`: **Absolute url** of the js file. (See [Setting up](https://github.com/mebjas/CSRF-Protector-PHP/wiki/Setting-up-CSRF-Protector-PHP-in-your-web-application) for more information)
1818
- `tokenLength`: length of csrfp token, Default `10`
19+
- `secureCookie`: sets the "secure" HTTPS flag on the cookie. <br>**Default: `false`**
1920
- `disabledJavascriptMessage`: messaged to be shown if js is disabled (string)
2021
- `verifyGetFor`: regex rules for those urls for which csrfp validation should be enabled for `GET` requests also. (View [verifyGetFor rules](https://github.com/mebjas/CSRF-Protector-PHP/wiki/verifyGetFor-rules) for more information)

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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public static function changeRequestType($type)
1515
self::$requestType = $type;
1616
}
1717

18+
/**
19+
* Function to check for a string value anywhere within HTTP response headers
20+
* Returns true on first match of $needle in header names or values
21+
*/
1822
public static function checkHeader($needle)
1923
{
2024
$haystack = xdebug_get_headers();
@@ -24,6 +28,23 @@ public static function checkHeader($needle)
2428
}
2529
return false;
2630
}
31+
32+
/**
33+
* Function to return the string value of the last response header
34+
* identified by name $needle
35+
*/
36+
public static function getHeaderValue($needle)
37+
{
38+
$haystack = xdebug_get_headers();
39+
foreach ($haystack as $key => $value) {
40+
if (strpos($value, $needle) === 0) {
41+
// Deliberately overwrite to accept the last rather than first match
42+
// as xdebug_get_headers() will accumulate all set headers
43+
list(,$hvalue) = explode(':', $value, 2);
44+
}
45+
}
46+
return $hvalue;
47+
}
2748
}
2849

2950

@@ -44,6 +65,7 @@ public function setUp()
4465
{
4566
csrfprotector::$config['jsPath'] = '../js/csrfprotector.js';
4667
csrfprotector::$config['CSRFP_TOKEN'] = 'csrfp_token';
68+
csrfprotector::$config['secureCookie'] = false;
4769

4870

4971

@@ -54,6 +76,7 @@ public function setUp()
5476
$_POST[csrfprotector::$config['CSRFP_TOKEN']] = $_GET[csrfprotector::$config['CSRFP_TOKEN']] = '123';
5577
$_SESSION[csrfprotector::$config['CSRFP_TOKEN']] = array('abc'); //token mismatch - leading to failed validation
5678
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
79+
$_SERVER['HTTPS'] = null;
5780

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

@@ -90,6 +113,22 @@ public function testRefreshToken()
90113
$this->assertTrue(csrfp_wrapper::checkHeader($_SESSION[csrfprotector::$config['CSRFP_TOKEN']][1]));
91114
}
92115

116+
/**
117+
* test secure flag is set in the token cookie when requested
118+
*/
119+
public function testSecureCookie()
120+
{
121+
$_SERVER['REQUEST_METHOD'] = 'POST';
122+
$_SESSION[csrfprotector::$config['CSRFP_TOKEN']] = array('123abcd');
123+
124+
csrfprotector::$config['secureCookie'] = false;
125+
csrfprotector::refreshToken();
126+
$this->assertNotRegExp('/; secure/', csrfp_wrapper::getHeaderValue('Set-Cookie'));
127+
128+
csrfprotector::$config['secureCookie'] = true;
129+
csrfprotector::refreshToken();
130+
$this->assertRegExp('/; secure/', csrfp_wrapper::getHeaderValue('Set-Cookie'));
131+
}
93132

94133
/**
95134
* test authorise post -> log directory exception

0 commit comments

Comments
 (0)