diff --git a/packages/engine/src/services/publicNetwork/publicNetwork.test.ts b/packages/engine/src/services/publicNetwork/publicNetwork.test.ts index 10e21bece0..53881e49be 100644 --- a/packages/engine/src/services/publicNetwork/publicNetwork.test.ts +++ b/packages/engine/src/services/publicNetwork/publicNetwork.test.ts @@ -193,6 +193,11 @@ describe("refusal class", () => { ["metadata.google.internal", "internal-name"], ["localhost", "internal-name"], ["[::1]", "ipv6"], + ["[::ffff:a9fe:a9fe]", "link-local"], + ["[::ffff:169.254.169.254]", "link-local"], + ["[64:ff9b::a00:1]", "private"], + ["[::ffff:808:808]", "ipv6"], + ["[2001:db8::1]", "ipv6"], ])("names %s as %s", (host, hostClass) => expect(refusedClass(() => assertPublicHostname(host))).toBe(hostClass), ); diff --git a/packages/engine/src/services/publicNetwork/publicNetwork.ts b/packages/engine/src/services/publicNetwork/publicNetwork.ts index 72a6b8c5cb..479c9ad6ac 100644 --- a/packages/engine/src/services/publicNetwork/publicNetwork.ts +++ b/packages/engine/src/services/publicNetwork/publicNetwork.ts @@ -6,6 +6,7 @@ import { request as httpRequest, type IncomingMessage, type ClientRequest } from import { request as httpsRequest, type RequestOptions } from "node:https"; import { connect as tlsConnect } from "node:tls"; import { + carriedIPv4, ipv4HostClass, isBlockedNetworkHost, type NonPublicIPv4Class, @@ -45,13 +46,21 @@ function refusedIPv4Class(address: string): RefusedHostClass { return hostClass === "public" ? "reserved" : hostClass; } +function refusedIPv6Class(address: string): RefusedHostClass { + const ipv4 = carriedIPv4(address); + const hostClass = ipv4 === undefined ? "public" : ipv4HostClass(ipv4); + return hostClass === "public" ? "ipv6" : hostClass; +} + export function assertPublicHostname(hostname: string): string { const host = hostname.toLowerCase().replace(/\.$/, ""); if (isIP(host) === 4) { if (!isPublicIPv4(host)) throw new NonPublicHostError(PUBLIC_IPV4, refusedIPv4Class(host)); return host; } - if (host.startsWith("[")) throw new NonPublicHostError(PUBLIC_HOSTNAME, "ipv6"); + if (host.startsWith("[")) { + throw new NonPublicHostError(PUBLIC_HOSTNAME, refusedIPv6Class(host.slice(1, -1))); + } if ( !host.includes(".") || !/^[a-z0-9.-]+$/.test(host) || diff --git a/packages/engine/src/utils/networkPolicy.ts b/packages/engine/src/utils/networkPolicy.ts index 84e76141f5..81cfaf24eb 100644 --- a/packages/engine/src/utils/networkPolicy.ts +++ b/packages/engine/src/utils/networkPolicy.ts @@ -100,6 +100,10 @@ function embeddedIPv4(address: string): string | undefined { return [high! >> 8, high! & 255, low! >> 8, low! & 255].join("."); } +export function carriedIPv4(address: string): string | undefined { + return MAPPED_OR_NAT64_IPV6.check(address, "ipv6") ? embeddedIPv4(address) : undefined; +} + const NON_PUBLIC_IPV6_ADDRESSES = new BlockList(); for (const [network, prefix] of [ ["::", 128], @@ -131,7 +135,7 @@ export function isBlockedNetworkHost(hostname: string): boolean { if (addressType === 0) return false; if (inOperatorRange(h, addressType === 4 ? "ipv4" : "ipv6")) return true; if (addressType === 6 && MAPPED_OR_NAT64_IPV6.check(h, "ipv6")) { - const ipv4 = embeddedIPv4(h); + const ipv4 = carriedIPv4(h); return ipv4 === undefined || isBlockedNetworkHost(ipv4); } return addressType === 4 diff --git a/scripts/package-size-budgets.json b/scripts/package-size-budgets.json index ec42955b98..f9c03c3aa5 100644 --- a/scripts/package-size-budgets.json +++ b/scripts/package-size-budgets.json @@ -13,7 +13,7 @@ }, "@hyperframes/engine": { "packed": 490452, - "unpacked": 1643000 + "unpacked": 1742862 }, "@hyperframes/gcp-cloud-run": { "packed": 49412,