Skip to content

armgit / ente

Public Imported from GitHub
0

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

[server] RateLimit max sessions in an hour

Neeraj Guptad3d419e12d3d
diff --git a/server/pkg/controller/user/srp.go b/server/pkg/controller/user/srp.go
index b371db2198..9010d6a341 100644
--- a/server/pkg/controller/user/srp.go
+++ b/server/pkg/controller/user/srp.go
@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
+ "fmt"
"github.com/ente-io/museum/ente"
"github.com/ente-io/museum/pkg/utils/auth"
"github.com/ente-io/stacktrace"
@@ -14,7 +15,11 @@ import (
"net/http"
)
-const Srp4096Params = 4096
+const (
+ Srp4096Params = 4096
+ // MaxUnverifiedSessionInAnHour is the number of unverified sessions in the last hour
+ MaxUnverifiedSessionInAnHour = 10
+)
func (c *UserController) SetupSRP(context *gin.Context, userID int64, req ente.SetupSRPRequest) (*ente.SetupSRPResponse, error) {
srpB, sessionID, err := c.createAndInsertSRPSession(context, req.SrpUserID, req.SRPVerifier, req.SRPA)
@@ -160,6 +165,19 @@ func (c *UserController) createAndInsertSRPSession(
srpA string,
) (*string, *uuid.UUID, error) {
+ unverifiedSessions, err := c.UserAuthRepo.GetUnverifiedSessionsInLastHour(srpUserID)
+ if err != nil {
+ return nil, nil, stacktrace.Propagate(err, "")
+ }
+ if unverifiedSessions >= MaxUnverifiedSessionInAnHour {
+ go c.DiscordController.NotifyPotentialAbuse(fmt.Sprintf("Too many unverified sessions for user %s", srpUserID.String()))
+ return nil, nil, stacktrace.Propagate(&ente.ApiError{
+ Code: "TOO_MANY_UNVERIFIED_SESSIONS",
+ HttpStatusCode: http.StatusTooManyRequests,
+ }, "")
+
+ }
+
serverSecret := srp.GenKey()
srpParams := srp.GetParams(Srp4096Params)
srpServer := srp.NewServer(srpParams, convertStringToBytes(srpVerifier), serverSecret)
diff --git a/server/pkg/middleware/rate_limit.go b/server/pkg/middleware/rate_limit.go
index 9bbf056baa..fc268e0bd0 100644
--- a/server/pkg/middleware/rate_limit.go
+++ b/server/pkg/middleware/rate_limit.go
@@ -152,6 +152,7 @@ func (r *RateLimitMiddleware) getLimiter(reqPath string, reqMethod string) *limi
reqPath == "/users/srp/attributes" ||
(reqPath == "/cast/device-info" && reqMethod == "POST") ||
(reqPath == "/cast/device-info/" && reqMethod == "POST") ||
+ reqPath == "/users/srp/create-session" ||
reqPath == "/users/srp/verify-session" ||
reqPath == "/family/invite-info/:token" ||
reqPath == "/family/add-member" ||
diff --git a/server/pkg/repo/srp.go b/server/pkg/repo/srp.go
index 33e5097ac4..9a8480e6ac 100644
--- a/server/pkg/repo/srp.go
+++ b/server/pkg/repo/srp.go
@@ -19,6 +19,12 @@ func (repo *UserAuthRepository) AddSRPSession(srpUserID uuid.UUID, serverKey str
return id, stacktrace.Propagate(err, "")
}
+func (repo *UserAuthRepository) GetUnverifiedSessionsInLastHour(srpUserID uuid.UUID) (int64, error) {
+ var count int64
+ err := repo.DB.QueryRow(`SELECT COUNT(*) FROM srp_sessions WHERE srp_user_id = $1 AND has_verified = false AND created_at > (now_utc_micro_seconds() - (60::BIGINT * 60 * 1000 * 1000))`, srpUserID).Scan(&count)
+ return count, stacktrace.Propagate(err, "")
+}
+
func (repo *UserAuthRepository) GetSRPAuthEntity(ctx context.Context, userID int64) (*ente.SRPAuthEntity, error) {
result := ente.SRPAuthEntity{}
row := repo.DB.QueryRowContext(ctx, `SELECT user_id, srp_user_id, salt, verifier FROM srp_auth WHERE user_id = $1`, userID)