Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(workers): check the IPv4 address embedded in NAT64 addresses (#3275)

Mohamed Bassem2e136b7bb2c5
diff --git a/apps/workers/network.test.ts b/apps/workers/network.test.ts
index 98936c4c..3ea8cbe4 100644
--- a/apps/workers/network.test.ts
+++ b/apps/workers/network.test.ts
@@ -130,3 +130,55 @@ describe("createPinnedLookup", () => {
expect(result).toEqual({ address: "93.184.216.34", family: 4 });
});
});
+
+describe("IPv4-embedding IPv6 addresses", () => {
+ // DNS can return these in AAAA records, and a NAT64/SIIT translator on the
+ // host network routes them to the embedded IPv4 address, so they must be
+ // judged by that address.
+ test.each([
+ "64:ff9b::7f00:1", // NAT64 -> 127.0.0.1
+ "64:ff9b::a9fe:a9fe", // NAT64 -> 169.254.169.254
+ "64:ff9b::a00:1", // NAT64 -> 10.0.0.1
+ "::ffff:0:7f00:1", // IPv4-translated -> 127.0.0.1
+ "::ffff:0:a9fe:a9fe", // IPv4-translated -> 169.254.169.254
+ "::ffff:127.0.0.1", // IPv4-mapped -> 127.0.0.1
+ "::ffff:169.254.169.254", // IPv4-mapped -> 169.254.169.254
+ ])("refuses to return %s", async (address) => {
+ const lookup = createPinnedLookup([address]);
+
+ const error = await new Promise<NodeJS.ErrnoException>(
+ (resolve, reject) => {
+ lookup("rebind.example", { family: 6 }, (lookupError) => {
+ if (lookupError) {
+ resolve(lookupError);
+ } else {
+ reject(new Error(`Expected the lookup to reject ${address}`));
+ }
+ });
+ },
+ );
+
+ expect(error.code).toBe("ENOTFOUND");
+ });
+
+ test("allows NAT64 addresses of public IPv4 hosts (DNS64 networks)", async () => {
+ // 64:ff9b::808:808 -> 8.8.8.8
+ const lookup = createPinnedLookup(["64:ff9b::808:808"]);
+
+ const result = await new Promise<{ address: string; family: number }>(
+ (resolve, reject) => {
+ lookup("dns64.example", { family: 6 }, (error, address, family) => {
+ if (error) {
+ reject(error);
+ } else if (typeof address !== "string" || family === undefined) {
+ reject(new Error("Expected a single lookup result"));
+ } else {
+ resolve({ address, family });
+ }
+ });
+ },
+ );
+
+ expect(result).toEqual({ address: "64:ff9b::808:808", family: 6 });
+ });
+});
diff --git a/apps/workers/network.ts b/apps/workers/network.ts
index 9466d2ca..384a140c 100644
--- a/apps/workers/network.ts
+++ b/apps/workers/network.ts
@@ -75,19 +75,25 @@ async function resolveHostAddresses(hostname: string): Promise<string[]> {
throw new Error(errorMessage);
}
+// IPv6 ranges that carry an IPv4 address in their last 32 bits, which a
+// translator (NAT64/SIIT) on the host network may route to that IPv4 address:
+// IPv4-mapped (::ffff:0:0/96), the NAT64 well-known prefix (64:ff9b::/96,
+// RFC 6052) and IPv4-translated addresses (::ffff:0:0:0/96, RFC 6145).
+// They aren't blocked wholesale because on IPv6-only networks DNS64
+// synthesizes NAT64 addresses for every public IPv4-only site.
+const IPV4_EMBEDDING_RANGES = new Set(["ipv4Mapped", "rfc6052", "rfc6145"]);
+
function isAddressForbidden(address: string): boolean {
if (!ipaddr.isValid(address)) {
return true;
}
const parsed = ipaddr.parse(address);
- if (
- parsed.kind() === "ipv6" &&
- (parsed as ipaddr.IPv6).isIPv4MappedAddress()
- ) {
- const mapped = (parsed as ipaddr.IPv6).toIPv4Address();
- return DISALLOWED_IP_RANGES.has(mapped.range());
+ const range = parsed.range();
+ if (parsed.kind() === "ipv6" && IPV4_EMBEDDING_RANGES.has(range)) {
+ const embedded = new ipaddr.IPv4(parsed.toByteArray().slice(12));
+ return DISALLOWED_IP_RANGES.has(embedded.range());
}
- return DISALLOWED_IP_RANGES.has(parsed.range());
+ return DISALLOWED_IP_RANGES.has(range);
}
export function getBookmarkDomain(url?: string | null): string | undefined {