From 6abd58ee211b5c63298a202254b41a9ab00a9fa3 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Wed, 3 Sep 2025 20:43:15 +0200 Subject: [PATCH] Update index.ts --- backend/src/lib/ip/index.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/backend/src/lib/ip/index.ts b/backend/src/lib/ip/index.ts index 11aaf3b64..9b594a583 100644 --- a/backend/src/lib/ip/index.ts +++ b/backend/src/lib/ip/index.ts @@ -1,5 +1,7 @@ import net from "node:net"; +import RE2 from "re2"; + import { ForbiddenRequestError } from "../errors"; export enum IPType { @@ -7,6 +9,8 @@ export enum IPType { IPV6 = "ipv6" } +const PORT_REGEX = new RE2(/^\d+$/); + /** * Strips port from IP address if present. * Handles both IPv4 (e.g. 1.2.3.4:1234) and IPv6 (e.g. [2001:db8::1]:8080) formats. @@ -19,14 +23,14 @@ const stripPort = (ip: string): { ipAddress: string } => { if (endBracketIndex === -1) return { ipAddress: ip }; const ipPart = ip.slice(1, endBracketIndex); const portPart = ip.slice(endBracketIndex + 2); - if (!portPart || !/^\d+$/.test(portPart)) return { ipAddress: ip }; + if (!portPart || !PORT_REGEX.test(portPart)) return { ipAddress: ip }; return { ipAddress: ipPart }; } // Handle IPv4 with port (e.g. 1.2.3.4:1234) if (ip.includes(":")) { const [ipPart, portPart] = ip.split(":"); - if (!portPart || !/^\d+$/.test(portPart)) return { ipAddress: ip }; + if (!portPart || !PORT_REGEX.test(portPart)) return { ipAddress: ip }; return { ipAddress: ipPart }; }