explicitly parse a text response into JSON

This commit is contained in:
tophf 2018-07-05 11:40:23 +03:00
parent 561e7c585b
commit 9992ae3374
2 changed files with 7 additions and 3 deletions

View File

@ -364,12 +364,13 @@ document.documentElement.appendChild(document.createElement('script')).text = `(
return;
}
const xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.onloadend = xhr.onerror = () => {
window.stop();
top.postMessage({
id: data.xhr.id,
status: xhr.status,
// [being overcautious] a string response is used instead of relying on responseType=json
// because it was invoked in a web page context so another extension may have incorrectly spoofed it
response: xhr.response,
}, EXTENSION_ORIGIN);
};

View File

@ -832,8 +832,11 @@ window.addEventListener('showStyles:done', function _() {
chrome.webRequest.onBeforeRequest.removeListener(stripResources);
searchFrameQueue.delete(data.id);
clearTimeout(timeout);
if (data.response && data.status < 400) {
resolve(data.response);
// [being overcautious] a string response is used instead of relying on responseType=json
// because it was invoked in a web page context so another extension may have incorrectly spoofed it
const json = tryJSONparse(data.response);
if (json && data.status < 400) {
resolve(json);
} else {
reject(data.status);
}