Skip to content

Commit

Permalink
bugfix:fixing special character issue on email addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni committed Jun 8, 2022
1 parent e6f5f3d commit 18b70eb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion send.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"net/http"
"net/url"
"time"
)

Expand Down Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 18b70eb

Please sign in to comment.