Skip to content

Commit

Permalink
Scriptlet params edge case fix (#3558)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmod committed Oct 28, 2023
1 parent 15f841c commit 46cf8ea
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 10 deletions.
27 changes: 18 additions & 9 deletions packages/adblocker/src/filters/cosmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ export default class CosmeticFilter implements IFilter {
let inRegexp = false;
let objectNesting = 0;
let lastCharIsBackslash = false;
let inArgument = false;

for (; index < selector.length; index += 1) {
const char = selector[index];
Expand Down Expand Up @@ -715,17 +716,25 @@ export default class CosmeticFilter implements IFilter {
inRegexp = false;
}
} else {
if (char === '"') {
inDoubleQuotes = true;
} else if (char === "'") {
inSingleQuotes = true;
} else if (char === '{') {
objectNesting += 1;
} else if (char === '/') {
inRegexp = true;
} else if (char === ',') {
if (inArgument === false) {
if (char === ' ') {
// ignore
} else if (char === '"' && selector.indexOf('"', index + 1) > 0) {
inDoubleQuotes = true;
} else if (char === "'" && selector.indexOf("'", index + 1) > 0) {
inSingleQuotes = true;
} else if (char === '{' && selector.indexOf('}', index + 1) > 0) {
objectNesting += 1;
} else if (char === '/' && selector.indexOf('/', index + 1) > 0) {
inRegexp = true;
} else {
inArgument = true;
}
}
if (char === ',') {
parts.push(selector.slice(lastComaIndex + 1, index).trim());
lastComaIndex = index;
inArgument = false;
}
}
}
Expand Down
70 changes: 69 additions & 1 deletion packages/adblocker/test/parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,70 @@ describe('scriptlets arguments parsing', () => {

it('complex', () => {
for (const [scriptlet, expected] of [
[
'script-name, {x, y',
{
name: 'script-name',
args: ['{x', 'y'],
},
],
[
'script-name, "x, y',
{
name: 'script-name',
args: ['"x', 'y'],
},
],
[
"script-name, 'x, y",
{
name: 'script-name',
args: ["'x", 'y'],
},
],
[
'script-name, /x, y',
{
name: 'script-name',
args: ['/x', 'y'],
},
],
[
'xml-prune,a/b,///,,c',
{
name: 'xml-prune',
args: ['a/b', '///', '', 'c'],
},
],
[
`xml-prune, xpath(//*[name()="MPD"]/@mediaPresentationDuration | //*[name()="Period"][.//*[name()="BaseURL" and contains(text()\\,'/ads-')]] | //*[name()="Period"]/@start), Period[id^="Ad"i], .mpd`,
{
name: 'xml-prune',
args: [
`xpath(//*[name()="MPD"]/@mediaPresentationDuration | //*[name()="Period"][.//*[name()="BaseURL" and contains(text()\\,'/ads-')]] | //*[name()="Period"]/@start)`,
'Period[id^="Ad"i]',
'.mpd',
],
},
],
[
'xml-prune,a/b,,c',
{
name: 'xml-prune',
args: ['a/b', '', 'c'],
},
],
[
'xml-prune, xpath(//*[name()="Period"][.//*[@value="Ad"]] | //*[name()="Period"]/@start), [value="Ad"], .mpd',
{
name: 'xml-prune',
args: [
'xpath(//*[name()="Period"][.//*[@value="Ad"]] | //*[name()="Period"]/@start)',
'[value="Ad"]',
'.mpd',
],
},
],
[
'acs, Math, /\\}\\s*\\(.*?\\b(self|this|window)\\b.*?\\)/',
{
Expand Down Expand Up @@ -2227,7 +2291,11 @@ describe('scriptlets arguments parsing', () => {
'trusted-replace-fetch-response, /\\"adPlacements.*?\\"\\}\\}\\}\\]\\,/, , url:player?key= method:/post/i bodyUsed:true',
{
name: 'trusted-replace-fetch-response',
args: ['/\\"adPlacements.*?\\"\\}\\}\\}\\]\\,/', '', 'url:player?key= method:/post/i bodyUsed:true'],
args: [
'/\\"adPlacements.*?\\"\\}\\}\\}\\]\\,/',
'',
'url:player?key= method:/post/i bodyUsed:true',
],
},
],
[
Expand Down

0 comments on commit 46cf8ea

Please sign in to comment.