Skip to content

Commit 5921e50

Browse files
committed
Fix reverse lookup of ##^responseheader(...) filters
Related issue: uBlockOrigin/uBlock-issues#3544
1 parent 9bb1a2b commit 5921e50

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

src/js/httpheader-filtering.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
Home: https://github.com/gorhill/uBlock
2020
*/
2121

22-
import * as sfp from './static-filtering-parser.js';
2322
import { StaticExtFilteringHostnameDB } from './static-ext-filtering-db.js';
2423
import { entityFromDomain } from './uri-utils.js';
2524
import logger from './logger.js';
@@ -83,8 +82,7 @@ httpheaderFilteringEngine.compile = function(parser, writer) {
8382
writer.select('HTTPHEADER_FILTERS');
8483

8584
const isException = parser.isException();
86-
const root = parser.getBranchFromType(sfp.NODE_TYPE_EXT_PATTERN_RESPONSEHEADER);
87-
const headerName = parser.getNodeString(root);
85+
const headerName = parser.getResponseheaderName();
8886

8987
// Tokenless is meaningful only for exception filters.
9088
if ( headerName === '' && isException === false ) { return; }

src/js/reverselookup-worker.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const fromExtendedFilter = function(details) {
120120
// The longer the needle, the lower the number of false positives.
121121
// https://github.com/uBlockOrigin/uBlock-issues/issues/1139
122122
// Mind that there is no guarantee a selector has `\w` characters.
123-
const needle = selector.match(/\w+|\*/g).reduce(function(a, b) {
123+
const needle = (details.needle || selector).match(/\w+|\*/g).reduce(function(a, b) {
124124
return a.length > b.length ? a : b;
125125
});
126126

@@ -213,6 +213,12 @@ const fromExtendedFilter = function(details) {
213213
/* fallthrough */
214214
case 64: {
215215
if ( exception !== ((fargs[2] & 0b001) !== 0) ) { break; }
216+
if ( /^responseheader\(.+\)$/.test(selector) ) {
217+
if ( fargs[3] !== needle ) { break; }
218+
if ( hostnameMatches(fargs[1]) === false ) { break; }
219+
found = fargs[1] + prefix + selector;
220+
break;
221+
}
216222
const isProcedural = (fargs[2] & 0b010) !== 0;
217223
if (
218224
isProcedural === false && fargs[3] !== selector ||
@@ -236,11 +242,13 @@ const fromExtendedFilter = function(details) {
236242
// Scriptlet injection
237243
case 32:
238244
if ( exception !== ((fargs[2] & 0b001) !== 0) ) { break; }
239-
if ( fargs[3] !== details.compiled ) { break; }
245+
if ( fargs[3] !== details.needle ) { break; }
240246
if ( hostnameMatches(fargs[1]) ) {
241247
found = fargs[1] + prefix + selector;
242248
}
243249
break;
250+
default:
251+
break;
244252
}
245253
if ( found !== undefined ) {
246254
if ( response[found] === undefined ) {

src/js/reverselookup.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const initWorker = function() {
9393
};
9494

9595
for ( const listKey in µb.availableFilterLists ) {
96-
if ( µb.availableFilterLists.hasOwnProperty(listKey) === false ) {
96+
if ( Object.prototype.hasOwnProperty.call(µb.availableFilterLists, listKey) === false ) {
9797
continue;
9898
}
9999
const entry = µb.availableFilterLists[listKey];
@@ -167,9 +167,11 @@ const fromExtendedFilter = async function(details) {
167167
nativeCssHas: vAPI.webextFlavor.env.includes('native_css_has'),
168168
});
169169
parser.parse(details.rawFilter);
170-
let compiled;
170+
let needle;
171171
if ( parser.isScriptletFilter() ) {
172-
compiled = JSON.stringify(parser.getScriptletArgs());
172+
needle = JSON.stringify(parser.getScriptletArgs());
173+
} else if ( parser.isResponseheaderFilter() ) {
174+
needle = parser.getResponseheaderName();
173175
}
174176

175177
worker.postMessage({
@@ -188,7 +190,7 @@ const fromExtendedFilter = async function(details) {
188190
details.url
189191
) === 2,
190192
rawFilter: details.rawFilter,
191-
compiled,
193+
needle,
192194
});
193195

194196
return new Promise(resolve => {

src/js/static-filtering-parser.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,6 +2494,12 @@ export class AstFilterParser {
24942494
return head;
24952495
}
24962496

2497+
getResponseheaderName() {
2498+
if ( this.isResponseheaderFilter() === false ) { return ''; }
2499+
const root = this.getBranchFromType(NODE_TYPE_EXT_PATTERN_RESPONSEHEADER);
2500+
return this.getNodeString(root);
2501+
}
2502+
24972503
parseExtPatternHtml(parent) {
24982504
const beg = this.nodes[parent+NODE_BEG_INDEX];
24992505
const end = this.nodes[parent+NODE_END_INDEX];

0 commit comments

Comments
 (0)