Skip to content

bug: [Security] IP Spoofing Rate Limit Bypass (Billing DoS) #431

Description

@Vedhant26

Description

There is a critical IP Spoofing / Rate Limit Bypass vulnerability in the application's AI Chat endpoint. This vulnerability allows an attacker to exhaust the server's AI token quota (Gemini, Groq, Mistral), causing a Billing DoS (Denial of Service).

Vulnerability Details:
The rate limiting mechanism in app/api/chat/route.ts relies on getClientIp from lib/api-utils.ts to identify the user's IP.
However, getClientIp unconditionally trusts the X-Forwarded-For HTTP header without verifying if the request came from a trusted proxy:

export function getClientIp(request: Request): string {
    const forwarded = request.headers.get("x-forwarded-for");
    if (forwarded) return forwarded.split(",")[0].trim();
    return "unknown";
}

Exploitation Scenario:

  1. The AI Chat endpoint limits users to 20 requests per minute based on their IP.
  2. An attacker can write a simple script that sends a POST request to /api/chat with a continuously changing, spoofed X-Forwarded-For header (e.g., X-Forwarded-For: 1.2.3.4, then 1.2.3.5).
  3. Since getClientIp extracts the spoofed IP, the rate limiter treats every request as coming from a new user.
  4. The attacker bypasses the 20-request limit entirely and rapidly drains the platform's API credits, costing the repository owner money.

Impact

  • Severity: High / Critical (depending on financial impact)
  • Attack Vector: IP Spoofing
  • Consequences: API Quota Exhaustion / Billing DoS

Proposed Fix

I have updated getClientIp to prioritize the X-Real-IP header, which is set securely by load balancers and reverse proxies (like Vercel) and cannot be easily spoofed by the client. I've prepared a PR with this fix.

Could you please assign this to me, @piyushdotcomm?
Labels to apply: bug, level 3, security, advanced

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions