π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Make SRP verification attempts atomic
Neeraj Guptacae5642035f1
diff --git a/server/pkg/controller/user/srp.go b/server/pkg/controller/user/srp.goindex 6d1f3ef3d4..f1f0ca8845 100644--- a/server/pkg/controller/user/srp.go+++ b/server/pkg/controller/user/srp.go@@ -282,36 +282,39 @@ func (c *UserController) verifySRPSession(ctx context.Context,if len(srpM1Bytes) != 32 {return nil, ente.NewBadRequestWithMessage(fmt.Sprintf("srpM1 size is %d, expected 32", len(srpM1Bytes)))}- srpSession, err := c.UserAuthRepo.GetSrpSessionEntity(ctx, sessionID)+ srpSession, err := c.UserAuthRepo.ReserveSrpSessionAttempt(ctx, sessionID, 5)if err != nil {- // Do not reveal whether the session exists.- if errors.Is(err, sql.ErrNoRows) {- // Match normal verification timing.- time.Sleep(time.Duration(10+mathRand.Intn(20)) * time.Millisecond)- return nil, stacktrace.Propagate(ente.ErrInvalidPassword, "session not found")+ if !errors.Is(err, sql.ErrNoRows) {+ return nil, stacktrace.Propagate(err, "")}- return nil, stacktrace.Propagate(err, "")+ srpSession, err = c.UserAuthRepo.GetSrpSessionEntity(ctx, sessionID)+ if err != nil {+ // Do not reveal whether the session exists.+ if errors.Is(err, sql.ErrNoRows) {+ // Match normal verification timing.+ time.Sleep(time.Duration(10+mathRand.Intn(20)) * time.Millisecond)+ return nil, stacktrace.Propagate(ente.ErrInvalidPassword, "session not found")+ }+ return nil, stacktrace.Propagate(err, "")+ }+ if srpSession.IsVerified {+ return nil, stacktrace.Propagate(&ente.ApiError{+ Code: "SESSION_ALREADY_VERIFIED",+ HttpStatusCode: http.StatusGone,+ }, "")+ }+ return nil, stacktrace.Propagate(&ente.ApiError{+ Code: "TOO_MANY_WRONG_ATTEMPTS",+ HttpStatusCode: http.StatusGone,+ }, "")}if srpSession.IsFake {- // Match real-session timing and attempt bookkeeping.+ // Match real-session timing.time.Sleep(time.Duration(20+mathRand.Intn(30)) * time.Millisecond)- _ = c.UserAuthRepo.IncrementSrpSessionAttemptCount(ctx, sessionID)return nil, stacktrace.Propagate(ente.ErrInvalidPassword, "fake session verification")}- if srpSession.IsVerified {- return nil, stacktrace.Propagate(&ente.ApiError{- Code: "SESSION_ALREADY_VERIFIED",- HttpStatusCode: http.StatusGone,- }, "")- } else if srpSession.AttemptCount >= 5 {- return nil, stacktrace.Propagate(&ente.ApiError{- Code: "TOO_MANY_WRONG_ATTEMPTS",- HttpStatusCode: http.StatusGone,- }, "")- }-srpParams := srp.GetParams(Srp4096Params)srpVerifierBytes, err := convertStringToBytes(srpVerifier)if err != nil {@@ -336,16 +339,17 @@ func (c *UserController) verifySRPSession(ctx context.Context,srpM2Bytes, err := srpServer.CheckM1(srpM1Bytes)if err != nil {- err2 := c.UserAuthRepo.IncrementSrpSessionAttemptCount(ctx, sessionID)- if err2 != nil {- return nil, stacktrace.Propagate(err2, "")- }return nil, stacktrace.Propagate(ente.ErrInvalidPassword, "failed to verify srp session")- } else {- err2 := c.UserAuthRepo.SetSrpSessionVerified(ctx, sessionID)- if err2 != nil {- return nil, stacktrace.Propagate(err2, "")- }+ }+ claimed, err := c.UserAuthRepo.TrySetSrpSessionVerified(ctx, sessionID)+ if err != nil {+ return nil, stacktrace.Propagate(err, "")+ }+ if !claimed {+ return nil, stacktrace.Propagate(&ente.ApiError{+ Code: "SESSION_ALREADY_VERIFIED",+ HttpStatusCode: http.StatusGone,+ }, "")}srpM2 := convertBytesToString(srpM2Bytes)return &srpM2, nildiff --git a/server/pkg/repo/srp.go b/server/pkg/repo/srp.goindex bf1dce089d..8457a427ef 100644--- a/server/pkg/repo/srp.go+++ b/server/pkg/repo/srp.go@@ -166,14 +166,28 @@ func (repo *UserAuthRepository) GetSrpSessionEntity(ctx context.Context, sessionreturn &result, nil}-func (repo *UserAuthRepository) IncrementSrpSessionAttemptCount(ctx context.Context, sessionID uuid.UUID) error {- _, err := repo.DB.ExecContext(ctx, `UPDATE srp_sessions SET attempt_count = attempt_count + 1 WHERE id = $1`, sessionID)- return stacktrace.Propagate(err, "")+func (repo *UserAuthRepository) ReserveSrpSessionAttempt(ctx context.Context, sessionID uuid.UUID, limit int) (*ente.SRPSessionEntity, error) {+ result := ente.SRPSessionEntity{}+ row := repo.DB.QueryRowContext(ctx, `UPDATE srp_sessions SET attempt_count = attempt_count + 1+ WHERE id = $1 AND has_verified = false AND attempt_count < $2+ RETURNING id, srp_user_id, server_key, srp_a, has_verified, attempt_count, COALESCE(is_fake, false)`, sessionID, limit)+ err := row.Scan(&result.ID, &result.SRPUserID, &result.ServerKey, &result.SRP_A, &result.IsVerified, &result.AttemptCount, &result.IsFake)+ if err != nil {+ return nil, stacktrace.Propagate(err, "")+ }+ return &result, nil}-func (repo *UserAuthRepository) SetSrpSessionVerified(ctx context.Context, sessionID uuid.UUID) error {- _, err := repo.DB.ExecContext(ctx, `UPDATE srp_sessions SET has_verified = true WHERE id = $1`, sessionID)- return stacktrace.Propagate(err, "")+func (repo *UserAuthRepository) TrySetSrpSessionVerified(ctx context.Context, sessionID uuid.UUID) (bool, error) {+ result, err := repo.DB.ExecContext(ctx, `UPDATE srp_sessions SET has_verified = true WHERE id = $1 AND has_verified = false`, sessionID)+ if err != nil {+ return false, stacktrace.Propagate(err, "")+ }+ rowsAffected, err := result.RowsAffected()+ if err != nil {+ return false, stacktrace.Propagate(err, "")+ }+ return rowsAffected == 1, nil}func (repo *UserAuthRepository) CleanupOldFakeSessions(ctx context.Context) (int64, error) {