Skip to content

Commit fb5c208

Browse files
committed
added support to case when apache_request_headers is missing
server now looks for token in payload else from header. In case of header first from - apache_request_headers else from superglobal $_SERVER
1 parent d0017ad commit fb5c208

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

libs/csrf/csrfprotector.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,8 @@ public static function authorizePost()
201201
//set request type to POST
202202
self::$requestType = "POST";
203203

204-
// look for token in header, then in payload
205-
$token = false;
206-
if (isset(apache_request_headers()[self::$config['CSRFP_TOKEN']])) {
207-
$token = apache_request_headers()[self::$config['CSRFP_TOKEN']];
208-
} else if (isset($_POST[self::$config['CSRFP_TOKEN']])) {
209-
$token = $_POST[self::$config['CSRFP_TOKEN']];
210-
}
204+
// look for token in payload else from header
205+
$token = self::getTokenFromRequest();
211206

212207
//currently for same origin only
213208
if (!($token && isset($_SESSION[self::$config['CSRFP_TOKEN']])
@@ -234,6 +229,37 @@ public static function authorizePost()
234229
}
235230
}
236231

232+
/*
233+
* Fucntion: getTokenFromRequest
234+
* function to get token in case of POST request
235+
*
236+
* Parameters:
237+
* void
238+
*
239+
* Returns:
240+
* any (string / bool) - token retrieved from header or form payload
241+
*/
242+
private static function getTokenFromRequest() {
243+
// look for token in header, then in payload
244+
if (isset($_POST[self::$config['CSRFP_TOKEN']])) {
245+
return $_POST[self::$config['CSRFP_TOKEN']];
246+
}
247+
248+
if (function_exists('apache_request_headers')) {
249+
if (isset(apache_request_headers()[self::$config['CSRFP_TOKEN']])) {
250+
return apache_request_headers()[self::$config['CSRFP_TOKEN']];
251+
}
252+
} else {
253+
$serverKey = 'HTTP_' .strtoupper(self::$config['CSRFP_TOKEN']);
254+
$serverKey = str_replace('-', '_', $serverKey);
255+
if (isset($_SERVER[$serverKey])) {
256+
return $_SERVER[$serverKey];
257+
}
258+
}
259+
260+
return false;
261+
}
262+
237263
/*
238264
* Function: isValidToken
239265
* function to check the validity of token in session array

0 commit comments

Comments
 (0)