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
15 changes: 15 additions & 0 deletions pkgs/lab-ops-hub/container/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("."))
Comment on lines +69 to +70

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate the IDNA-encoded Python hostname length

When the container validator receives a Unicode hostname directly, these checks measure Unicode code points rather than the DNS A-label octets they are meant to constrain. For example, a label of 58 ü characters has only 58 Python characters but encodes to a 64-byte IDNA label, so validate_crawl_url accepts it and the request later fails inside urllib, producing a 502 instead of the intended validation error. Encode each non-IP label with IDNA and reject encoding failures or encoded labels/names that exceed the limits.

Useful? React with 👍 / 👎.

):
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
Expand Down
12 changes: 12 additions & 0 deletions pkgs/lab-ops-hub/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}` };
}
Expand Down
19 changes: 19 additions & 0 deletions pkgs/lab-ops-hub/test/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, { status: number; location?: string }>) {
const seen: string[] = [];
const fetchFn = async (url: string): Promise<Response> => {
Expand Down
16 changes: 16 additions & 0 deletions pkgs/lab-ops-hub/test/test_container_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
Loading