Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fix vuln in replay attack

Neeraj Gupta021b30c6b34d
diff --git a/server/pkg/controller/user/twofactor.go b/server/pkg/controller/user/twofactor.go
index 66883569e2..3695e7ebcf 100644
--- a/server/pkg/controller/user/twofactor.go
+++ b/server/pkg/controller/user/twofactor.go
@@ -124,26 +124,6 @@ func (c *UserController) VerifyTwoFactor(context *gin.Context, sessionID string,
return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(ente.ErrBadRequest, "")
}
- // Check for OTP replay attack
- hashData := fmt.Sprintf("%d:%s", userID, otp)
- hash := sha256.Sum256([]byte(hashData))
- otpHash := hex.EncodeToString(hash[:])
-
- isUsed, err := c.TwoFactorRepo.IsOTPCodeUsed(userID, otpHash)
- if err != nil {
- return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(err, "Failed to check OTP usage")
- }
- if isUsed {
- msg := fmt.Sprintf("Potential 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")
- }
-
secret, err := c.TwoFactorRepo.GetTwoFactorSecret(userID)
if err != nil {
return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(err, "")
@@ -156,9 +136,25 @@ func (c *UserController) VerifyTwoFactor(context *gin.Context, sessionID string,
return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(ente.ErrIncorrectTOTP, "")
}
- // Record OTP as used
- if err = c.TwoFactorRepo.RecordUsedOTPCode(userID, otpHash); err != nil {
+ // Try to record OTP atomically - this will fail if already used
+ hashData := fmt.Sprintf("%d:%s", userID, otp)
+ hash := sha256.Sum256([]byte(hashData))
+ otpHash := hex.EncodeToString(hash[:])
+
+ wasNew, err := c.TwoFactorRepo.TryRecordUsedOTPCode(userID, otpHash)
+ if err != nil {
log.WithError(err).Error("Failed to record used OTP code")
+ // Continue anyway to not break authentication
+ } else if !wasNew {
+ // Code was already used - replay attack
+ 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")
}
response, err := c.GetKeyAttributeAndToken(context, userID)
diff --git a/server/pkg/repo/twofactor.go b/server/pkg/repo/twofactor.go
index c4b4e6b5da..0d8de90574 100644
--- a/server/pkg/repo/twofactor.go
+++ b/server/pkg/repo/twofactor.go
@@ -147,29 +147,22 @@ func (repo *TwoFactorRepository) RecordWrongAttempt(sessionID string) error {
return nil
}
-// IsOTPCodeUsed checks if an OTP code has already been used
-func (repo *TwoFactorRepository) IsOTPCodeUsed(userID int64, codeHash string) (bool, error) {
- var exists bool
- row := repo.DB.QueryRow(`SELECT EXISTS(
- SELECT 1 FROM two_factor_used_codes
- WHERE user_id = $1 AND code_hash = $2
- )`, userID, codeHash)
- err := row.Scan(&exists)
+// TryRecordUsedOTPCode atomically tries to record an OTP code as used
+// Returns true if the code was newly recorded, false if it already existed (replay attack)
+func (repo *TwoFactorRepository) TryRecordUsedOTPCode(userID int64, codeHash string) (bool, error) {
+ result, err := repo.DB.Exec(`INSERT INTO two_factor_used_codes(user_id, code_hash, used_at)
+ VALUES($1, $2, $3) ON CONFLICT (user_id, code_hash) DO NOTHING`,
+ userID, codeHash, time.Microseconds())
if err != nil {
- return false, stacktrace.Propagate(err, "Failed to check if OTP code is used")
+ return false, stacktrace.Propagate(err, "Failed to record used OTP code")
}
- return exists, nil
-}
-// RecordUsedOTPCode records that an OTP code has been used
-func (repo *TwoFactorRepository) RecordUsedOTPCode(userID int64, codeHash string) error {
- _, err := repo.DB.Exec(`INSERT INTO two_factor_used_codes(user_id, code_hash, used_at)
- VALUES($1, $2, $3) ON CONFLICT (user_id, code_hash) DO NOTHING`,
- userID, codeHash, time.Microseconds())
+ rowsAffected, err := result.RowsAffected()
if err != nil {
- return stacktrace.Propagate(err, "Failed to record used OTP code")
+ return false, stacktrace.Propagate(err, "Failed to get rows affected")
}
- return nil
+
+ return rowsAffected > 0, nil
}
// RemoveExpiredUsedOTPCodes removes OTP codes older than the specified duration