From 59aa235952a9289cfe72e4fb9f8a7d8f4c80be9a Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 25 Sep 2022 08:51:48 -0400 Subject: [PATCH] Forbid relative selector at top level Related commit: - https://github.com/gorhill/uBlock/commit/a71b71e4c8a2037fc68970bc8912a76732edaade --- src/js/static-filtering-parser.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/js/static-filtering-parser.js b/src/js/static-filtering-parser.js index 6a43af719..01809e6fc 100644 --- a/src/js/static-filtering-parser.js +++ b/src/js/static-filtering-parser.js @@ -1336,7 +1336,7 @@ Parser.prototype.SelectorCompiler = class { `${cssClassOrId}(?:${cssClassOrId})*(?:${cssAttribute})*` + '|' + `${cssAttribute}(?:${cssAttribute})*` + ')'; - const cssCombinator = '(?:\\s+|\\s*[>+~]\\s*)'; + const cssCombinator = '(?:\\s+|\\s*[+>~]\\s*)'; this.reCommonSelector = new RegExp( `^${cssSimple}(?:${cssCombinator}${cssSimple})*$` ); @@ -1386,6 +1386,7 @@ Parser.prototype.SelectorCompiler = class { [ 'contains', 'has-text' ], [ 'has', 'has' ], ]); + this.reIsRelativeSelector = /^\s*[+>~]/; this.reExtendedSyntax = /\[-(?:abp|ext)-[a-z-]+=(['"])(?:.+?)(?:\1)\]/; this.reExtendedSyntaxReplacer = /\[-(?:abp|ext)-([a-z-]+)=(['"])(.+?)\2\]/g; this.abpProceduralOpReplacer = /:-abp-(?:contains|has)\(/g; @@ -1430,6 +1431,9 @@ Parser.prototype.SelectorCompiler = class { } } + // Relative selectors not allowed at top level. + if ( this.reIsRelativeSelector.test(raw) ) { return false; } + if ( this.reCommonSelector.test(raw) ) { out.compiled = raw; return true; @@ -1442,7 +1446,6 @@ Parser.prototype.SelectorCompiler = class { out.compiled.raw = raw; out.compiled = JSON.stringify(out.compiled); } - return true; }