diff --git a/js/net/src/connection/endpoint.test.ts b/js/net/src/connection/endpoint.test.ts index 6ac2dddcc28..e2f225883ea 100644 --- a/js/net/src/connection/endpoint.test.ts +++ b/js/net/src/connection/endpoint.test.ts @@ -3,38 +3,17 @@ import { isLocal, parse, sameHost, show } from "./endpoint.ts"; const local = (url: string) => isLocal(parse(new URL(url))); -test("local literals are recognized in every spelling the dial accepts", () => { - const spellings = [ - "https://127.0.0.1/", - "https://localhost/", - "https://localhost./", - "https://a.localhost/", - "https://[::1]/", - "https://[::]/", - "https://10.0.0.1/", - "https://172.16.0.1/", - "https://192.168.1.1/", - "https://169.254.1.1/", - "https://0.0.0.0/", - "https://[::ffff:10.0.0.1]/", - "https://[fe80::1]/", - "https://[fc00::1]/", - "moqt://127.0.0.1/", - "moqt://[::ffff:127.0.0.1]/", - // Shorthand, hex, and decimal IPv4, which raw QUIC dials as 127.0.0.1. - "moql://127.1/", - "moql://0x7f000001/", - "moql://2130706433/", - "moql://0177.0.0.1/", - "moql://LOCALHOST/", - "moql://0/", - ]; - for (const url of spellings) expect(local(url), url).toBe(true); +// Replays `rs/moq-net/fixtures/endpoints.json`, which moq-net's `is_local` replays too, so the +// two classifiers cannot drift. +const endpoints = (await Bun.file( + new URL("../../../../rs/moq-net/fixtures/endpoints.json", import.meta.url), +).json()) as { + url: string; + local: boolean; +}[]; - for (const url of ["https://example.com/", "https://8.8.8.8/", "https://172.32.0.1/", "https://[2606:4700::1]/"]) { - expect(local(url), url).toBe(false); - } - expect(local("moql://8.8.8.8/")).toBe(false); +test("local literals are recognized in every spelling the dial accepts", () => { + for (const { url, local: expected } of endpoints) expect(local(url), url).toBe(expected); }); test("a URL with no host is refused, since nothing here dials one", () => { diff --git a/quest/m0/goaway-loopback.md b/quest/m0/goaway-loopback.md index 66be522ce06..5022e709e25 100644 --- a/quest/m0/goaway-loopback.md +++ b/quest/m0/goaway-loopback.md @@ -1,39 +1,27 @@ -# [S] GOAWAY redirects to loopback are refused in every spelling, on main and release +# [S] Backport the GOAWAY loopback refusal to release ## Goal -A GOAWAY from a non-local relay cannot steer a `redirect: "follow"` client, -and the tokens it carries, to a loopback, private, or link-local address, -whatever literal spelling the URI uses. A hostname that resolves to such -an address is out of scope: classifying DNS results would race the dial's -own lookup. Rust's `is_local` judges a non-special -scheme's host the way the native dial resolves it, as js/net's typed endpoint -does since [moq-dev/moq#8222](https://github.com/moq-dev/moq/pull/8222), and -both fixes reach the `release` line in its next patch. +The `release` line refuses a GOAWAY from a non-local relay that steers a +`redirect: "follow"` client, and the tokens it carries, to a loopback, +private, or link-local address in any literal IPv4 spelling (`127.1`, +`0x7f000001`, `2130706433`, `0177.0.0.1`) or a trailing-dot `localhost.`, +in both Rust and js/net, so the next patch ships the fix `main` has. ## Plan -Planned 2026-11-08 (maintainer, approved follow-up of -[moq-dev/moq#8222](https://github.com/moq-dev/moq/pull/8222)). Milestone: -m0, since it is a hostile-input fix in shipped code plus a `release` -backport. +`main` fixed both classifiers and shares their cases in +`rs/moq-net/fixtures/endpoints.json`. Port the smallest fix to `release` as +PRs into `release`, keeping the published API (CONTRIBUTING.md); the +maintainer cuts the patch. -- Rust on `main`: `is_local` in `rs/moq-net/src/reconnect/redirect.rs` - parses a `Host::Domain` with `str::parse::`, which refuses - shorthand and numeric IPv4 (`127.1`, `0x7f000001`, `2130706433`), so - `moqt://127.1` counts as non-local while the native dial may still reach - 127.0.0.1. Classify the host the dial will use: decode the IPv4 spellings - WHATWG's host parser accepts (as js/net reads a non-special host back - through https), and treat a trailing-dot `localhost.` as `localhost`, as - `js/net/src/connection/endpoint.ts` does. -- Test both sides from one fixture of endpoint spellings and expected - verdicts, replayed by `rs/moq-net` and `js/net`, so the two classifiers - cannot drift again. Put it beside the other shared corpora (see - [one fixture layout](/quest/m4/corpus-fixtures.md)). -- Backport to `release`: js/net's GOAWAY classification - (`js/net/src/connection/goaway.ts` `isLocal` on the opaque `moql:`/`moqt:` - host) and Rust's (`rs/moq-tokio/src/connection.rs` `is_local` there) both - have the hole. Port the smallest fix to each as PRs into `release`; the - maintainer cuts the patch. +- Rust: `is_local` in `rs/moq-tokio/src/connection.rs` parses a + `Host::Domain` with `str::parse::`. Read the host back through an + `https://` URL, as `main`'s `rs/moq-net/src/reconnect/redirect.rs` does, + and strip a trailing dot before the `localhost` check. +- JS: `isLocal` in `js/net/src/connection/goaway.ts` reads the opaque + `moql:`/`moqt:` hostname. Reparse a non-special URL's host through https + before classifying, as `main`'s `js/net/src/connection/endpoint.ts` does. +- Cover the shorthand spellings in each side's existing tests. Public API: none. Wire: none. Behavior: such redirects are refused. diff --git a/rs/moq-net/fixtures/endpoints.json b/rs/moq-net/fixtures/endpoints.json new file mode 100644 index 00000000000..9df08168a73 --- /dev/null +++ b/rs/moq-net/fixtures/endpoints.json @@ -0,0 +1,122 @@ +[ + { + "url": "https://127.0.0.1/", + "local": true + }, + { + "url": "https://localhost/", + "local": true + }, + { + "url": "https://localhost./", + "local": true + }, + { + "url": "https://a.localhost/", + "local": true + }, + { + "url": "https://[::1]/", + "local": true + }, + { + "url": "https://[::]/", + "local": true + }, + { + "url": "https://10.0.0.1/", + "local": true + }, + { + "url": "https://172.16.0.1/", + "local": true + }, + { + "url": "https://192.168.1.1/", + "local": true + }, + { + "url": "https://169.254.1.1/", + "local": true + }, + { + "url": "https://0.0.0.0/", + "local": true + }, + { + "url": "https://[::ffff:127.0.0.1]/", + "local": true + }, + { + "url": "https://[::ffff:10.0.0.1]/", + "local": true + }, + { + "url": "https://[fe80::1]/", + "local": true + }, + { + "url": "https://[fc00::1]/", + "local": true + }, + { + "url": "moqt://127.0.0.1/", + "local": true + }, + { + "url": "moqt://[::ffff:127.0.0.1]/", + "local": true + }, + { + "url": "moqt://localhost./", + "local": true + }, + { + "url": "moql://127.1/", + "local": true + }, + { + "url": "moql://0x7f000001/", + "local": true + }, + { + "url": "moql://2130706433/", + "local": true + }, + { + "url": "moql://0177.0.0.1/", + "local": true + }, + { + "url": "moql://LOCALHOST/", + "local": true + }, + { + "url": "moql://0/", + "local": true + }, + { + "url": "https://example.com/", + "local": false + }, + { + "url": "https://8.8.8.8/", + "local": false + }, + { + "url": "https://172.32.0.1/", + "local": false + }, + { + "url": "https://[2606:4700::1]/", + "local": false + }, + { + "url": "moql://8.8.8.8/", + "local": false + }, + { + "url": "moqt://relay.example/", + "local": false + } +] diff --git a/rs/moq-net/src/reconnect/redirect.rs b/rs/moq-net/src/reconnect/redirect.rs index cf2b6325745..b10dae7dea6 100644 --- a/rs/moq-net/src/reconnect/redirect.rs +++ b/rs/moq-net/src/reconnect/redirect.rs @@ -111,12 +111,26 @@ fn scheme_tier(scheme: &str) -> u8 { /// "verified remote". fn is_local(url: &Url) -> bool { match url.host() { - // A non-special scheme (`moqt://127.0.0.1`, and every other scheme but - // `http`/`https`/`ws`/`wss`) parses its host as a domain even when it is an - // address literal, so the literal check can't be left to the URL parser. - Some(url::Host::Domain(host)) => match host.parse::() { - Ok(ip) => is_local_ip(ip), - Err(_) => host == "localhost" || host.ends_with(".localhost"), + // A non-special scheme (`moqt://127.1`, and every other scheme but + // `http`/`https`/`ws`/`wss`) keeps its host opaque, while the dial resolves it + // like an address in any spelling `inet_aton` accepts. Reading it back through + // https decodes the same IPv4 spellings (WHATWG's host parser), as js/net does. + Some(url::Host::Domain(host)) => match Url::parse(&format!("https://{host}/")) + .ok() + .and_then(|u| u.host().map(|h| h.to_owned())) + { + Some(url::Host::Ipv4(ip)) => is_local_ip(ip.into()), + Some(url::Host::Ipv6(ip)) => is_local_ip(ip.into()), + Some(url::Host::Domain(name)) => { + // A trailing dot names the same host from the root. + let name = name.strip_suffix('.').unwrap_or(&name); + name == "localhost" || name.ends_with(".localhost") + } + // Unparsable as a special host: judge the opaque text as written. + None => match host.parse::() { + Ok(ip) => is_local_ip(ip), + Err(_) => host == "localhost" || host.ends_with(".localhost"), + }, }, Some(url::Host::Ipv4(ip)) => is_local_ip(ip.into()), Some(url::Host::Ipv6(ip)) => is_local_ip(ip.into()), @@ -238,24 +252,16 @@ mod tests { /// reach. IPv4-mapped and link-local forms are the easy ones to miss. #[test] fn local_targets_are_recognized() { - let local = [ - "https://127.0.0.1/", - "https://localhost/", - "https://[::1]/", - "https://10.0.0.1/", - "https://169.254.1.1/", - "https://[::ffff:127.0.0.1]/", - "https://[::ffff:10.0.0.1]/", - "https://[fe80::1]/", - "https://[fc00::1]/", - "https://0.0.0.0/", - ]; - for url in local { - assert!(is_local(&url.parse().unwrap()), "{url} should be local"); + // Shared with js/net's endpoint test, so the two classifiers cannot drift. + #[derive(serde::Deserialize)] + struct Case { + url: String, + local: bool, } - - for url in ["https://example.com/", "https://8.8.8.8/", "https://[2606:4700::1]/"] { - assert!(!is_local(&url.parse().unwrap()), "{url} should not be local"); + let cases: Vec = + serde_json::from_str(include_str!("../../fixtures/endpoints.json")).expect("a malformed fixture"); + for case in cases { + assert_eq!(is_local(&url(&case.url)), case.local, "{}", case.url); } // A public endpoint may not redirect us inward, but a local one may stay local. @@ -266,6 +272,11 @@ mod tests { &public, false ))); + assert!(refused(Redirect::Follow.target( + "moqt://127.1/", + &url("moqt://relay.example/"), + false + ))); assert_eq!( Redirect::Follow .target("https://127.0.0.1:9999/", &localhost, false)