[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) => {
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);
}