Skip to content

Commit 8cde814

Browse files
Merge pull request #213 from AikidoSec/patch-ip
Add IP address when rate limited
2 parents f55f008 + dc317aa commit 8cde814

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

library/sources/express/shouldRateLimitRequest.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@ import { Agent } from "../../agent/Agent";
22
import { Context } from "../../agent/Context";
33
import { tryParseURL } from "../../helpers/tryParseURL";
44

5-
export function shouldRateLimitRequest(context: Context, agent: Agent) {
5+
type Result =
6+
| {
7+
block: false;
8+
}
9+
| {
10+
block: true;
11+
trigger: "ip";
12+
}
13+
| {
14+
block: true;
15+
trigger: "user";
16+
};
17+
18+
export function shouldRateLimitRequest(context: Context, agent: Agent): Result {
619
const rateLimiting = getRateLimitingForContext(context, agent);
720

821
if (!rateLimiting) {
9-
return false;
22+
return { block: false };
1023
}
1124

1225
const { config, route } = rateLimiting;
@@ -25,7 +38,7 @@ export function shouldRateLimitRequest(context: Context, agent: Agent) {
2538
context.consumedRateLimitForIP = true;
2639

2740
if (!allowed) {
28-
return true;
41+
return { block: true, trigger: "ip" };
2942
}
3043
}
3144

@@ -43,11 +56,11 @@ export function shouldRateLimitRequest(context: Context, agent: Agent) {
4356
context.consumedRateLimitForUser = true;
4457

4558
if (!allowed) {
46-
return true;
59+
return { block: true, trigger: "user" };
4760
}
4861
}
4962

50-
return false;
63+
return { block: false };
5164
}
5265

5366
function getRateLimitingForContext(context: Context, agent: Agent) {

library/sources/express/wrapRequestHandler.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@ export function wrapRequestHandler(
2929
return res.status(403).send("You are blocked by Aikido runtime.");
3030
}
3131

32-
const shouldRateLimit = shouldRateLimitRequest(context, agent);
32+
const result = shouldRateLimitRequest(context, agent);
3333

34-
if (shouldRateLimit) {
35-
return res.status(429).send("You are rate limited by Aikido runtime.");
34+
if (result.block) {
35+
let message = "You are rate limited by Aikido runtime.";
36+
if (result.trigger === "ip") {
37+
message += ` (Your IP: ${context.remoteAddress})`;
38+
}
39+
40+
return res.status(429).send(message);
3641
}
3742

3843
return handler(req, res, next);

0 commit comments

Comments
 (0)