diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index 810f557ce7..61ab8c62d4 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -57,6 +57,21 @@ def validate_crawl_url(raw): # structure. if ":" not in host and any(label == "" for label in host.split(".")): raise ValueError(f"refusing to crawl host with an empty label {host}") + # A label over 63 octets, or a name over 253 characters, can never + # be a DNS name at all (RFC 1035 §2.3.4: 63-octet labels, 255-octet + # wire names = 253 characters of text without the root dot, which + # the strip above already removed) — refuse it up front instead of + # certifying it as a public DNS name and letting the resolver fail + # on it deep inside the fetcher, matching the Worker's validator + # (#513). Character length is a lower bound on the IDNA-encoded + # length, so this can never refuse a name that encodes within the + # limits. IPv6 literals are judged as addresses, as above. + if ":" not in host and ( + len(host) > 253 or any(len(label) > 63 for label in host.split(".")) + ): + raise ValueError( + f"refusing to crawl host with an overlong label or name {host}" + ) # A raw % in the host is a percent-encoded spelling (or an IPv6 zone # id): WHATWG URL parsing percent-decodes the host before judging it # — %6cocalhost is localhost — and rejects the rest outright, while diff --git a/pkgs/lab-ops-hub/src/lib.ts b/pkgs/lab-ops-hub/src/lib.ts index c36092e02f..e93c447aa2 100644 --- a/pkgs/lab-ops-hub/src/lib.ts +++ b/pkgs/lab-ops-hub/src/lib.ts @@ -153,6 +153,18 @@ export function validateCrawlUrl(raw: string): { ok: true; url: string } | { ok: if (!host.startsWith("[") && host.split(".").some((label) => label === "")) { return { ok: false, error: `refusing to crawl host with an empty label ${host}` }; } + // A label over 63 octets, or a name over 253 characters, can never + // be a DNS name at all (RFC 1035 §2.3.4: 63-octet labels, 255-octet + // wire names = 253 characters of text without the root dot, which + // the strip above already removed) — refuse it up front instead of + // certifying it as a public DNS name and letting the resolver fail + // on it, matching the container's validator (#513). u.hostname is + // already the punycode (IDNA-encoded) form, so these lengths are + // the wire lengths exactly. Bracketed IPv6 literals are exempt, as + // with the empty-label check above. + if (!host.startsWith("[") && (host.length > 253 || host.split(".").some((label) => label.length > 63))) { + return { ok: false, error: `refusing to crawl host with an overlong label or name ${host}` }; + } if (host === "localhost" || BLOCKED_HOST_SUFFIXES.some((s) => host.endsWith(s))) { return { ok: false, error: `refusing to crawl local host ${host}` }; } diff --git a/pkgs/lab-ops-hub/test/lib.test.ts b/pkgs/lab-ops-hub/test/lib.test.ts index 206447ba05..be3cf8ecc5 100644 --- a/pkgs/lab-ops-hub/test/lib.test.ts +++ b/pkgs/lab-ops-hub/test/lib.test.ts @@ -163,6 +163,25 @@ test("validateCrawlUrl refuses empty-label hosts (#511)", () => { assert.equal(validateCrawlUrl("http://example.com/").ok, true); }); +test("validateCrawlUrl refuses overlong DNS names (#513)", () => { + // RFC 1035 §2.3.4: no label over 63 octets, no name over 253 + // characters of text. Neither can ever resolve; refuse up front. + const blocked = [ + `http://${"b".repeat(64)}.com/`, + `http://${["a".repeat(63), "a".repeat(63), "a".repeat(63), "a".repeat(63), "com"].join(".")}/`, + ]; + for (const u of blocked) { + assert.equal(validateCrawlUrl(u).ok, false, u); + } + // Controls at the limit stay allowed: a 63-char label, and a + // 253-char name (63+63+63+61 + 3 dots). + assert.equal(validateCrawlUrl(`http://${"b".repeat(63)}.com/`).ok, true); + assert.equal( + validateCrawlUrl(`http://${["a".repeat(63), "a".repeat(63), "a".repeat(63), "a".repeat(61)].join(".")}/`).ok, + true, + ); +}); + function stubFetcher(routes: Record) { const seen: string[] = []; const fetchFn = async (url: string): Promise => { diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index 2e2cd463aa..7c35afe7be 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -51,6 +51,22 @@ def test_refuses_empty_label_hosts(self): self.assertAccepted("http://example.com./") self.assertAccepted("http://example.com/") + def test_refuses_overlong_dns_names(self): + # RFC 1035 §2.3.4: no label over 63 octets, no name over 253 + # characters of text. Neither can ever resolve; refuse up + # front instead of certifying them as public DNS names (#513). + overlong_name = ".".join(["a" * 63] * 4 + ["com"]) # 259 chars + for url in ( + "http://" + "b" * 64 + ".com/", + "http://" + overlong_name + "/", + ): + self.assertRefused(url) + # Controls at the limit stay allowed: a 63-char label, and a + # 253-char name (63+63+63+61 + 3 dots). + self.assertAccepted("http://" + "b" * 63 + ".com/") + at_limit_name = ".".join(["a" * 63] * 3 + ["a" * 61]) # 253 chars + self.assertAccepted("http://" + at_limit_name + "/") + def test_refuses_dotted_quad_non_public(self): for url in ( "http://127.0.0.1/",