-
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: only increment login rate limit on failed attempts #386
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
Changes from all commits
288af4c
4d95c92
dc77b57
6c8c2bf
b7a1865
2eb9893
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,29 +13,42 @@ const WINDOW_SECONDS = 15 * 60; // 15 minutes | |
| const LOGIN_MAX_ATTEMPTS = 5; | ||
| const FORGOT_PASSWORD_MAX_ATTEMPTS = 5; | ||
|
|
||
| async function checkRateLimit( | ||
| key: string, | ||
| maxAttempts: number, | ||
| ): Promise<boolean> { | ||
| async function recordAttempt(key: string): Promise<number> { | ||
| const redis = getClient(); | ||
| const results = await redis | ||
| .multi() | ||
| .incr(key) | ||
| .expire(key, WINDOW_SECONDS) | ||
| .exec(); | ||
| const count = results && results[0] ? (results[0][1] as number) : 0; | ||
| return count <= maxAttempts; | ||
| return results && results[0] ? (results[0][1] as number) : 0; | ||
| } | ||
|
|
||
| export async function checkLoginRateLimit(ip: string): Promise<boolean> { | ||
| return checkRateLimit(`rate_limit:login:${ip}`, LOGIN_MAX_ATTEMPTS); | ||
| export async function checkLoginAttempt(ip: string): Promise<boolean> { | ||
| const count = await recordAttempt(`rate_limit:login:${ip}`); | ||
| return count <= LOGIN_MAX_ATTEMPTS; | ||
| } | ||
|
|
||
|
Comment on lines
+26
to
30
|
||
| export async function checkForgotPasswordRateLimit( | ||
| ip: string, | ||
| ): Promise<boolean> { | ||
| return checkRateLimit( | ||
| `rate_limit:forgot_password:${ip}`, | ||
| FORGOT_PASSWORD_MAX_ATTEMPTS, | ||
| ); | ||
| export async function rollbackLoginAttempt(ip: string): Promise<void> { | ||
| const redis = getClient(); | ||
| const key = `rate_limit:login:${ip}`; | ||
|
|
||
| const results = await redis | ||
| .multi() | ||
| .decr(key) | ||
| .expire(key, WINDOW_SECONDS) | ||
| .exec(); | ||
|
|
||
| const newCount = | ||
| results && results[0] && Array.isArray(results[0]) | ||
| ? (results[0][1] as number) | ||
| : 0; | ||
|
|
||
| if (newCount <= 0) { | ||
|
Comment on lines
+40
to
+46
|
||
| await redis.del(key); | ||
| } | ||
| } | ||
|
|
||
| export async function checkForgotPasswordAttempt(ip: string): Promise<boolean> { | ||
| const count = await recordAttempt(`rate_limit:forgot_password:${ip}`); | ||
| return count <= FORGOT_PASSWORD_MAX_ATTEMPTS; | ||
|
Comment on lines
+51
to
+53
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.