Skip to content

armgit / ente

Public Imported from GitHub
0

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

[server] Track wrong attempts for 2fa session verification

Neeraj Guptaf10b40facce5
diff --git a/server/pkg/controller/user/twofactor.go b/server/pkg/controller/user/twofactor.go
index bace007b22..142bb6ef18 100644
--- a/server/pkg/controller/user/twofactor.go
+++ b/server/pkg/controller/user/twofactor.go
@@ -3,6 +3,7 @@ package user
import (
"bytes"
"encoding/base64"
+ "fmt"
"image/png"
"github.com/ente-io/museum/pkg/utils/network"
@@ -14,6 +15,7 @@ import (
"github.com/ente-io/stacktrace"
"github.com/gin-gonic/gin"
"github.com/pquerna/otp/totp"
+ log "github.com/sirupsen/logrus"
)
// SetupTwoFactor generates a two factor secret and sends it to user to setup his authenticator app with
@@ -101,6 +103,17 @@ 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, "")
@@ -114,6 +127,9 @@ func (c *UserController) VerifyTwoFactor(context *gin.Context, sessionID string,
}
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, "")
}
response, err := c.GetKeyAttributeAndToken(context, userID)
diff --git a/server/pkg/repo/twofactor.go b/server/pkg/repo/twofactor.go
index fdbe228a18..1a2e1774ee 100644
--- a/server/pkg/repo/twofactor.go
+++ b/server/pkg/repo/twofactor.go
@@ -124,3 +124,25 @@ func (repo *TwoFactorRepository) RemoveExpiredTempTwoFactorSecrets() error {
time.Microseconds())
return stacktrace.Propagate(err, "")
}
+
+// GetWrongAttempts returns the wrong attempt count for the given two factor session
+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")
+ }
+ return wrongAttempt, nil
+}
+
+// RecordWrongAttempt increases the wrong_attempt count for the given two factor session.
+// This is used to track and prevent brute-force attacks on two-factor verification
+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)
+ if err != nil {
+ return stacktrace.Propagate(err, "Failed to update wrong attempt count")
+ }
+ return nil
+}