From 9ff83075d66702656df9ae59d779db19107470f4 Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Wed, 14 Jun 2023 20:08:34 +0530 Subject: [PATCH] Improve IP Ban (#9512) Co-authored-by: Leo Giovanetti --- apps/web/middleware.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index f9842960e0..5cb9c038be 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -11,6 +11,12 @@ const middleware: NextMiddleware = async (req) => { const url = req.nextUrl; const requestHeaders = new Headers(req.headers); + if (isIpInBanlist(req) && url.pathname !== "/api/nope") { + // DDOS Prevention: Immediately end request with no response - Avoids a redirect as well initiated by NextAuth on invalid callback + req.nextUrl.pathname = "/api/nope"; + return NextResponse.redirect(req.nextUrl); + } + if (!url.pathname.startsWith("/api")) { // // NOTE: When tRPC hits an error a 500 is returned, when this is received @@ -79,6 +85,7 @@ const middleware: NextMiddleware = async (req) => { export const config = { matcher: [ + "/:path*", "/api/collect-events/:path*", "/api/auth/:path*", "/apps/routing_forms/:path*",