Skip to content

Commit a43d1d8

Browse files
committed
Improve href-sanitizer scriptlet
Related issue: uBlockOrigin/uBlock-issues#3650
1 parent 41a6531 commit a43d1d8

File tree

1 file changed

+4
-22
lines changed

1 file changed

+4
-22
lines changed

src/js/resources/href-sanitizer.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,17 @@ function hrefSanitizer(
9191
}
9292
return '';
9393
};
94-
const extractParam = (href, source) => {
95-
if ( Boolean(source) === false ) { return href; }
96-
const recursive = source.includes('?', 1);
97-
const end = recursive ? source.indexOf('?', 1) : source.length;
98-
try {
99-
const url = new URL(href, document.location);
100-
let value = url.searchParams.get(source.slice(1, end));
101-
if ( value === null ) { return href }
102-
if ( recursive ) { return extractParam(value, source.slice(end)); }
103-
return value;
104-
} catch {
105-
}
106-
return href;
107-
};
10894
const extractURL = (elem, source) => {
10995
if ( /^\[.*\]$/.test(source) ) {
11096
return elem.getAttribute(source.slice(1,-1).trim()) || '';
11197
}
11298
if ( source === 'text' ) {
11399
return elem.textContent
114-
.replace(/^[^\x21-\x7e]+/, '') // remove leading invalid characters
115-
.replace(/[^\x21-\x7e]+$/, '') // remove trailing invalid characters
116-
;
100+
.replace(/^[^\x21-\x7e]+|/, '') // remove leading invalid characters
101+
.replace(/[^\x21-\x7e]+$/, ''); // remove trailing invalid characters
117102
}
118-
if ( source.startsWith('?') === false ) { return ''; }
119-
const steps = source.replace(/(\S)\?/g, '\\1?').split(/\s+/);
120-
const url = steps.length === 1
121-
? extractParam(elem.href, source)
122-
: urlSkip(elem.href, false, steps);
103+
const steps = source.replace(/(\S)\?/g, '\\1 ?').split(/\s+/);
104+
const url = urlSkip(elem.href, false, steps);
123105
if ( url === undefined ) { return; }
124106
return url.replace(/ /g, '%20');
125107
};

0 commit comments

Comments
 (0)