Skip to content

Commit 944236a

Browse files
committed
Merge branch 'master' into dev-master-issue80
2 parents c756bf3 + 756129f commit 944236a

File tree

5 files changed

+118
-13
lines changed

5 files changed

+118
-13
lines changed

libs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ CSRFProtector configuration
1515
- `customErrorMessage`: **Error Message** to be shown to user. Only this text will be shown!<br>**Default: null**
1616
- `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)
1717
- `tokenLength`: length of csrfp token, Default `10`
18-
- `secureCookie`: sets the "secure" HTTPS flag on the cookie. <br>**Default: `false`**
18+
- `cookieConfig`: Array of parameter values for set cookie method. supports three properties: `path`, `domain`, `secure`. They have same meaning as respective parameters of `setcookie` method: [learn more - php.net]
1919
- `disabledJavascriptMessage`: messaged to be shown if js is disabled (string)
2020
- `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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
"customErrorMessage" => "",
1818
"jsUrl" => "",
1919
"tokenLength" => 10,
20-
"secureCookie" => false,
20+
"cookieConfig" => array(
21+
"path" => '',
22+
"domain" => '',
23+
"secure" => false
24+
),
2125
"disabledJavascriptMessage" => "This site attempts to protect users against <a href=\"https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29\">
2226
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.
2327
See details of your web browser for how to enable JavaScript.",

libs/csrf/csrfprotector.php

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,47 @@ class baseJSFileNotFoundExceptio extends \exception {};
2222
class incompleteConfigurationException extends \exception {};
2323
class alreadyInitializedException extends \exception {};
2424

25+
/**
26+
* Cookie config class
27+
*/
28+
class cookieConfig
29+
{
30+
/**
31+
* Variable: $path
32+
* path parameter for setcookie method
33+
* @var string
34+
*/
35+
public $path = '';
36+
37+
/**
38+
* Variable: $domain
39+
* domain parameter for setcookie method
40+
* @var string
41+
*/
42+
public $domain = '';
43+
44+
/**
45+
* Variable: $secure
46+
* secure parameter for setcookie method
47+
* @var bool
48+
*/
49+
public $secure = false;
50+
51+
/**
52+
* Function: constructor
53+
*
54+
* Parameters:
55+
* $cfg - config array loaded from config file;
56+
*/
57+
function __construct($cfg) {
58+
if ($cfg !== null) {
59+
if (isset($cfg['path'])) $this->path = $cfg['path'];
60+
if (isset($cfg['domain'])) $this->domain = $cfg['domain'];
61+
if (isset($cfg['secure'])) $this->secure = (bool) $cfg['secure'];
62+
}
63+
}
64+
}
65+
2566
class csrfProtector
2667
{
2768
/*
@@ -50,6 +91,13 @@ class csrfProtector
5091
*/
5192
private static $isValidHTML = false;
5293

94+
/**
95+
* Variable: $cookieConfig
96+
* Array of parameters for the setcookie method
97+
* @var cookieConfig;
98+
*/
99+
private static $cookieConfig = null;
100+
53101
/*
54102
* Variable: $requestType
55103
* Varaible to store weather request type is post or get
@@ -144,6 +192,11 @@ public static function init($length = null, $action = null)
144192
if (self::$config['CSRFP_TOKEN'] == '')
145193
self::$config['CSRFP_TOKEN'] = CSRFP_TOKEN;
146194

195+
// load parameters for setcookie method
196+
if (!isset(self::$config['cookieConfig']))
197+
self::$config['cookieConfig'] = array();
198+
self::$cookieConfig = new cookieConfig(self::$config['cookieConfig']);
199+
147200
// Validate the config if everythings filled out
148201
// TODO: collect all missing values and throw exception together
149202
foreach (self::$requiredConfigurations as $value) {
@@ -366,17 +419,23 @@ public static function refreshToken()
366419
|| !is_array($_SESSION[self::$config['CSRFP_TOKEN']]))
367420
$_SESSION[self::$config['CSRFP_TOKEN']] = array();
368421

369-
//set token to session for server side validation
422+
// set token to session for server side validation
370423
array_push($_SESSION[self::$config['CSRFP_TOKEN']], $token);
371424

372-
//set token to cookie for client side processing
373-
// TODO: all the params must be loaded from config
374-
setcookie(self::$config['CSRFP_TOKEN'],
425+
// set token to cookie for client side processing
426+
if (self::$cookieConfig === null) {
427+
if (!isset(self::$config['cookieConfig']))
428+
self::$config['cookieConfig'] = array();
429+
self::$cookieConfig = new cookieConfig(self::$config['cookieConfig']);
430+
}
431+
432+
setcookie(
433+
self::$config['CSRFP_TOKEN'],
375434
$token,
376435
time() + self::$cookieExpiryTime,
377-
'',
378-
'',
379-
(array_key_exists('secureCookie', self::$config) ? (bool)self::$config['secureCookie'] : false));
436+
self::$cookieConfig->path,
437+
self::$cookieConfig->domain,
438+
(bool) self::$cookieConfig->secure);
380439
}
381440

382441
/*

test/config.test.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
"customErrorMessage" => "",
1818
"jsUrl" => "http://localhost/csrfp/js/csrfprotector.js",
1919
"tokenLength" => 10,
20-
"secureCookie" => false,
20+
"cookieConfig" => array(
21+
"path" => '',
22+
"domain" => '',
23+
"secure" => false
24+
),
2125
"disabledJavascriptMessage" => "This site attempts to protect users against <a href=\"https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29\">
2226
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.
2327
See details of your web browser for how to enable JavaScript.",

test/csrfprotector_test.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function setUp()
9191
$this->logDir = __DIR__ .'/logs';
9292

9393
csrfprotector::$config['CSRFP_TOKEN'] = 'csrfp_token';
94-
csrfprotector::$config['secureCookie'] = false;
94+
csrfprotector::$config['cookieConfig'] = array('secure' => false);
9595
csrfprotector::$config['logDirectory'] = '../test/logs';
9696

9797
$_SERVER['REQUEST_URI'] = 'temp'; // For logging
@@ -142,6 +142,35 @@ public function testRefreshToken()
142142
$this->assertTrue(csrfp_wrapper::checkHeader($_SESSION[csrfprotector::$config['CSRFP_TOKEN']][1]));
143143
}
144144

145+
/**
146+
* Function to check cookieconfig class
147+
*/
148+
public function testCookieConfigClass() {
149+
$cfg = array(
150+
"path" => "abcd",
151+
"secure" => true,
152+
"domain" => "abcd",
153+
);
154+
155+
// simple test
156+
$cookieConfig = new cookieConfig($cfg);
157+
$this->assertEquals($cookieConfig->path, "abcd");
158+
$this->assertEquals($cookieConfig->domain, "abcd");
159+
$this->assertEquals($cookieConfig->secure, true);
160+
161+
// default value test
162+
$cookieConfig = new cookieConfig(array());
163+
$this->assertEquals($cookieConfig->path, '');
164+
$this->assertEquals($cookieConfig->domain, '');
165+
$this->assertEquals($cookieConfig->secure, false);
166+
167+
// secure as string
168+
$cookieConfig = new cookieConfig(array('secure' => 'true'));
169+
$this->assertEquals($cookieConfig->secure, true);
170+
$cookieConfig = new cookieConfig(array('secure' => 'false'));
171+
$this->assertEquals($cookieConfig->secure, true);
172+
}
173+
145174
/**
146175
* test secure flag is set in the token cookie when requested
147176
*/
@@ -150,11 +179,20 @@ public function testSecureCookie()
150179
$_SERVER['REQUEST_METHOD'] = 'POST';
151180
$_SESSION[csrfprotector::$config['CSRFP_TOKEN']] = array('123abcd');
152181

153-
csrfprotector::$config['secureCookie'] = false;
182+
// this one would generally fails, as init was already called and now private static
183+
// property is set with secure as false;
184+
$csrfp = new csrfProtector;
185+
$reflection = new \ReflectionClass(get_class($csrfp));
186+
$property = $reflection->getProperty('cookieConfig');
187+
$property->setAccessible(true);
188+
189+
// change value to false
190+
$property->setValue($csrfp, new cookieConfig(array('secure' => false)));
154191
csrfprotector::refreshToken();
155192
$this->assertNotRegExp('/; secure/', csrfp_wrapper::getHeaderValue('Set-Cookie'));
156193

157-
csrfprotector::$config['secureCookie'] = true;
194+
// change value to true
195+
$property->setValue($csrfp, new cookieConfig(array('secure' => true)));
158196
csrfprotector::refreshToken();
159197
$this->assertRegExp('/; secure/', csrfp_wrapper::getHeaderValue('Set-Cookie'));
160198
}

0 commit comments

Comments
 (0)