[mv3] Fix generating allowAllRequests rule when default mode is no-filtering

This commit is contained in:
Raymond Hill 2024-08-04 11:58:09 -04:00
parent 3668445679
commit 417dab538c
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -74,6 +74,7 @@ const pruneHostnameFromSet = (hostname, hnSet) => {
/******************************************************************************/ /******************************************************************************/
const eqSets = (setBefore, setAfter) => { const eqSets = (setBefore, setAfter) => {
if ( setBefore.size !== setAfter.size ) { return false; }
for ( const hn of setAfter ) { for ( const hn of setAfter ) {
if ( setBefore.has(hn) === false ) { return false; } if ( setBefore.has(hn) === false ) { return false; }
} }
@ -283,23 +284,48 @@ async function writeFilteringModeDetails(afterDetails) {
async function filteringModesToDNR(modes) { async function filteringModesToDNR(modes) {
const dynamicRuleMap = await getDynamicRules(); const dynamicRuleMap = await getDynamicRules();
const trustedRule = dynamicRuleMap.get(TRUSTED_DIRECTIVE_BASE_RULE_ID+0); const trustedRule = dynamicRuleMap.get(TRUSTED_DIRECTIVE_BASE_RULE_ID+0);
const trustedDomainSet = new Set(trustedRule?.condition.requestDomains); const beforeRequestDomainSet = new Set(trustedRule?.condition.requestDomains);
if ( trustedDomainSet.size !== 0 ) { const beforeExcludedRrequestDomainSet = new Set(trustedRule?.condition.excludedRequestDomains);
if ( eqSets(trustedDomainSet, modes.none) ) { return; } if ( trustedRule !== undefined && beforeRequestDomainSet.size === 0 ) {
} else if ( trustedRule !== undefined ) { beforeRequestDomainSet.add('all-urls');
if ( modes.none.has('all-urls') ) { return; } } else {
beforeExcludedRrequestDomainSet.add('all-urls');
} }
const removeRuleIds = [];
if ( trustedRule !== undefined ) { const noneHostnames = new Set([ ...modes.none ]);
removeRuleIds.push(TRUSTED_DIRECTIVE_BASE_RULE_ID+0); const notNoneHostnames = new Set([ ...modes.basic, ...modes.optimal, ...modes.complete ]);
removeRuleIds.push(TRUSTED_DIRECTIVE_BASE_RULE_ID+1); let afterRequestDomainSet = new Set();
dynamicRuleMap.delete(TRUSTED_DIRECTIVE_BASE_RULE_ID+0); let afterExcludedRequestDomainSet = new Set();
dynamicRuleMap.delete(TRUSTED_DIRECTIVE_BASE_RULE_ID+1); 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 addRules = [];
const noneHostnames = [ ...modes.none ]; if (
const notNoneHostnames = [ ...modes.basic, ...modes.optimal, ...modes.complete ]; allowEverywhere ||
if ( noneHostnames.length !== 0 ) { afterRequestDomainSet.size !== 0 ||
afterExcludedRequestDomainSet.size !== 0
) {
const rule0 = { const rule0 = {
id: TRUSTED_DIRECTIVE_BASE_RULE_ID+0, id: TRUSTED_DIRECTIVE_BASE_RULE_ID+0,
action: { type: 'allowAllRequests' }, action: { type: 'allowAllRequests' },
@ -308,10 +334,10 @@ async function filteringModesToDNR(modes) {
}, },
priority: 100, priority: 100,
}; };
if ( modes.none.has('all-urls') === false ) { if ( afterRequestDomainSet.size !== 0 ) {
rule0.condition.requestDomains = noneHostnames.slice(); rule0.condition.requestDomains = Array.from(afterRequestDomainSet);
} else if ( notNoneHostnames.length !== 0 ) { } else if ( afterExcludedRequestDomainSet.size !== 0 ) {
rule0.condition.excludedRequestDomains = notNoneHostnames.slice(); rule0.condition.excludedRequestDomains = Array.from(afterExcludedRequestDomainSet);
} }
addRules.push(rule0); addRules.push(rule0);
dynamicRuleMap.set(TRUSTED_DIRECTIVE_BASE_RULE_ID+0, rule0); dynamicRuleMap.set(TRUSTED_DIRECTIVE_BASE_RULE_ID+0, rule0);
@ -324,21 +350,19 @@ async function filteringModesToDNR(modes) {
}, },
priority: 100, priority: 100,
}; };
if ( modes.none.has('all-urls') === false ) { if ( rule0.condition.requestDomains ) {
rule1.condition.initiatorDomains = noneHostnames.slice(); rule1.condition.initiatorDomains = rule0.condition.requestDomains.slice();
} else if ( notNoneHostnames.length !== 0 ) { } else if ( rule0.condition.excludedRequestDomains ) {
rule1.condition.excludedInitiatorDomains = notNoneHostnames.slice(); rule1.condition.excludedInitiatorDomains = rule0.condition.excludedRequestDomains.slice();
} }
addRules.push(rule1); addRules.push(rule1);
dynamicRuleMap.set(TRUSTED_DIRECTIVE_BASE_RULE_ID+1, rule1); dynamicRuleMap.set(TRUSTED_DIRECTIVE_BASE_RULE_ID+1, rule1);
} }
const updateOptions = {};
const updateOptions = { removeRuleIds };
if ( addRules.length ) { if ( addRules.length ) {
updateOptions.addRules = addRules; updateOptions.addRules = addRules;
} }
if ( removeRuleIds.length ) {
updateOptions.removeRuleIds = removeRuleIds;
}
await dnr.updateDynamicRules(updateOptions); await dnr.updateDynamicRules(updateOptions);
} }