Public: anyone can read this; the crew can change it.

The egress proxy judges an IPv6 destination by its bytes, so `::`, `0::1` and embedded-v4 forms are private #14997

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14901-the-egress-proxy-judges-an-ipv6-destinat → main 1 commit fork

  • Merged. scout/14901-the-egress-proxy-judges-an-ipv6-destinat was merged into main as 67af484.

Review

Sign in to review this change.
2 files changed +125 −24
src/apps/egress.ts +45 −24
@@ -23,7 +23,7 @@ import { isIP } from "node:net";
2323 import { timingSafeEqual } from "node:crypto";
2424 import type { Socket } from "bun";
2525 import { neverGrantable, PAYMENT_HOSTS } from "../core/egress_grants.ts";
26 import { addressBytes } from "../kernel/cidr.ts";
26 import { addressBytes, inCidr, parseCidr, type Cidr } from "../kernel/cidr.ts";
2727 import { during } from "../perf/jobs.ts";
2828 import { type PinnedSocket, pinCellSocket } from "./cell_socket.ts";
2929
@@ -456,31 +456,42 @@ export function hostAllowed(
456456 * name is only ever checked here after it has been resolved.
457457 */
458458 export function isPublicAddress(address: string): boolean {
459 // An IPv4 address in IPv6 clothes, in either spelling (`::ffff:10.0.0.1`,
460 // `::ffff:a00:1`), is judged as the IPv4 address it is.
459 // Judged by its bytes, never its spelling (#14901): `0::1`, `0:0:0:0:0:0:0:0`
460 // and `::127.0.0.1` are the same addresses as `::1`, `::` and loopback. An
461 // IPv4 address in IPv6 clothes (`::ffff:10.0.0.1`, `::ffff:a00:1`) is the
462 // IPv4 address it is, and so is the one NAT64 or 6to4 carries inside.
461463 const bytes = addressBytes(address);
462 const ip = bytes?.length === 4 ? bytes.join(".") : address;
463 if (isIP(ip) === 4) {
464 const [a = 0, b = 0] = ip.split(".").map(Number);
465 if (a === 0 || a === 10 || a === 127 || a >= 224) return false;
466 if (a === 100 && b >= 64 && b < 128) return false; // shared address space
467 if (a === 169 && b === 254) return false; // link-local, metadata
468 if (a === 172 && b >= 16 && b < 32) return false;
469 if (a === 192 && (b === 168 || b === 0)) return false;
470 if (a === 198 && (b === 18 || b === 19)) return false;
471 return true;
472 }
473 if (isIP(ip) === 6) {
474 const lower = ip.toLowerCase();
475 if (lower === "::" || lower === "::1") return false;
476 if (/^f[cd]/.test(lower)) return false; // unique local
477 if (/^fe[89ab]/.test(lower)) return false; // link-local
478 if (lower.startsWith("ff")) return false; // multicast
479 return true;
480 }
481 return false;
464 if (bytes === undefined) return false;
465 if (bytes.length === 4) return publicIpv4(bytes);
466 if (V6_PRIVATE.some((c) => inCidr(address, c))) return false;
467 if (inCidr(address, NAT64)) return publicIpv4(bytes.slice(12, 16));
468 if (inCidr(address, SIX_TO_FOUR)) return publicIpv4(bytes.slice(2, 6));
469 return true;
482470 }
483471
472 function publicIpv4(bytes: Uint8Array): boolean {
473 const [a = 0, b = 0] = bytes;
474 if (a === 0 || a === 10 || a === 127 || a >= 224) return false;
475 if (a === 100 && b >= 64 && b < 128) return false; // shared address space
476 if (a === 169 && b === 254) return false; // link-local, metadata
477 if (a === 172 && b >= 16 && b < 32) return false;
478 if (a === 192 && (b === 168 || b === 0)) return false;
479 if (a === 198 && (b === 18 || b === 19)) return false;
480 return true;
481 }
482
483 /** IPv6 ranges with nothing public behind them, as parsed bytes. */
484 const V6_PRIVATE = [
485 "::/96", // unspecified, loopback and the old IPv4-compatible `::a.b.c.d`
486 "64:ff9b:1::/48", // NAT64 local use
487 "fc00::/7", // unique local
488 "fe80::/10", // link-local
489 "fec0::/10", // site-local, deprecated but still routed by some
490 "ff00::/8", // multicast
491 ].map((c) => parseCidr(c)!) satisfies Cidr[];
492 const NAT64 = parseCidr("64:ff9b::/96")!;
493 const SIX_TO_FOUR = parseCidr("2002::/16")!;
494
484495 /**
485496 * How the gate says a name did not resolve; it rides the status line too, so a client
486497 * that reads only the status can tell it from a refusal.
@@ -505,12 +516,22 @@ export type Verdict =
505516 | { ok: true; address: string; undeclared: boolean }
506517 | { ok: false; why: string; policy?: { private: boolean } };
507518
519 /**
520 * An IPv6 literal in its one short spelling (`0:0::1` is `::1`), so a CONNECT
521 * host is compared with origins, grants and declared hosts as the address it
522 * is. A name or IPv4 literal is returned as given.
523 */
524 function canonicalLiteral(name: string): string {
525 if (isIP(name) !== 6) return name;
526 return URL.parse(`http://[${name}]/`)?.hostname.replace(/^\[|\]$/g, "") ?? name;
527 }
528
508529 export async function decide(
509530 policy: EgressPolicy,
510531 host: string,
511532 port: number,
512533 ): Promise<Verdict> {
513 const name = host.replace(/^\[|\]$/g, "").toLowerCase();
534 const name = canonicalLiteral(host.replace(/^\[|\]$/g, "").toLowerCase());
514535 const origin = policy.origins.some(
515536 (o) => o.host.toLowerCase() === name && o.port === port,
516537 );
test/egress-ipv6-literal.test.ts added +80
@@ -0,0 +1,80 @@
1 /**
2 * The egress gate judges an IPv6 destination by its parsed bytes, never by
3 * how it is spelled (#14901): every spelling of loopback, unspecified,
4 * site-local and an IPv4 address carried inside IPv6 is private, and a real
5 * public IPv6 address still passes.
6 */
7
8 import { describe, expect, test } from "bun:test";
9 import { decide, isPublicAddress, type EgressPolicy } from "../src/apps/egress.ts";
10
11 const PRIVATE: readonly string[] = [
12 "::",
13 "0::0",
14 "0:0:0:0:0:0:0:0",
15 "::1",
16 "0::1",
17 "0:0:0:0:0:0:0:1",
18 "0000:0000::0001",
19 "::127.0.0.1", // IPv4-compatible
20 "::a9fe:a9fe", // the same form in hex: 169.254.169.254
21 "::8.8.8.8", // deprecated form: nothing routes it
22 "fec0::1", // site-local
23 "FEFF::1",
24 "64:ff9b::127.0.0.1", // NAT64 to loopback
25 "64:ff9b::a9fe:a9fe", // NAT64 to the metadata address
26 "64:ff9b:1::1", // NAT64 local use
27 "2002:7f00:1::1", // 6to4 of 127.0.0.1
28 "2002:a9fe:a9fe::", // 6to4 of 169.254.169.254
29 "2002:0a00:0001::1", // 6to4 of 10.0.0.1
30 "::ffff:127.0.0.1",
31 "0:0:0:0:0:ffff:7f00:1",
32 "fd00::1",
33 "fe80::1",
34 "ff02::1",
35 ];
36
37 const PUBLIC: readonly string[] = [
38 "2606:4700::1111",
39 "2001:4860:4860::8888",
40 "2a00:1450:4001:80b::200e",
41 "64:ff9b::8.8.8.8", // NAT64 of a public address: DNS64 hands these out
42 "2002:808:808::1", // 6to4 of 8.8.8.8
43 ];
44
45 describe("an IPv6 destination is judged by its bytes", () => {
46 test.each(PRIVATE.map((a) => [a]))("%s is private", (address) => {
47 expect(isPublicAddress(address)).toBe(false);
48 });
49
50 test.each(PUBLIC.map((a) => [a]))("%s is public", (address) => {
51 expect(isPublicAddress(address)).toBe(true);
52 });
53
54 test("a CONNECT to any private spelling is refused before anything resolves or dials", async () => {
55 let resolved = 0;
56 const policy: EgressPolicy = {
57 hosts: [],
58 origins: [],
59 resolve: async (name) => {
60 resolved++;
61 return [name];
62 },
63 };
64 for (const address of PRIVATE) {
65 const verdict = await decide(policy, `[${address}]`, 443);
66 expect([address, verdict.ok, verdict.ok ? undefined : verdict.policy?.private]).toEqual([address, false, true]);
67 }
68 expect(resolved).toBe(0);
69 });
70
71 test("a public IPv6 literal is never called private, and any spelling of an origin is that origin", async () => {
72 const policy: EgressPolicy = { hosts: [], origins: [{ host: "2606:4700::1111", port: 443 }], resolve: async (name) => [name] };
73 for (const spelling of ["2606:4700::1111", "2606:4700:0:0:0:0:0:1111", "[2606:4700:0000::1111]"]) {
74 const verdict = await decide(policy, spelling, 443);
75 expect([spelling, verdict.ok]).toEqual([spelling, true]);
76 }
77 const undeclared = await decide(policy, "[2001:4860:4860::8888]", 443);
78 expect(undeclared.ok ? undefined : undeclared.policy?.private).toBe(false);
79 });
80 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs