Skip to content

Commit 56a602c

Browse files
committed
AG-24853 Fix scriptlet parser
Merge in ADGUARD-FILTERS/tsurlfilter from fix/AG-24853 to master Squashed commit of the following: commit 0fea738 Author: scripthunter7 <[email protected]> Date: Mon Aug 14 14:31:05 2023 +0200 Update tests commit b25fd5f Author: scripthunter7 <[email protected]> Date: Mon Aug 14 14:30:56 2023 +0200 Fix scriptlet parser
1 parent b9f6378 commit 56a602c

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

packages/agtree/src/parser/cosmetic/body/scriptlet.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
CLOSE_PARENTHESIS,
99
COMMA,
1010
EMPTY,
11+
ESCAPE_CHARACTER,
1112
OPEN_PARENTHESIS,
1213
SEMICOLON,
1314
SPACE,
@@ -87,28 +88,21 @@ export class ScriptletInjectionBodyParser {
8788
// Save the offset of the opening parentheses
8889
const openingParenthesesIndex = offset;
8990

90-
// Find closing parentheses
91-
// eslint-disable-next-line max-len
92-
const closingParenthesesIndex = StringUtils.findUnescapedNonStringNonRegexChar(raw, CLOSE_PARENTHESIS, openingParenthesesIndex + 1);
91+
// Skip whitespace from the end
92+
const closingParenthesesIndex = StringUtils.skipWSBack(raw, raw.length - 1);
9393

9494
// Closing parentheses should be present
95-
if (closingParenthesesIndex === -1) {
95+
if (
96+
raw[closingParenthesesIndex] !== CLOSE_PARENTHESIS
97+
|| raw[closingParenthesesIndex - 1] === ESCAPE_CHARACTER
98+
) {
9699
throw new AdblockSyntaxError(
97100
// eslint-disable-next-line max-len
98101
`Invalid AdGuard/uBlock scriptlet call, no closing parentheses '${CLOSE_PARENTHESIS}' found`,
99102
locRange(loc, offset, raw.length),
100103
);
101104
}
102105

103-
// Shouldn't have any characters after the closing parentheses
104-
if (StringUtils.skipWSBack(raw) !== closingParenthesesIndex) {
105-
throw new AdblockSyntaxError(
106-
// eslint-disable-next-line max-len
107-
`Invalid AdGuard/uBlock scriptlet call, unexpected characters after the closing parentheses '${CLOSE_PARENTHESIS}'`,
108-
locRange(loc, closingParenthesesIndex + 1, raw.length),
109-
);
110-
}
111-
112106
// Parse parameter list
113107
const params = ParameterListParser.parse(
114108
raw.substring(openingParenthesesIndex + 1, closingParenthesesIndex),

packages/agtree/test/parser/cosmetic/body/scriptlet.test.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -750,28 +750,42 @@ describe('ScriptletInjectionBodyParser', () => {
750750
() => ScriptletInjectionBodyParser.parseAdgAndUboScriptletCall('//scriptlet(arg0)a'),
751751
).toThrowError(
752752
// eslint-disable-next-line max-len
753-
`Invalid AdGuard/uBlock scriptlet call, unexpected characters after the closing parentheses '${CLOSE_PARENTHESIS}'`,
753+
`Invalid AdGuard/uBlock scriptlet call, no closing parentheses '${CLOSE_PARENTHESIS}' found`,
754754
);
755755

756756
expect(
757757
() => ScriptletInjectionBodyParser.parseAdgAndUboScriptletCall('//scriptlet(arg0) a'),
758758
).toThrowError(
759759
// eslint-disable-next-line max-len
760-
`Invalid AdGuard/uBlock scriptlet call, unexpected characters after the closing parentheses '${CLOSE_PARENTHESIS}'`,
760+
`Invalid AdGuard/uBlock scriptlet call, no closing parentheses '${CLOSE_PARENTHESIS}' found`,
761+
);
762+
763+
expect(
764+
() => ScriptletInjectionBodyParser.parseAdgAndUboScriptletCall(String.raw`//scriptlet(arg0\)`),
765+
).toThrowError(
766+
// eslint-disable-next-line max-len
767+
`Invalid AdGuard/uBlock scriptlet call, no closing parentheses '${CLOSE_PARENTHESIS}' found`,
761768
);
762769

763770
expect(
764771
() => ScriptletInjectionBodyParser.parseAdgAndUboScriptletCall('js(arg0)a'),
765772
).toThrowError(
766773
// eslint-disable-next-line max-len
767-
`Invalid AdGuard/uBlock scriptlet call, unexpected characters after the closing parentheses '${CLOSE_PARENTHESIS}'`,
774+
`Invalid AdGuard/uBlock scriptlet call, no closing parentheses '${CLOSE_PARENTHESIS}' found`,
768775
);
769776

770777
expect(
771778
() => ScriptletInjectionBodyParser.parseAdgAndUboScriptletCall('js(arg0) a'),
772779
).toThrowError(
773780
// eslint-disable-next-line max-len
774-
`Invalid AdGuard/uBlock scriptlet call, unexpected characters after the closing parentheses '${CLOSE_PARENTHESIS}'`,
781+
`Invalid AdGuard/uBlock scriptlet call, no closing parentheses '${CLOSE_PARENTHESIS}' found`,
782+
);
783+
784+
expect(
785+
() => ScriptletInjectionBodyParser.parseAdgAndUboScriptletCall(String.raw`js(arg0\)`),
786+
).toThrowError(
787+
// eslint-disable-next-line max-len
788+
`Invalid AdGuard/uBlock scriptlet call, no closing parentheses '${CLOSE_PARENTHESIS}' found`,
775789
);
776790
});
777791

0 commit comments

Comments
 (0)