From b4c63f6ae53e18ec773d644cce1b2ce52f18efe5 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 18 Oct 2022 08:48:31 -0400 Subject: [PATCH] [mv3] Handle permissions being revoked while uBOL is disabled --- platform/mv3/extension/js/background.js | 21 ++++++++++------ platform/mv3/extension/js/mode-manager.js | 28 +++++++++++++++------ platform/mv3/scriptlets/no-windowopen-if.js | 4 +-- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/platform/mv3/extension/js/background.js b/platform/mv3/extension/js/background.js index 547f573e2..45a879af2 100644 --- a/platform/mv3/extension/js/background.js +++ b/platform/mv3/extension/js/background.js @@ -50,7 +50,7 @@ import { setFilteringMode, getDefaultFilteringMode, setDefaultFilteringMode, - syncWithDemotedOrigins, + syncWithBrowserPermissions, } from './mode-manager.js'; /******************************************************************************/ @@ -154,13 +154,15 @@ function hasOmnipotence() { }); } -function onPermissionsRemoved(permissions) { - if ( permissions.origins?.includes('') ) { +async function onPermissionsRemoved() { + const beforeMode = await getDefaultFilteringMode(); + const modified = await syncWithBrowserPermissions(); + if ( modified === false ) { return; } + const afterMode = await getDefaultFilteringMode(); + if ( beforeMode > 1 && afterMode <= 1 ) { updateDynamicRules(); } - syncWithDemotedOrigins(permissions.origins).then(( ) => { - registerInjectables(permissions.origins); - }); + registerInjectables(); } /******************************************************************************/ @@ -287,6 +289,9 @@ async function start() { }); } + // Permissions may have been removed while the extension was disabled + await onPermissionsRemoved(); + // Unsure whether the browser remembers correctly registered css/scripts // after we quit the browser. For now uBOL will check unconditionally at // launch time whether content css/scripts are properly registered. @@ -307,7 +312,9 @@ async function start() { runtime.onMessage.addListener(onMessage); - browser.permissions.onRemoved.addListener(onPermissionsRemoved); + browser.permissions.onRemoved.addListener( + ( ) => { onPermissionsRemoved(); } + ); if ( rulesetConfig.firstRun ) { runtime.openOptionsPage(); diff --git a/platform/mv3/extension/js/mode-manager.js b/platform/mv3/extension/js/mode-manager.js index 0b61c4667..e2a94a973 100644 --- a/platform/mv3/extension/js/mode-manager.js +++ b/platform/mv3/extension/js/mode-manager.js @@ -25,7 +25,10 @@ /******************************************************************************/ -import { dnr } from './ext.js'; +import { + browser, + dnr, +} from './ext.js'; import { hostnamesFromMatches, @@ -326,22 +329,31 @@ async function setDefaultFilteringMode(afterLevel) { /******************************************************************************/ -async function syncWithDemotedOrigins(demotedOrigins) { - const demotedHostnames = new Set(hostnamesFromMatches(demotedOrigins)); - if ( demotedHostnames.has('all-urls') ) { +async function syncWithBrowserPermissions() { + const permissions = await browser.permissions.getAll(); + const allowedHostnames = new Set(hostnamesFromMatches(permissions.origins || [])); + const beforeMode = await getDefaultFilteringMode(); + let modified = false; + if ( beforeMode > 1 && allowedHostnames.has('all-urls') === false ) { await setDefaultFilteringMode(1); + modified = true; } + const afterMode = await getDefaultFilteringMode(); + if ( afterMode > 1 ) { return false; } const filteringModes = await getFilteringModeDetails(); const { extendedSpecific, extendedGeneric } = filteringModes; for ( const hn of extendedSpecific ) { - if ( demotedHostnames.has(hn) === false ) { continue; } + if ( allowedHostnames.has(hn) ) { continue; } extendedSpecific.delete(hn); + modified = true; } for ( const hn of extendedGeneric ) { - if ( demotedHostnames.has(hn) === false ) { continue; } + if ( allowedHostnames.has(hn) ) { continue; } extendedGeneric.delete(hn); + modified = true; } - return setFilteringModeDetails(filteringModes); + await setFilteringModeDetails(filteringModes); + return modified; } /******************************************************************************/ @@ -362,5 +374,5 @@ export { setDefaultFilteringMode, getFilteringModeDetails, getAllTrustedSiteDirectives, - syncWithDemotedOrigins, + syncWithBrowserPermissions, }; diff --git a/platform/mv3/scriptlets/no-windowopen-if.js b/platform/mv3/scriptlets/no-windowopen-if.js index 9e65cc422..e1b30241c 100644 --- a/platform/mv3/scriptlets/no-windowopen-if.js +++ b/platform/mv3/scriptlets/no-windowopen-if.js @@ -28,8 +28,8 @@ /******************************************************************************/ -/// name no-windowOpen-if -/// alias no-windowopen-if +/// name no-windowopen-if +/// alias no-windowOpen-if /// alias nowoif /******************************************************************************/