11<?php
2-
32if (!defined ('__CSRF_PROTECTOR__ ' )) {
43 define ('__CSRF_PROTECTOR__ ' , true ); // to avoid multiple declaration errors
54
@@ -90,10 +89,17 @@ class csrfProtector
9089 /**
9190 * Variable: $cookieConfig
9291 * Array of parameters for the setcookie method
93- * @var cookieConfig;
92+ * @var array<any>
9493 */
9594 private static $ cookieConfig = null ;
9695
96+ /**
97+ * Variable: $tokenHeaderKey
98+ * Key value in header array, which contain the token
99+ * @var string
100+ */
101+ private static $ tokenHeaderKey = null ;
102+
97103 /*
98104 * Variable: $requestType
99105 * Varaible to store weather request type is post or get
@@ -188,6 +194,9 @@ public static function init($length = null, $action = null)
188194 if (self ::$ config ['CSRFP_TOKEN ' ] == '' )
189195 self ::$ config ['CSRFP_TOKEN ' ] = CSRFP_TOKEN ;
190196
197+ self ::$ tokenHeaderKey = 'HTTP_ ' .strtoupper (self ::$ config ['CSRFP_TOKEN ' ]);
198+ self ::$ tokenHeaderKey = str_replace ('- ' , '_ ' , self ::$ tokenHeaderKey );
199+
191200 // load parameters for setcookie method
192201 if (!isset (self ::$ config ['cookieConfig ' ]))
193202 self ::$ config ['cookieConfig ' ] = array ();
@@ -248,19 +257,19 @@ public static function authorizePost()
248257 //set request type to POST
249258 self ::$ requestType = "POST " ;
250259
260+ // look for token in payload else from header
261+ $ token = self ::getTokenFromRequest ();
262+
251263 //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 )))) {
256266
257267 //action in case of failed validation
258- self ::failedValidationAction ();
268+ self ::failedValidationAction ();
259269 } else {
260270 self ::refreshToken (); //refresh token for successfull validation
261271 }
262272 } else if (!static ::isURLallowed ()) {
263-
264273 //currently for same origin only
265274 if (!(isset ($ _GET [self ::$ config ['CSRFP_TOKEN ' ]])
266275 && isset ($ _SESSION [self ::$ config ['CSRFP_TOKEN ' ]])
@@ -275,6 +284,36 @@ public static function authorizePost()
275284 }
276285 }
277286
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+
278317 /*
279318 * Function: isValidToken
280319 * function to check the validity of token in session array
0 commit comments