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
This commit is contained in:
Raymond Hill 2025-09-15 09:26:09 -04:00
parent e1028c299f
commit 02e5248d57
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -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; }
}