From b4baf35ed8a14b58f9db6dde6a33b36d6095bcc1 Mon Sep 17 00:00:00 2001 From: mhsanaei Date: Thu, 5 Sep 2024 15:16:15 +0200 Subject: [PATCH] Update Email Validation --- web/service/inbound.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/web/service/inbound.go b/web/service/inbound.go index fa0ecf310..cbfa831ac 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -2,7 +2,9 @@ package service import ( "encoding/json" + "errors" "fmt" + "regexp" "strconv" "strings" "time" @@ -411,6 +413,12 @@ func (s *InboundService) AddInboundClient(data *model.Inbound) (bool, error) { return false, err } + email := clients[0].Email + valid, err := validateEmail(email) + if !valid { + return false, err + } + var settings map[string]interface{} err = json.Unmarshal([]byte(data.Settings), &settings) if err != nil { @@ -601,6 +609,12 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, clientId strin return false, err } + email := clients[0].Email + valid, err := validateEmail(email) + if !valid { + return false, err + } + var settings map[string]interface{} err = json.Unmarshal([]byte(data.Settings), &settings) if err != nil { @@ -2007,3 +2021,20 @@ func (s *InboundService) MigrateDB() { func (s *InboundService) GetOnlineClients() []string { return p.GetOnlineClients() } + +func validateEmail(email string) (bool, error) { + if strings.Contains(email, " ") { + return false, errors.New("email contains spaces, please remove them") + } + + if email != strings.ToLower(email) { + return false, errors.New("email contains uppercase letters, please convert to lowercase") + } + + emailPattern := `^[a-z0-9._-]+$` + if !regexp.MustCompile(emailPattern).MatchString(email) { + return false, errors.New("email contains invalid characters, please use only lowercase letters, digits, dots, dashes, and underscores") + } + + return true, nil +}