From 9df2bd01bdb009f280e4ed1543aac2583da712e4 Mon Sep 17 00:00:00 2001 From: Xeronith Date: Wed, 12 Jul 2023 09:55:07 +0330 Subject: [PATCH] feat(project): :safety_vest: improve validation --- app/commands/reset_password.go | 2 +- app/validators/email_validator.go | 8 ++------ app/validators/username_validator.go | 8 ++------ app/validators/webfinger_validator.go | 8 ++------ components/constants/regex.go | 11 ----------- components/constants/regexp.go | 12 ++++++++++++ 6 files changed, 19 insertions(+), 30 deletions(-) delete mode 100644 components/constants/regex.go create mode 100644 components/constants/regexp.go diff --git a/app/commands/reset_password.go b/app/commands/reset_password.go index 89f0a57..1813383 100644 --- a/app/commands/reset_password.go +++ b/app/commands/reset_password.go @@ -7,7 +7,7 @@ import ( ) func ResetPassword(x IDispatcher, usernameOrEmail string) (IResetPasswordResult, error) { - isEmail := x.MatchString(EMAIL, usernameOrEmail) + isEmail := REGEXP_EMAIL.MatchString(usernameOrEmail) if !isEmail && !UsernameIsValid(usernameOrEmail) { return nil, ERROR_INVALID_PARAMETERS } diff --git a/app/validators/email_validator.go b/app/validators/email_validator.go index a6f993a..683280b 100644 --- a/app/validators/email_validator.go +++ b/app/validators/email_validator.go @@ -1,11 +1,7 @@ package validators -import ( - "regexp" - - . "github.com/reiver/greatape/components/constants" -) +import . "github.com/reiver/greatape/components/constants" func EmailIsValid(input string) bool { - return regexp.MustCompile(EMAIL).MatchString(input) + return REGEXP_EMAIL.MatchString(input) } diff --git a/app/validators/username_validator.go b/app/validators/username_validator.go index 2d2dd84..068deb7 100644 --- a/app/validators/username_validator.go +++ b/app/validators/username_validator.go @@ -1,13 +1,9 @@ package validators -import ( - "regexp" - - . "github.com/reiver/greatape/components/constants" -) +import . "github.com/reiver/greatape/components/constants" func UsernameIsValid(username string) bool { - if !regexp.MustCompile(USERNAME).MatchString(username) { + if !REGEXP_USERNAME.MatchString(username) { return false } diff --git a/app/validators/webfinger_validator.go b/app/validators/webfinger_validator.go index 252cd63..cdc9dcb 100644 --- a/app/validators/webfinger_validator.go +++ b/app/validators/webfinger_validator.go @@ -1,11 +1,7 @@ package validators -import ( - "regexp" - - . "github.com/reiver/greatape/components/constants" -) +import . "github.com/reiver/greatape/components/constants" func WebfingerIsValid(webfinger string) bool { - return regexp.MustCompile(WEBFINGER).MatchString(webfinger) + return REGEXP_WEBFINGER.MatchString(webfinger) } diff --git a/components/constants/regex.go b/components/constants/regex.go deleted file mode 100644 index 43b8ffa..0000000 --- a/components/constants/regex.go +++ /dev/null @@ -1,11 +0,0 @@ -package constants - -// noinspection GoSnakeCaseUsage, GoUnusedConst -const ( - USERNAME = "^[a-z0-9_\\.]{5,16}$" - PASSWORD = "^.{6,}$" - PHONE_NUMBER = "^9\\d{9}$" - URL = "^(?:(?:https?|ftp):\\/\\/)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?$" - EMAIL = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$" - WEBFINGER = "^acct:[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$" -) diff --git a/components/constants/regexp.go b/components/constants/regexp.go new file mode 100644 index 0000000..c07233b --- /dev/null +++ b/components/constants/regexp.go @@ -0,0 +1,12 @@ +package constants + +import "regexp" + +var ( + REGEXP_USERNAME = regexp.MustCompile(`^[a-z0-9_\.]{5,16}$`) + REGEXP_PASSWORD = regexp.MustCompile(`^.{6,}$`) + REGEXP_PHONE_NUMBER = regexp.MustCompile(`^9\d{9}$`) + REGEXP_URL = regexp.MustCompile(`^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/|\/|\/\/)?[A-z0-9_-]*?[:]?[A-z0-9_-]*?[@]?[A-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$`) + REGEXP_EMAIL = regexp.MustCompile(`^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$`) + REGEXP_WEBFINGER = regexp.MustCompile(`^acct:[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$`) +)