From 54e4a2be3e7a9fcf69b8b215ce0563f3900d2dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 14 Nov 2026 20:59:29 -0500 Subject: [PATCH] fix(engine): judge mapped and NAT64 addresses by the IPv4 they carry; producer exports the rule (#7789) * fix(engine): NAT64 addresses count as non-public; producer exports the address rule * chore(producer): drop the new section comment * fix(engine): judge mapped and NAT64 addresses by the IPv4 address they carry * test(engine): operator ranges hold through mapped and NAT64 addresses --- .../engine/src/utils/networkPolicy.test.ts | 18 +++++++++++++++ packages/engine/src/utils/networkPolicy.ts | 23 ++++++++++++++++++- .../producer/src/index.networkRules.test.ts | 16 +++++++++++++ packages/producer/src/index.ts | 1 + 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 packages/producer/src/index.networkRules.test.ts diff --git a/packages/engine/src/utils/networkPolicy.test.ts b/packages/engine/src/utils/networkPolicy.test.ts index eafca8d710..f304e2c645 100644 --- a/packages/engine/src/utils/networkPolicy.test.ts +++ b/packages/engine/src/utils/networkPolicy.test.ts @@ -9,6 +9,22 @@ describe("special-use network list", () => { it.each(["192.0.66.108", "192.0.1.1", "192.0.3.1"])("leaves public %s reachable", (ip) => expect(isBlockedNetworkHost(ip)).toBe(false), ); + it.each([ + "64:ff9b::a00:1", + "64:ff9b::7f00:1", + "64:ff9b::10.0.0.1", + "64:ff9b::a9fe:a9fe", + "64:ff9b:1::808:808", + "::ffff:10.0.0.1", + "::ffff:a9fe:a9fe", + "::ffff:127.0.0.1", + ])("blocks %s: the IPv4 address it carries is not public", (ip) => + expect(isBlockedNetworkHost(ip)).toBe(true), + ); + it.each(["64:ff9b::808:808", "64:ff9b::8.8.8.8", "::ffff:8.8.8.8", "::ffff:808:808"])( + "leaves %s reachable: it carries a public IPv4 address (DNS64 answers look like this)", + (ip) => expect(isBlockedNetworkHost(ip)).toBe(false), + ); }); describe("IPv4 host class", () => { @@ -38,6 +54,8 @@ describe("operator ranges", () => { expect(ipv4HostClass("192.160.83.14")).toBe("private"); expect(isBlockedNetworkHost("2600:1f00::1")).toBe(true); expect(isBlockedNetworkHost("192.161.0.1")).toBe(false); + expect(isBlockedNetworkHost("::ffff:192.160.83.14")).toBe(true); + expect(isBlockedNetworkHost("64:ff9b::c0a0:530e")).toBe(true); }); it.each(["192.160.0.0", "192.160.0.0/33", "not-a-network/8"])( diff --git a/packages/engine/src/utils/networkPolicy.ts b/packages/engine/src/utils/networkPolicy.ts index 6ed14c4d3d..84e76141f5 100644 --- a/packages/engine/src/utils/networkPolicy.ts +++ b/packages/engine/src/utils/networkPolicy.ts @@ -83,16 +83,33 @@ function inOperatorRange(address: string, family: "ipv4" | "ipv6"): boolean { return ranges === "malformed" || ranges.check(address, family); } +const MAPPED_OR_NAT64_IPV6 = new BlockList(); +MAPPED_OR_NAT64_IPV6.addSubnet("::ffff:0:0", 96, "ipv6"); +MAPPED_OR_NAT64_IPV6.addSubnet("64:ff9b::", 96, "ipv6"); + +function embeddedIPv4(address: string): string | undefined { + const tail = address.slice(address.lastIndexOf(":") + 1); + if (isIP(tail) === 4) return tail; + const [head = "", rest] = address.split("::"); + const left = head ? head.split(":") : []; + const right = rest ? rest.split(":") : []; + const groups = [...left, ...Array(8 - left.length - right.length).fill("0"), ...right]; + if (groups.length !== 8) return undefined; + const [high, low] = groups.slice(6).map((group) => parseInt(group, 16)); + if (!Number.isInteger(high) || !Number.isInteger(low)) return undefined; + return [high! >> 8, high! & 255, low! >> 8, low! & 255].join("."); +} + const NON_PUBLIC_IPV6_ADDRESSES = new BlockList(); for (const [network, prefix] of [ ["::", 128], ["::1", 128], - ["::ffff:0:0", 96], ["fc00::", 7], ["fe80::", 10], ["fec0::", 10], ["ff00::", 8], ["2001:db8::", 32], + ["64:ff9b:1::", 48], ] as const) { NON_PUBLIC_IPV6_ADDRESSES.addSubnet(network, prefix, "ipv6"); } @@ -113,6 +130,10 @@ export function isBlockedNetworkHost(hostname: string): boolean { const addressType = isIP(h); 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); + return ipv4 === undefined || isBlockedNetworkHost(ipv4); + } return addressType === 4 ? NON_PUBLIC_IPV4_ADDRESSES.check(h, "ipv4") : NON_PUBLIC_IPV6_ADDRESSES.check(h, "ipv6"); diff --git a/packages/producer/src/index.networkRules.test.ts b/packages/producer/src/index.networkRules.test.ts new file mode 100644 index 0000000000..2db064ec8d --- /dev/null +++ b/packages/producer/src/index.networkRules.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vitest"; +import { isBlockedNetworkHost } from "./index.js"; + +describe("isBlockedNetworkHost from the producer entry", () => { + it.each([ + "10.0.0.1", + "100.64.0.1", + "169.254.169.254", + "198.18.0.1", + "240.0.0.1", + "64:ff9b::a00:1", + "localhost", + ])("refuses %s", (host) => expect(isBlockedNetworkHost(host)).toBe(true)); + + it("allows a public address", () => expect(isBlockedNetworkHost("93.184.216.34")).toBe(false)); +}); diff --git a/packages/producer/src/index.ts b/packages/producer/src/index.ts index a339817859..136aff34b1 100644 --- a/packages/producer/src/index.ts +++ b/packages/producer/src/index.ts @@ -109,6 +109,7 @@ export { prepareHeifImageInputs } from "./services/heifImagePrep.js"; // ── Video frame injection (Hyperframes-specific hook) ─────────────────────── export { createVideoFrameInjector } from "@hyperframes/engine"; +export { isBlockedNetworkHost } from "@hyperframes/engine"; // ── Configuration ─────────────────────────────────────────────────────────── export { resolveConfig, DEFAULT_CONFIG, type ProducerConfig } from "./config.js";