Skip to content

Commit 9b780fd

Browse files
authored
feat: add Email Length validation (#430)
1 parent a11fb69 commit 9b780fd

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

helpers/mail/mail_v3.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,20 @@ package mail
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"log"
67
"net/mail"
8+
"strings"
9+
)
10+
11+
const (
12+
// RFC 3696 ( https://tools.ietf.org/html/rfc3696#section-3 )
13+
// The domain part (after the "@") must not exceed 255 characters
14+
maxEmailDomainLength = 255
15+
// The "local part" (before the "@") must not exceed 64 characters
16+
maxEmailLocalLength = 64
17+
// Max email length must not exceed 320 characters.
18+
maxEmailLength = maxEmailDomainLength + maxEmailLocalLength + 1
719
)
820

921
// SGMailV3 contains mail struct
@@ -725,5 +737,21 @@ func ParseEmail(emailInfo string) (*Email, error) {
725737
if err != nil {
726738
return nil, err
727739
}
740+
741+
if len(e.Address) > maxEmailLength {
742+
return nil, fmt.Errorf("Invalid email length. Total length should not exceed %d characters.", maxEmailLength)
743+
}
744+
745+
parts := strings.Split(e.Address, "@")
746+
local, domain := parts[0], parts[1]
747+
748+
if len(domain) > maxEmailDomainLength {
749+
return nil, fmt.Errorf("Invalid email length. Domain length should not exceed %d characters.", maxEmailDomainLength)
750+
}
751+
752+
if len(local) > maxEmailLocalLength {
753+
return nil, fmt.Errorf("Invalid email length. Local part length should not exceed %d characters.", maxEmailLocalLength)
754+
}
755+
728756
return NewEmail(e.Name, e.Address), nil
729757
}

helpers/mail/mail_v3_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,3 +814,25 @@ func TestParseInvalidEmail(t *testing.T) {
814814
t.Error("Expected an error to be thrown from ParseEmail")
815815
}
816816
}
817+
818+
func TestParseInvalidEmailLength(t *testing.T) {
819+
_, err := ParseEmail("example example <[email protected]>")
820+
if err != nil {
821+
t.Error("ParseEmail should have been parsed successfully")
822+
}
823+
824+
_, err = ParseEmail("example example <exampleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee@example.com>")
825+
if err == nil {
826+
t.Error("Expected an error to be thrown from ParseEmail")
827+
}
828+
829+
_, err = ParseEmail("example example <example@exampleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.com>")
830+
if err == nil {
831+
t.Error("Expected an error to be thrown from ParseEmail")
832+
}
833+
834+
_, err = ParseEmail("example example <exampleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee@exampleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.com>")
835+
if err == nil {
836+
t.Error("Expected an error to be thrown from ParseEmail")
837+
}
838+
}

0 commit comments

Comments
 (0)