From 4a83b803282e970f2c69bee4b8e698e575af0b06 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 11 Jul 2023 14:38:35 -0400 Subject: [PATCH] [mv3] Properly enforce generic cosmetic exception filters Related issue: - https://github.com/uBlockOrigin/uBOL-issues/issues/58 --- platform/mv3/make-rulesets.js | 32 +++++++++++++++++++++++++++----- src/js/static-dnr-filtering.js | 8 ++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/platform/mv3/make-rulesets.js b/platform/mv3/make-rulesets.js index 15db7d9f5e59d..5f95b6c512c17 100644 --- a/platform/mv3/make-rulesets.js +++ b/platform/mv3/make-rulesets.js @@ -396,13 +396,26 @@ function loadAllSourceScriptlets() { /******************************************************************************/ -async function processGenericCosmeticFilters(assetDetails, bucketsMap) { +async function processGenericCosmeticFilters(assetDetails, bucketsMap, exceptionSet) { if ( bucketsMap === undefined ) { return 0; } + if ( exceptionSet ) { + for ( const [ hash, selectors ] of bucketsMap ) { + let i = selectors.length; + while ( i-- ) { + const selector = selectors[i]; + if ( exceptionSet.has(selector) === false ) { continue; } + selectors.splice(i, 1); + //log(`\tRemoving excepted generic filter ##${selector}`); + } + if ( selectors.length === 0 ) { + bucketsMap.delete(hash); + } + } + } if ( bucketsMap.size === 0 ) { return 0; } const bucketsList = Array.from(bucketsMap); const count = bucketsList.reduce((a, v) => a += v[1].length, 0); if ( count === 0 ) { return 0; } - const selectorLists = bucketsList.map(v => [ v[0], v[1].join(',') ]); const originalScriptletMap = await loadAllSourceScriptlets(); @@ -427,8 +440,15 @@ async function processGenericCosmeticFilters(assetDetails, bucketsMap) { /******************************************************************************/ -async function processGenericHighCosmeticFilters(assetDetails, selectorSet) { +async function processGenericHighCosmeticFilters(assetDetails, selectorSet, exceptionSet) { if ( selectorSet === undefined ) { return 0; } + if ( exceptionSet ) { + for ( const selector of selectorSet ) { + if ( exceptionSet.has(selector) === false ) { continue; } + selectorSet.delete(selector); + //log(`\tRemoving excepted generic filter ##${selector}`); + } + } if ( selectorSet.size === 0 ) { return 0; } const selectorLists = Array.from(selectorSet).sort().join(',\n'); const originalScriptletMap = await loadAllSourceScriptlets(); @@ -925,11 +945,13 @@ async function rulesetFromURLs(assetDetails) { const genericCosmeticStats = await processGenericCosmeticFilters( assetDetails, - results.genericCosmetic + results.genericCosmetic, + results.genericCosmeticExceptions ); const genericHighCosmeticStats = await processGenericHighCosmeticFilters( assetDetails, - results.genericHighCosmetic + results.genericHighCosmetic, + results.genericCosmeticExceptions ); const specificCosmeticStats = await processCosmeticFilters( assetDetails, diff --git a/src/js/static-dnr-filtering.js b/src/js/static-dnr-filtering.js index 4177991b9d085..df3126c71e813 100644 --- a/src/js/static-dnr-filtering.js +++ b/src/js/static-dnr-filtering.js @@ -150,6 +150,13 @@ function addExtendedToDNR(context, parser) { const { compiled } = parser.result; if ( compiled === undefined ) { return; } if ( compiled.length <= 1 ) { return; } + if ( parser.isException() ) { + if ( context.genericCosmeticExceptions === undefined ) { + context.genericCosmeticExceptions = new Set(); + } + context.genericCosmeticExceptions.add(compiled); + return; + } if ( compiled.charCodeAt(0) === 0x7B /* '{' */ ) { return; } const key = keyFromSelector(compiled); if ( key === undefined ) { @@ -298,6 +305,7 @@ async function dnrRulesetFromRawLists(lists, options = {}) { network: staticNetFilteringEngine.dnrFromCompiled('end', context), genericCosmetic: context.genericCosmeticFilters, genericHighCosmetic: context.genericHighCosmeticFilters, + genericCosmeticExceptions: context.genericCosmeticExceptions, specificCosmetic: context.specificCosmeticFilters, scriptlet: context.scriptletFilters, };