Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scriptlets: inject quoted arguments without quotes #3559

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/adblocker/src/filters/cosmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,17 @@ export default class CosmeticFilter implements IFilter {
return undefined;
}

return { name: parts[0], args: parts.slice(1) };
const args = parts.slice(1).map((part) => {
if (
(part.startsWith(`'`) && part.endsWith(`'`)) ||
(part.startsWith(`"`) && part.endsWith(`"`))
) {
return part.substring(1, part.length - 1);
}
return part;
});

return { name: parts[0], args };
}

public getScript(js: Map<string, string>): string | undefined {
Expand Down
15 changes: 13 additions & 2 deletions packages/adblocker/test/parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2023,11 +2023,11 @@ describe('scriptlets arguments parsing', () => {
{ name: 'acis', args: ['document.createElement', '/break;case $.|popunder/'] },
],
['acs, atob, -0x1', { name: 'acs', args: ['atob', '-0x1'] }],
["acs, atob, 'shift'", { name: 'acs', args: ['atob', "'shift'"] }],
["acs, atob, 'shift'", { name: 'acs', args: ['atob', 'shift'] }],
["acs, Date, ='\\x", { name: 'acs', args: ['Date', "='\\x"] }],
[
`acs, decodeURIComponent, "'shift'"`,
{ name: 'acs', args: ['decodeURIComponent', `"'shift'"`] },
{ name: 'acs', args: ['decodeURIComponent', `'shift'`] },
],
[
`acs, document.getElementById, /\\$\\('body'\\)|\\$\\("body"\\)/`,
Expand Down Expand Up @@ -2057,6 +2057,17 @@ describe('scriptlets arguments parsing', () => {
});
});

it('removes wrapping quotes', () => {
expect(CosmeticFilter.parse('foo.com##+js(script-name, "a")')?.parseScript()).to.eql({
name: 'script-name',
args: ['a'],
});
expect(CosmeticFilter.parse("foo.com##+js(script-name, 'a')")?.parseScript()).to.eql({
name: 'script-name',
args: ['a'],
});
});

it('handles escaping of comas', () => {
expect(CosmeticFilter.parse('foo.com##+js(script-name, foo \\,bar)')?.parseScript()).to.eql({
name: 'script-name',
Expand Down