From a558af1038704ae54aac2cb83f54741cb0171a26 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Tue, 13 Apr 2021 23:50:39 +0300 Subject: [PATCH] Fix crash from passing an invalid regex source to TRegex validation function --- backend/src/validatorUtils.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/backend/src/validatorUtils.ts b/backend/src/validatorUtils.ts index 1db917c0..5292ca96 100644 --- a/backend/src/validatorUtils.ts +++ b/backend/src/validatorUtils.ts @@ -27,7 +27,15 @@ export const TRegex = new t.Type( (s): s is RegExp => s instanceof RegExp, (from, to) => either.chain(t.string.validate(from, to), s => { - return t.success(inputPatternToRegExp(s)); + try { + return t.success(inputPatternToRegExp(s)); + } catch (err) { + if (err instanceof InvalidRegexError) { + return t.failure(s, [], err.message); + } + + throw err; + } }), s => `/${s.source}/${s.flags}`, );