diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index 7137e76328..3f03b31e6e 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -31,6 +31,12 @@ def validate_crawl_url(raw): if u.scheme not in ("http", "https"): raise ValueError("only http: and https: URLs may be crawled") host = (u.hostname or "").lower() + # A single trailing dot is the DNS root dot (the FQDN spelling of the + # same host: "localhost." is "localhost", "127.0.0.1." is 127.0.0.1); + # strip it before the checks so the dotted spelling cannot evade + # them, matching the Worker's validateCrawlUrl (#466). + if host.endswith("."): + host = host[:-1] if not host: raise ValueError("url has no host") if host == "localhost" or host.endswith(_BLOCKED_SUFFIXES): diff --git a/pkgs/lab-ops-hub/src/lib.ts b/pkgs/lab-ops-hub/src/lib.ts index ff756435fe..d24a337c1e 100644 --- a/pkgs/lab-ops-hub/src/lib.ts +++ b/pkgs/lab-ops-hub/src/lib.ts @@ -72,7 +72,11 @@ export function validateCrawlUrl(raw: string): { ok: true; url: string } | { ok: } // WHATWG URL normalises integer/hex IPv4 forms (e.g. 2130706433, // 0x7f.1) to dotted-quad, and keeps IPv6 literals bracketed. - const host = u.hostname.toLowerCase(); + // A single trailing dot is the DNS root dot (the FQDN spelling of the + // same host: "localhost." is "localhost"); strip it before the checks + // so the dotted spelling cannot evade them (#466). + const rawHost = u.hostname.toLowerCase(); + const host = rawHost.endsWith(".") ? rawHost.slice(0, -1) : rawHost; if (!host) return { ok: false, error: "url has no 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 0b6af7a0bf..bd98d7df71 100644 --- a/pkgs/lab-ops-hub/test/lib.test.ts +++ b/pkgs/lab-ops-hub/test/lib.test.ts @@ -41,6 +41,15 @@ test("validateCrawlUrl refuses loopback / private / link-local targets (#441)", } }); +test("validateCrawlUrl refuses trailing-dot spellings of blocked hosts (#466)", () => { + const blocked = ["http://localhost./", "http://foo.localhost./", "http://printer.local./", "http://nas.internal./"]; + for (const u of blocked) { + assert.equal(validateCrawlUrl(u).ok, false, u); + } + // A public name keeps its fully-qualified spelling. + assert.equal(validateCrawlUrl("http://example.com./").ok, true); +}); + function stubFetcher(routes: Record) { const seen: string[] = []; const fetchFn = async (url: string): Promise => { @@ -73,6 +82,7 @@ test("fetchFollowingValidatedRedirects refuses redirects to non-public targets ( "http://192.168.1.1/", "https://[::1]/", "http://localhost:8787/", + "http://localhost./", "file:///etc/passwd", ]) { const { fetchFn, seen } = stubFetcher({