Skip to content

Commit a14bb9e

Browse files
committed
fix for issue 80
#80 Added support for content-type = application json in both client and server side.
1 parent 32f29e3 commit a14bb9e

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

js/csrfprotector.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,22 @@ function csrfprotector_init() {
290290
function new_send(data) {
291291
if (this.method.toLowerCase() === 'post') {
292292
if (data !== null && typeof data === 'object') {
293-
data.append(CSRFP.CSRFP_TOKEN, CSRFP._getAuthKey());
293+
data[CSRFP.CSRFP_TOKEN] = CSRFP._getAuthKey();
294294
} 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+
295309
if (typeof data != "undefined") {
296310
data += "&";
297311
} else {
@@ -303,12 +317,27 @@ function csrfprotector_init() {
303317
return this.old_send(data);
304318
}
305319

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+
306333
if (window.XMLHttpRequest) {
307334
// Wrapping
308335
XMLHttpRequest.prototype.old_send = XMLHttpRequest.prototype.send;
309336
XMLHttpRequest.prototype.old_open = XMLHttpRequest.prototype.open;
337+
XMLHttpRequest.prototype.old_setRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
310338
XMLHttpRequest.prototype.open = new_open;
311339
XMLHttpRequest.prototype.send = new_send;
340+
XMLHttpRequest.prototype.setRequestHeader = new_setRequestHeader;
312341
}
313342
if (typeof ActiveXObject !== 'undefined') {
314343
ActiveXObject.prototype.old_send = ActiveXObject.prototype.send;

libs/csrf/csrfprotector.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class alreadyInitializedException extends \exception {};
2525

2626
class csrfProtector
2727
{
28+
/*
29+
* application/json content type
30+
*/
31+
const JSONCONTENTTYPE = "application/json";
32+
2833
/*
2934
* Variable: $cookieExpiryTime
3035
* expiry time for cookie
@@ -195,14 +200,30 @@ public static function authorizePost()
195200
//set request type to POST
196201
self::$requestType = "POST";
197202

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+
}
219+
}
220+
198221
//currently for same origin only
199-
if (!(isset($_POST[self::$config['CSRFP_TOKEN']])
200-
&& isset($_SESSION[self::$config['CSRFP_TOKEN']])
201-
&& (self::isValidToken($_POST[self::$config['CSRFP_TOKEN']]))
202-
)) {
222+
if (!($token && isset($_SESSION[self::$config['CSRFP_TOKEN']])
223+
&& (self::isValidToken($token)))) {
203224

204225
//action in case of failed validation
205-
self::failedValidationAction();
226+
self::failedValidationAction();
206227
} else {
207228
self::refreshToken(); //refresh token for successfull validation
208229
}

0 commit comments

Comments
 (0)