Skip to content

Commit 1ee41fb

Browse files
FEATURE (auth): Add rate limiting for sign in edpoint to not allow brute force
1 parent 529f080 commit 1ee41fb

File tree

6 files changed

+21
-19
lines changed

6 files changed

+21
-19
lines changed

backend/.pre-commit-config.yaml

Lines changed: 0 additions & 15 deletions
This file was deleted.

backend/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/swaggo/gin-swagger v1.6.0
2020
github.com/swaggo/swag v1.16.4
2121
golang.org/x/crypto v0.39.0
22+
golang.org/x/time v0.12.0
2223
gorm.io/driver/postgres v1.5.11
2324
gorm.io/gorm v1.26.1
2425
)

backend/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
252252
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
253253
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
254254
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
255+
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
256+
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
255257
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
256258
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
257259
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=

backend/internal/features/users/controller.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"net/http"
55

66
"github.com/gin-gonic/gin"
7+
"golang.org/x/time/rate"
78
)
89

910
type UserController struct {
10-
userService *UserService
11+
userService *UserService
12+
signinLimiter *rate.Limiter
1113
}
1214

1315
func (c *UserController) RegisterRoutes(router *gin.RouterGroup) {
@@ -51,8 +53,18 @@ func (c *UserController) SignUp(ctx *gin.Context) {
5153
// @Param request body SignInRequest true "User signin data"
5254
// @Success 200 {object} SignInResponse
5355
// @Failure 400
56+
// @Failure 429 {object} map[string]string "Rate limit exceeded"
5457
// @Router /users/signin [post]
5558
func (c *UserController) SignIn(ctx *gin.Context) {
59+
// We use rate limiter to prevent brute force attacks
60+
if !c.signinLimiter.Allow() {
61+
ctx.JSON(
62+
http.StatusTooManyRequests,
63+
gin.H{"error": "Rate limit exceeded. Please try again later."},
64+
)
65+
return
66+
}
67+
5668
var request SignInRequest
5769
if err := ctx.ShouldBindJSON(&request); err != nil {
5870
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request format"})

backend/internal/features/users/di.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package users
22

33
import (
44
user_repositories "postgresus-backend/internal/features/users/repositories"
5+
6+
"golang.org/x/time/rate"
57
)
68

79
var secretKeyRepository = &user_repositories.SecretKeyRepository{}
@@ -12,6 +14,7 @@ var userService = &UserService{
1214
}
1315
var userController = &UserController{
1416
userService,
17+
rate.NewLimiter(rate.Limit(3), 3), // 3 RPS with burst of 3
1518
}
1619

1720
func GetUserService() *UserService {

contribute/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,14 @@ Notifications flow:
8686

8787
Extra:
8888

89-
- add brute force protection on auth (via local RPS limiter) (in progress by Rostislav Dugin)
90-
- create pretty website like rybbit.io with demo
9189
- add HTTPS for Postgresus
9290
- add simple SQL queries via UI
9391
- add support of Kubernetes Helm
92+
- create pretty website like rybbit.io with demo
9493

9594
Monitoring flow:
9695

97-
- add system metrics (CPU, RAM, disk, IO)
96+
- add system metrics (CPU, RAM, disk, IO) (in progress by Rostislav Dugin)
9897
- add queries stats (slowest, most frequent, etc. via pg_stat_statements)
9998
- add alerting for slow queries (listen for slow query and if they reach >100ms - send message)
10099
- add alerting for high resource usage (listen for high resource usage and if they reach >90% - send message)

0 commit comments

Comments
 (0)