Skip to content

Commit 99c6821

Browse files
Merge pull request #215 from AikidoSec/patch-trust-proxy
Add env var for trusting proxy
2 parents 5af69f0 + 09ce580 commit 99c6821

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

docs/proxy.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Proxy settings
2+
3+
We'll automatically use the `x-forwarded-for` header to determine the client's IP address when behind a proxy. If you're publicly exposing your server, you may need to set the `AIKIDO_TRUST_PROXY` env var to `false` to ensure that the correct IP address is used. Otherwise, someone could potentially spoof their IP address and thus bypassing the rate limiting.

library/helpers/getIPAddressFromRequest.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isIP } from "net";
33

44
export function getIPAddressFromRequest(req: IncomingMessage) {
55
if (req.headers) {
6-
if (typeof req.headers["x-forwarded-for"] === "string") {
6+
if (typeof req.headers["x-forwarded-for"] === "string" && trustProxy()) {
77
const xForwardedFor = getClientIpFromXForwardedFor(
88
req.headers["x-forwarded-for"]
99
);
@@ -48,3 +48,16 @@ function getClientIpFromXForwardedFor(value: string) {
4848

4949
return null;
5050
}
51+
52+
function trustProxy() {
53+
if (!process.env.AIKIDO_TRUST_PROXY) {
54+
// Trust proxy by default
55+
// Most of the time, the application is behind a reverse proxy
56+
return true;
57+
}
58+
59+
return (
60+
process.env.AIKIDO_TRUST_PROXY === "1" ||
61+
process.env.AIKIDO_TRUST_PROXY === "true"
62+
);
63+
}

0 commit comments

Comments
 (0)