Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validateEmail should normalise emails #1790

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/api/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"net/http"
"strings"
"time"

"github.com/didip/tollbooth/v5"
Expand Down Expand Up @@ -559,7 +560,7 @@ func (a *API) validateEmail(email string) (string, error) {
return "", badRequestError(ErrorCodeValidationFailed, "Unable to validate email address: "+err.Error())
}

return email, nil
return strings.ToLower(email), nil
}

func validateSentWithinFrequencyLimit(sentAt *time.Time, frequency time.Duration) error {
Expand Down
43 changes: 43 additions & 0 deletions internal/api/mail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,49 @@ func (ts *MailTestSuite) SetupTest() {
require.NoError(ts.T(), ts.API.db.Create(u), "Error saving new user")
}

func (ts *MailTestSuite) TestValidateEmail() {
cases := []struct {
desc string
email string
expectedEmail string
expectedError error
}{
{
desc: "valid email",
email: "test@example.com",
expectedEmail: "test@example.com",
expectedError: nil,
},
{
desc: "email should be normalized",
email: "TEST@EXAMPLE.COM",
expectedEmail: "test@example.com",
expectedError: nil,
},
{
desc: "empty email should return error",
email: "",
expectedEmail: "",
expectedError: badRequestError(ErrorCodeValidationFailed, "An email address is required"),
},
{
desc: "email length exceeds 255 characters",
// email has 256 characters
email: "testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest@example.com",
expectedEmail: "",
expectedError: badRequestError(ErrorCodeValidationFailed, "An email address is too long"),
},
}

for _, c := range cases {
ts.Run(c.desc, func() {
email, err := ts.API.validateEmail(c.email)
require.Equal(ts.T(), c.expectedError, err)
require.Equal(ts.T(), c.expectedEmail, email)
})
}
}

func (ts *MailTestSuite) TestGenerateLink() {
// create admin jwt
claims := &AccessTokenClaims{
Expand Down
14 changes: 14 additions & 0 deletions internal/api/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -900,6 +901,19 @@ func (ts *VerifyTestSuite) TestVerifyValidOtp() {
tokenHash: crypto.GenerateTokenHash(u.GetEmail(), "123456"),
},
},
{
desc: "Valid Email OTP (email casing shouldn't matter)",
sentTime: time.Now(),
body: map[string]interface{}{
"type": mail.EmailOTPVerification,
"token": "123456",
"email": strings.ToUpper(u.GetEmail()),
},
expected: expected{
code: http.StatusOK,
tokenHash: crypto.GenerateTokenHash(u.GetEmail(), "123456"),
},
},
{
desc: "Valid Email Change OTP",
sentTime: time.Now(),
Expand Down
Loading