[MEDIUM] fix(security): prevent rate-limit bypass via X-Forwarded-For header spoofing#79
Open
katnisscalls99 wants to merge 1 commit into
Open
Conversation
…poofing
VULNERABILITY: Both getClientIp() implementations (rate-limiter.js and
middleware.js) blindly trusted the first value in the X-Forwarded-For header.
Because HTTP clients fully control this header when there is no upstream proxy
stripping it, an attacker can rotate it on every request to bypass all
IP-based rate limiting.
Affected endpoints (all rely on IP-based limiting):
- POST /api/auth/send-sms — 5 req/min (OTP spam, SMS cost amplification)
- POST /api/auth/verify-sms — 5 req/min (OTP brute-force)
- POST /api/auth/backup-pin — 10 req/min via middleware (PIN brute-force)
- All /api/* endpoints — 60 req/min via middleware
Attack PoC:
for i in {1..1000}; do
curl -X POST /api/auth/send-sms \
-H "X-Forwarded-For: 1.2.3.$i" \
-d '{"phoneNumber":"+15551234567"}'
done
# Each request sees a different IP → rate limit never triggers.
Fix:
- Introduce TRUSTED_PROXY_COUNT env var (default 0).
- When > 0, read the correct IP from the right-hand side of X-Forwarded-For
(the proxy-appended, unforged position).
- When 0 (direct internet, no trusted proxy), skip X-Forwarded-For entirely
and rely only on X-Real-IP, which nginx/ALB typically sets from the real
connection address after stripping any client-supplied value.
- Add detailed inline documentation to guide operators.
Operators behind Cloudflare / nginx should set TRUSTED_PROXY_COUNT=1 in their
deployment environment.
Severity: MEDIUM — bypasses brute-force / spam protections on auth endpoints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
VULNERABILITY: Both
getClientIp()implementations (rate-limiter.js and middleware.js) blindly trusted the first value in theX-Forwarded-Forheader. Because HTTP clients fully control this header when there is no upstream proxy stripping it, an attacker can rotate it on every request to bypass all IP-based rate limiting.Affected endpoints (all rely on IP-based limiting):
POST /api/auth/send-sms— 5 req/min (OTP spam, SMS cost amplification)POST /api/auth/verify-sms— 5 req/min (OTP brute-force)POST /api/auth/backup-pin— 10 req/min via middleware (PIN brute-force)/api/*endpoints — 60 req/min via middlewareAttack PoC:
Fix:
TRUSTED_PROXY_COUNTenv var (default 0).X-Real-IP, which nginx/ALB typically sets from the real connection address after stripping any client-supplied value.Operators behind Cloudflare / nginx should set
TRUSTED_PROXY_COUNT=1in their deployment environment.Severity: MEDIUM — bypasses brute-force / spam protections on auth endpoints