1
1
<?php
2
-
3
2
if (!defined ('__CSRF_PROTECTOR__ ' )) {
4
3
define ('__CSRF_PROTECTOR__ ' , true ); // to avoid multiple declaration errors
5
4
@@ -90,10 +89,17 @@ class csrfProtector
90
89
/**
91
90
* Variable: $cookieConfig
92
91
* Array of parameters for the setcookie method
93
- * @var cookieConfig;
92
+ * @var array<any>
94
93
*/
95
94
private static $ cookieConfig = null ;
96
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
+
97
103
/*
98
104
* Variable: $requestType
99
105
* Varaible to store weather request type is post or get
@@ -188,6 +194,9 @@ public static function init($length = null, $action = null)
188
194
if (self ::$ config ['CSRFP_TOKEN ' ] == '' )
189
195
self ::$ config ['CSRFP_TOKEN ' ] = CSRFP_TOKEN ;
190
196
197
+ self ::$ tokenHeaderKey = 'HTTP_ ' .strtoupper (self ::$ config ['CSRFP_TOKEN ' ]);
198
+ self ::$ tokenHeaderKey = str_replace ('- ' , '_ ' , self ::$ tokenHeaderKey );
199
+
191
200
// load parameters for setcookie method
192
201
if (!isset (self ::$ config ['cookieConfig ' ]))
193
202
self ::$ config ['cookieConfig ' ] = array ();
@@ -248,19 +257,19 @@ public static function authorizePost()
248
257
//set request type to POST
249
258
self ::$ requestType = "POST " ;
250
259
260
+ // look for token in payload else from header
261
+ $ token = self ::getTokenFromRequest ();
262
+
251
263
//currently for same origin only
252
- if (!(isset ($ _POST [self ::$ config ['CSRFP_TOKEN ' ]])
253
- && isset ($ _SESSION [self ::$ config ['CSRFP_TOKEN ' ]])
254
- && (self ::isValidToken ($ _POST [self ::$ config ['CSRFP_TOKEN ' ]]))
255
- )) {
264
+ if (!($ token && isset ($ _SESSION [self ::$ config ['CSRFP_TOKEN ' ]])
265
+ && (self ::isValidToken ($ token )))) {
256
266
257
267
//action in case of failed validation
258
- self ::failedValidationAction ();
268
+ self ::failedValidationAction ();
259
269
} else {
260
270
self ::refreshToken (); //refresh token for successfull validation
261
271
}
262
272
} else if (!static ::isURLallowed ()) {
263
-
264
273
//currently for same origin only
265
274
if (!(isset ($ _GET [self ::$ config ['CSRFP_TOKEN ' ]])
266
275
&& isset ($ _SESSION [self ::$ config ['CSRFP_TOKEN ' ]])
@@ -275,6 +284,36 @@ public static function authorizePost()
275
284
}
276
285
}
277
286
287
+ /*
288
+ * Fucntion: getTokenFromRequest
289
+ * function to get token in case of POST request
290
+ *
291
+ * Parameters:
292
+ * void
293
+ *
294
+ * Returns:
295
+ * any (string / bool) - token retrieved from header or form payload
296
+ */
297
+ private static function getTokenFromRequest () {
298
+ // look for in $_POST, then header
299
+ if (isset ($ _POST [self ::$ config ['CSRFP_TOKEN ' ]])) {
300
+ return $ _POST [self ::$ config ['CSRFP_TOKEN ' ]];
301
+ }
302
+
303
+ if (function_exists ('apache_request_headers ' )) {
304
+ if (isset (apache_request_headers ()[self ::$ config ['CSRFP_TOKEN ' ]])) {
305
+ return apache_request_headers ()[self ::$ config ['CSRFP_TOKEN ' ]];
306
+ }
307
+ }
308
+
309
+ if (self ::$ tokenHeaderKey === null ) return false ;
310
+ if (isset ($ _SERVER [self ::$ tokenHeaderKey ])) {
311
+ return $ _SERVER [self ::$ tokenHeaderKey ];
312
+ }
313
+
314
+ return false ;
315
+ }
316
+
278
317
/*
279
318
* Function: isValidToken
280
319
* function to check the validity of token in session array
0 commit comments