From d1f7f5ee8c1111c2b12a2870c64a830ca0f4fd04 Mon Sep 17 00:00:00 2001 From: Lilia Date: Tue, 24 Oct 2017 15:54:46 -0700 Subject: [PATCH] Fix json parsing exceptions obscuring server errors (#1605) I got a 413 (Rate limit exceeded) error from the server while fetching prekeys. The client tried to parse the response as json since we expect json from the prekey endpoint, which threw an exception because the response was not json. This change prevents us from treating the response as json unless it has the Content-Type header set accordingly. If for some reason, the client and server disagree on whether the response should be or is json, we'll default to treating it as text. // FREEBIE --- js/libtextsecure.js | 7 ++++--- libtextsecure/api.js | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index fa5d03ea0..59af9145e 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -37501,12 +37501,13 @@ var TextSecureServer = (function() { } window.nodeFetch(url, fetchOptions).then(function(response) { var resultPromise; - if (options.responseType === 'json') { + if (options.responseType === 'json' + && response.headers.get('Content-Type') === 'application/json') { resultPromise = response.json(); - } else if (!options.responseType || options.responseType === 'text') { - resultPromise = response.text(); } else if (options.responseType === 'arraybuffer') { resultPromise = response.buffer(); + } else { + resultPromise = response.text(); } return resultPromise.then(function(result) { if (options.responseType === 'arraybuffer') { diff --git a/libtextsecure/api.js b/libtextsecure/api.js index 0c20d8416..aa8ee2c03 100644 --- a/libtextsecure/api.js +++ b/libtextsecure/api.js @@ -64,12 +64,13 @@ var TextSecureServer = (function() { } window.nodeFetch(url, fetchOptions).then(function(response) { var resultPromise; - if (options.responseType === 'json') { + if (options.responseType === 'json' + && response.headers.get('Content-Type') === 'application/json') { resultPromise = response.json(); - } else if (!options.responseType || options.responseType === 'text') { - resultPromise = response.text(); } else if (options.responseType === 'arraybuffer') { resultPromise = response.buffer(); + } else { + resultPromise = response.text(); } return resultPromise.then(function(result) { if (options.responseType === 'arraybuffer') {