Skip to content

Commit ab150c1

Browse files
authored
Feature/Allow special characters in email regex pattern (#207)
* Updated emailRegexPattern constant * Updated mailfrom/rcptto handlers tests
1 parent d398e98 commit ab150c1

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

consts.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ const (
5353
// Regex patterns
5454
availableCmdsRegexPattern = `(?i)helo|ehlo|mail from:|rcpt to:|data|rset|noop|quit`
5555
domainRegexPattern = `(?i)([\p{L}0-9]+([\-.]{1}[\p{L}0-9]+)*\.\p{L}{2,63}|localhost)`
56-
emailRegexPattern = `(?i)(?:[\p{L}\p{N}\s]*?<?)*?([a-zA-Z0-9][-a-zA-Z0-9.]*[a-zA-Z0-9]@` + domainRegexPattern + `)>*`
56+
localPartChars = `[a-zA-Z0-9.!#$%&'*+\-/=?^_\x60{|}~]`
57+
emailRegexPattern = `(?i)(?:[\p{L}\p{N}\s]*?<?)*?(` + localPartChars + `+(?:\.` + localPartChars + `+)*@` + domainRegexPattern + `)>*`
5758
ipAddressRegexPattern = `(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`
5859
addressLiteralRegexPattern = `|\[` + ipAddressRegexPattern + `\]`
5960

handler_mailfrom_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,26 @@ func TestHandlerMailfromMailfromEmail(t *testing.T) {
260260
assert.Equal(t, emptyString, handler.mailfromEmail("MAIL FROM: "+invalidEmail))
261261
})
262262

263-
t.Run("when request includes invalid email starting with dot", func(t *testing.T) {
264-
invalidEmail := "[email protected]"
263+
t.Run("when request includes email with plus sign", func(t *testing.T) {
264+
email := "[email protected]"
265+
assert.Equal(t, email, handler.mailfromEmail("MAIL FROM: "+email))
266+
})
265267

266-
assert.Equal(t, emptyString, handler.mailfromEmail("MAIL FROM: "+invalidEmail))
268+
t.Run("when request includes email with multiple special characters", func(t *testing.T) {
269+
email := "user.name+tag!#$%@example.com"
270+
assert.Equal(t, email, handler.mailfromEmail("MAIL FROM: "+email))
267271
})
268272

269-
t.Run("when request includes invalid email ending with dot before @", func(t *testing.T) {
270-
invalidEmail := "[email protected]"
273+
t.Run("when request includes email with special characters and angle brackets", func(t *testing.T) {
274+
rawEmail := "[email protected]"
275+
request := "MAIL FROM: <" + rawEmail + ">"
276+
assert.Equal(t, rawEmail, handler.mailfromEmail(request))
277+
})
271278

272-
assert.Equal(t, emptyString, handler.mailfromEmail("MAIL FROM: "+invalidEmail))
279+
t.Run("when request includes email with special characters and display name", func(t *testing.T) {
280+
rawEmail := "[email protected]"
281+
request := "MAIL FROM: Support Team <" + rawEmail + ">"
282+
assert.Equal(t, rawEmail, handler.mailfromEmail(request))
273283
})
274284
}
275285

handler_rcptto_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,18 +359,6 @@ func TestHandlerRcpttoRcpttoEmail(t *testing.T) {
359359

360360
assert.Equal(t, emptyString, handler.rcpttoEmail("RCPT TO: "+invalidEmail))
361361
})
362-
363-
t.Run("when request includes invalid email starting with dot", func(t *testing.T) {
364-
invalidEmail := "[email protected]"
365-
366-
assert.Equal(t, emptyString, handler.rcpttoEmail("RCPT TO: "+invalidEmail))
367-
})
368-
369-
t.Run("when request includes invalid email ending with dot before @", func(t *testing.T) {
370-
invalidEmail := "[email protected]"
371-
372-
assert.Equal(t, emptyString, handler.rcpttoEmail("RCPT TO: "+invalidEmail))
373-
})
374362
}
375363

376364
func TestHandlerRcpttoIsBlacklistedEmail(t *testing.T) {

0 commit comments

Comments
 (0)