Skip to content

Commit

Permalink
Don't match network filter-derived regexes against non-network URIs
Browse files Browse the repository at this point in the history
Context: element picker

Related issue:
uBlockOrigin/uBlock-issues#3142
  • Loading branch information
gorhill committed Feb 26, 2024
1 parent f7e00e4 commit 2262a12
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/js/scriptlets/epicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,21 @@ const filterToDOMInterface = (( ) => {
const reCaret = '(?:[^%.0-9a-z_-]|$)';
const rePseudoElements = /:(?::?after|:?before|:[a-z-]+)$/;

const matchElemToRegex = (elem, re) => {
const srcProp = netFilter1stSources[elem.localName];
let src = elem[srcProp];
if ( src instanceof SVGAnimatedString ) {
src = src.baseVal;
}
if ( typeof src === 'string' && /^https?:\/\//.test(src) ) {
if ( re.test(src) ) { return srcProp; }
}
src = elem.currentSrc;
if ( typeof src === 'string' && /^https?:\/\//.test(src) ) {
if ( re.test(src) ) { return srcProp; }
}
};

// Net filters: we need to lookup manually -- translating into a foolproof
// CSS selector is just not possible.
//
Expand Down Expand Up @@ -672,28 +687,21 @@ const filterToDOMInterface = (( ) => {
// Lookup by tag names.
// https://github.com/uBlockOrigin/uBlock-issues/issues/2260
// Maybe get to the actual URL indirectly.
//
// https://github.com/uBlockOrigin/uBlock-issues/issues/3142
// Don't try to match against non-network URIs.
const elems = document.querySelectorAll(
Object.keys(netFilter1stSources).join()
);
for ( const elem of elems ) {
const srcProp = netFilter1stSources[elem.localName];
let src = elem[srcProp];
if ( src instanceof SVGAnimatedString ) {
src = src.baseVal;
}
if (
typeof src === 'string' &&
reFilter.test(src) ||
typeof elem.currentSrc === 'string' &&
reFilter.test(elem.currentSrc)
) {
out.push({
elem,
src: srcProp,
opt: filterTypes[elem.localName],
style: vAPI.hideStyle,
});
}
const srcProp = matchElemToRegex(elem, reFilter);
if ( srcProp === undefined ) { continue; }
out.push({
elem,
src: srcProp,
opt: filterTypes[elem.localName],
style: vAPI.hideStyle,
});
}

// Find matching background image in current set of candidate elements.
Expand Down

0 comments on commit 2262a12

Please sign in to comment.