Skip to content

Commit

Permalink
AG-24853 Fix scriptlet parser
Browse files Browse the repository at this point in the history
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
  • Loading branch information
scripthunter7 committed Aug 14, 2023
1 parent b9f6378 commit 56a602c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
20 changes: 7 additions & 13 deletions packages/agtree/src/parser/cosmetic/body/scriptlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CLOSE_PARENTHESIS,
COMMA,
EMPTY,
ESCAPE_CHARACTER,
OPEN_PARENTHESIS,
SEMICOLON,
SPACE,
Expand Down Expand Up @@ -87,28 +88,21 @@ export class ScriptletInjectionBodyParser {
// Save the offset of the opening parentheses
const openingParenthesesIndex = offset;

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

// Closing parentheses should be present
if (closingParenthesesIndex === -1) {
if (
raw[closingParenthesesIndex] !== CLOSE_PARENTHESIS
|| raw[closingParenthesesIndex - 1] === ESCAPE_CHARACTER
) {
throw new AdblockSyntaxError(
// eslint-disable-next-line max-len
`Invalid AdGuard/uBlock scriptlet call, no closing parentheses '${CLOSE_PARENTHESIS}' found`,
locRange(loc, offset, raw.length),
);
}

// Shouldn't have any characters after the closing parentheses
if (StringUtils.skipWSBack(raw) !== closingParenthesesIndex) {
throw new AdblockSyntaxError(
// eslint-disable-next-line max-len
`Invalid AdGuard/uBlock scriptlet call, unexpected characters after the closing parentheses '${CLOSE_PARENTHESIS}'`,
locRange(loc, closingParenthesesIndex + 1, raw.length),
);
}

// Parse parameter list
const params = ParameterListParser.parse(
raw.substring(openingParenthesesIndex + 1, closingParenthesesIndex),
Expand Down
22 changes: 18 additions & 4 deletions packages/agtree/test/parser/cosmetic/body/scriptlet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,28 +750,42 @@ describe('ScriptletInjectionBodyParser', () => {
() => ScriptletInjectionBodyParser.parseAdgAndUboScriptletCall('//scriptlet(arg0)a'),
).toThrowError(
// eslint-disable-next-line max-len
`Invalid AdGuard/uBlock scriptlet call, unexpected characters after the closing parentheses '${CLOSE_PARENTHESIS}'`,
`Invalid AdGuard/uBlock scriptlet call, no closing parentheses '${CLOSE_PARENTHESIS}' found`,
);

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

expect(
() => ScriptletInjectionBodyParser.parseAdgAndUboScriptletCall(String.raw`//scriptlet(arg0\)`),
).toThrowError(
// eslint-disable-next-line max-len
`Invalid AdGuard/uBlock scriptlet call, no closing parentheses '${CLOSE_PARENTHESIS}' found`,
);

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

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

expect(
() => ScriptletInjectionBodyParser.parseAdgAndUboScriptletCall(String.raw`js(arg0\)`),
).toThrowError(
// eslint-disable-next-line max-len
`Invalid AdGuard/uBlock scriptlet call, no closing parentheses '${CLOSE_PARENTHESIS}' found`,
);
});

Expand Down

0 comments on commit 56a602c

Please sign in to comment.