From ff358d2448bb9d02b389c9c2b17482ad3ab0f0f8 Mon Sep 17 00:00:00 2001 From: Alexei Date: Wed, 6 Sep 2023 12:29:26 -0400 Subject: [PATCH 1/2] Handle messages gotten before background is ready --- src/js/options.js | 8 -------- src/js/popup.js | 8 -------- src/js/webrequest.js | 17 +++++++++++++++-- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/js/options.js b/src/js/options.js index 820e624b67..b55ba0ff02 100644 --- a/src/js/options.js +++ b/src/js/options.js @@ -1035,14 +1035,6 @@ $(function () { chrome.runtime.sendMessage({ type: "getOptionsData", }, (response) => { - if (!response) { - // event page/extension service worker is still starting up, retry - // async w/ non-zero delay to avoid locking up the messaging channel - setTimeout(function () { - getOptionsData(); - }, 10); - return; - } OPTIONS_DATA = response; loadOptions(); }); diff --git a/src/js/popup.js b/src/js/popup.js index 542def628e..de8214bb8e 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -881,14 +881,6 @@ $(function () { tabId: tab.id, tabUrl: tab.url }, (response) => { - if (!response) { - // event page/extension service worker is still starting up, retry - // async w/ non-zero delay to avoid locking up the messaging channel - setTimeout(function () { - getPopupData(tab); - }, 10); - return; - } setPopupData(response); refreshPopup(); init(); diff --git a/src/js/webrequest.js b/src/js/webrequest.js index 0b57aae765..fa7c62ccde 100644 --- a/src/js/webrequest.js +++ b/src/js/webrequest.js @@ -1186,8 +1186,16 @@ function getSurrogateWidget(name, data, frame_url) { // NOTE: sender.tab is available for content script (not popup) messages only function dispatcher(request, sender, sendResponse) { + // messages may arrive before Privacy Badger is ready + // for example, user clicks to open the popup when the ephemeral background + // process is not running; the getPopupData message from the popup causes + // the background to start but the background is not yet ready to respond if (!badger.INITIALIZED) { - return sendResponse(); + setTimeout(function () { + dispatcher(request, sender, sendResponse); + }, 50); + // indicate this is an async response to chrome.runtime.onMessage + return true; } // messages from content scripts are to be treated with greater caution: @@ -1210,7 +1218,12 @@ function dispatcher(request, sender, sendResponse) { "widgetFromSurrogate", "widgetReplacementReady", ]; - if (!KNOWN_CONTENT_SCRIPT_MESSAGES.includes(request.type)) { + if (KNOWN_CONTENT_SCRIPT_MESSAGES.includes(request.type)) { + if (!sender.tab) { + console.error("Dropping malformed content script message %o from %s", request, sender.url); + return sendResponse(); + } + } else { console.error("Rejected unknown message %o from %s", request, sender.url); return sendResponse(); } From 3e8c49099de093c51f289f88e909a8c5c8d23e59 Mon Sep 17 00:00:00 2001 From: Alexei Date: Thu, 7 Sep 2023 10:01:02 -0400 Subject: [PATCH 2/2] Remove init. check from delayed tab data cleanup No need to check for initialization since the background process will either still be around in 20 seconds (and presumably initialized by then), or it won't and the timer will get destroyed. --- src/js/webrequest.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/js/webrequest.js b/src/js/webrequest.js index fa7c62ccde..b03d136376 100644 --- a/src/js/webrequest.js +++ b/src/js/webrequest.js @@ -606,14 +606,12 @@ function onHeadersReceived(details) { /** * Event handler when a tab gets removed * - * @param {Integer} tab_id Id of the tab + * @param {Integer} tab_id ID of the tab */ function onTabRemoved(tab_id) { - if (badger.INITIALIZED) { - setTimeout(function () { - forgetTab(tab_id); - }, 20000); - } + setTimeout(function () { + forgetTab(tab_id); + }, utils.oneSecond() * 20); } /**