From 18b70eb2eb8ca806c2e3804139ef9737664b3754 Mon Sep 17 00:00:00 2001 From: Antonio Pagano Date: Wed, 8 Jun 2022 15:34:17 -0500 Subject: [PATCH] bugfix:fixing special character issue on email addresses --- send.go | 3 ++- send_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/send.go b/send.go index c3fd2c0..a1b8940 100644 --- a/send.go +++ b/send.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "net/http" + "net/url" "time" ) @@ -34,7 +35,7 @@ func (h *handler) send(w http.ResponseWriter, r *http.Request) { return } - loginLink := fmt.Sprintf("%v?token=%v&email=%v", h.validatePath(), tt, user.EmailAddress()) + loginLink := fmt.Sprintf("%v?token=%v&email=%v", h.validatePath(), tt, url.QueryEscape(user.EmailAddress())) mm, err := h.composeMessage(user, loginLink) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/send_test.go b/send_test.go index 5bc15d4..b5f3ac6 100644 --- a/send_test.go +++ b/send_test.go @@ -136,6 +136,38 @@ func TestSend(t *testing.T) { testhelpers.Contains(tt, string(m.Bodies[0].Content), "http://127.0.0.1:8080/auth/validate") }) + t.Run("User found email encoded", func(tt *testing.T) { + email := "mailo+2@wawand.co" + finder := func(token string) (maildoor.Emailable, error) { + return testUser(email), nil + } + var m maildoor.Message + sender := func(message *maildoor.Message) error { + m = *message + return nil + } + + h, err := maildoor.NewWithOptions("secret", maildoor.UseFinder(finder), maildoor.UseSender(sender)) + testhelpers.NoError(t, err) + + token, err := maildoor.GenerateJWT(time.Second*10, []byte("secret")) + testhelpers.NoError(t, err) + + req := httptest.NewRequest(http.MethodPost, "/auth/send/", nil) + req.Form = url.Values{ + "CSRFToken": []string{token}, + "email": []string{email}, + } + + w := httptest.NewRecorder() + + h.ServeHTTP(w, req) + testhelpers.Equals(tt, http.StatusOK, w.Code) + testhelpers.Equals(tt, "mailo+2@wawand.co", m.To) + testhelpers.Contains(tt, string(m.Bodies[0].Content), "http://127.0.0.1:8080/auth/validate") + testhelpers.Contains(tt, string(m.Bodies[0].Content), fmt.Sprintf("email=%v", url.QueryEscape(email))) + }) + t.Run("User found error sending", func(tt *testing.T) { finder := func(token string) (maildoor.Emailable, error) { return testUser("mailo@wawand.co"), nil