Skip to content

fix(security): Rate limiting bypass via X-Forwarded-For spoofing (#181) - #213

Closed
vybe wants to merge 0 commit into
mainfrom
fix/181-xff-rate-limit-bypass
Closed

vybe wants to merge 0 commit into
mainfrom
fix/181-xff-rate-limit-bypass

Conversation

@vybe

@vybe vybe commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Root cause: _get_client_ip trusted the client-supplied X-Forwarded-For header unconditionally, allowing an attacker to rotate spoofed IPs and bypass the per-IP rate limit entirely (pentest finding 3.2.4, CVSS 5.1)
  • Fix: _get_client_ip now validates whether the direct TCP peer is a trusted proxy (RFC-1918 ranges by default) before reading X-Real-IP/X-Forwarded-For; public-IP connections use their raw TCP IP as the rate-limit key
  • Defence-in-depth: Added a per-token secondary rate limit (60 msg/min per public link) so IP-rotating flood attacks are capped even if a custom proxy config is misconfigured

Changes

File Change
src/backend/routers/public.py Fixed _get_client_ip; added trusted-proxy allowlist; added per-token rate limit
src/backend/db/public_links.py Added count_recent_messages_by_token()
src/backend/database.py Exposed new method on Database facade
tests/test_ip_rate_limit_fix.py 14 unit tests (all passing)

Test plan

  • pytest tests/test_ip_rate_limit_fix.py -v — 14/14 pass
  • Pentest PoC scenario covered: 35 requests with rotating X-Forwarded-For: 10.0.0.$i all resolve to the same real IP
  • Trusted-proxy matching verified for Docker bridge (172.16/12), 192.168/16, 10/8, loopback
  • TRUSTED_PROXIES env var override tested
  • Manual smoke test: start Trinity, send chat via public link through nginx, confirm rate limiting works

Risk

Low for default Docker Compose deployments (all traffic via nginx in RFC-1918). Medium for deployments behind an external CDN/proxy — operators must set TRUSTED_PROXIES=<cidr>.

Closes #181

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SEC: Rate Limiting Bypass with X-Forwarded-For Header Spoofing

1 participant