Skip to content

Commit

Permalink
Mind that multiple uritransform may apply to a single request
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Feb 15, 2024
1 parent 9bff0c2 commit 2a5a444
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/js/static-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -5289,28 +5289,37 @@ FilterContainer.prototype.redirectRequest = function(redirectEngine, fctxt) {
FilterContainer.prototype.transformRequest = function(fctxt) {
const directives = this.matchAndFetchModifiers(fctxt, 'uritransform');
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 before = `${redirectURL.pathname}${redirectURL.search}${redirectURL.hash}`;
if ( cache.re.test(before) !== true ) { return; }
const after = before.replace(cache.re, cache.replacement);
if ( after === before ) { return; }
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;
fctxt.redirectURL = redirectURL.href;
return directives;
const out = [];
for ( const directive of directives ) {
if ( (directive.bits & ALLOW_REALM) !== 0 ) {
out.push(directive);
continue;
}
const { refs } = directive;
if ( refs instanceof Object === false ) { continue; }
if ( refs.$cache === null ) {
refs.$cache = sfp.parseReplaceValue(refs.value);
}
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) {
Expand Down

0 comments on commit 2a5a444

Please sign in to comment.