From 8507d637e5190836caec390fed3a9cff13b4e131 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 28 Mar 2023 08:35:09 -0400 Subject: [PATCH] Add support for negated hostnames in HTML filters Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/6 --- src/js/html-filtering.js | 38 ++++++++++++++++++++++------------- src/js/static-filtering-io.js | 5 +++++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/js/html-filtering.js b/src/js/html-filtering.js index 6c388c47c..ce824b015 100644 --- a/src/js/html-filtering.js +++ b/src/js/html-filtering.js @@ -299,6 +299,14 @@ function applyCSSSelector(details, selector) { return modified; } +function logError(writer, msg) { + logger.writeOne({ + realm: 'message', + type: 'error', + text: msg.replace('{who}', writer.properties.get('name') || '?') + }); +} + htmlFilteringEngine.reset = function() { filterDB.clear(); pselectors.clear(); @@ -316,13 +324,7 @@ htmlFilteringEngine.compile = function(parser, writer) { const isException = parser.isException(); const { raw, compiled } = parser.result; if ( compiled === undefined ) { - const who = writer.properties.get('name') || '?'; - logger.writeOne({ - realm: 'message', - type: 'error', - text: `Invalid HTML filter in ${who}: ##${raw}` - }); - return; + return logError(writer, `Invalid HTML filter in {who}: ##${raw}`); } writer.select('HTML_FILTERS'); @@ -335,20 +337,28 @@ htmlFilteringEngine.compile = function(parser, writer) { return; } - // TODO: Mind negated hostnames, they are currently discarded. - + const compiledFilters = []; + let hasOnlyNegated = true; for ( const { hn, not, bad } of parser.getExtFilterDomainIterator() ) { if ( bad ) { continue; } - let kind = 0; - if ( isException ) { - if ( not ) { continue; } - kind |= 0b01; + let kind = isException ? 0b01 : 0b00; + if ( not ) { + kind ^= 0b01; + } else { + hasOnlyNegated = false; } if ( compiled.charCodeAt(0) === 0x7B /* '{' */ ) { kind |= 0b10; } - writer.push([ 64, hn, kind, compiled ]); + compiledFilters.push([ 64, hn, kind, compiled ]); } + + // Not allowed since it's equivalent to forbidden generic HTML filters + if ( isException === false && hasOnlyNegated ) { + return logError(writer, `Invalid HTML filter in {who}: ##${raw}`); + } + + writer.pushMany(compiledFilters); }; htmlFilteringEngine.fromCompiledContent = function(reader) { diff --git a/src/js/static-filtering-io.js b/src/js/static-filtering-io.js index 941c8413c..d1d811604 100644 --- a/src/js/static-filtering-io.js +++ b/src/js/static-filtering-io.js @@ -44,6 +44,11 @@ class CompiledListWriter { push(args) { this.block.push(serialize(args)); } + pushMany(many) { + for ( const args of many ) { + this.block.push(serialize(args)); + } + } last() { if ( Array.isArray(this.block) && this.block.length !== 0 ) { return this.block[this.block.length - 1];