@@ -65,11 +65,6 @@ function __construct($cfg) {
65
65
66
66
class csrfProtector
67
67
{
68
- /*
69
- * application/json content type
70
- */
71
- const JSONCONTENTTYPE = "application/json " ;
72
-
73
68
/*
74
69
* Variable: $cookieExpiryTime
75
70
* expiry time for cookie
@@ -94,10 +89,17 @@ class csrfProtector
94
89
/**
95
90
* Variable: $cookieConfig
96
91
* Array of parameters for the setcookie method
97
- * @var cookieConfig;
92
+ * @var array<any>
98
93
*/
99
94
private static $ cookieConfig = null ;
100
95
96
+ /**
97
+ * Variable: $tokenHeaderKey
98
+ * Key value in header array, which contain the token
99
+ * @var string
100
+ */
101
+ private static $ tokenHeaderKey = null ;
102
+
101
103
/*
102
104
* Variable: $requestType
103
105
* Varaible to store weather request type is post or get
@@ -192,6 +194,9 @@ public static function init($length = null, $action = null)
192
194
if (self ::$ config ['CSRFP_TOKEN ' ] == '' )
193
195
self ::$ config ['CSRFP_TOKEN ' ] = CSRFP_TOKEN ;
194
196
197
+ self ::$ tokenHeaderKey = 'HTTP_ ' .strtoupper (self ::$ config ['CSRFP_TOKEN ' ]);
198
+ self ::$ tokenHeaderKey = str_replace ('- ' , '_ ' , self ::$ tokenHeaderKey );
199
+
195
200
// load parameters for setcookie method
196
201
if (!isset (self ::$ config ['cookieConfig ' ]))
197
202
self ::$ config ['cookieConfig ' ] = array ();
@@ -211,8 +216,6 @@ public static function init($length = null, $action = null)
211
216
}
212
217
}
213
218
214
- // TODO: initialize the setcookie params, based on config;
215
-
216
219
// Authorise the incoming request
217
220
self ::authorizePost ();
218
221
@@ -267,7 +270,6 @@ public static function authorizePost()
267
270
self ::refreshToken (); //refresh token for successfull validation
268
271
}
269
272
} else if (!static ::isURLallowed ()) {
270
-
271
273
//currently for same origin only
272
274
if (!(isset ($ _GET [self ::$ config ['CSRFP_TOKEN ' ]])
273
275
&& isset ($ _SESSION [self ::$ config ['CSRFP_TOKEN ' ]])
@@ -293,7 +295,7 @@ public static function authorizePost()
293
295
* any (string / bool) - token retrieved from header or form payload
294
296
*/
295
297
private static function getTokenFromRequest () {
296
- // look for token in header , then in payload
298
+ // look for in $_POST , then header
297
299
if (isset ($ _POST [self ::$ config ['CSRFP_TOKEN ' ]])) {
298
300
return $ _POST [self ::$ config ['CSRFP_TOKEN ' ]];
299
301
}
@@ -302,12 +304,11 @@ private static function getTokenFromRequest() {
302
304
if (isset (apache_request_headers ()[self ::$ config ['CSRFP_TOKEN ' ]])) {
303
305
return apache_request_headers ()[self ::$ config ['CSRFP_TOKEN ' ]];
304
306
}
305
- } else {
306
- $ serverKey = 'HTTP_ ' .strtoupper (self ::$ config ['CSRFP_TOKEN ' ]);
307
- $ serverKey = str_replace ('- ' , '_ ' , $ serverKey );
308
- if (isset ($ _SERVER [$ serverKey ])) {
309
- return $ _SERVER [$ serverKey ];
310
- }
307
+ }
308
+
309
+ if (self ::$ tokenHeaderKey === null ) return false ;
310
+ if (isset ($ _SERVER [self ::$ tokenHeaderKey ])) {
311
+ return $ _SERVER [self ::$ tokenHeaderKey ];
311
312
}
312
313
313
314
return false ;
0 commit comments