From a43d1d8c428ca5538a1acd0a39c1c346cfd0a744 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 4 Oct 2025 10:22:10 -0400 Subject: [PATCH] Improve `href-sanitizer` scriptlet Related issue: https://github.com/uBlockOrigin/uBlock-issues/issues/3650 --- src/js/resources/href-sanitizer.js | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/src/js/resources/href-sanitizer.js b/src/js/resources/href-sanitizer.js index eb88c27c3..ae43b4906 100644 --- a/src/js/resources/href-sanitizer.js +++ b/src/js/resources/href-sanitizer.js @@ -91,35 +91,17 @@ 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 { - } - return href; - }; const extractURL = (elem, source) => { if ( /^\[.*\]$/.test(source) ) { return elem.getAttribute(source.slice(1,-1).trim()) || ''; } if ( source === 'text' ) { return elem.textContent - .replace(/^[^\x21-\x7e]+/, '') // remove leading invalid characters - .replace(/[^\x21-\x7e]+$/, '') // remove trailing invalid characters - ; + .replace(/^[^\x21-\x7e]+|/, '') // remove leading invalid characters + .replace(/[^\x21-\x7e]+$/, ''); // remove trailing invalid characters } - 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); + 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'); };