1
1
<?php
2
-
2
+ date_default_timezone_set ( ' UTC ' );
3
3
require_once __DIR__ .'/../libs/csrf/csrfprotector.php ' ;
4
4
5
5
/**
@@ -52,14 +52,16 @@ public function setUp()
52
52
$ _SERVER ['HTTP_HOST ' ] = 'test ' ; // For isUrlAllowed
53
53
$ _SERVER ['PHP_SELF ' ] = '/index.php ' ; // For authorizePost
54
54
$ _POST [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = $ _GET [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = '123 ' ;
55
- $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = 'abc ' ; //token mismatch - leading to failed validation
55
+ $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = array ( 'abc ' ) ; //token mismatch - leading to failed validation
56
56
$ _SERVER ['SERVER_PROTOCOL ' ] = 'HTTP/1.1 ' ;
57
57
58
58
$ this ->config = include (__DIR__ .'/../libs/config.sample.php ' );
59
59
60
60
// Create an instance of config file -- for testing
61
61
$ data = file_get_contents (__DIR__ .'/../libs/config.sample.php ' );
62
- file_put_contents (__DIR__ .'/../libs/config.php ' , $ data );
62
+ file_put_contents (__DIR__ .'/../libs/config.php ' , $ data );
63
+
64
+ if (!defined ('__TESTING_CSRFP__ ' )) define ('__TESTING_CSRFP__ ' , true );
63
65
}
64
66
65
67
/**
@@ -76,17 +78,16 @@ public function tearDown()
76
78
public function testRefreshToken ()
77
79
{
78
80
79
- $ val = $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = $ _COOKIE [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = '123abcd ' ;
80
-
81
-
81
+ $ val = $ _COOKIE [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = '123abcd ' ;
82
+ $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = array ('123abcd ' );
82
83
csrfProtector::$ config ['tokenLength ' ] = 20 ;
83
84
csrfProtector::refreshToken ();
84
85
85
- $ this ->assertTrue (strcmp ($ val , $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]]) != 0 );
86
+ $ this ->assertTrue (strcmp ($ val , $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]][ 1 ] ) != 0 );
86
87
87
88
$ this ->assertTrue (csrfP_wrapper::checkHeader ('Set-Cookie ' ));
88
89
$ this ->assertTrue (csrfP_wrapper::checkHeader ('csrfp_token ' ));
89
- $ this ->assertTrue (csrfp_wrapper::checkHeader ($ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]]));
90
+ $ this ->assertTrue (csrfp_wrapper::checkHeader ($ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]][ 1 ] ));
90
91
}
91
92
92
93
@@ -247,29 +248,32 @@ public function testAuthorisePost_failedAction_6()
247
248
*/
248
249
public function testAuthorisePost_success ()
249
250
{
251
+
250
252
$ _SERVER ['REQUEST_METHOD ' ] = 'POST ' ;
251
- $ _POST [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = $ _GET [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]];
253
+ $ _POST [csrfprotector::$ config ['CSRFP_TOKEN ' ]]
254
+ = $ _GET [csrfprotector::$ config ['CSRFP_TOKEN ' ]]
255
+ = $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]][0 ];
252
256
$ temp = $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]];
253
257
254
258
csrfprotector::authorizePost (); //will create new session and cookies
255
-
256
- $ this ->assertFalse ($ temp == $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]]);
259
+ $ this ->assertFalse ($ temp == $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]][0 ]);
257
260
$ this ->assertTrue (csrfp_wrapper::checkHeader ('Set-Cookie ' ));
258
261
$ this ->assertTrue (csrfp_wrapper::checkHeader ('csrfp_token ' ));
259
- $ this ->assertTrue (csrfp_wrapper::checkHeader ($ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]])); // Combine these 3 later
262
+ // $this->assertTrue(csrfp_wrapper::checkHeader($_SESSION[csrfprotector::$config['CSRFP_TOKEN']][0 ])); // Combine these 3 later
260
263
261
264
// For get method
262
265
$ _SERVER ['REQUEST_METHOD ' ] = 'GET ' ;
263
266
csrfp_wrapper::changeRequestType ('GET ' );
264
- $ _POST [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = $ _GET [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]];
267
+ $ _POST [csrfprotector::$ config ['CSRFP_TOKEN ' ]]
268
+ = $ _GET [csrfprotector::$ config ['CSRFP_TOKEN ' ]]
269
+ = $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]][0 ];
265
270
$ temp = $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]];
266
271
267
272
csrfprotector::authorizePost (); //will create new session and cookies
268
-
269
273
$ this ->assertFalse ($ temp == $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]]);
270
274
$ this ->assertTrue (csrfp_wrapper::checkHeader ('Set-Cookie ' ));
271
275
$ this ->assertTrue (csrfp_wrapper::checkHeader ('csrfp_token ' ));
272
- $ this ->assertTrue (csrfp_wrapper::checkHeader ($ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]])); // Combine these 3 later
276
+ // $this->assertTrue(csrfp_wrapper::checkHeader($_SESSION[csrfprotector::$config['CSRFP_TOKEN']][0 ])); // Combine these 3 later
273
277
}
274
278
275
279
/**
@@ -388,11 +392,12 @@ public function testisURLallowed()
388
392
public function testModCSRFPEnabledException ()
389
393
{
390
394
putenv ('mod_csrfp_enabled=true ' );
391
- $ temp = $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = $ _COOKIE [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = 'abc ' ;
395
+ $ temp = $ _COOKIE [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = 'abc ' ;
396
+ $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]] = array ('abc ' );
392
397
csrfProtector::init ();
393
398
394
399
// Assuming no cookie change
395
- $ this ->assertTrue ($ temp == $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]]);
400
+ $ this ->assertTrue ($ temp == $ _SESSION [csrfprotector::$ config ['CSRFP_TOKEN ' ]][ 0 ] );
396
401
$ this ->assertTrue ($ temp == $ _COOKIE [csrfprotector::$ config ['CSRFP_TOKEN ' ]]);
397
402
}
398
403
}
0 commit comments