Skip to content

fix(middleware): prioritize request.ip and x-real-ip to prevent rate limit spoofing (#1845)#1866

Open
swarupio wants to merge 6 commits into
JhaSourav07:mainfrom
swarupio:fix/rate-limiting-ip-spoofing
Open

fix(middleware): prioritize request.ip and x-real-ip to prevent rate limit spoofing (#1845)#1866
swarupio wants to merge 6 commits into
JhaSourav07:mainfrom
swarupio:fix/rate-limiting-ip-spoofing

Conversation

@swarupio
Copy link
Copy Markdown
Contributor

Description
Fixes #1845

This PR addresses the IP spoofing vulnerability in the rate-limiting middleware. Previously, the middleware blindly trusted the x-forwarded-for header, which allowed users to bypass the 60 req/min limit by injecting fake IPs.

Changes made:

Refactored middleware.ts to prioritize authoritative, platform-provided headers.

The middleware now attempts to use x-real-ip (set securely by reverse proxies like Vercel/Nginx) as the primary source of truth.

x-forwarded-for has been relegated to a secondary fallback, and is now properly parsed with .trim() to prevent spacing-based spoofing tricks.

Replaced the missing NextRequest .ip property with explicit header extraction to satisfy strict TypeScript (npm run typecheck) requirements.

Screenshot 2026-05-30 234720

Pillar
[ ] 🎨 Pillar 1 — New Theme Design

[ ] 📐 Pillar 2 — Geometric SVG Improvement

[ ] 🕐 Pillar 3 — Timezone Logic Optimization

[x] 🔐 Other (Security, Middleware, Rate Limiting)

Checklist before requesting a review:
[x] I have read the CONTRIBUTING.md file.

[x] I have tested these changes locally.

[x] I have run npm run typecheck and npm run lint locally and resolved all errors.

[x] My commits follow the Conventional Commits format.

[x] I have made sure that I have only one commit to merge in this PR.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 30, 2026

@swarupio is attempting to deploy a commit to the jhasourav07's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the needs-details This PR is missing required description details. label May 30, 2026
@github-actions
Copy link
Copy Markdown

👋 Hey @swarupio, it looks like you didn't use our PR template!

The section ## Description is missing from your PR description.

Please update your PR description to include all required sections so we can review this properly:

  • ## Description — What does this PR do? Which issue does it fix?
  • ## Pillar — Which contribution pillar does this fall under?
  • ## Checklist — Have you ticked off the quality checklist?

You can find the full template in CONTRIBUTING.md. Just edit your PR description and the needs-details label will be removed automatically. 🙌

@github-actions github-actions Bot added the needs-rebase This PR has merge conflicts and needs a rebase. label May 30, 2026
@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

5 similar comments
@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions github-actions Bot added status:blocked This PR is blocked due to a failing CI check. and removed needs-rebase This PR has merge conflicts and needs a rebase. labels May 30, 2026
@github-actions
Copy link
Copy Markdown

🚨 Hey @swarupio, the CI Pipeline is failing on this PR and it has been marked as status:blocked.

Please fix the issues before this can be reviewed. Here's how:

1. Run checks locally before pushing:

npm run format:check   # Check Prettier formatting
npm run lint           # Run ESLint
npm run typecheck      # TypeScript type check
npm run test           # Run unit tests (Vitest)
npm run build          # Verify production build passes

2. Auto-fix common issues:

npm run format         # Auto-fix formatting with Prettier
npm run lint -- --fix  # Auto-fix lint errors where possible

3. Check the full failure log here:
👉 View CI Run

Once you push a fix and the CI passes, the status:blocked label will be removed automatically. 💪

@swarupio swarupio force-pushed the fix/rate-limiting-ip-spoofing branch from d8703ea to 057c3d5 Compare May 30, 2026 18:35
@github-actions github-actions Bot added the needs-rebase This PR has merge conflicts and needs a rebase. label May 30, 2026
@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

1 similar comment
@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions github-actions Bot removed the needs-rebase This PR has merge conflicts and needs a rebase. label May 30, 2026
@swarupio swarupio force-pushed the fix/rate-limiting-ip-spoofing branch from fb9ef0e to 057c3d5 Compare May 30, 2026 18:41
@github-actions github-actions Bot added the needs-rebase This PR has merge conflicts and needs a rebase. label May 30, 2026
@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

1 similar comment
@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions github-actions Bot removed the needs-rebase This PR has merge conflicts and needs a rebase. label May 30, 2026
@swarupio swarupio force-pushed the fix/rate-limiting-ip-spoofing branch from bab1130 to 057c3d5 Compare May 30, 2026 18:44
@github-actions github-actions Bot added the needs-rebase This PR has merge conflicts and needs a rebase. label May 30, 2026
@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions github-actions Bot added the type:bug Something isn't working as expected label May 30, 2026
@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @swarupio, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

Copy link
Copy Markdown
Collaborator

@Aamod007 Aamod007 left a comment

Choose a reason for hiding this comment

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

The IP prioritization change breaks the existing test 'prefers x-forwarded-for over x-real-ip' which expects \1.2.3.4\ but now gets \9.9.9.9. Either update that test to match the new behavior, or reconsider whether x-real-ip should take priority over x-forwarded-for in all cases.

Copy link
Copy Markdown
Collaborator

@Aamod007 Aamod007 left a comment

Choose a reason for hiding this comment

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

Unapproving — this PR has the \status:blocked\ and
eeds-rebase\ labels. Please resolve the blocking issues and re-request review.

Copy link
Copy Markdown
Collaborator

@Aamod007 Aamod007 left a comment

Choose a reason for hiding this comment

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

This PR has \status:blocked\ and
eeds-rebase\ labels. Please resolve before re-requesting.

@swarupio swarupio requested a review from Aamod007 May 30, 2026 19:11
@github-actions github-actions Bot removed the needs-rebase This PR has merge conflicts and needs a rebase. label May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-details This PR is missing required description details. status:blocked This PR is blocked due to a failing CI check. type:bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Rate Limiting Context Loss via IP Spoofing

2 participants