3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-14 22:05:01 +00:00

Fixes to RegExpRunner usage in !search and !bansearch, validate that input regex is valid

This commit is contained in:
Dragory 2020-08-10 01:31:32 +03:00
parent 1d5dd10bff
commit 0af03978e5
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
3 changed files with 94 additions and 15 deletions

View file

@ -7,13 +7,19 @@ import safeRegex from "safe-regex";
const regexWithFlags = /^\/(.*?)\/([i]*)$/;
export class InvalidRegexError extends Error {}
/**
* This function supports two input syntaxes for regexes: /<pattern>/<flags> and just <pattern>
*/
export function inputPatternToRegExp(pattern: string) {
const advancedSyntaxMatch = pattern.match(regexWithFlags);
const [finalPattern, flags] = advancedSyntaxMatch ? [advancedSyntaxMatch[1], advancedSyntaxMatch[2]] : [pattern, ""];
return new RegExp(finalPattern, flags);
try {
return new RegExp(finalPattern, flags);
} catch (e) {
throw new InvalidRegexError(e.message);
}
}
export const TRegex = new t.Type<RegExp, string>(