Skip to content

Commit

Permalink
Fix reverse lookup of ##^responseheader(...) filters
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Feb 20, 2025
1 parent 9bb1a2b commit 5921e50
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/js/httpheader-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
Home: https://github.com/gorhill/uBlock
*/

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

const isException = parser.isException();
const root = parser.getBranchFromType(sfp.NODE_TYPE_EXT_PATTERN_RESPONSEHEADER);
const headerName = parser.getNodeString(root);
const headerName = parser.getResponseheaderName();

// Tokenless is meaningful only for exception filters.
if ( headerName === '' && isException === false ) { return; }
Expand Down
12 changes: 10 additions & 2 deletions src/js/reverselookup-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const fromExtendedFilter = function(details) {
// The longer the needle, the lower the number of false positives.
// https://github.com/uBlockOrigin/uBlock-issues/issues/1139
// Mind that there is no guarantee a selector has `\w` characters.
const needle = selector.match(/\w+|\*/g).reduce(function(a, b) {
const needle = (details.needle || selector).match(/\w+|\*/g).reduce(function(a, b) {
return a.length > b.length ? a : b;
});

Expand Down Expand Up @@ -213,6 +213,12 @@ const fromExtendedFilter = function(details) {
/* fallthrough */
case 64: {
if ( exception !== ((fargs[2] & 0b001) !== 0) ) { break; }
if ( /^responseheader\(.+\)$/.test(selector) ) {
if ( fargs[3] !== needle ) { break; }
if ( hostnameMatches(fargs[1]) === false ) { break; }
found = fargs[1] + prefix + selector;
break;
}
const isProcedural = (fargs[2] & 0b010) !== 0;
if (
isProcedural === false && fargs[3] !== selector ||
Expand All @@ -236,11 +242,13 @@ const fromExtendedFilter = function(details) {
// Scriptlet injection
case 32:
if ( exception !== ((fargs[2] & 0b001) !== 0) ) { break; }
if ( fargs[3] !== details.compiled ) { break; }
if ( fargs[3] !== details.needle ) { break; }
if ( hostnameMatches(fargs[1]) ) {
found = fargs[1] + prefix + selector;
}
break;
default:
break;
}
if ( found !== undefined ) {
if ( response[found] === undefined ) {
Expand Down
10 changes: 6 additions & 4 deletions src/js/reverselookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const initWorker = function() {
};

for ( const listKey in µb.availableFilterLists ) {
if ( µb.availableFilterLists.hasOwnProperty(listKey) === false ) {
if ( Object.prototype.hasOwnProperty.call(µb.availableFilterLists, listKey) === false ) {
continue;
}
const entry = µb.availableFilterLists[listKey];
Expand Down Expand Up @@ -167,9 +167,11 @@ const fromExtendedFilter = async function(details) {
nativeCssHas: vAPI.webextFlavor.env.includes('native_css_has'),
});
parser.parse(details.rawFilter);
let compiled;
let needle;
if ( parser.isScriptletFilter() ) {
compiled = JSON.stringify(parser.getScriptletArgs());
needle = JSON.stringify(parser.getScriptletArgs());
} else if ( parser.isResponseheaderFilter() ) {
needle = parser.getResponseheaderName();
}

worker.postMessage({
Expand All @@ -188,7 +190,7 @@ const fromExtendedFilter = async function(details) {
details.url
) === 2,
rawFilter: details.rawFilter,
compiled,
needle,
});

return new Promise(resolve => {
Expand Down
6 changes: 6 additions & 0 deletions src/js/static-filtering-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,12 @@ export class AstFilterParser {
return head;
}

getResponseheaderName() {
if ( this.isResponseheaderFilter() === false ) { return ''; }
const root = this.getBranchFromType(NODE_TYPE_EXT_PATTERN_RESPONSEHEADER);
return this.getNodeString(root);
}

parseExtPatternHtml(parent) {
const beg = this.nodes[parent+NODE_BEG_INDEX];
const end = this.nodes[parent+NODE_END_INDEX];
Expand Down

0 comments on commit 5921e50

Please sign in to comment.