Skip to content

Commit d8e4505

Browse files
committed
refactor: update rateLimiter to check try
This changes adds a new method called `.canTry` to the rate limiter to check if there are tokens remaining in the bucket. It also adds suggestions from @oxy to make sure the user can brute force past the rate limiter.
1 parent a8719e1 commit d8e4505

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/node/routes/login.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ export class RateLimiter {
1717
private readonly minuteLimiter = new Limiter(2, "minute")
1818
private readonly hourLimiter = new Limiter(12, "hour")
1919

20+
public canTry(): boolean {
21+
return this.minuteLimiter.getTokensRemaining() > 0 || this.hourLimiter.getTokensRemaining() > 0
22+
}
23+
2024
public try(): boolean {
21-
if (this.minuteLimiter.tryRemoveTokens(1)) {
22-
return true
25+
if (this.canTry()) {
26+
return this.minuteLimiter.tryRemoveTokens(1) || this.hourLimiter.tryRemoveTokens(1)
2327
}
24-
return this.hourLimiter.tryRemoveTokens(1)
28+
return false
2529
}
2630
}
2731

@@ -59,6 +63,11 @@ router.get("/", async (req, res) => {
5963

6064
router.post("/", async (req, res) => {
6165
try {
66+
// Check to see if they exceeded their login attempts
67+
if (!limiter.canTry()) {
68+
throw new Error("Login rate limited!")
69+
}
70+
6271
if (!req.body.password) {
6372
throw new Error("Missing password")
6473
}

0 commit comments

Comments
 (0)