Skip to content

Commit 7499910

Browse files
[FIX] refactor error messages and FQDN regex usage
1 parent 4c7fd1e commit 7499910

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

mail/internal/email/email.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ func NewFromReader(r io.Reader) (*Email, error) {
127127

128128
contentType, _, err = mime.ParseMediaType(p.header.Get("Content-Type"))
129129
if err != nil {
130-
return msg, apperror.NewErrorf("Could not parse Content-Type header with value %q", p.header.Get("Content-Type")).AddError(err)
130+
return msg, apperror.NewErrorf("could not parse Content-Type header with value %q", p.header.Get("Content-Type")).AddError(err)
131131
}
132132

133133
if cd := p.header.Get("Content-Disposition"); cd != "" {
134134
cd, params, err := mime.ParseMediaType(p.header.Get("Content-Disposition"))
135135
if err != nil {
136-
return msg, apperror.NewErrorf("Could not parse Content-Disposition header with value %q", p.header.Get("Content-Disposition")).AddError(err)
136+
return msg, apperror.NewErrorf("could not parse Content-Disposition header with value %q", p.header.Get("Content-Disposition")).AddError(err)
137137
}
138138
filename, filenameDefined := params["filename"]
139139
if cd == "attachment" || (cd == "inline" && filenameDefined) {
@@ -728,7 +728,7 @@ func parseMIMEParts(hs textproto.MIMEHeader, b io.Reader) ([]*part, error) {
728728
}
729729
ct, params, err := mime.ParseMediaType(hs.Get("Content-Type"))
730730
if err != nil {
731-
return ps, apperror.NewErrorf("Could not parse Content-Type header with value %q", hs.Get("Content-Type")).AddError(err)
731+
return ps, apperror.NewErrorf("could not parse Content-Type header with value %q", hs.Get("Content-Type")).AddError(err)
732732
}
733733
if strings.HasPrefix(ct, "multipart/") {
734734
if _, ok := params["boundary"]; !ok {
@@ -749,7 +749,7 @@ func parseMIMEParts(hs textproto.MIMEHeader, b io.Reader) ([]*part, error) {
749749
}
750750
subct, _, err := mime.ParseMediaType(p.Header.Get("Content-Type"))
751751
if err != nil {
752-
return ps, apperror.NewErrorf("Could not parse Content-Type header with value %q", p.Header.Get("Content-Type")).AddError(err)
752+
return ps, apperror.NewErrorf("could not parse Content-Type header with value %q", p.Header.Get("Content-Type")).AddError(err)
753753
}
754754
if strings.HasPrefix(subct, "multipart/") {
755755
sps, err := parseMIMEParts(p.Header, p)

mail/security.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import (
1212
"golang.org/x/time/rate"
1313
)
1414

15-
var fqdnPattern = regexp.MustCompile(`^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{1,}$`)
15+
// FQDNPattern validates a fully qualified domain name (FQDN) with the following rules:
16+
// - One or more labels separated by dots.
17+
// - Each label is 1-63 characters: alphanumeric and hyphens, but cannot start or end with a hyphen.
18+
// - The final label (TLD) must be alphabetic and at least one character.
19+
var FQDNPattern = regexp.MustCompile(`^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{1,}$`)
1620

1721
// SecurityManager handles security validations and tracking for both SMTP client and server
1822
type SecurityManager struct {
@@ -368,7 +372,7 @@ func (sm *SecurityManager) isValidFQDN(hostname string) bool {
368372
}
369373

370374
// Validate format using regex
371-
return fqdnPattern.MatchString(hostname)
375+
return FQDNPattern.MatchString(hostname)
372376
}
373377

374378
// cleanup periodically cleans up old entries

0 commit comments

Comments
 (0)