-
Notifications
You must be signed in to change notification settings - Fork 3k
feat(auth): add login brute-force protection #2723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
koishi514-Z
wants to merge
2
commits into
Tencent:main
Choose a base branch
from
koishi514-Z:fix/issue-2513-login-protection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strconv" | ||
| "sync" | ||
| "time" | ||
|
|
||
| apperrors "github.com/Tencent/WeKnora/internal/errors" | ||
| "github.com/Tencent/WeKnora/internal/ratelimit" | ||
| "github.com/gin-gonic/gin" | ||
| "github.com/redis/go-redis/v9" | ||
| ) | ||
|
|
||
| const loginRateLimitKeyPrefix = "auth:login:ratelimit:" | ||
|
|
||
| var ( | ||
| loginLimiterOnce sync.Once | ||
| loginLimiter *ratelimit.Limiter | ||
| ) | ||
|
|
||
| // loginRateLimiter creates one shared limiter for password logins. Redis makes | ||
| // its sliding window effective across application instances; the limiter's | ||
| // local fallback keeps standalone deployments protected when Redis is absent. | ||
| func loginRateLimiter(redisClient *redis.Client, window time.Duration) *ratelimit.Limiter { | ||
| loginLimiterOnce.Do(func() { | ||
| loginLimiter = ratelimit.New(redisClient, loginRateLimitKeyPrefix, window, "") | ||
| stopCh := make(chan struct{}) | ||
| go loginLimiter.StartCleanup(stopCh) | ||
| }) | ||
| return loginLimiter | ||
| } | ||
|
|
||
| // LoginRateLimit protects POST /auth/login from online password guessing. The | ||
| // key is the resolved client IP; Router's trusted-proxy policy prevents a | ||
| // caller from bypassing the budget by forging X-Forwarded-For. | ||
| func LoginRateLimit(redisClient *redis.Client, max int, window time.Duration) gin.HandlerFunc { | ||
| limiter := loginRateLimiter(redisClient, window) | ||
| retryAfter := int(window.Seconds()) | ||
| if retryAfter < 1 { | ||
| retryAfter = 1 | ||
| } | ||
| return func(c *gin.Context) { | ||
| if !limiter.Allow(c.Request.Context(), c.ClientIP(), max) { | ||
| c.Header("Retry-After", strconv.Itoa(retryAfter)) | ||
| c.Error(&apperrors.AppError{ | ||
| Code: apperrors.ErrTooManyRequests, | ||
| Message: "too many login attempts; please retry later", | ||
| HTTPCode: http.StatusTooManyRequests, | ||
| }) | ||
| c.Abort() | ||
| return | ||
| } | ||
| c.Next() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestLoginRateLimitRejectsRequestsOverBudget(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| // Use a distinct max/window for this test. The package singleton is created | ||
| // only once, so this test owns the first construction in this package. | ||
| r := gin.New() | ||
| r.Use(ErrorHandler()) | ||
| r.POST("/login", LoginRateLimit(nil, 2, time.Hour), func(c *gin.Context) { | ||
| c.Status(http.StatusNoContent) | ||
| }) | ||
|
|
||
| for i := 0; i < 2; i++ { | ||
| w := httptest.NewRecorder() | ||
| r.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/login", nil)) | ||
| require.Equal(t, http.StatusNoContent, w.Code) | ||
| } | ||
|
|
||
| w := httptest.NewRecorder() | ||
| r.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/login", nil)) | ||
| require.Equal(t, http.StatusTooManyRequests, w.Code) | ||
| require.Equal(t, "3600", w.Header().Get("Retry-After")) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这会不会导致公司网络使用同一个出口IP情况下集体被封禁。或者有恶意用户刷接口导致集体429