Fix regression in href-sanitizer scriptlet

Related feedback:
https://github.com/uBlockOrigin/uBlock-issues/issues/2531#issuecomment-2561968581

Regression from:
https://github.com/gorhill/uBlock/commit/9bf8d53ebe
This commit is contained in:
Raymond Hill 2024-12-25 14:26:00 -05:00
parent f7fa3139af
commit 42638f0e44
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -91,6 +91,20 @@ function hrefSanitizer(
}
return '';
};
const extractParam = (href, source) => {
if ( Boolean(source) === false ) { return href; }
const recursive = source.includes('?', 1);
const end = recursive ? source.indexOf('?', 1) : source.length;
try {
const url = new URL(href, document.location);
let value = url.searchParams.get(source.slice(1, end));
if ( value === null ) { return href }
if ( recursive ) { return extractParam(value, source.slice(end)); }
return value;
} catch(x) {
}
return href;
};
const extractURL = (elem, source) => {
if ( /^\[.*\]$/.test(source) ) {
return elem.getAttribute(source.slice(1,-1).trim()) || '';
@ -101,13 +115,13 @@ function hrefSanitizer(
.replace(/[^\x21-\x7e]+$/, '') // remove trailing invalid characters
;
}
if ( source.startsWith('?') ) {
const steps = source.replace(/(\S)\?/g, '\\1?').split(/\s+/);
const url = urlSkip(elem.href, false, steps);
if ( url === undefined ) { return; }
return url.replace(/ /g, '%20');
}
return '';
if ( source.startsWith('?') === false ) { return ''; }
const steps = source.replace(/(\S)\?/g, '\\1?').split(/\s+/);
const url = steps.length === 1
? extractParam(elem.href, source)
: urlSkip(elem.href, false, steps);
if ( url === undefined ) { return; }
return url.replace(/ /g, '%20');
};
const sanitize = ( ) => {
let elems = [];