Ipv6 rate limit key error#46
Conversation
Co-authored-by: me <me@kentcdodds.com>
Co-authored-by: me <me@kentcdodds.com>
Co-authored-by: me <me@kentcdodds.com>
Co-authored-by: me <me@kentcdodds.com>
Co-authored-by: me <me@kentcdodds.com>
Co-authored-by: me <me@kentcdodds.com>
|
Cursor Agent can help with this pull request. Just |
| req.ip ?? | ||
| req.socket.remoteAddress ?? | ||
| '0.0.0.0' | ||
| return ipKeyGenerator(clientIp) |
There was a problem hiding this comment.
Potential incorrect argument type passed to ipKeyGenerator
High Severity
The ipKeyGenerator function from express-rate-limit is a KeyGenerator type that typically expects a Request object as its parameter, not a string. The code calls ipKeyGenerator(clientIp) where clientIp is a string. If this function expects Request, it would attempt to access clientIp.ip (undefined on a string), potentially causing runtime errors or the same ERR_ERL_KEY_GEN_IPV6 error the PR aims to fix. The test plan only initializes the rate limiter without making actual requests, so this wouldn't be caught during testing.
Additional Locations (1)
There was a problem hiding this comment.
In express-rate-limit v8.2.1, ipKeyGenerator is defined as ipKeyGenerator(ip: string, ipv6Subnet?: number | false), so passing the clientIp string is the correct usage.


Fixes
ERR_ERL_KEY_GEN_IPV6by usingipKeyGeneratorto correctly normalize client IP addresses forexpress-rate-limit. This ensures proper handling of IPv6 addresses and prevents validation errors in production logs. Also updates documentation examples for consistency.Test Plan
express-rate-limitinitialization:node -e "import rateLimit, { ipKeyGenerator } from 'express-rate-limit'; import express from 'express'; const app = express(); const rateLimitDefault = { windowMs: 60 * 1000, max: 500, standardHeaders: true, legacyHeaders: false, validate: { trustProxy: false }, keyGenerator: (req) => { const clientIp = req.get('fly-client-ip') ?? req.ip ?? req.socket.remoteAddress ?? '0.0.0.0'; return ipKeyGenerator(clientIp); }, }; app.use(rateLimit(rateLimitDefault)); console.log('rate limit initialized');"ERR_ERL_KEY_GEN_IPV6error.Checklist
Screenshots
N/A
Note
Low Risk
Small, localized change to rate-limit key generation and documentation; primary risk is unintended rate-limit bucketing changes for some clients/proxies.
Overview
Fixes
express-rate-limitkey generation to correctly handle IPv6 by importing and usingipKeyGenerator, and by providing a more robust client IP fallback chain (fly-client-ip→req.ip→socket.remoteAddress).Updates
epic-securitydocumentation examples to use the sameipKeyGeneratorapproach (and to prefer API keys when present) so guidance matches runtime behavior.Written by Cursor Bugbot for commit b785721. This will update automatically on new commits. Configure here.