Skip to content

Commit

Permalink
Merge pull request #215 from AikidoSec/patch-trust-proxy
Browse files Browse the repository at this point in the history
Add env var for trusting proxy
  • Loading branch information
willem-delbare authored May 30, 2024
2 parents 5af69f0 + 09ce580 commit 99c6821
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/proxy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Proxy settings

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.
15 changes: 14 additions & 1 deletion library/helpers/getIPAddressFromRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isIP } from "net";

export function getIPAddressFromRequest(req: IncomingMessage) {
if (req.headers) {
if (typeof req.headers["x-forwarded-for"] === "string") {
if (typeof req.headers["x-forwarded-for"] === "string" && trustProxy()) {
const xForwardedFor = getClientIpFromXForwardedFor(
req.headers["x-forwarded-for"]
);
Expand Down Expand Up @@ -48,3 +48,16 @@ function getClientIpFromXForwardedFor(value: string) {

return null;
}

function trustProxy() {
if (!process.env.AIKIDO_TRUST_PROXY) {
// Trust proxy by default
// Most of the time, the application is behind a reverse proxy
return true;
}

return (
process.env.AIKIDO_TRUST_PROXY === "1" ||
process.env.AIKIDO_TRUST_PROXY === "true"
);
}

0 comments on commit 99c6821

Please sign in to comment.