Skip to content

Commit

Permalink
Avoid parsing query parameters with URLSearchParams
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Sep 12, 2021
1 parent 670c5f1 commit a3a66d0
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/js/static-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -4366,9 +4366,20 @@ FilterContainer.prototype.filterQuery = function(fctxt) {
if ( qpos === -1 ) { return; }
let hpos = url.indexOf('#', qpos + 1);
if ( hpos === -1 ) { hpos = url.length; }
const params = new Map(
new URLSearchParams(url.slice(qpos + 1, hpos))
);
const params = new Map();
const query = url.slice(qpos + 1, hpos);
for ( let i = 0; i < query.length; ) {
let pos = query.indexOf('&', i);
if ( pos === -1 ) { pos = query.length; }
const kv = query.slice(i, pos);
i = pos + 1;
pos = kv.indexOf('=');
if ( pos !== -1 ) {
params.set(kv.slice(0, pos), kv.slice(pos + 1));
} else {
params.set(kv, '');
}
}
const inParamCount = params.size;
const out = [];
for ( const directive of directives ) {
Expand Down Expand Up @@ -4405,7 +4416,10 @@ FilterContainer.prototype.filterQuery = function(fctxt) {
}
if ( re === undefined ) { continue; }
let filtered = false;
for ( const [ key, value ] of params ) {
for ( const [ key, raw ] of params ) {
let value = raw;
try { value = decodeURIComponent(value); }
catch(ex) { }
if ( re.test(`${key}=${value}`) === not ) { continue; }
if ( isException === false ) { params.delete(key); }
filtered = true;
Expand All @@ -4419,7 +4433,7 @@ FilterContainer.prototype.filterQuery = function(fctxt) {
fctxt.redirectURL = url.slice(0, qpos);
if ( params.size !== 0 ) {
fctxt.redirectURL += '?' + Array.from(params).map(a =>
a[1] === '' ? a[0] : `${a[0]}=${encodeURIComponent(a[1])}`
a[1] === '' ? a[0] : `${a[0]}=${a[1]}`
).join('&');
}
if ( hpos !== url.length ) {
Expand Down

0 comments on commit a3a66d0

Please sign in to comment.