Skip to content

Commit

Permalink
[mv3] Properly enforce generic cosmetic exception filters
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBOL-home#58
  • Loading branch information
gorhill committed Jul 11, 2023
1 parent 4a570c1 commit 4a83b80
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
32 changes: 27 additions & 5 deletions platform/mv3/make-rulesets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/js/static-dnr-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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,
};
Expand Down

0 comments on commit 4a83b80

Please sign in to comment.