π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Enforce atomic TOTP attempt limits
Neeraj Guptac4d52c761637
diff --git a/server/pkg/controller/user/twofactor.go b/server/pkg/controller/user/twofactor.goindex 0b6d49ad01..0a6afda949 100644--- a/server/pkg/controller/user/twofactor.go+++ b/server/pkg/controller/user/twofactor.go@@ -102,17 +102,6 @@ func (c *UserController) VerifyTwoFactor(context *gin.Context, sessionID string,if err != nil {return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(err, "")}- wrongAttempt, err := c.TwoFactorRepo.GetWrongAttempts(sessionID)- if err != nil {- return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(err, "")- }-- if wrongAttempt >= 10 {- msg := fmt.Sprintf("Too many wrong two-factor verification attempts for userID: %d", userID)- go c.DiscordController.NotifyPotentialAbuse(msg)- return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(ente.ErrTooManyBadRequest, "Too many wrong attempts, please request a new verification session")- }-isTwoFactorEnabled, err := c.UserRepo.IsTwoFactorEnabled(userID)if err != nil {return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(err, "")@@ -125,11 +114,17 @@ func (c *UserController) VerifyTwoFactor(context *gin.Context, sessionID string,if err != nil {return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(err, "")}+ reserved, err := c.TwoFactorRepo.ReserveTwoFactorAttempt(sessionID)+ if err != nil {+ return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(err, "")+ }+ if !reserved {+ msg := fmt.Sprintf("Too many wrong two-factor verification attempts for userID: %d", userID)+ go c.DiscordController.NotifyPotentialAbuse(msg)+ return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(ente.ErrTooManyBadRequest, "Too many wrong attempts, please request a new verification session")+ }valid := totp.Validate(otp, secret)if !valid {- if err = c.TwoFactorRepo.RecordWrongAttempt(sessionID); err != nil {- log.WithError(err).Warn("Failed to track wrong attempt for two-factor session")- }return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(ente.ErrIncorrectTOTP, "")}@@ -145,10 +140,6 @@ func (c *UserController) VerifyTwoFactor(context *gin.Context, sessionID string,msg := fmt.Sprintf("Replay attack detected for userID: %d - OTP code reused", userID)log.Warn(msg)go c.DiscordController.NotifyPotentialAbuse(msg)-- if err = c.TwoFactorRepo.RecordWrongAttempt(sessionID); err != nil {- log.WithError(err).Warn("Failed to track wrong attempt for two-factor session")- }return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(ente.ErrIncorrectTOTP, "OTP code has already been used")}diff --git a/server/pkg/repo/twofactor.go b/server/pkg/repo/twofactor.goindex 7731d9de37..cb13eec955 100644--- a/server/pkg/repo/twofactor.go+++ b/server/pkg/repo/twofactor.go@@ -114,23 +114,17 @@ func (repo *TwoFactorRepository) RemoveExpiredTempTwoFactorSecrets() error {return stacktrace.Propagate(err, "")}-func (repo *TwoFactorRepository) GetWrongAttempts(sessionID string) (int, error) {- row := repo.DB.QueryRow(`SELECT wrong_attempt FROM two_factor_sessions WHERE session_id = $1`,- sessionID)- var wrongAttempt int- if err := row.Scan(&wrongAttempt); err != nil {- return 0, stacktrace.Propagate(err, "Failed to scan row")+func (repo *TwoFactorRepository) ReserveTwoFactorAttempt(sessionID string) (bool, error) {+ result, err := repo.DB.Exec(`UPDATE two_factor_sessions SET wrong_attempt = wrong_attempt + 1+ WHERE session_id = $1 AND expiration_time > $2 AND wrong_attempt < 10`, sessionID, time.Microseconds())+ if err != nil {+ return false, stacktrace.Propagate(err, "Failed to reserve two-factor attempt")}- return wrongAttempt, nil-}--func (repo *TwoFactorRepository) RecordWrongAttempt(sessionID string) error {- _, err := repo.DB.Exec(`UPDATE two_factor_sessions SET wrong_attempt = wrong_attempt + 1- WHERE session_id = $1`, sessionID)+ rowsAffected, err := result.RowsAffected()if err != nil {- return stacktrace.Propagate(err, "Failed to update wrong attempt count")+ return false, stacktrace.Propagate(err, "Failed to get rows affected")}- return nil+ return rowsAffected == 1, nil}// TryRecordUsedOTPCode atomically tries to record an OTP code as used