From de6f8fae6015b8311b9f7e5190ef6c56de56f898 Mon Sep 17 00:00:00 2001 From: deepakchethan Date: Fri, 2 Oct 2020 19:34:47 +0530 Subject: [PATCH 1/4] Parse regexp before validating user input Clean up code Bug fixes Add comments --- app/client/src/widgets/InputWidget.tsx | 30 +++++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/app/client/src/widgets/InputWidget.tsx b/app/client/src/widgets/InputWidget.tsx index f34a1b7280de..20bdfdde4e84 100644 --- a/app/client/src/widgets/InputWidget.tsx +++ b/app/client/src/widgets/InputWidget.tsx @@ -51,25 +51,39 @@ class InputWidget extends BaseWidget { return { isValid: `{{ function(){ + let parsedRegex = null; + if (regex) { + // break up the regexp pattern into 4 parts: given regex, regex prefix , regex pattern, regex flags + // Example /appsmith/i will be split into ["/appsmith/gi", "/", "appsmith", "gi"] + const regexParts = regex.match(/(\\/?)(.+)\\1([a-z]*)/i); + if (regexParts === null) { + parsedRegex = new RegExp(regex); + } + // if we don't have a regex flags (gmisuy), convert provided string into regexp directly + if (regexParts[3] && !/^(?!.*?(.).*?\\1)[gmisuy]+$/.test(regexParts[3])) { + parsedRegex = RegExp(regex); + } + // if we have a regex flags, use it to form regexp + return new RegExp(regexParts[2], regexParts[3]); + } if (this.inputType === "EMAIL") { const emailRegex = new RegExp(/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/); return emailRegex.test(this.text); - } - else if (this.inputType === "NUMBER") { + } else if (this.inputType === "NUMBER") { return !isNaN(this.text) - } - else if (this.isRequired) { + } + else if(this.isRequired) { if(this.text && this.text.length) { - if(this.regex) { - return new RegExp(this.regex).test(this.text) + if(parsedRegex) { + return parsedRegex.test(this.text) } else { return true; } } else { return false; } - } if (this.regex) { - return new RegExp(this.regex).test(this.text) + } if(parsedRegex) { + return parsedRegex.test(this.text) } else { return true; } From 9dfbf8e872dda5824ed1523dd454a989c366ef2c Mon Sep 17 00:00:00 2001 From: deepakchethan Date: Tue, 6 Oct 2020 17:38:41 +0530 Subject: [PATCH 2/4] Bug fixes --- app/client/src/widgets/InputWidget.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/client/src/widgets/InputWidget.tsx b/app/client/src/widgets/InputWidget.tsx index 20bdfdde4e84..5e9492a3c22b 100644 --- a/app/client/src/widgets/InputWidget.tsx +++ b/app/client/src/widgets/InputWidget.tsx @@ -64,7 +64,7 @@ class InputWidget extends BaseWidget { parsedRegex = RegExp(regex); } // if we have a regex flags, use it to form regexp - return new RegExp(regexParts[2], regexParts[3]); + parsedRegex = new RegExp(regexParts[2], regexParts[3]); } if (this.inputType === "EMAIL") { const emailRegex = new RegExp(/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/); From 56b0d1ca1c354f279aa90b0ecc0724870afc2c56 Mon Sep 17 00:00:00 2001 From: deepakchethan Date: Tue, 6 Oct 2020 17:40:05 +0530 Subject: [PATCH 3/4] Revert some code --- app/client/src/widgets/InputWidget.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/client/src/widgets/InputWidget.tsx b/app/client/src/widgets/InputWidget.tsx index 5e9492a3c22b..e5b77d2a0145 100644 --- a/app/client/src/widgets/InputWidget.tsx +++ b/app/client/src/widgets/InputWidget.tsx @@ -69,10 +69,11 @@ class InputWidget extends BaseWidget { if (this.inputType === "EMAIL") { const emailRegex = new RegExp(/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/); return emailRegex.test(this.text); - } else if (this.inputType === "NUMBER") { - return !isNaN(this.text) } - else if(this.isRequired) { + else if (this.inputType === "NUMBER") { + return !isNaN(this.text) + } + else if (this.isRequired) { if(this.text && this.text.length) { if(parsedRegex) { return parsedRegex.test(this.text) From 6deec784ca3c31659956929893533381198dab61 Mon Sep 17 00:00:00 2001 From: deepakchethan Date: Tue, 6 Oct 2020 17:57:18 +0530 Subject: [PATCH 4/4] test --- app/client/src/widgets/InputWidget.tsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/client/src/widgets/InputWidget.tsx b/app/client/src/widgets/InputWidget.tsx index e5b77d2a0145..ee2685f9570b 100644 --- a/app/client/src/widgets/InputWidget.tsx +++ b/app/client/src/widgets/InputWidget.tsx @@ -52,16 +52,16 @@ class InputWidget extends BaseWidget { isValid: `{{ function(){ let parsedRegex = null; - if (regex) { + if (this.regex) { // break up the regexp pattern into 4 parts: given regex, regex prefix , regex pattern, regex flags // Example /appsmith/i will be split into ["/appsmith/gi", "/", "appsmith", "gi"] - const regexParts = regex.match(/(\\/?)(.+)\\1([a-z]*)/i); + const regexParts = this.regex.match(/(\\/?)(.+)\\1([a-z]*)/i); if (regexParts === null) { - parsedRegex = new RegExp(regex); + parsedRegex = new RegExp(this.regex); } // if we don't have a regex flags (gmisuy), convert provided string into regexp directly if (regexParts[3] && !/^(?!.*?(.).*?\\1)[gmisuy]+$/.test(regexParts[3])) { - parsedRegex = RegExp(regex); + parsedRegex = RegExp(this.regex); } // if we have a regex flags, use it to form regexp parsedRegex = new RegExp(regexParts[2], regexParts[3]); @@ -69,13 +69,13 @@ class InputWidget extends BaseWidget { if (this.inputType === "EMAIL") { const emailRegex = new RegExp(/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/); return emailRegex.test(this.text); - } + } else if (this.inputType === "NUMBER") { return !isNaN(this.text) } else if (this.isRequired) { if(this.text && this.text.length) { - if(parsedRegex) { + if (parsedRegex) { return parsedRegex.test(this.text) } else { return true; @@ -83,7 +83,7 @@ class InputWidget extends BaseWidget { } else { return false; } - } if(parsedRegex) { + } if (parsedRegex) { return parsedRegex.test(this.text) } else { return true;