Skip to content

Commit 6453d50

Browse files
committed
Throw exception if init() is called multiple times
added check for mutiple call, added a test case for this, Corrected a test case, and logic to temporarily create a config file during test config file from test file.
1 parent 95e2bad commit 6453d50

File tree

4 files changed

+69
-20
lines changed

4 files changed

+69
-20
lines changed

.travis.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,14 @@ php:
88
- "7.1"
99
- hhvm
1010
- nightly
11-
- hhvm-3.3
12-
- hhvm-3.6
13-
- hhvm-3.9
14-
- hhvm-3.12
15-
- hhvm-nightly
1611

1712
matrix:
1813
allow_failures:
19-
- os: osx
2014
- php: nightly
2115
- php: hhvm
22-
- php: hhvm-3.3
23-
- php: hhvm-3.6
24-
- php: hhvm-3.9
25-
- php: hhvm-3.12
26-
- php: hhvm-nightly
2716

2817
os:
2918
- linux
30-
- osx
3119

3220
before_script:
3321
- wget http://getcomposer.org/composer.phar

libs/csrf/csrfprotector.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class jsFileNotFoundException extends \exception {};
2121
class logFileWriteError extends \exception {};
2222
class baseJSFileNotFoundExceptio extends \exception {};
2323
class incompleteConfigurationException extends \exception {};
24+
class alreadyInitializedException extends \exception {};
2425

2526
class csrfProtector
2627
{
@@ -93,6 +94,13 @@ class csrfProtector
9394
*/
9495
public static function init($length = null, $action = null)
9596
{
97+
/*
98+
* Check if init has already been called.
99+
*/
100+
if (count(self::$config) > 0) {
101+
throw new alreadyInitializedException("OWASP CSRFProtector: library was already initialized.");
102+
}
103+
96104
/*
97105
* if mod_csrfp already enabled, no verification, no filtering
98106
* Already done by mod_csrfp
@@ -150,7 +158,8 @@ public static function init($length = null, $action = null)
150158
self::authorizePost();
151159

152160
// Initialize output buffering handler
153-
ob_start('csrfProtector::ob_handler');
161+
if (!defined('__TESTING_CSRFP__'))
162+
ob_start('csrfProtector::ob_handler');
154163

155164
if (!isset($_COOKIE[self::$config['CSRFP_TOKEN']])
156165
|| !isset($_SESSION[self::$config['CSRFP_TOKEN']])

test/config.test.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Configuration file for CSRF Protector
4+
* Necessary configurations are (library would throw exception otherwise)
5+
* ---- logDirectory
6+
* ---- failedAuthAction
7+
* ---- jsPath
8+
* ---- jsUrl
9+
* ---- tokenLength
10+
*/
11+
return array(
12+
"CSRFP_TOKEN" => "csrfp_token",
13+
"logDirectory" => "../log",
14+
"failedAuthAction" => array(
15+
"GET" => 0,
16+
"POST" => 0),
17+
"errorRedirectionPage" => "",
18+
"customErrorMessage" => "",
19+
"jsPath" => "../js/csrfprotector.js",
20+
"jsUrl" => "http://localhost/csrfp/js/csrfprotector.js",
21+
"tokenLength" => 10,
22+
"secureCookie" => false,
23+
"disabledJavascriptMessage" => "This site attempts to protect users against <a href=\"https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29\">
24+
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.
25+
See details of your web browser for how to enable JavaScript.",
26+
"verifyGetFor" => array()
27+
);

test/csrfprotector_test.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ public function setUp()
7171
csrfprotector::$config['CSRFP_TOKEN'] = 'csrfp_token';
7272
csrfprotector::$config['secureCookie'] = false;
7373

74-
75-
7674
$_SERVER['REQUEST_URI'] = 'temp'; // For logging
7775
$_SERVER['REQUEST_SCHEME'] = 'http'; // For authorizePost
7876
$_SERVER['HTTP_HOST'] = 'test'; // For isUrlAllowed
@@ -85,10 +83,10 @@ public function setUp()
8583
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
8684
$_SERVER['HTTPS'] = null;
8785

88-
$this->config = include(__DIR__ .'/../libs/config.sample.php');
86+
$this->config = include(__DIR__ .'/config.test.php');
8987

9088
// Create an instance of config file -- for testing
91-
$data = file_get_contents(__DIR__ .'/../libs/config.sample.php');
89+
$data = file_get_contents(__DIR__ .'/config.test.php');
9290
file_put_contents(__DIR__ .'/../libs/config.php', $data);
9391

9492
if (!defined('__TESTING_CSRFP__')) define('__TESTING_CSRFP__', true);
@@ -439,10 +437,37 @@ public function testModCSRFPEnabledException()
439437
putenv('mod_csrfp_enabled=true');
440438
$temp = $_COOKIE[csrfprotector::$config['CSRFP_TOKEN']] = 'abc';
441439
$_SESSION[csrfprotector::$config['CSRFP_TOKEN']] = array('abc');
440+
441+
csrfProtector::$config = array();
442442
csrfProtector::init();
443443

444-
// Assuming no cookie change
445-
$this->assertTrue($temp == $_SESSION[csrfprotector::$config['CSRFP_TOKEN']][0]);
446-
$this->assertTrue($temp == $_COOKIE[csrfprotector::$config['CSRFP_TOKEN']]);
444+
// Assuming no config was added
445+
$this->assertTrue(count(csrfProtector::$config) == 0);
446+
447+
// unset the env variable
448+
putenv('mod_csrfp_enabled');
449+
}
450+
451+
/**
452+
* Test for exception thrown when init() method is called multiple times
453+
*/
454+
public function testMultipleInitializeException()
455+
{
456+
csrfProtector::$config = array();
457+
$this->assertTrue(count(csrfProtector::$config) == 0);
458+
459+
$_SERVER['REQUEST_METHOD'] = 'GET';
460+
csrfProtector::init();
461+
462+
$this->assertTrue(count(csrfProtector::$config) == 11);
463+
try {
464+
csrfProtector::init();
465+
$this->fail("alreadyInitializedException not raised");
466+
} catch (alreadyInitializedException $ex) {
467+
// pass
468+
$this->assertTrue(true);
469+
} catch (Exception $ex) {
470+
$this->fail("exception other than alreadyInitializedException failed");
471+
}
447472
}
448473
}

0 commit comments

Comments
 (0)