Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Updates IP Library to fix #3517

Daniel Waghorna7f33d669fc6
diff --git a/backend/src/lib/ip/index.test.ts b/backend/src/lib/ip/index.test.ts
new file mode 100644
index 0000000000..5f3b64facd
--- /dev/null
+++ b/backend/src/lib/ip/index.test.ts
@@ -0,0 +1,121 @@
+import { extractIPDetails, IPType, isValidCidr, isValidIp, isValidIpOrCidr } from "./index";
+
+describe("IP Validation", () => {
+ describe("isValidIp", () => {
+ test("should validate IPv4 addresses with ports", () => {
+ expect(isValidIp("192.168.1.1:8080")).toBe(true);
+ expect(isValidIp("10.0.0.1:1234")).toBe(true);
+ expect(isValidIp("172.16.0.1:80")).toBe(true);
+ });
+
+ test("should validate IPv6 addresses with ports", () => {
+ expect(isValidIp("[2001:db8::1]:8080")).toBe(true);
+ expect(isValidIp("[fe80::1ff:fe23:4567:890a]:1234")).toBe(true);
+ expect(isValidIp("[::1]:80")).toBe(true);
+ });
+
+ test("should validate regular IPv4 addresses", () => {
+ expect(isValidIp("192.168.1.1")).toBe(true);
+ expect(isValidIp("10.0.0.1")).toBe(true);
+ expect(isValidIp("172.16.0.1")).toBe(true);
+ });
+
+ test("should validate regular IPv6 addresses", () => {
+ expect(isValidIp("2001:db8::1")).toBe(true);
+ expect(isValidIp("fe80::1ff:fe23:4567:890a")).toBe(true);
+ expect(isValidIp("::1")).toBe(true);
+ });
+
+ test("should reject invalid IP addresses", () => {
+ expect(isValidIp("256.256.256.256")).toBe(false);
+ expect(isValidIp("192.168.1")).toBe(false);
+ expect(isValidIp("192.168.1.1.1")).toBe(false);
+ expect(isValidIp("2001:db8::1::1")).toBe(false);
+ expect(isValidIp("invalid")).toBe(false);
+ });
+
+ test("should reject malformed IP addresses with ports", () => {
+ expect(isValidIp("192.168.1.1:")).toBe(false);
+ expect(isValidIp("192.168.1.1:abc")).toBe(false);
+ expect(isValidIp("[2001:db8::1]")).toBe(false);
+ expect(isValidIp("[2001:db8::1]:")).toBe(false);
+ expect(isValidIp("[2001:db8::1]:abc")).toBe(false);
+ });
+ });
+
+ describe("isValidCidr", () => {
+ test("should validate IPv4 CIDR blocks", () => {
+ expect(isValidCidr("192.168.1.0/24")).toBe(true);
+ expect(isValidCidr("10.0.0.0/8")).toBe(true);
+ expect(isValidCidr("172.16.0.0/16")).toBe(true);
+ });
+
+ test("should validate IPv6 CIDR blocks", () => {
+ expect(isValidCidr("2001:db8::/32")).toBe(true);
+ expect(isValidCidr("fe80::/10")).toBe(true);
+ expect(isValidCidr("::/0")).toBe(true);
+ });
+
+ test("should reject invalid CIDR blocks", () => {
+ expect(isValidCidr("192.168.1.0/33")).toBe(false);
+ expect(isValidCidr("2001:db8::/129")).toBe(false);
+ expect(isValidCidr("192.168.1.0/abc")).toBe(false);
+ expect(isValidCidr("invalid/24")).toBe(false);
+ });
+ });
+
+ describe("isValidIpOrCidr", () => {
+ test("should validate both IP addresses and CIDR blocks", () => {
+ expect(isValidIpOrCidr("192.168.1.1")).toBe(true);
+ expect(isValidIpOrCidr("2001:db8::1")).toBe(true);
+ expect(isValidIpOrCidr("192.168.1.0/24")).toBe(true);
+ expect(isValidIpOrCidr("2001:db8::/32")).toBe(true);
+ });
+
+ test("should reject invalid inputs", () => {
+ expect(isValidIpOrCidr("invalid")).toBe(false);
+ expect(isValidIpOrCidr("192.168.1.0/33")).toBe(false);
+ expect(isValidIpOrCidr("2001:db8::/129")).toBe(false);
+ });
+ });
+
+ describe("extractIPDetails", () => {
+ test("should extract IPv4 address details", () => {
+ const result = extractIPDetails("192.168.1.1");
+ expect(result).toEqual({
+ ipAddress: "192.168.1.1",
+ type: IPType.IPV4
+ });
+ });
+
+ test("should extract IPv6 address details", () => {
+ const result = extractIPDetails("2001:db8::1");
+ expect(result).toEqual({
+ ipAddress: "2001:db8::1",
+ type: IPType.IPV6
+ });
+ });
+
+ test("should extract IPv4 CIDR details", () => {
+ const result = extractIPDetails("192.168.1.0/24");
+ expect(result).toEqual({
+ ipAddress: "192.168.1.0",
+ type: IPType.IPV4,
+ prefix: 24
+ });
+ });
+
+ test("should extract IPv6 CIDR details", () => {
+ const result = extractIPDetails("2001:db8::/32");
+ expect(result).toEqual({
+ ipAddress: "2001:db8::",
+ type: IPType.IPV6,
+ prefix: 32
+ });
+ });
+
+ test("should throw error for invalid IP", () => {
+ expect(() => extractIPDetails("invalid")).toThrow("Failed to extract IP details");
+ });
+ });
+});
diff --git a/backend/src/lib/ip/index.ts b/backend/src/lib/ip/index.ts
index 0b35a2759a..2bb75ed7b3 100644
--- a/backend/src/lib/ip/index.ts
+++ b/backend/src/lib/ip/index.ts
@@ -7,25 +7,56 @@ export enum IPType {
IPV6 = "ipv6"
}
+/**
+ * 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.
+ * Returns the IP address without port and a boolean indicating if a port was present.
+ */
+const stripPort = (ip: string): { ipAddress: string; hasPort: boolean } => {
+ // Handle IPv6 with port (e.g. [2001:db8::1]:8080)
+ if (ip.startsWith("[") && ip.includes("]:")) {
+ const endBracketIndex = ip.indexOf("]");
+ if (endBracketIndex === -1) return { ipAddress: ip, hasPort: false };
+ const ipPart = ip.slice(1, endBracketIndex);
+ const portPart = ip.slice(endBracketIndex + 2);
+ if (!portPart || !/^\d+$/.test(portPart)) return { ipAddress: ip, hasPort: false };
+ return { ipAddress: ipPart, hasPort: true };
+ }
+
+ // 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, hasPort: false };
+ return { ipAddress: ipPart, hasPort: true };
+ }
+
+ return { ipAddress: ip, hasPort: false };
+};
+
/**
* Return details of IP [ip]:
* - If [ip] is a specific IP address then return the IPv4/IPv6 address
* - If [ip] is a subnet then return the network IPv4/IPv6 address and prefix
*/
export const extractIPDetails = (ip: string) => {
- if (net.isIPv4(ip))
+ const { ipAddress } = stripPort(ip);
+
+ // Handle IPv6 addresses with brackets
+ const cleanIp = ipAddress.startsWith("[") && ipAddress.endsWith("]") ? ipAddress.slice(1, -1) : ipAddress;
+
+ if (net.isIPv4(cleanIp))
return {
- ipAddress: ip,
+ ipAddress: cleanIp,
type: IPType.IPV4
};
- if (net.isIPv6(ip))
+ if (net.isIPv6(cleanIp))
return {
- ipAddress: ip,
+ ipAddress: cleanIp,
type: IPType.IPV6
};
- const [ipNet, prefix] = ip.split("/");
+ const [ipNet, prefix] = cleanIp.split("/");
let type;
switch (net.isIP(ipNet)) {
@@ -57,7 +88,8 @@ export const extractIPDetails = (ip: string) => {
*
*/
export const isValidCidr = (cidr: string): boolean => {
- const [ip, prefix] = cidr.split("/");
+ const { ipAddress } = stripPort(cidr);
+ const [ip, prefix] = ipAddress.split("/");
const prefixNum = parseInt(prefix, 10);
@@ -90,13 +122,15 @@ export const isValidCidr = (cidr: string): boolean => {
*
*/
export const isValidIpOrCidr = (ip: string): boolean => {
+ const { ipAddress } = stripPort(ip);
+
// if the string contains a slash, treat it as a CIDR block
- if (ip.includes("/")) {
- return isValidCidr(ip);
+ if (ipAddress.includes("/")) {
+ return isValidCidr(ipAddress);
}
// otherwise, treat it as a standalone IP address
- if (net.isIPv4(ip) || net.isIPv6(ip)) {
+ if (net.isIPv4(ipAddress) || net.isIPv6(ipAddress)) {
return true;
}
@@ -104,7 +138,8 @@ export const isValidIpOrCidr = (ip: string): boolean => {
};
export const isValidIp = (ip: string) => {
- return net.isIPv4(ip) || net.isIPv6(ip);
+ const { ipAddress } = stripPort(ip);
+ return net.isIPv4(ipAddress) || net.isIPv6(ipAddress);
};
export type TIp = {
@@ -112,6 +147,7 @@ export type TIp = {
type: IPType;
prefix: number;
};
+
/**
* Validates the IP address [ipAddress] against the trusted IPs [trustedIps].
*/
@@ -127,7 +163,8 @@ export const checkIPAgainstBlocklist = ({ ipAddress, trustedIps }: { ipAddress:
}
const { type } = extractIPDetails(ipAddress);
- const check = blockList.check(ipAddress, type);
+ const { ipAddress: cleanIp } = stripPort(ipAddress);
+ const check = blockList.check(cleanIp, type);
if (!check)
throw new ForbiddenRequestError({