From 8ba71f09d756b96f081f423efb5006ce6b628974 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 13 Dec 2024 10:54:01 -0500 Subject: [PATCH] Improve quote usage in filter options and scriptlets Related feedback: https://github.com/uBlockOrigin/uBlock-issues/issues/760#issuecomment-2540436382 Using quotes in filter option values is meant to remove ambiguity when the value contains special characters. This was not working when the value started with `$`. For example, fixes usage of quotes in: $removeparam='$deep_link' Also, fixed logger output for scriptlets using empty parameters in quotes. --- src/js/scriptlet-filtering-core.js | 2 +- src/js/static-filtering-parser.js | 19 ++++++++----------- src/js/static-net-filtering.js | 2 +- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/js/scriptlet-filtering-core.js b/src/js/scriptlet-filtering-core.js index 907844fbc198a..0fec05c2ba7a2 100644 --- a/src/js/scriptlet-filtering-core.js +++ b/src/js/scriptlet-filtering-core.js @@ -99,7 +99,7 @@ const patchScriptlet = (content, arglist) => { }; const requote = s => { - if ( /^(["'`]).+\1$|,/.test(s) === false ) { return s; } + if ( /^(["'`]).*\1$|,|^$/.test(s) === false ) { return s; } if ( s.includes("'") === false ) { return `'${s}'`; } if ( s.includes('"') === false ) { return `"${s}"`; } if ( s.includes('`') === false ) { return `\`${s}\``; } diff --git a/src/js/static-filtering-parser.js b/src/js/static-filtering-parser.js index 2be165eb107ef..d3b4ca48416cf 100644 --- a/src/js/static-filtering-parser.js +++ b/src/js/static-filtering-parser.js @@ -1477,18 +1477,15 @@ export class AstFilterParser { if ( j === -1 ) { return end; } if ( (j+1) === end ) { return end; } for (;;) { - const before = s.charCodeAt(j-1); - if ( j !== start && before === 0x24 /* $ */ ) { return -1; } - const after = s.charCodeAt(j+1); - if ( - after !== 0x29 /* ) */ && - after !== 0x2F /* / */ && - after !== 0x7C /* | */ && - before !== 0x5C /* \ */ - ) { - return j; + const before = s.charAt(j-1); + if ( before === '$' ) { return -1; } + const after = s.charAt(j+1); + if ( ')/|'.includes(after) === false ) { + if ( before === '' || '"\'\\`'.includes(before) === false ) { + return j; + } } - if ( j <= start ) { break; } + if ( j === start ) { break; } j = s.lastIndexOf('$', j-1); if ( j === -1 ) { break; } } diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index f084180ffccf3..58b6570705c2d 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -419,7 +419,7 @@ class LogData { } static requote(s) { - if ( /^(["'`]).+\1$|,/.test(s) === false ) { return s; } + if ( /^\$|^(["'`]).*\1$|,/.test(s) === false ) { return s; } if ( s.includes("'") === false ) { return `'${s}'`; } if ( s.includes('"') === false ) { return `"${s}"`; } if ( s.includes('`') === false ) { return `\`${s}\``; }