From a3f7ef4ebb9c998c2fce7f6485073311cfc45c7a Mon Sep 17 00:00:00 2001 From: Zachary Belford Date: Thu, 5 Dec 2024 10:10:04 -0800 Subject: [PATCH] fix: dont count localhost for ratelimit --- src/middlewares/rateLimit.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/middlewares/rateLimit.ts b/src/middlewares/rateLimit.ts index 324337af..a5b422de 100644 --- a/src/middlewares/rateLimit.ts +++ b/src/middlewares/rateLimit.ts @@ -27,6 +27,17 @@ async function checkRequest(ip: string, request: RpcRequest): Promise { return await requestersList.isRequestOkay(ip, request.method, request.params) } +function isInternalRequest(ip: string): boolean { + // Check for localhost and internal IP patterns + return ( + ip === 'localhost' || + ip === '127.0.0.1' || + ip.startsWith('127.') || + ip === '0.0.0.0' || + ip === '::1' + ) +} + export async function rateLimitMiddleware(req: Request, res: Response, next: NextFunction) { if (!config.rateLimit) { next() @@ -38,6 +49,12 @@ export async function rateLimitMiddleware(req: Request, res: Response, next: Nex ip = ip.substring(7) } + // Skip rate limiting for internal requests + if (isInternalRequest(ip)) { + next() + return + } + const requests: RpcRequest[] = Array.isArray(req.body) ? req.body : [req.body] try {