From ad1a6ea51386feccfb1da0bc0fc4f5ace101b71b Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Tue, 27 Sep 2022 11:58:12 +1000 Subject: A better fix for bugs #58131 --- bg/ResponseProcessor.js | 4 +++- main_background.js | 29 ++++++++++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/bg/ResponseProcessor.js b/bg/ResponseProcessor.js index 922ff19..3c90d9c 100644 --- a/bg/ResponseProcessor.js +++ b/bg/ResponseProcessor.js @@ -83,6 +83,8 @@ class ResponseTextFilter { if (res) return res; const { requestId } = request; + // create a filter to modify response data, see + // Mozilla/Add-ons/WebExtensions/API/webRequest/filterResponseData const filter = browser.webRequest.filterResponseData(requestId); let buffer = []; @@ -118,7 +120,7 @@ class ResponseTextFilter { } catch (e) { console.error(e); } - if (editedText !== null && editedText.indexOf('/* LibreJS: script accepted') !== 0) { + if (editedText !== null && editedText !== response.text) { // we changed the content, let's re-encode const encoded = new TextEncoder().encode(editedText); // pre-pending the UTF-8 BOM will force the charset per HTML 5 specs diff --git a/main_background.js b/main_background.js index f947657..49815d5 100644 --- a/main_background.js +++ b/main_background.js @@ -378,10 +378,11 @@ async function onTabActivated({ tabId }) { /** * Checks script and updates the report entry accordingly. * - * Asynchronous function, returns the final edited script as a - * string. + * Asynchronous function, returns the final edited script as a string, + * or unedited script if passAccWlist is true and the script is + * accepted or whitelisted. */ -async function checkScriptAndUpdateReport(scriptSrc, url, tabId, whitelisted, isExternal = false) { +async function checkScriptAndUpdateReport(scriptSrc, url, tabId, whitelisted, isExternal = false, passAccWlist = false) { const scriptName = url.split('/').pop(); if (whitelisted) { if (tabId !== -1) { @@ -392,7 +393,7 @@ async function checkScriptAndUpdateReport(scriptSrc, url, tabId, whitelisted, is : 'Address whitelisted by user'; addReportEntry(tabId, { 'whitelisted': [site || url, reason], url }); } - if (scriptSrc.startsWith('javascript:')) + if (scriptSrc.startsWith('javascript:') || passAccWlist) return scriptSrc; else return `/* LibreJS: script whitelisted by user preference. */\n${scriptSrc}`; @@ -414,15 +415,17 @@ async function checkScriptAndUpdateReport(scriptSrc, url, tabId, whitelisted, is return scriptSrc.startsWith('javascript:') ? `javascript:void(${encodeURIComponent(edited)})` : edited; } - case 'whitelisted': { - return scriptSrc.startsWith('javascript:') - ? scriptSrc : `/* LibreJS: script ${actionType} by user. */\n${scriptSrc}`; - } + case 'whitelisted': + case 'accepted': + { + return (scriptSrc.startsWith('javascript:') || passAccWlist) + ? scriptSrc : `/* LibreJS: script ${actionType} by user. */\n${scriptSrc}`; + } + // blocked default: { - const scriptSource = accepted ? scriptSrc : editedSource; return scriptSrc.startsWith('javascript:') - ? (accepted ? scriptSource : `javascript:void(/* ${scriptSource} */)`) - : `/* LibreJS: script ${actionType}. */\n${scriptSource}`; + ? `javascript:void(/* ${editedSource} */)` + : `/* LibreJS: script ${actionType}. */\n${editedSource}`; } } } @@ -472,7 +475,7 @@ async function blockBlacklistedScripts(request) { * and external script inclusions in search of non-free JavaScript */ -var ResponseHandler = { +const ResponseHandler = { /** * Enforce white/black lists for url/site early (hashes will be handled later) */ @@ -554,7 +557,7 @@ var ResponseHandler = { async function handleScript(response, whitelisted) { const { text, request } = response; const { url, tabId } = request; - return await checkScriptAndUpdateReport(text, ListStore.urlItem(url), tabId, whitelisted, isExternal = true); + return await checkScriptAndUpdateReport(text, ListStore.urlItem(url), tabId, whitelisted, isExternal = true, passAccWlist = true); } /** -- cgit v1.2.3