Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
feat(validator): enhance IPv6 handling in URL validation
Victor Hugo dos Santosdbe7b7530f89
diff --git a/backend/src/lib/validator/safe-request.test.ts b/backend/src/lib/validator/safe-request.test.tsindex 526d1638b0..efa961ad5b 100644--- a/backend/src/lib/validator/safe-request.test.ts+++ b/backend/src/lib/validator/safe-request.test.ts@@ -173,6 +173,28 @@ describe("safe-request SSRF helpers", () => {setLookup([]);await expect(validateAndPinUrl("https://example.com")).rejects.toThrow(/Could not resolve hostname/i);});++ it("handles IPv6 literal URLs — strips the brackets new URL() adds", async () => {+ // public-ish v6 literal; should NOT hit dns.lookup at all+ const result = await validateAndPinUrl(`http://[${PUBLIC_IP_V6}]/`);+ expect(result).toEqual({ hostname: PUBLIC_IP_V6, entries: [{ address: PUBLIC_IP_V6, family: 6 }] });+ expect(lookupMock).not.toHaveBeenCalled();+ });++ it("rejects IPv6 loopback literal `[::1]` via the local-IP check, not a DNS error", async () => {+ await expect(validateAndPinUrl("http://[::1]/")).rejects.toThrow(/Local IPs not allowed/i);+ expect(lookupMock).not.toHaveBeenCalled();+ });++ it("rejects IPv4-mapped IPv6 loopback `[::ffff:127.0.0.1]` via the local-IP check", async () => {+ await expect(validateAndPinUrl("http://[::ffff:127.0.0.1]/")).rejects.toThrow(/Local IPs not allowed/i);+ expect(lookupMock).not.toHaveBeenCalled();+ });++ it("rejects the hex form of IPv4-mapped loopback `[::ffff:7f00:1]` (same address, different notation)", async () => {+ await expect(validateAndPinUrl("http://[::ffff:7f00:1]/")).rejects.toThrow(/Local IPs not allowed/i);+ expect(lookupMock).not.toHaveBeenCalled();+ });});describe("buildSsrfSafeAgent / pinned lookup", () => {diff --git a/backend/src/lib/validator/safe-request.ts b/backend/src/lib/validator/safe-request.tsindex 2d5c0cfd09..18a9664ef1 100644--- a/backend/src/lib/validator/safe-request.ts+++ b/backend/src/lib/validator/safe-request.ts@@ -41,14 +41,22 @@ export const validateAndPinUrl = async (throw new BadRequestError({ message: "URLs with user credentials (e.g., user:pass@) are not allowed" });}+ // IPv6 literal hostnames come back from `new URL()` wrapped in brackets+ // (e.g. "[::1]"). isIP and dns.lookup both reject the bracketed form, so+ // strip them before passing the host on.+ const rawHost =+ validUrl.hostname.startsWith("[") && validUrl.hostname.endsWith("]")+ ? validUrl.hostname.slice(1, -1)+ : validUrl.hostname;+let entries: LookupAddress[];- if (isIP(validUrl.hostname)) {- entries = [{ address: validUrl.hostname, family: isIP(validUrl.hostname) }];+ if (isIP(rawHost)) {+ entries = [{ address: rawHost, family: isIP(rawHost) }];} else {- if (validUrl.hostname === "localhost" || validUrl.hostname === "host.docker.internal") {+ if (rawHost === "localhost" || rawHost === "host.docker.internal") {throw new BadRequestError({ message: "Local IPs not allowed as URL" });}- const lookups = await dns.lookup(validUrl.hostname, { all: true });+ const lookups = await dns.lookup(rawHost, { all: true });if (!lookups || lookups.length === 0) {throw new BadRequestError({ message: "Could not resolve hostname to any IP address" });@@ -63,13 +71,13 @@ export const validateAndPinUrl = async (// Block Infisical's own infrastructure (DB, Redis, etc.).await verifyHostInputValidity({- host: validUrl.hostname,+ host: rawHost,isGateway: false,isDynamicSecret: false,preResolvedIps: entries.map((e) => e.address)});- return { hostname: validUrl.hostname, entries };+ return { hostname: rawHost, entries };};type TLookupOneCallback = (err: NodeJS.ErrnoException | null, address: string, family: number) => void;