Skip to content

fix(security): resolve IP extraction bypass flaw in rate limiter#1789

Open
ArshVermaGit wants to merge 1 commit into
Priyanshu-byte-coder:mainfrom
ArshVermaGit:main_3
Open

fix(security): resolve IP extraction bypass flaw in rate limiter#1789
ArshVermaGit wants to merge 1 commit into
Priyanshu-byte-coder:mainfrom
ArshVermaGit:main_3

Conversation

@ArshVermaGit
Copy link
Copy Markdown
Contributor

Description

This PR addresses a critical flaw in the IP extraction logic within the contact rate limiter (src/lib/contact-rate-limit.ts).

Previously, the logic used an improper short-circuit evaluation:
`(req.headers.get("x-forwarded-for") || req.headers.get("x-real-ip") || "127.0.0.1") ?? ...`
Because "127.0.0.1" is a truthy string, this expression would always evaluate to 127.0.0.1 if an x-forwarded-for header was missing, entirely bypassing the extraction of x-real-ip and making the rate limiter trivial to spoof or bypass. It also failed to correctly extract the originating client IP when x-forwarded-for contained a comma-separated list of proxies.

Resolved Issue

Resolves #1788

Changes Made

  • Robust IP Extraction: Rewrote getContactClientIp to properly prioritize the IP resolution chain:
    1. req.ip (Standard NextRequest IP, cast safely to resolve TS typing issues)
    2. First address in x-forwarded-for (properly split and trimmed)
    3. x-real-ip
    4. Fallback cleanly to "unknown" instead of hardcoding localhost.
  • Type Safety: Ensured that the fallback logic appeases the TypeScript compiler for Next.js 15+ environments where NextRequest typing for .ip can be strict/missing.

Impact

  • Security: The contact endpoint is now properly protected against IP spoofing and rate-limit bypasses. Malicious users can no longer circumvent the limiter by manipulating headers.

Testing

  • Ran the rate limit unit test suite (npm run test test/contact-rate-limit.test.ts).
  • Verified that tests previously failing due to incorrect parsing now pass with a 100% success rate.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 31, 2026

@ArshVermaGit is attempting to deploy a commit to the PRIYANSHU DOSHI's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added gssoc26 GSSoC 2026 contribution type:bug GSSoC type bonus: bug fix type:security GSSoC type bonus: security (+20 pts) labels May 31, 2026
@github-actions
Copy link
Copy Markdown

GSSoC Label Checklist 🏷️

@Priyanshu-byte-coder — please apply the appropriate labels before merging:

Difficulty (pick one):

  • level:beginner — 20 pts
  • level:intermediate — 35 pts
  • level:advanced — 55 pts
  • level:critical — 80 pts

Quality (optional):

  • quality:clean — ×1.2 multiplier
  • quality:exceptional — ×1.5 multiplier

Validation (required to score):

  • gssoc:approved — counts for points
  • gssoc:invalid / gssoc:spam / gssoc:ai-slop — does not score

Type labels (type:*) are auto-detected from files and title. Review and adjust if needed.
Points formula: (difficulty × quality_multiplier) + type_bonus

Copy link
Copy Markdown
Contributor Author

@ArshVermaGit ArshVermaGit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Priyanshu-byte-coder ! Issue #1788 has been resolved. Please review the PR and merge it under GSSoC. Thanks!

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

Labels

gssoc26 GSSoC 2026 contribution type:bug GSSoC type bonus: bug fix type:security GSSoC type bonus: security (+20 pts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: IP extraction logic in rate limiters is flawed and easily bypassable

1 participant