Skip to content

Commit

Permalink
Bugfix/Invalid email address parsing for MAILFROM, RCPTTO commands (#185
Browse files Browse the repository at this point in the history
)

* Updated emailRegexPattern
* Updated mailfrom/rcptto handlers tests
  • Loading branch information
bestwebua authored Nov 14, 2024
1 parent ae9f6d6 commit 0a11be2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.3.2] - 2024-11-14

### Fixed

- Fixed issue with [invalid email address parsing](https://github.com/mocktools/go-smtp-mock/issues/135) for `MAIL FROM` and `RCPT TO` commands

## [2.3.1] - 2024-08-04

### Updated
Expand Down
2 changes: 1 addition & 1 deletion consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const (
// Regex patterns
availableCmdsRegexPattern = `(?i)helo|ehlo|mail from:|rcpt to:|data|rset|noop|quit`
domainRegexPattern = `(?i)([\p{L}0-9]+([\-.]{1}[\p{L}0-9]+)*\.\p{L}{2,63}|localhost)`
emailRegexPattern = `(?i)<?((.+)@` + domainRegexPattern + `)>?`
emailRegexPattern = `(?i)<?([\p{L}0-9][\p{L}0-9\-.]*[\p{L}0-9]+@` + domainRegexPattern + `)>?`
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}`
addressLiteralRegexPattern = `|\[` + ipAddressRegexPattern + `\]`

Expand Down
18 changes: 18 additions & 0 deletions handler_mailfrom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,24 @@ func TestHandlerMailfromMailfromEmail(t *testing.T) {

assert.Equal(t, emptyString, handler.mailfromEmail("MAIL FROM: "+invalidEmail))
})

t.Run("when request includes invalid email with multiple @ symbols", func(t *testing.T) {
invalidEmail := "[email protected]@b.com"

assert.Equal(t, emptyString, handler.mailfromEmail("MAIL FROM: "+invalidEmail))
})

t.Run("when request includes invalid email starting with dot", func(t *testing.T) {
invalidEmail := "[email protected]"

assert.Equal(t, emptyString, handler.mailfromEmail("MAIL FROM: "+invalidEmail))
})

t.Run("when request includes invalid email ending with dot before @", func(t *testing.T) {
invalidEmail := "[email protected]"

assert.Equal(t, emptyString, handler.mailfromEmail("MAIL FROM: "+invalidEmail))
})
}

func TestHandlerHeloIsBlacklistedEmail(t *testing.T) {
Expand Down
18 changes: 18 additions & 0 deletions handler_rcptto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,24 @@ func TestHandlerRcpttoRcpttoEmail(t *testing.T) {

assert.Equal(t, emptyString, handler.rcpttoEmail("RCPT TO: "+invalidEmail))
})

t.Run("when request includes invalid email address, wrong format", func(t *testing.T) {
invalidEmail := "[email protected]@b.com"

assert.Equal(t, emptyString, handler.rcpttoEmail("RCPT TO: "+invalidEmail))
})

t.Run("when request includes invalid email starting with dot", func(t *testing.T) {
invalidEmail := "[email protected]"

assert.Equal(t, emptyString, handler.rcpttoEmail("RCPT TO: "+invalidEmail))
})

t.Run("when request includes invalid email ending with dot before @", func(t *testing.T) {
invalidEmail := "[email protected]"

assert.Equal(t, emptyString, handler.rcpttoEmail("RCPT TO: "+invalidEmail))
})
}

func TestHandlerRcpttoIsBlacklistedEmail(t *testing.T) {
Expand Down

0 comments on commit 0a11be2

Please sign in to comment.