Add support for negated hostnames in HTML filters

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/6
This commit is contained in:
Raymond Hill 2023-03-28 08:35:09 -04:00
parent 3658d70cc4
commit 8507d637e5
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 29 additions and 14 deletions

View File

@ -299,6 +299,14 @@ function applyCSSSelector(details, selector) {
return modified; return modified;
} }
function logError(writer, msg) {
logger.writeOne({
realm: 'message',
type: 'error',
text: msg.replace('{who}', writer.properties.get('name') || '?')
});
}
htmlFilteringEngine.reset = function() { htmlFilteringEngine.reset = function() {
filterDB.clear(); filterDB.clear();
pselectors.clear(); pselectors.clear();
@ -316,13 +324,7 @@ htmlFilteringEngine.compile = function(parser, writer) {
const isException = parser.isException(); const isException = parser.isException();
const { raw, compiled } = parser.result; const { raw, compiled } = parser.result;
if ( compiled === undefined ) { if ( compiled === undefined ) {
const who = writer.properties.get('name') || '?'; return logError(writer, `Invalid HTML filter in {who}: ##${raw}`);
logger.writeOne({
realm: 'message',
type: 'error',
text: `Invalid HTML filter in ${who}: ##${raw}`
});
return;
} }
writer.select('HTML_FILTERS'); writer.select('HTML_FILTERS');
@ -335,20 +337,28 @@ htmlFilteringEngine.compile = function(parser, writer) {
return; return;
} }
// TODO: Mind negated hostnames, they are currently discarded. const compiledFilters = [];
let hasOnlyNegated = true;
for ( const { hn, not, bad } of parser.getExtFilterDomainIterator() ) { for ( const { hn, not, bad } of parser.getExtFilterDomainIterator() ) {
if ( bad ) { continue; } if ( bad ) { continue; }
let kind = 0; let kind = isException ? 0b01 : 0b00;
if ( isException ) { if ( not ) {
if ( not ) { continue; } kind ^= 0b01;
kind |= 0b01; } else {
hasOnlyNegated = false;
} }
if ( compiled.charCodeAt(0) === 0x7B /* '{' */ ) { if ( compiled.charCodeAt(0) === 0x7B /* '{' */ ) {
kind |= 0b10; 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) { htmlFilteringEngine.fromCompiledContent = function(reader) {

View File

@ -44,6 +44,11 @@ class CompiledListWriter {
push(args) { push(args) {
this.block.push(serialize(args)); this.block.push(serialize(args));
} }
pushMany(many) {
for ( const args of many ) {
this.block.push(serialize(args));
}
}
last() { last() {
if ( Array.isArray(this.block) && this.block.length !== 0 ) { if ( Array.isArray(this.block) && this.block.length !== 0 ) {
return this.block[this.block.length - 1]; return this.block[this.block.length - 1];