Skip to content

Commit d0017ad

Browse files
committed
transport of token moved to headers for XHR post
On js side, token is added to request header in case of POST request. On server side - POST request, it looks for data in header, if failed then looks for data in post payload.
1 parent a14bb9e commit d0017ad

File tree

2 files changed

+11
-56
lines changed

2 files changed

+11
-56
lines changed

js/csrfprotector.js

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -289,55 +289,18 @@ function csrfprotector_init() {
289289
*/
290290
function new_send(data) {
291291
if (this.method.toLowerCase() === 'post') {
292-
if (data !== null && typeof data === 'object') {
293-
data[CSRFP.CSRFP_TOKEN] = CSRFP._getAuthKey();
294-
} else {
295-
// Added support for content type == application / json
296-
if (this.headers && 'Content-Type' in this.headers
297-
&& this.headers['Content-Type'] === 'application/json') {
298-
try {
299-
data = JSON.parse(data)
300-
data[CSRFP.CSRFP_TOKEN] = CSRFP._getAuthKey();
301-
return this.old_send(JSON.stringify(data));
302-
303-
} catch (ex) {
304-
console.log("[ERROR] [CSRF Protector] Unable to parse content ",
305-
"when content-type is application/json", ex);
306-
}
307-
}
308-
309-
if (typeof data != "undefined") {
310-
data += "&";
311-
} else {
312-
data = "";
313-
}
314-
data += CSRFP.CSRFP_TOKEN +"=" +CSRFP._getAuthKey();
315-
}
292+
// attach the token in request header
293+
this.setRequestHeader(CSRFP.CSRFP_TOKEN, CSRFP._getAuthKey());
316294
}
317295
return this.old_send(data);
318296
}
319297

320-
/**
321-
* Wrapper method to override setRequestHeader method of
322-
* XMLHttpRequests
323-
* @param: header - header name
324-
* @param: value - header value
325-
*/
326-
function new_setRequestHeader(header, value) {
327-
if (!this.headers) this.headers = {};
328-
this.headers[header] = value;
329-
330-
this.old_setRequestHeader(header, value);
331-
}
332-
333298
if (window.XMLHttpRequest) {
334299
// Wrapping
335300
XMLHttpRequest.prototype.old_send = XMLHttpRequest.prototype.send;
336301
XMLHttpRequest.prototype.old_open = XMLHttpRequest.prototype.open;
337-
XMLHttpRequest.prototype.old_setRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
338302
XMLHttpRequest.prototype.open = new_open;
339303
XMLHttpRequest.prototype.send = new_send;
340-
XMLHttpRequest.prototype.setRequestHeader = new_setRequestHeader;
341304
}
342305
if (typeof ActiveXObject !== 'undefined') {
343306
ActiveXObject.prototype.old_send = ActiveXObject.prototype.send;

libs/csrf/csrfprotector.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
if (!defined('__CSRF_PROTECTOR__')) {
43
define('__CSRF_PROTECTOR__', true); // to avoid multiple declaration errors
54

@@ -159,6 +158,8 @@ public static function init($length = null, $action = null)
159158
}
160159
}
161160

161+
// TODO: initialize the setcookie params, based on config;
162+
162163
// Authorise the incoming request
163164
self::authorizePost();
164165

@@ -200,22 +201,12 @@ public static function authorizePost()
200201
//set request type to POST
201202
self::$requestType = "POST";
202203

203-
$token = (isset($_POST[self::$config['CSRFP_TOKEN']]))
204-
? $_POST[self::$config['CSRFP_TOKEN']] : false;
205-
206-
if ($_SERVER["CONTENT_TYPE"] === self::JSONCONTENTTYPE) {
207-
try {
208-
$request_body = file_get_contents('php://input');
209-
$request_body = json_decode($request_body, true);
210-
if (isset($request_body[self::$config['CSRFP_TOKEN']])) {
211-
$token = $request_body[self::$config['CSRFP_TOKEN']];
212-
}
213-
} catch (Exception $ex) {
214-
// silently absorb this exception
215-
// it could be because IO is blocked or json decode fails
216-
// either way log it or add some handleing
217-
// TODO ^^
218-
}
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']];
219210
}
220211

221212
//currently for same origin only
@@ -353,6 +344,7 @@ public static function refreshToken()
353344
array_push($_SESSION[self::$config['CSRFP_TOKEN']], $token);
354345

355346
//set token to cookie for client side processing
347+
// TODO: all the params must be loaded from config
356348
setcookie(self::$config['CSRFP_TOKEN'],
357349
$token,
358350
time() + self::$cookieExpiryTime,

0 commit comments

Comments
 (0)