mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-09-23 00:41:36 +00:00
fix(validation): Simplify keyGenerator function in createRouteLimiter
This commit is contained in:
parent
67e14821f7
commit
aa36869e7d
2 changed files with 2 additions and 8 deletions
Binary file not shown.
|
Before Width: | Height: | Size: 435 KiB After Width: | Height: | Size: 30 KiB |
|
|
@ -19,7 +19,7 @@
|
|||
*/
|
||||
|
||||
import path from 'node:path';
|
||||
import rateLimit, { type RateLimitRequestHandler, ipKeyGenerator } from 'express-rate-limit';
|
||||
import rateLimit, { ipKeyGenerator, type RateLimitRequestHandler } from 'express-rate-limit';
|
||||
import type { Request } from 'express';
|
||||
|
||||
/**
|
||||
|
|
@ -138,9 +138,6 @@ export interface RouteLimiterOverrides {
|
|||
* - keyGenerator: req.ip with a socket.remoteAddress fallback so abruptly
|
||||
* closed connections do not trigger ERR_ERL_UNDEFINED_IP_ADDRESS
|
||||
* (which would 500 the request via Express's default error handler).
|
||||
* The IP is passed through `ipKeyGenerator` so IPv6 addresses are
|
||||
* normalised to their /56 subnet — without this, each IPv6 address
|
||||
* gets its own counter and the limit is trivially bypassed (#1360).
|
||||
* Caller must wire `app.set('trust proxy', ...)` correctly — see
|
||||
* createServer in api.ts.
|
||||
*
|
||||
|
|
@ -154,10 +151,7 @@ export function createRouteLimiter(opts?: RouteLimiterOverrides): RateLimitReque
|
|||
standardHeaders: 'draft-7',
|
||||
legacyHeaders: false,
|
||||
passOnStoreError: true,
|
||||
keyGenerator: (req: Request) => {
|
||||
const ip = req.ip ?? req.socket?.remoteAddress;
|
||||
return ip ? ipKeyGenerator(ip) : 'unknown';
|
||||
},
|
||||
keyGenerator: (req: Request) => ipKeyGenerator(req.ip ?? req.socket?.remoteAddress ?? ''),
|
||||
message: { error: 'Too many requests, please try again later.' },
|
||||
...opts,
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue