From cc005936ed81ee545377f2627e56b6fa8bf60a3f Mon Sep 17 00:00:00 2001 From: xsalon00 Date: Apr 25 2022 19:07:47 +0000 Subject: Fix early loading of module configuration fpDetectionOn was undefined after the first installation (badge grayed out) Also update config variables when modified via advanced options --- diff --git a/common/fp_detect_background.js b/common/fp_detect_background.js index b4d509b..bb3b6cc 100644 --- a/common/fp_detect_background.js +++ b/common/fp_detect_background.js @@ -135,7 +135,7 @@ const FPD_DEF_SETTINGS = { description: "Allow the extension to react whenever there is a high likelihood of fingerprinting.", description2: [ "• Interrupt network traffic for the page to prevent possible fingerprint leakage.", - "• Try to clear browser storage of the page to remove possibly cached fingerprint. (No additional permissions required.)", + "• Clear some browser storage of the page to remove possibly cached fingerprint. (No additional permissions required.)", "• Clearing: localStorage, sessionStorage, JS cookies, IndexedDB, caches, window.name", "NOTE: Blocking behavior may break some functionality on fingerprinting websites." ] @@ -146,7 +146,7 @@ const FPD_DEF_SETTINGS = { description: "Allow the extension to react whenever there is a high likelihood of fingerprinting.", description2: [ "• Interrupt network traffic for the page to prevent possible fingerprint leakage.", - "• Reliably clear all available storage mechanisms of the page where fingerprint may be cached. (Requires BrowsingData permission.)", + "• Clear all available storage mechanisms of the page where fingerprint may be cached. (Requires BrowsingData permission.)", "• Clearing: localStorage, sessionStorage, cookies, IndexedDB, caches, window.name, fileSystems, WebSQL, serviceWorkers", "NOTE: Blocking behavior may break some functionality on fingerprinting websites." ], @@ -200,16 +200,7 @@ function initFpd() { } // load configuration and settings from storage - let loadConfiguration = (name) => { - browser.storage.sync.get([name]).then(function(result) { - if (result[name] != undefined) { - this[name] = result[name]; - } - }); - } - loadConfiguration("fpDetectionOn"); - loadConfiguration("fpdWhitelist"); - loadConfiguration("fpdSettings"); + fpdLoadConfiguration(); // take care of unsupported resources for cross-browser behaviour uniformity balanceUnsupportedWrappers(); @@ -799,6 +790,10 @@ function fpdCommonMessageListener(record, sender) { fpdSettings[record.id] = record.value; browser.storage.sync.set({"fpdSettings": fpdSettings}); break; + case "fpd-load-config": + // load current configuration + fpdLoadConfiguration(); + break; case "fpd-clear-storage": // clear browser storage for the origin clearStorageFacilities(record.url); @@ -851,6 +846,17 @@ function fpdRequestCancel(requestDetails) { */ /** + * The function that loads module configuration from sync storage. + */ +function fpdLoadConfiguration() { + browser.storage.sync.get(["fpDetectionOn", "fpdWhitelist", "fpdSettings"]).then(function(result) { + fpDetectionOn = result.fpDetectionOn ? true : false; + fpdWhitelist = result.fpdWhitelist ? result.fpdWhitelist : {}; + fpdSettings = result.fpdSettings ? result.fpdSettings : {}; + }); +} + +/** * The function transforming recursive groups definition into indexed fpGroups object. * * \param input Group object from loaded JSON format. @@ -1025,7 +1031,7 @@ function evaluateFingerprinting(tabId) { // block request and clear cache data only if "blocking" behavior is set if (fpdSettings.behavior > 1) { - // clear local and session storage (using content script) for every frame in this tab (required?) + // clear storages (using content script) for every frame in this tab if (tabId >= 0) { browser.tabs.sendMessage(tabId, { cleanStorage: true, diff --git a/common/http_shield_common.js b/common/http_shield_common.js index b24121b..8b39447 100644 --- a/common/http_shield_common.js +++ b/common/http_shield_common.js @@ -124,18 +124,40 @@ const NBS_DEF_SETTINGS = { } }; -/// \cond (Exclude this section from the doxygen documentation. If this section is not excluded, it is documented as a separate function.) -browser.storage.sync.get(["nbsWhitelist"]).then(function(result){ - if (result.nbsWhitelist != undefined) - doNotBlockHosts = result.nbsWhitelist; -}); +/** + * The function that loads module configuration from sync storage. + */ +function nbsLoadConfiguration() { + browser.storage.sync.get(["requestShieldOn", "nbsWhitelist", "nbsSettings"]).then(function(result) { + //If found object is true or undefined, turn the requestShieldOn + if (result.requestShieldOn == undefined || result.requestShieldOn) + { + //Hook up the listeners + browser.webRequest.onBeforeSendHeaders.addListener( + beforeSendHeadersListener, + {urls: [""]}, + ["blocking", "requestHeaders"] + ); -browser.storage.sync.get(["nbsSettings"]).then(function(result){ - if (result.nbsSettings != undefined) - nbsSettings = result.nbsSettings; -}); + if (typeof onResponseStartedListener === "function") + { + browser.webRequest.onResponseStarted.addListener( + onResponseStartedListener, + {urls: [""]}, + ["responseHeaders"] + ); + } + } + doNotBlockHosts = result.nbsWhitelist ? result.nbsWhitelist : {}; + nbsSettings = result.nbsSettings ? result.nbsSettings : {}; + }); +} + +/// \cond (Exclude this section from the doxygen documentation. If this section is not excluded, it is documented as a separate function.) /// Hook up the listener for receiving messages +nbsLoadConfiguration(); + browser.runtime.onMessage.addListener(nbsCommonMessageListener); browser.runtime.onMessage.addListener(nbsMessageListener); browser.runtime.onMessage.addListener(nbsSettingsListener); @@ -146,31 +168,6 @@ browser.permissions.onRemoved.addListener((permissions) => { browser.storage.sync.set({"nbsSettings": nbsSettings}); }); -/// Check the storage for requestShieldOn object -browser.storage.sync.get(["requestShieldOn"]).then(function(result){ - //If found object is true or undefined, turn the requestShieldOn - if (result.requestShieldOn == undefined || result.requestShieldOn) - { - //Hook up the listeners - browser.webRequest.onBeforeSendHeaders.addListener( - beforeSendHeadersListener, - {urls: [""]}, - ["blocking", "requestHeaders"] - ); - - if (typeof onResponseStartedListener === "function") - { - browser.webRequest.onResponseStarted.addListener( - onResponseStartedListener, - {urls: [""]}, - ["responseHeaders"] - ); - } - } -}); -/// \endcond - -/// \cond (Exclude this section from the doxygen documentation. If this section is not excluded, it is documented as a separate function.) // Obtain file path in user's file system and read CSV file with IPv4 local zones readFile(browser.runtime.getURL("ipv4.dat")) .then(_res => { @@ -628,6 +625,10 @@ function nbsSettingsListener(message) nbsSettings[message.id] = message.value; browser.storage.sync.set({"nbsSettings": nbsSettings}); } + else if (message.purpose === "nbs-load-config") { + // load current configuration + nbsLoadConfiguration(); + } } /** diff --git a/common/update.js b/common/update.js index 189dde1..cbf8913 100644 --- a/common/update.js +++ b/common/update.js @@ -394,7 +394,21 @@ function installUpdate() { } item.version = 6.4; } - browser.storage.sync.set(item); + browser.storage.sync.set(item).then(() => { + // origin of update.js must be recognized (background script vs. options page) + if (typeof fpdLoadConfiguration === "function") { + fpdLoadConfiguration(); + } + else { + browser.runtime.sendMessage({purpose: "fpd-load-config"}); + } + if (typeof nbsLoadConfiguration === "function") { + nbsLoadConfiguration(); + } + else { + browser.runtime.sendMessage({purpose: "nbs-load-config"}) + } + }); }); } browser.runtime.onInstalled.addListener(installUpdate);