From 1cb660b94e0adb6d4e15e91e3e4011279393efd9 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 17 Aug 2024 10:11:18 -0400 Subject: [PATCH] Fix plain exceptions not overriding block filters using `header=` option Related issue: https://github.com/uBlockOrigin/uBlock-issues/issues/3347 --- src/js/static-net-filtering.js | 137 +++++++++++++++------------------ 1 file changed, 64 insertions(+), 73 deletions(-) diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index 9a252fdce33cd..96aaff79ef7a7 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -19,28 +19,15 @@ Home: https://github.com/gorhill/uBlock */ -/* globals vAPI */ - -'use strict'; +import * as sfp from './static-filtering-parser.js'; -/******************************************************************************/ +import { domainFromHostname, hostnameFromNetworkURL } from './uri-utils.js'; +import { dropTask, queueTask } from './tasks.js'; -import { queueTask, dropTask } from './tasks.js'; import BidiTrieContainer from './biditrie.js'; -import HNTrieContainer from './hntrie.js'; import { CompiledListReader } from './static-filtering-io.js'; -import * as sfp from './static-filtering-parser.js'; - -import { - domainFromHostname, - hostnameFromNetworkURL, -} from './uri-utils.js'; - -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility -// -// This import would be best done dynamically, but since dynamic imports are -// not supported by older browsers, for now a static import is necessary. import { FilteringContext } from './filtering-context.js'; +import HNTrieContainer from './hntrie.js'; /******************************************************************************/ @@ -3423,63 +3410,63 @@ class FilterCompiler { processOptionWithValue(parser, id) { switch ( id ) { - case sfp.NODE_TYPE_NET_OPTION_NAME_CSP: - if ( this.processCspOption(parser.getNetOptionValue(id)) === false ) { return false; } - break; - case sfp.NODE_TYPE_NET_OPTION_NAME_DENYALLOW: - this.denyallowOpt = this.processHostnameList( - parser.getNetFilterDenyallowOptionIterator(), - ); - if ( this.denyallowOpt === '' ) { return false; } - this.optionUnitBits |= DENYALLOW_BIT; - break; - case sfp.NODE_TYPE_NET_OPTION_NAME_FROM: - this.fromDomainOpt = this.processHostnameList( - parser.getNetFilterFromOptionIterator(), - this.fromDomainOptList - ); - if ( this.fromDomainOpt === '' ) { return false; } - this.optionUnitBits |= FROM_BIT; - break; - case sfp.NODE_TYPE_NET_OPTION_NAME_HEADER: { - this.headerOpt = parser.getNetOptionValue(id) || ''; - this.optionUnitBits |= HEADER_BIT; - break; + case sfp.NODE_TYPE_NET_OPTION_NAME_CSP: + if ( this.processCspOption(parser.getNetOptionValue(id)) === false ) { return false; } + break; + case sfp.NODE_TYPE_NET_OPTION_NAME_DENYALLOW: + this.denyallowOpt = this.processHostnameList( + parser.getNetFilterDenyallowOptionIterator(), + ); + if ( this.denyallowOpt === '' ) { return false; } + this.optionUnitBits |= DENYALLOW_BIT; + break; + case sfp.NODE_TYPE_NET_OPTION_NAME_FROM: + this.fromDomainOpt = this.processHostnameList( + parser.getNetFilterFromOptionIterator(), + this.fromDomainOptList + ); + if ( this.fromDomainOpt === '' ) { return false; } + this.optionUnitBits |= FROM_BIT; + break; + case sfp.NODE_TYPE_NET_OPTION_NAME_HEADER: { + this.headerOpt = parser.getNetOptionValue(id) || ''; + this.optionUnitBits |= HEADER_BIT; + break; + } + case sfp.NODE_TYPE_NET_OPTION_NAME_METHOD: + this.processMethodOption(parser.getNetOptionValue(id)); + this.optionUnitBits |= METHOD_BIT; + break; + case sfp.NODE_TYPE_NET_OPTION_NAME_PERMISSIONS: + case sfp.NODE_TYPE_NET_OPTION_NAME_REDIRECTRULE: + case sfp.NODE_TYPE_NET_OPTION_NAME_REMOVEPARAM: + case sfp.NODE_TYPE_NET_OPTION_NAME_REPLACE: + case sfp.NODE_TYPE_NET_OPTION_NAME_URLTRANSFORM: + if ( this.processModifierOption(id, parser.getNetOptionValue(id)) === false ) { + return false; } - case sfp.NODE_TYPE_NET_OPTION_NAME_METHOD: - this.processMethodOption(parser.getNetOptionValue(id)); - this.optionUnitBits |= METHOD_BIT; - break; - case sfp.NODE_TYPE_NET_OPTION_NAME_PERMISSIONS: - case sfp.NODE_TYPE_NET_OPTION_NAME_REDIRECTRULE: - case sfp.NODE_TYPE_NET_OPTION_NAME_REMOVEPARAM: - case sfp.NODE_TYPE_NET_OPTION_NAME_REPLACE: - case sfp.NODE_TYPE_NET_OPTION_NAME_URLTRANSFORM: - if ( this.processModifierOption(id, parser.getNetOptionValue(id)) === false ) { - return false; - } - this.optionUnitBits |= MODIFY_BIT; - break; - case sfp.NODE_TYPE_NET_OPTION_NAME_REDIRECT: { - const actualId = this.action === ALLOW_REALM - ? sfp.NODE_TYPE_NET_OPTION_NAME_REDIRECTRULE - : id; - if ( this.processModifierOption(actualId, parser.getNetOptionValue(id)) === false ) { - return false; - } - this.optionUnitBits |= MODIFY_BIT; - break; + this.optionUnitBits |= MODIFY_BIT; + break; + case sfp.NODE_TYPE_NET_OPTION_NAME_REDIRECT: { + const actualId = this.action === ALLOW_REALM + ? sfp.NODE_TYPE_NET_OPTION_NAME_REDIRECTRULE + : id; + if ( this.processModifierOption(actualId, parser.getNetOptionValue(id)) === false ) { + return false; } - case sfp.NODE_TYPE_NET_OPTION_NAME_TO: - this.toDomainOpt = this.processHostnameList( - parser.getNetFilterToOptionIterator(), - this.toDomainOptList - ); - if ( this.toDomainOpt === '' ) { return false; } - this.optionUnitBits |= TO_BIT; - break; - default: - break; + this.optionUnitBits |= MODIFY_BIT; + break; + } + case sfp.NODE_TYPE_NET_OPTION_NAME_TO: + this.toDomainOpt = this.processHostnameList( + parser.getNetFilterToOptionIterator(), + this.toDomainOptList + ); + if ( this.toDomainOpt === '' ) { return false; } + this.optionUnitBits |= TO_BIT; + break; + default: + break; } return true; } @@ -3798,7 +3785,7 @@ class FilterCompiler { isJustOrigin() { if ( this.optionUnitBits !== FROM_BIT ) { return false; } if ( this.isRegex ) { return false; } - if ( /[\/~]/.test(this.fromDomainOpt) ) { return false; } + if ( /[/~]/.test(this.fromDomainOpt) ) { return false; } if ( this.pattern === '*' ) { return true; } if ( this.anchor !== 0b010 ) { return false; } if ( /^(?:http[s*]?:(?:\/\/)?)$/.test(this.pattern) ) { return true; } @@ -5156,6 +5143,10 @@ StaticNetFilteringEngine.prototype.matchHeaders = function(fctxt, headers) { if ( r !== 0 && $isBlockImportant !== true ) { if ( this.realmMatchString(HEADERS_REALM | ALLOW_REALM, typeBits, partyBits) ) { r = 2; + } else if ( this.realmMatchString(ALLOW_REALM, typeBits, partyBits) ) { + r = 2; + } + if ( r === 2 ) { if ( this.realmMatchString(HEADERS_REALM | BLOCKIMPORTANT_REALM, typeBits, partyBits) ) { r = 1; }