Skip to content

Commit

Permalink
fix RegExpUtils.isRegexPattern(). AG-35542
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit b79063f
Author: Slava Leleka <[email protected]>
Date:   Thu Sep 19 18:05:48 2024 +0300

    add more tests

commit b1cddad
Author: Slava Leleka <[email protected]>
Date:   Thu Sep 19 18:02:24 2024 +0300

    fix RegExpUtils.isRegexPattern() if a regexp contains a slash inside the pattern
  • Loading branch information
slavaleleka committed Sep 19, 2024
1 parent aa68ebb commit 13862c2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
8 changes: 8 additions & 0 deletions packages/agtree/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ The format is based on [Keep a Changelog][keepachangelog], and this project adhe
[keepachangelog]: https://keepachangelog.com/en/1.0.0/
[semver]: https://semver.org/spec/v2.0.0.html

## [2.1.2] - 2024-09-19

### Fixed

- `RegExpUtils.isRegexPattern()` if a regexp contains a slash inside the pattern.

[2.1.2]: https://github.com/AdguardTeam/tsurlfilter/releases/tag/agtree-v2.1.2

## [2.1.1] - 2024-09-19

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion packages/agtree/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adguard/agtree",
"version": "2.1.1",
"version": "2.1.2",
"description": "Tool set for working with adblock filter lists",
"keywords": [
"adblock",
Expand Down
16 changes: 8 additions & 8 deletions packages/agtree/src/utils/regexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
REGEX_MARKER,
SLASH,
} from './constants';
import { StringUtils } from './string';

// Special RegExp constants
export const REGEX_START = CARET; // '^'
Expand Down Expand Up @@ -70,22 +69,23 @@ export const SPECIAL_REGEX_SYMBOLS = new Set([
*/
export class RegExpUtils {
/**
* Checks whether a string is a RegExp pattern.
* Checks whether a string possibly is a RegExp pattern.
* Flags are not supported.
*
* Note: it does not perform a full validation of the pattern,
* it just checks if the string starts and ends with a slash.
*
* @param pattern - Pattern to check
* @returns `true` if the string is a RegExp pattern, `false` otherwise
*/
public static isRegexPattern(pattern: string): boolean {
const trimmedPattern = pattern.trim();

// Avoid false positives
if (trimmedPattern.length > REGEX_MARKER.length * 2 && trimmedPattern.startsWith(REGEX_MARKER)) {
const last = StringUtils.findNextUnescapedCharacter(trimmedPattern, REGEX_MARKER, REGEX_MARKER.length);
return last === trimmedPattern.length - 1;
}

return false;
return trimmedPattern.length > REGEX_MARKER.length * 2
&& trimmedPattern.startsWith(REGEX_MARKER)
&& trimmedPattern.endsWith(REGEX_MARKER)
&& trimmedPattern[REGEX_MARKER.length - 2] !== ESCAPE_CHARACTER;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/agtree/test/utils/regexp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ describe('RegExpUtils', () => {
actual: '/^regex$/',
expected: true,
},
{
actual: '/ex[[ampl[[e.com///.*/banner/',
expected: true,
},
{
actual: String.raw`/^htt[[[ps?:\/\/.*(example1|example2)\.(com|org)\//`,
expected: true,
},
{
actual: String.raw`/\.example\.com/.*[a-zA-Z0-9]({4}/`,
expected: true,
},
])('isRegexPattern should return \'$expected\' for \'$actual\'', ({ actual, expected }) => {
expect(RegExpUtils.isRegexPattern(actual)).toBe(expected);
});
Expand Down

0 comments on commit 13862c2

Please sign in to comment.