Skip to content

Commit

Permalink
Add support for negated hostnames in HTML filters
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Mar 28, 2023
1 parent 3658d70 commit 8507d63
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/js/html-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
Expand All @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/js/static-filtering-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 8507d63

Please sign in to comment.