From 3f325cea256c5fd082a1c029d27f63ab93440ba6 Mon Sep 17 00:00:00 2001 From: hackademix Date: Tue, 5 Mar 2019 00:58:13 +0100 Subject: Fixed inline whitelisting regressions (issue #35). --- bg/ListManager.js | 9 +++++++- common/Storage.js | 3 +++ html/display_panel/content/main_panel.js | 4 ++-- main_background.js | 38 ++++++++++++++++---------------- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/bg/ListManager.js b/bg/ListManager.js index e0a85e9..354866d 100644 --- a/bg/ListManager.js +++ b/bg/ListManager.js @@ -50,6 +50,14 @@ class ListManager { */ getStatus(key, defValue = "unknown") { let {blacklist, whitelist} = this.lists; + let inline = ListStore.viewSourceItem(key); + if (inline) { + return blacklist.contains(inline) + ? "blacklisted" + : whitelist.contains(inline) ? "whitelisted" + : defValue; + } + let match = key.match(/\(([^)]+)\)(?=[^()]*$)/); if (!match) { let url = ListStore.urlItem(key); @@ -61,7 +69,6 @@ class ListManager { } let [hashItem, srcHash] = match; // (hash), hash - return blacklist.contains(hashItem) ? "blacklisted" : this.builtInHashes.has(srcHash) || whitelist.contains(hashItem) ? "whitelisted" diff --git a/common/Storage.js b/common/Storage.js index a83ce8f..415b4bb 100644 --- a/common/Storage.js +++ b/common/Storage.js @@ -66,6 +66,9 @@ class ListStore { }); } + static viewSourceItem(url) { + return url.startsWith("view-source:") && url.replace(/#line\d+/,"#"); + } static hashItem(hash) { return hash.startsWith("(") ? hash : `(${hash})`; } diff --git a/html/display_panel/content/main_panel.js b/html/display_panel/content/main_panel.js index 022be8c..83c3a6b 100644 --- a/html/display_panel/content/main_panel.js +++ b/html/display_panel/content/main_panel.js @@ -132,9 +132,9 @@ function createList(data, group){ a.href = scriptId.split("(")[0]; if (scriptId.startsWith("view-source:")) { a.target ="LibreJS-ViewSource"; - let source = scriptId.split("\n")[1]; + let source = scriptId.match(/\n([^]*)/); if (source) { - li.querySelector(".source").textContent = decodeURIComponent(source); + li.querySelector(".source").textContent = source[1]; li.querySelector(".toggle-source").style.display = "inline"; } scriptId = scriptId.replace(viewSourceToHuman, "$3 at line $2 of $1"); diff --git a/main_background.js b/main_background.js index 7bbc237..4442c6d 100644 --- a/main_background.js +++ b/main_background.js @@ -336,7 +336,11 @@ async function connected(p) { for (let action of ["whitelist", "blacklist", "forget"]) { if (m[action]) { let [key] = m[action]; - if (m.site) key = ListStore.siteItem(key); + if (m.site) { + key = ListStore.siteItem(key); + } else { + key = ListStore.viewSourceItem(key) || key; + } await listManager[action](key); update = true; } @@ -776,23 +780,20 @@ async function get_script(response, url, tabId = -1, whitelisted = false, index let report = activityReports[tabId] || (activityReports[tabId] = await createReport({tabId})); updateBadge(tabId, report, !verdict); let category = await addReportEntry(tabId, sourceHash, {"url": domain, [verdict ? "accepted" : "blocked"]: [url, reason]}); - let scriptSource = verdict ? response : editedSource; switch(category) { case "blacklisted": - if (response.startsWith("javascript:")) - return result(`# LibreJS: script ${category} by user.`); - else - return result(`/* LibreJS: script ${category} by user. */`); + editedSource = `/* LibreJS: script ${category} by user. */`; + return result(response.startsWith("javascript:") + ? `javascript:void(${encodeURIComponent(editedSource)})` : editedSource); case "whitelisted": - if (response.startsWith("javascript:")) - return result(scriptSource); - else - return result(`/* LibreJS: script ${category} by user. */\n${scriptSource}`); + return result(response.startsWith("javascript:") + ? response : `/* LibreJS: script ${category} by user. */\n${response}`); default: - if (response.startsWith("javascript:")) - return result(scriptSource); - else - return result(`/* LibreJS: script ${category}. */\n${scriptSource}`); + let scriptSource = verdict ? response : editedSource; + return result(response.startsWith("javascript:") + ? (verdict ? scriptSource : `javascript:void(/* ${scriptSource} */)`) + : `/* LibreJS: script ${category}. */\n${scriptSource}` + ); } } @@ -1059,7 +1060,7 @@ async function editHtml(html, documentUrl, tabId, frameId, whitelisted){ extras = "(0)"; } else if (license) { line = html.substring(0, html.indexOf(first_script_src)).split(/\n/).length; - extras = "\n" + encodeURIComponent(first_script_src); + extras = "\n" + first_script_src; } let viewUrl = line ? `view-source:${documentUrl}#line${line}(<${meta_element ? meta_element.tagName : "SCRIPT"}>)${extras}` : url; addReportEntry(tabId, url, {url, "accepted":[viewUrl, `Global license for the page: ${license}`]}); @@ -1087,10 +1088,9 @@ async function editHtml(html, documentUrl, tabId, frameId, whitelisted){ if (dejaVu.has(key)) { edited = dejaVu.get(key); } else { - let url = `view-source:${documentUrl}#line${line}(<${element.tagName} ${name}>)\n${encodeURIComponent(value.trim())}`; + let url = `view-source:${documentUrl}#line${line}(<${element.tagName} ${name}>)\n${value.trim()}`; if (name === "href") value = decodeURIComponent(value); - edited = await get_script(value, url, tabId, whitelist.contains(documentUrl)); - dejaVu.set(key, edited); + edited = await get_script(value, url, tabId, whitelist.contains(url)); dejaVu.set(key, edited); } if (edited && edited !== value) { modified = true; @@ -1114,7 +1114,7 @@ async function editHtml(html, documentUrl, tabId, frameId, whitelisted){ if (dejaVu.has(source)) { editedSource = dejaVu.get(source); } else { - let url = `view-source:${documentUrl}#line${line}(