fix(rate-limit): wrap IP fallback with ipKeyGenerator for IPv6 safety#3452
fix(rate-limit): wrap IP fallback with ipKeyGenerator for IPv6 safety#3452PierreBrisorgueil merged 2 commits intomasterfrom
Conversation
Node startup was crashing with ERR_ERL_KEY_GEN_IPV6 on auth routes because the custom keyGenerator used req.ip directly. express-rate-limit v7+ refuses raw IPs in custom key generators to prevent IPv6 users from bypassing limits by rotating through their /64 prefix. Import the ipKeyGenerator helper and wrap only the IP fallback; userId keying stays prioritized for authenticated requests. Refs #3450
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 22 minutes and 43 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughUpdated the rate limiter middleware to import and use the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3452 +/- ##
=======================================
Coverage 85.58% 85.58%
=======================================
Files 113 113
Lines 2878 2879 +1
Branches 796 797 +1
=======================================
+ Hits 2463 2464 +1
Misses 329 329
Partials 86 86 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@lib/middlewares/rateLimiter.js`:
- Line 31: Add a JSDoc header for the fallback inline key generator assigned to
keyGenerator (or extract it as a named function like fallbackKeyGenerator) that
documents the parameter and return type: include a one-line description, `@param`
{Request} req (or appropriate type) describing the request object, and `@returns`
{string} describing the returned key string; update the assignment to use the
named function or leave the inline function but place the JSDoc immediately
above it, and reference ipKeyGenerator(req.ip) inside the implementation as
currently used.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: a055254d-031b-482d-a125-4962824543c1
📒 Files selected for processing (2)
lib/middlewares/rateLimiter.jslib/middlewares/tests/rateLimiter.unit.tests.js
There was a problem hiding this comment.
Pull request overview
This PR updates the centralized rate-limiter middleware to normalize the IP-based fallback key using express-rate-limit’s ipKeyGenerator, addressing IPv6 safety requirements introduced in newer express-rate-limit versions and preventing startup validation failures on unauthenticated auth routes.
Changes:
- Updated the default limiter
keyGeneratorto preferreq.user._idand otherwise useipKeyGenerator(req.ip). - Updated unit tests to mock
ipKeyGeneratorand assert the normalized IP fallback behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
lib/middlewares/rateLimiter.js |
Imports ipKeyGenerator and wraps the IP fallback in the default keyGenerator to satisfy IPv6-safe keying expectations. |
lib/middlewares/tests/rateLimiter.unit.tests.js |
Extends the express-rate-limit mock with ipKeyGenerator and updates assertions for the new fallback key format. |
Summary
ERR_ERL_KEY_GEN_IPV6thrown by express-rate-limit v7+ when loading auth routes (login/register).ipKeyGeneratorhelper and wraps only the IP fallback in the centralized limiter's custom keyGenerator. User-id keying stays prioritized.ipKeyGeneratorand adjusts the two IP-fallback assertions.Why
On unauthenticated routes (
/auth/login,/auth/register) there is noreq.user, so the limiter always fell back to rawreq.ip. express-rate-limit v7 refuses this because raw IPv6 addresses need normalization viaipKeyGenerator— otherwise an IPv6 user can rotate their /64 prefix to bypass the limit.Changes
lib/middlewares/rateLimiter.js— importipKeyGenerator, wrapreq.ipfallback.lib/middlewares/tests/rateLimiter.unit.tests.js— mock exportsipKeyGenerator, updated two fallback tests.Test plan
npm run lintcleannpm run test:coverage— 751/751 passing, thresholds heldrateLimiter.unit.tests.js— 7/7 passingCloses #3450
Ref: https://express-rate-limit.github.io/ERR_ERL_KEY_GEN_IPV6/
Summary by CodeRabbit
Bug Fixes