Mind that multiple `uritransform` may apply to a single request

Related issue:
https://github.com/uBlockOrigin/uBlock-issues/issues/3125
This commit is contained in:
Raymond Hill 2024-02-15 14:34:50 -05:00
parent 9bff0c2f94
commit 2a5a444482
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 30 additions and 21 deletions

View File

@ -5289,28 +5289,37 @@ FilterContainer.prototype.redirectRequest = function(redirectEngine, fctxt) {
FilterContainer.prototype.transformRequest = function(fctxt) { FilterContainer.prototype.transformRequest = function(fctxt) {
const directives = this.matchAndFetchModifiers(fctxt, 'uritransform'); const directives = this.matchAndFetchModifiers(fctxt, 'uritransform');
if ( directives === undefined ) { return; } if ( directives === undefined ) { return; }
const directive = directives[directives.length-1];
if ( (directive.bits & ALLOW_REALM) !== 0 ) { return directives; }
if ( directive.refs instanceof Object === false ) { return; }
const { refs } = directive;
if ( refs.$cache === null ) {
refs.$cache = sfp.parseReplaceValue(refs.value);
}
const cache = refs.$cache;
if ( cache === undefined ) { return; }
const redirectURL = new URL(fctxt.url); const redirectURL = new URL(fctxt.url);
const before = `${redirectURL.pathname}${redirectURL.search}${redirectURL.hash}`; const out = [];
if ( cache.re.test(before) !== true ) { return; } for ( const directive of directives ) {
const after = before.replace(cache.re, cache.replacement); if ( (directive.bits & ALLOW_REALM) !== 0 ) {
if ( after === before ) { return; } out.push(directive);
const hashPos = after.indexOf('#'); continue;
redirectURL.hash = hashPos !== -1 ? after.slice(hashPos) : ''; }
const afterMinusHash = hashPos !== -1 ? after.slice(0, hashPos) : after; const { refs } = directive;
const searchPos = afterMinusHash.indexOf('?'); if ( refs instanceof Object === false ) { continue; }
redirectURL.search = searchPos !== -1 ? afterMinusHash.slice(searchPos) : ''; if ( refs.$cache === null ) {
redirectURL.pathname = searchPos !== -1 ? after.slice(0, searchPos) : after; refs.$cache = sfp.parseReplaceValue(refs.value);
fctxt.redirectURL = redirectURL.href; }
return directives; const cache = refs.$cache;
if ( cache === undefined ) { continue; }
const before = `${redirectURL.pathname}${redirectURL.search}${redirectURL.hash}`;
if ( cache.re.test(before) !== true ) { continue; }
const after = before.replace(cache.re, cache.replacement);
if ( after === before ) { continue; }
const hashPos = after.indexOf('#');
redirectURL.hash = hashPos !== -1 ? after.slice(hashPos) : '';
const afterMinusHash = hashPos !== -1 ? after.slice(0, hashPos) : after;
const searchPos = afterMinusHash.indexOf('?');
redirectURL.search = searchPos !== -1 ? afterMinusHash.slice(searchPos) : '';
redirectURL.pathname = searchPos !== -1 ? after.slice(0, searchPos) : after;
out.push(directive);
}
if ( out.length === 0 ) { return; }
if ( redirectURL.href !== fctxt.url ) {
fctxt.redirectURL = redirectURL.href;
}
return out;
}; };
function parseRedirectRequestValue(directive) { function parseRedirectRequestValue(directive) {