Skip to content

Commit

Permalink
fix: check freq on email change (#1090)
Browse files Browse the repository at this point in the history
## What kind of change does this PR introduce?
* Email change requests on `PUT /user` should adhere to the max
frequency rule.
  • Loading branch information
kangmingtay authored Apr 19, 2023
1 parent 7ca755a commit 659ca66
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/api/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ func (a *API) sendMagicLink(tx *storage.Connection, u *models.User, mailer maile
// sendEmailChange sends out an email change token to the new email.
func (a *API) sendEmailChange(tx *storage.Connection, config *conf.GlobalConfiguration, u *models.User, mailer mailer.Mailer, email string, referrerURL string, otpLength int) error {
var err error
if u.EmailChangeSentAt != nil && !u.EmailChangeSentAt.Add(config.SMTP.MaxFrequency).Before(time.Now()) {
return MaxFrequencyLimitError
}
otpNew, err := crypto.GenerateOtp(otpLength)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions internal/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"encoding/json"
"errors"
"net/http"

"github.com/fatih/structs"
Expand Down Expand Up @@ -176,6 +177,9 @@ func (a *API) UserUpdate(w http.ResponseWriter, r *http.Request) error {
mailer := a.Mailer(ctx)
referrer := a.getReferrer(r)
if terr = a.sendEmailChange(tx, config, user, mailer, params.Email, referrer, config.Mailer.OtpLength); terr != nil {
if errors.Is(terr, MaxFrequencyLimitError) {
return tooManyRequestsError("For security purposes, you can only request this once every 60 seconds")
}
return internalServerError("Error sending change email").WithInternalError(terr)
}
}
Expand Down

0 comments on commit 659ca66

Please sign in to comment.