From 02e5248d571ccaed7562b181bd7cc899b0a06547 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 15 Sep 2025 09:26:09 -0400 Subject: [PATCH] Fix potential infinite loop when scanning for `$` anchor Related issue: https://github.com/uBlockOrigin/uBlock-issues/issues/3799 An infinite loop in the network filter parser was triggered when the following conditions were fulfilled: - There was a network option `$` anchor - There were only whitespace character(s) preceding the anchor - There was an invalid filter option following the anchor --- src/js/static-filtering-parser.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/js/static-filtering-parser.js b/src/js/static-filtering-parser.js index 5fb530fc4..853bb1cc8 100644 --- a/src/js/static-filtering-parser.js +++ b/src/js/static-filtering-parser.js @@ -1200,7 +1200,7 @@ export class AstFilterParser { prev = this.linkRight(prev, next); patternBeg += 2; } - let anchorBeg = this.indexOfNetAnchor(parentStr, patternBeg); + let anchorBeg = this.indexOfNetAnchor(parentStr); if ( anchorBeg === -1 ) { return 0; } anchorBeg += parentBeg; if ( anchorBeg !== parentEnd ) { @@ -1508,9 +1508,9 @@ export class AstFilterParser { } } - indexOfNetAnchor(s, start = 0) { + indexOfNetAnchor(s) { const end = s.length; - if ( end === start ) { return end; } + if ( end === 0 ) { return end; } let j = s.lastIndexOf('$'); if ( j === -1 ) { return end; } if ( (j+1) === end ) { return end; } @@ -1518,7 +1518,7 @@ export class AstFilterParser { const before = s.charAt(j-1); if ( before === '$' ) { return -1; } if ( this.reNetOptionTokens.test(s.slice(j+1)) ) { return j; } - if ( j === start ) { break; } + if ( j === 0 ) { break; } j = s.lastIndexOf('$', j-1); if ( j === -1 ) { break; } }