From 417dab538c99adbf567279043d733fdad1465968 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 4 Aug 2024 11:58:09 -0400 Subject: [PATCH] [mv3] Fix generating `allowAllRequests` rule when default mode is no-filtering --- platform/mv3/extension/js/mode-manager.js | 76 +++++++++++++++-------- 1 file changed, 50 insertions(+), 26 deletions(-) diff --git a/platform/mv3/extension/js/mode-manager.js b/platform/mv3/extension/js/mode-manager.js index c6e7ddba6..c3b9a01bb 100644 --- a/platform/mv3/extension/js/mode-manager.js +++ b/platform/mv3/extension/js/mode-manager.js @@ -74,6 +74,7 @@ const pruneHostnameFromSet = (hostname, hnSet) => { /******************************************************************************/ const eqSets = (setBefore, setAfter) => { + if ( setBefore.size !== setAfter.size ) { return false; } for ( const hn of setAfter ) { if ( setBefore.has(hn) === false ) { return false; } } @@ -283,23 +284,48 @@ async function writeFilteringModeDetails(afterDetails) { async function filteringModesToDNR(modes) { const dynamicRuleMap = await getDynamicRules(); const trustedRule = dynamicRuleMap.get(TRUSTED_DIRECTIVE_BASE_RULE_ID+0); - const trustedDomainSet = new Set(trustedRule?.condition.requestDomains); - if ( trustedDomainSet.size !== 0 ) { - if ( eqSets(trustedDomainSet, modes.none) ) { return; } - } else if ( trustedRule !== undefined ) { - if ( modes.none.has('all-urls') ) { return; } + const beforeRequestDomainSet = new Set(trustedRule?.condition.requestDomains); + const beforeExcludedRrequestDomainSet = new Set(trustedRule?.condition.excludedRequestDomains); + if ( trustedRule !== undefined && beforeRequestDomainSet.size === 0 ) { + beforeRequestDomainSet.add('all-urls'); + } else { + beforeExcludedRrequestDomainSet.add('all-urls'); } - const removeRuleIds = []; - if ( trustedRule !== undefined ) { - removeRuleIds.push(TRUSTED_DIRECTIVE_BASE_RULE_ID+0); - removeRuleIds.push(TRUSTED_DIRECTIVE_BASE_RULE_ID+1); - dynamicRuleMap.delete(TRUSTED_DIRECTIVE_BASE_RULE_ID+0); - dynamicRuleMap.delete(TRUSTED_DIRECTIVE_BASE_RULE_ID+1); + + const noneHostnames = new Set([ ...modes.none ]); + const notNoneHostnames = new Set([ ...modes.basic, ...modes.optimal, ...modes.complete ]); + let afterRequestDomainSet = new Set(); + let afterExcludedRequestDomainSet = new Set(); + if ( noneHostnames.has('all-urls') ) { + afterRequestDomainSet = new Set([ 'all-urls' ]); + afterExcludedRequestDomainSet = notNoneHostnames; + } else { + afterRequestDomainSet = noneHostnames; + afterExcludedRequestDomainSet = new Set([ 'all-urls' ]); } + + if ( eqSets(beforeRequestDomainSet, afterRequestDomainSet) ) { + if ( eqSets(beforeExcludedRrequestDomainSet, afterExcludedRequestDomainSet) ) { + return; + } + } + + const removeRuleIds = [ + TRUSTED_DIRECTIVE_BASE_RULE_ID+0, + TRUSTED_DIRECTIVE_BASE_RULE_ID+1, + ]; + dynamicRuleMap.delete(TRUSTED_DIRECTIVE_BASE_RULE_ID+0); + dynamicRuleMap.delete(TRUSTED_DIRECTIVE_BASE_RULE_ID+1); + + const allowEverywhere = afterRequestDomainSet.delete('all-urls'); + afterExcludedRequestDomainSet.delete('all-urls'); + const addRules = []; - const noneHostnames = [ ...modes.none ]; - const notNoneHostnames = [ ...modes.basic, ...modes.optimal, ...modes.complete ]; - if ( noneHostnames.length !== 0 ) { + if ( + allowEverywhere || + afterRequestDomainSet.size !== 0 || + afterExcludedRequestDomainSet.size !== 0 + ) { const rule0 = { id: TRUSTED_DIRECTIVE_BASE_RULE_ID+0, action: { type: 'allowAllRequests' }, @@ -308,10 +334,10 @@ async function filteringModesToDNR(modes) { }, priority: 100, }; - if ( modes.none.has('all-urls') === false ) { - rule0.condition.requestDomains = noneHostnames.slice(); - } else if ( notNoneHostnames.length !== 0 ) { - rule0.condition.excludedRequestDomains = notNoneHostnames.slice(); + if ( afterRequestDomainSet.size !== 0 ) { + rule0.condition.requestDomains = Array.from(afterRequestDomainSet); + } else if ( afterExcludedRequestDomainSet.size !== 0 ) { + rule0.condition.excludedRequestDomains = Array.from(afterExcludedRequestDomainSet); } addRules.push(rule0); dynamicRuleMap.set(TRUSTED_DIRECTIVE_BASE_RULE_ID+0, rule0); @@ -324,21 +350,19 @@ async function filteringModesToDNR(modes) { }, priority: 100, }; - if ( modes.none.has('all-urls') === false ) { - rule1.condition.initiatorDomains = noneHostnames.slice(); - } else if ( notNoneHostnames.length !== 0 ) { - rule1.condition.excludedInitiatorDomains = notNoneHostnames.slice(); + if ( rule0.condition.requestDomains ) { + rule1.condition.initiatorDomains = rule0.condition.requestDomains.slice(); + } else if ( rule0.condition.excludedRequestDomains ) { + rule1.condition.excludedInitiatorDomains = rule0.condition.excludedRequestDomains.slice(); } addRules.push(rule1); dynamicRuleMap.set(TRUSTED_DIRECTIVE_BASE_RULE_ID+1, rule1); } - const updateOptions = {}; + + const updateOptions = { removeRuleIds }; if ( addRules.length ) { updateOptions.addRules = addRules; } - if ( removeRuleIds.length ) { - updateOptions.removeRuleIds = removeRuleIds; - } await dnr.updateDynamicRules(updateOptions); }