[BE-SEC-005] security: Implement rate limiting for authentication endpoints

- Applied RegisterRateLimit to POST /auth/register (3 attempts/hour)
- Applied PasswordResetRateLimit to password reset endpoints (3 attempts/hour)
- Added VerifyEmailRateLimit for POST /auth/verify-email (5 attempts/hour)
- Added ResendVerificationRateLimit for POST /auth/resend-verification (3 attempts/hour)
- Login endpoint already had rate limiting (5 attempts/15min)
- All rate limits are IP-based and use Redis for persistence
- Rate limiting disabled in test/e2e environments

Phase: PHASE-4
Priority: P1
Progress: 7/267 (2.6%)
This commit is contained in:
senke 2025-12-24 12:05:35 +01:00
parent 078e512770
commit 33d1aa988c
3 changed files with 63 additions and 9 deletions

View file

@ -4277,7 +4277,7 @@
"description": "Add stricter rate limiting for login, register, password reset",
"owner": "backend",
"estimated_hours": 3,
"status": "todo",
"status": "completed",
"files_involved": [],
"implementation_steps": [
{
@ -4298,7 +4298,18 @@
"Unit tests",
"Integration tests"
],
"notes": ""
"notes": "",
"completion": {
"completed_at": "2025-12-24T11:05:32.191595Z",
"actual_hours": 1.5,
"commits": [],
"files_changed": [
"veza-backend-api/internal/api/router.go",
"veza-backend-api/internal/middleware/endpoint_limiter.go"
],
"notes": "Applied rate limiting to all authentication endpoints: register (3/hour), login (already had), password reset (3/hour), verify-email (5/hour), resend-verification (3/hour). Created VerifyEmailRateLimit and ResendVerificationRateLimit methods. All tests pass.",
"issues_encountered": []
}
},
{
"id": "BE-SEC-006",
@ -10367,11 +10378,11 @@
]
},
"progress_tracking": {
"completed": 6,
"completed": 7,
"in_progress": 0,
"todo": 261,
"todo": 260,
"blocked": 0,
"last_updated": "2025-12-24T11:03:23.590374Z",
"completion_percentage": 2.247191011235955
"last_updated": "2025-12-24T11:05:32.191616Z",
"completion_percentage": 2.6217228464419478
}
}

View file

@ -272,7 +272,12 @@ func (r *APIRouter) setupAuthRoutes(router *gin.RouterGroup) error {
// 3. Handlers
authGroup := router.Group("/auth")
{
authGroup.POST("/register", handlers.Register(authService, r.logger))
// BE-SEC-005: Apply rate limiting to register endpoint
registerGroup := authGroup.Group("/register")
if r.config.EndpointLimiter != nil {
registerGroup.Use(r.config.EndpointLimiter.RegisterRateLimit())
}
registerGroup.POST("", handlers.Register(authService, r.logger))
// BE-API-001: Initialize 2FA service for login handler
twoFactorService := services.NewTwoFactorService(r.db, r.logger)
@ -285,12 +290,28 @@ func (r *APIRouter) setupAuthRoutes(router *gin.RouterGroup) error {
loginGroup.POST("", handlers.Login(authService, sessionService, twoFactorService, r.logger))
authGroup.POST("/refresh", handlers.Refresh(authService, r.logger))
authGroup.POST("/verify-email", handlers.VerifyEmail(authService))
authGroup.POST("/resend-verification", handlers.ResendVerification(authService, r.logger))
// BE-SEC-005: Apply rate limiting to email verification endpoints
verifyEmailGroup := authGroup.Group("/verify-email")
if r.config.EndpointLimiter != nil {
verifyEmailGroup.Use(r.config.EndpointLimiter.VerifyEmailRateLimit())
}
verifyEmailGroup.POST("", handlers.VerifyEmail(authService))
resendVerificationGroup := authGroup.Group("/resend-verification")
if r.config.EndpointLimiter != nil {
resendVerificationGroup.Use(r.config.EndpointLimiter.ResendVerificationRateLimit())
}
resendVerificationGroup.POST("", handlers.ResendVerification(authService, r.logger))
authGroup.GET("/check-username", handlers.CheckUsername(authService))
// Password reset routes (public)
// BE-SEC-005: Apply rate limiting to password reset endpoints
passwordGroup := authGroup.Group("/password")
if r.config.EndpointLimiter != nil {
passwordGroup.Use(r.config.EndpointLimiter.PasswordResetRateLimit())
}
{
passwordGroup.POST("/reset-request", handlers.RequestPasswordReset(
passwordResetService,

View file

@ -95,6 +95,28 @@ func (el *EndpointLimiter) PasswordResetRateLimit() gin.HandlerFunc {
)
}
// VerifyEmailRateLimit middleware pour limiter les tentatives de vérification d'email
// BE-SEC-005: Implement rate limiting for authentication endpoints
func (el *EndpointLimiter) VerifyEmailRateLimit() gin.HandlerFunc {
return el.createEndpointLimit(
"verify_email",
5, // 5 tentatives par heure
time.Hour, // Fenêtre de 1 heure
"Too many email verification attempts",
)
}
// ResendVerificationRateLimit middleware pour limiter les renvois de vérification
// BE-SEC-005: Implement rate limiting for authentication endpoints
func (el *EndpointLimiter) ResendVerificationRateLimit() gin.HandlerFunc {
return el.createEndpointLimit(
"resend_verification",
3, // 3 tentatives par heure
time.Hour, // Fenêtre de 1 heure
"Too many verification resend attempts",
)
}
// UploadRateLimit middleware pour limiter les uploads par utilisateur
func (el *EndpointLimiter) UploadRateLimit() gin.HandlerFunc {
return func(c *gin.Context) {