aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhackademix <giorgio@maone.net>2018-09-01 08:31:51 +0200
committerhackademix <giorgio@maone.net>2018-09-01 08:31:51 +0200
commitf6bf59a4ed44ec7323a76b11317f715c929a69b4 (patch)
treeadc49b2f1cd64d2a187d9b964bf29015ca8d6729
parentb724b4154b248b65fa93180b24d59e6c80b96d3b (diff)
Moved external licenses check into response pre-processing, in order to avoid filterResponse() as much as possible (fixing issue #11)
-rw-r--r--bg/ResponseProcessor.js10
-rw-r--r--content/externalLicenseChecker.js8
-rw-r--r--main_background.js49
3 files changed, 36 insertions, 31 deletions
diff --git a/bg/ResponseProcessor.js b/bg/ResponseProcessor.js
index 7c264a4..e3d6c45 100644
--- a/bg/ResponseProcessor.js
+++ b/bg/ResponseProcessor.js
@@ -33,8 +33,8 @@ class ResponseProcessor {
static install(handler, types = ["main_frame", "sub_frame", "script"]) {
if (listeners.has(handler)) return false;
- let listener =
- request => new ResponseTextFilter(request).process(handler);
+ let listener =
+ async request => await new ResponseTextFilter(request).process(handler);
listeners.set(handler, listener);
webRequestEvent.addListener(
listener,
@@ -70,12 +70,12 @@ class ResponseTextFilter {
(type === "script" || /\bhtml\b/i.test(md.contentType));
}
- process(handler) {
+ async process(handler) {
if (!this.canProcess) return ResponseProcessor.ACCEPT;
let {metaData, request} = this;
let response = {request, metaData}; // we keep it around allowing callbacks to store state
if (typeof handler.pre === "function") {
- let res = handler.pre(response);
+ let res = await handler.pre(response);
if (res) return res;
if (handler.post) handler = handler.post;
if (typeof handler !== "function") ResponseProcessor.ACCEPT;
@@ -101,7 +101,7 @@ class ResponseTextFilter {
} catch(e) {
console.error(e);
}
- if (metaData.forcedUTF8 ||
+ if (metaData.forcedUTF8 && request.type !== "script" ||
editedText !== null && response.text !== editedText) {
// if we changed the charset, the text or both, let's re-encode
filter.write(new TextEncoder().encode(editedText));
diff --git a/content/externalLicenseChecker.js b/content/externalLicenseChecker.js
index 89f871f..f150cea 100644
--- a/content/externalLicenseChecker.js
+++ b/content/externalLicenseChecker.js
@@ -79,9 +79,13 @@
}
browser.runtime.onMessage.addListener(async m => {
- if (m.action in handlers) {
+ if (m.action in handlers) try {
debug("Received message", m);
- return await handlers[m.action](m);
+ let result = await handlers[m.action](m);
+ console.debug("Returning", result);
+ return result;
+ } catch (e) {
+ console.error(e);
}
});
}
diff --git a/main_background.js b/main_background.js
index 0737926..a521d22 100644
--- a/main_background.js
+++ b/main_background.js
@@ -853,9 +853,9 @@ var ResponseHandler = {
/**
* Enforce white/black lists for url/site early (hashes will be handled later)
*/
- pre(response) {
+ async pre(response) {
let {request} = response;
- let {url, type, tabId} = request;
+ let {url, type, tabId, frameId} = request;
url = ListStore.urlItem(url);
let site = ListStore.siteItem(url);
@@ -878,15 +878,31 @@ var ResponseHandler = {
});
} else {
let whitelistedSite = whitelist.contains(site);
- if ((response.whitelisted = (whitelistedSite || whitelist.contains(url)))
- && type === "script") {
- // accept the script and stop processing
- addReportEntry(tabId, url, {url: topUrl,
- "whitelisted": [url, whitelistedSite ? `User whitelisted ${site}` : "Whitelisted by user"]});
- return ResponseProcessor.ACCEPT;
+ let whitelisted = response.whitelisted = whitelistedSite || whitelist.contains(url);
+ if (type === "script") {
+ if (whitelisted) {
+ // accept the script and stop processing
+ addReportEntry(tabId, url, {url: topUrl,
+ "whitelisted": [url, whitelistedSite ? `User whitelisted ${site}` : "Whitelisted by user"]});
+ return ResponseProcessor.ACCEPT;
+ } else {
+ let scriptInfo = await ExternalLicenses.check({url, tabId, frameId});
+ if (scriptInfo) {
+ let verdict, ret;
+ let msg = scriptInfo.toString();
+ if (scriptInfo.allFree) {
+ verdict = "accepted";
+ ret = ResponseProcessor.ACCEPT;
+ } else {
+ verdict = "blocked";
+ ret = ResponseProcessor.REJECT;
+ }
+ addReportEntry(tabId, url, {url, [verdict]: [url, msg]});
+ return ret;
+ }
+ }
}
}
-
// it's a page (it's too early to report) or an unknown script:
// let's keep processing
return ResponseProcessor.CONTINUE;
@@ -909,21 +925,6 @@ async function handle_script(response, whitelisted){
let {text, request} = response;
let {url, tabId, frameId} = request;
url = ListStore.urlItem(url);
- if (!whitelisted) {
- let scriptInfo = await ExternalLicenses.check({url, tabId, frameId});
- if (scriptInfo) {
- let verdict;
- let msg = scriptInfo.toString();
- if (scriptInfo.allFree) {
- verdict = "accepted";
- } else {
- verdict = "blocked";
- text = `/* ${msg} */`;
- }
- addReportEntry(tabId, url, {url, [verdict]: [url, msg]});
- return text;
- }
- }
let edited = await get_script(text, url, tabId, whitelisted, -2);
return Array.isArray(edited) ? edited[0] : edited;
}