Skip to content

Commit

Permalink
Improve quote usage in filter options and scriptlets
Browse files Browse the repository at this point in the history
Related feedback:
uBlockOrigin/uBlock-issues#760 (comment)

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.
  • Loading branch information
gorhill committed Dec 13, 2024
1 parent 2b6d67b commit 8ba71f0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/js/scriptlet-filtering-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}\``; }
Expand Down
19 changes: 8 additions & 11 deletions src/js/static-filtering-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/js/static-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}\``; }
Expand Down

0 comments on commit 8ba71f0

Please sign in to comment.