Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
Expand Down
11 changes: 10 additions & 1 deletion packages/engine/src/services/publicNetwork/publicNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) ||
Expand Down
6 changes: 5 additions & 1 deletion packages/engine/src/utils/networkPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scripts/package-size-budgets.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"@hyperframes/engine": {
"packed": 490452,
"unpacked": 1643000
"unpacked": 1742862
},
"@hyperframes/gcp-cloud-run": {
"packed": 49412,
Expand Down
Loading