From 72cc9a8fe830ea6ea919defa87990f64ce4f8aa6 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 21 Mar 2023 10:22:32 -0400 Subject: [PATCH] Fix broken http header filtering Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/2552 --- src/js/httpheader-filtering.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/js/httpheader-filtering.js b/src/js/httpheader-filtering.js index 10f314f56..bd3d7cad7 100644 --- a/src/js/httpheader-filtering.js +++ b/src/js/httpheader-filtering.js @@ -41,12 +41,10 @@ const $exceptions = new Set(); let acceptedCount = 0; let discardedCount = 0; -const headerIndexFromName = function(name, headers) { - let i = headers.length; - while ( i-- ) { - if ( headers[i].name.toLowerCase() === name ) { - return i; - } +const headerIndexFromName = function(name, headers, start = 0) { + for ( let i = start; i < headers.length; i++ ) { + if ( headers[i].name.toLowerCase() !== name ) { continue; } + return i; } return -1; }; @@ -138,7 +136,7 @@ httpheaderFilteringEngine.fromCompiledContent = function(reader) { duplicates.add(fingerprint); const args = reader.args(); if ( args.length < 4 ) { continue; } - filterDB.store(args[1], args[2], args[3].slice(15, -1)); + filterDB.store(args[1], args[2], args[3]); } }; @@ -175,18 +173,20 @@ httpheaderFilteringEngine.apply = function(fctxt, headers) { const hasGlobalException = $exceptions.has(''); let modified = false; + let i = 0; for ( const name of $headers ) { - for (;;) { - const i = headerIndexFromName(name, headers); - if ( i === -1 ) { break; } - const isExcepted = hasGlobalException || $exceptions.has(name); - if ( isExcepted ) { - if ( logger.enabled ) { - logOne(true, hasGlobalException ? '' : name, fctxt); - } - break; + const isExcepted = hasGlobalException || $exceptions.has(name); + if ( isExcepted ) { + if ( logger.enabled ) { + logOne(true, hasGlobalException ? '' : name, fctxt); } + continue; + } + i = 0; + for (;;) { + i = headerIndexFromName(name, headers, i); + if ( i === -1 ) { break; } headers.splice(i, 1); if ( logger.enabled ) { logOne(false, name, fctxt);