Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Make email OTT verification attempts atomic

Neeraj Guptad7797ba424dd
diff --git a/server/pkg/controller/user/userauth.go b/server/pkg/controller/user/userauth.go
index 877abac8c1..d87f33972b 100644
--- a/server/pkg/controller/user/userauth.go
+++ b/server/pkg/controller/user/userauth.go
@@ -267,21 +267,15 @@ func (c *UserController) verifyEmailOtt(context *gin.Context, email string, ott
if err != nil {
return stacktrace.Propagate(err, "")
}
- wrongAttempt, err := c.UserAuthRepo.GetMaxWrongAttempts(emailHash, app)
+ otts, limited, err := c.UserAuthRepo.ReserveOTTVerificationAttempt(emailHash, app, OTTWrongAttemptLimit)
if err != nil {
return stacktrace.Propagate(err, "")
}
-
- if wrongAttempt >= OTTWrongAttemptLimit {
+ if limited {
msg := fmt.Sprintf("Too many wrong ott verification attemp for app %s", app)
go c.DiscordController.NotifyPotentialAbuse(msg)
return stacktrace.Propagate(ente.ErrTooManyBadRequest, "User needs to wait before active ott are expired")
}
-
- otts, err := c.UserAuthRepo.GetValidOTTs(emailHash, app)
- if err != nil {
- return stacktrace.Propagate(err, "")
- }
if len(otts) < 1 {
return stacktrace.Propagate(ente.ErrExpiredOTT, "")
}
@@ -292,9 +286,6 @@ func (c *UserController) verifyEmailOtt(context *gin.Context, email string, ott
}
}
if !isValidOTT {
- if err = c.UserAuthRepo.RecordWrongAttemptForActiveOtt(emailHash, app); err != nil {
- log.WithError(err).Warn("Failed to track wrong attempt")
- }
return stacktrace.Propagate(ente.ErrIncorrectOTT, "")
}
removed, err := c.UserAuthRepo.RemoveOTT(emailHash, ott, app)
diff --git a/server/pkg/repo/userauth.go b/server/pkg/repo/userauth.go
index 0d4930dee3..406a574c2f 100644
--- a/server/pkg/repo/userauth.go
+++ b/server/pkg/repo/userauth.go
@@ -111,25 +111,48 @@ func (repo *UserAuthRepository) GetValidOTTs(emailHash string, app ente.App) ([]
return otts, nil
}
-func (repo *UserAuthRepository) GetMaxWrongAttempts(emailHash string, app ente.App) (int, error) {
- row := repo.DB.QueryRow(`SELECT COALESCE(MAX(wrong_attempt),0) FROM otts WHERE email_hash = $1 AND expiration_time > $2 AND app = $3`,
- emailHash, time.Microseconds(), app)
- var wrongAttempt int
- if err := row.Scan(&wrongAttempt); err != nil {
- return 0, stacktrace.Propagate(err, "Failed to scan row")
+func (repo *UserAuthRepository) ReserveOTTVerificationAttempt(emailHash string, app ente.App, limit int) ([]string, bool, error) {
+ tx, err := repo.DB.Begin()
+ if err != nil {
+ return nil, false, stacktrace.Propagate(err, "")
}
- return wrongAttempt, nil
-}
+ defer tx.Rollback()
-// RecordWrongAttemptForActiveOtt increases the wrong_attempt count for given emailHash and active ott.
-// Assuming tha we keep deleting expired OTT, max(wrong_attempt) can be used to track brute-force attack
-func (repo *UserAuthRepository) RecordWrongAttemptForActiveOtt(emailHash string, app ente.App) error {
- _, err := repo.DB.Exec(`UPDATE otts SET wrong_attempt = otts.wrong_attempt + 1
- WHERE email_hash = $1 AND expiration_time > $2 AND app=$3`, emailHash, time.Microseconds(), app)
+ rows, err := tx.Query(`SELECT ott, wrong_attempt FROM otts
+ WHERE email_hash = $1 AND app = $2 AND expiration_time > $3
+ ORDER BY ott FOR UPDATE`, emailHash, app, time.Microseconds())
if err != nil {
- return stacktrace.Propagate(err, "Failed to update wrong attempt count")
+ return nil, false, stacktrace.Propagate(err, "")
+ }
+ defer rows.Close()
+
+ otts := make([]string, 0)
+ limited := false
+ for rows.Next() {
+ var ott string
+ var wrongAttempt int
+ if err := rows.Scan(&ott, &wrongAttempt); err != nil {
+ return nil, false, stacktrace.Propagate(err, "")
+ }
+ otts = append(otts, ott)
+ limited = limited || wrongAttempt >= limit
+ }
+ if err := rows.Err(); err != nil {
+ return nil, false, stacktrace.Propagate(err, "")
+ }
+ if limited || len(otts) == 0 {
+ return otts, limited, nil
+ }
+
+ _, err = tx.Exec(`UPDATE otts SET wrong_attempt = wrong_attempt + 1
+ WHERE email_hash = $1 AND app = $2 AND ott = ANY($3)`, emailHash, app, pq.Array(otts))
+ if err != nil {
+ return nil, false, stacktrace.Propagate(err, "")
+ }
+ if err := tx.Commit(); err != nil {
+ return nil, false, stacktrace.Propagate(err, "")
}
- return nil
+ return otts, false, nil
}
func (repo *UserAuthRepository) AddToken(userID int64, app ente.App, token string, ip string, userAgent string) error {