diff --git a/js/net/src/connection/migrate.test.ts b/js/net/src/connection/migrate.test.ts index a9c71fbf051..0592d4b8df2 100644 --- a/js/net/src/connection/migrate.test.ts +++ b/js/net/src/connection/migrate.test.ts @@ -1,4 +1,4 @@ -import { afterEach, expect, test } from "bun:test"; +import { afterEach, expect, spyOn, test } from "bun:test"; import { Instant, Manual, Milli, Nano } from "@moq/clock"; import { RefusedRedirect } from "../error.ts"; import * as Lite from "../lite/index.ts"; @@ -8,7 +8,7 @@ import * as Path from "../path.ts"; import { Stream } from "../stream.ts"; import { accept } from "./accept.ts"; import { Connection, resetShared } from "./pool.ts"; -import { Reload } from "./reload.ts"; +import { Redirects, Reload } from "./reload.ts"; const original = globalThis.WebTransport; @@ -305,15 +305,27 @@ async function elapse(clock: Manual, pred: () => boolean): Promise { /** * Let session `hop` live for `life`, then have its peer redirect it to the other address, - * minting a fresh credential in the query when `rotate` is set. + * minting a fresh credential in the query or userinfo when `rotate` is set. * Returns once the client has acted on the GOAWAY, so any backoff timer is already armed. */ -async function bounce(clock: Manual, reload: Reload, dials: Dial[], hop: number, rotate = false): Promise { +async function bounce( + clock: Manual, + reload: Reload, + dials: Dial[], + hop: number, + rotate?: "query" | "userinfo", +): Promise { await waitUntil(() => dials.length === hop + 1 && reload.status.peek() === "connected"); const dial = dials[hop]; if (!dial) throw new Error(`no dial ${hop}`); const peer = new URL(dial.url).port === moved.port ? url : moved; - const other = rotate ? `${peer.href}?jwt=${hop}` : peer.href; + const next = new URL(peer); + if (rotate === "query") next.searchParams.set("jwt", String(hop)); + if (rotate === "userinfo") { + next.username = `token-${hop}`; + next.password = `secret-${hop}`; + } + const other = next.href; await clock.advanceAsync(Nano.fromMilli(life)); await goaway(dial.server, other); await waitUntil(() => reload.redirect.peek()?.href === other); @@ -321,9 +333,10 @@ async function bounce(clock: Manual, reload: Reload, dials: Dial[], hop: number, } test.each([ - ["", false], - [" even with fresh credentials on every redirect", true], -])("two peers redirecting to each other escalate the backoff instead of cycling%s", async (_, rotate) => { + ["", undefined], + [" even with fresh query credentials on every redirect", "query"], + [" even with fresh userinfo on every redirect", "userinfo"], +] as const)("two peers redirecting to each other escalate the backoff instead of cycling%s", async (_, rotate) => { const clock = new Manual(); const restore = clock.install(); const { dials } = fleet(); @@ -354,6 +367,36 @@ test.each([ } }); +test("redirect endpoint keys omit userinfo without changing the dial URL", async () => { + const clock = new Manual(); + const restore = clock.install(); + const { dials } = fleet(); + const from = new URL("https://first:secret@relay.example/room?jwt=a#x"); + const to = new URL("https://second:other@relay.example:5443/room?jwt=b#y"); + const moves = spyOn(Redirects.prototype, "move"); + const reload = new Reload({ + url: from, + websocket: { enabled: false }, + goaway: { handover }, + delay: { initial: Milli(10) }, + }); + + try { + await waitUntil(() => reload.status.peek() === "connected"); + await clock.advanceAsync(Nano.fromMilli(life)); + await goaway(dials[0]?.server as WebTransport, to.href); + await elapse(clock, () => dials.length === 2); + expect(moves.mock.calls).toEqual([["https://relay.example/room", "https://relay.example:5443/room"]]); + expect(dials.map((dial) => dial.url)).toEqual([from.href, to.href]); + expect(from.username).toBe("first"); + expect(from.password).toBe("secret"); + } finally { + reload.close(); + moves.mockRestore(); + restore(); + } +}); + test("a redirect loop stops at the retry window", async () => { const clock = new Manual(); const restore = clock.install(); diff --git a/js/net/src/connection/reload.ts b/js/net/src/connection/reload.ts index 700c8e69cbc..81c8c6e10ed 100644 --- a/js/net/src/connection/reload.ts +++ b/js/net/src/connection/reload.ts @@ -766,11 +766,13 @@ class Draining { } /** - * Where `url` dials, without its query or fragment, where a peer may mint fresh credentials on - * every redirect. Two URLs that differ only there reach the same endpoint. + * Where `url` dials, without its userinfo, query, or fragment, where a peer may mint fresh + * credentials on every redirect. Two URLs that differ only there reach the same endpoint. */ function endpoint(url: URL): string { const bare = new URL(url); + bare.username = ""; + bare.password = ""; bare.search = ""; bare.hash = ""; return bare.href; diff --git a/quest/m1/README.md b/quest/m1/README.md index e10595b0c0e..3bb4512d93d 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -100,7 +100,6 @@ lite-07 stream type here. - [A/V clock](/quest/m1/av-clock.md) - the audio playhead drives Sync.reference while audio plays, through per-track sync handles - [Publish channel count](/quest/m1/publish-audio-channel-count.md) - forcing a channel count on an Audio.Capture stops costing the subscriber gaps of silence, and the media fixture's cold-start audio-tone flake is fixed at its cause - [moq --listen admission test](/quest/m1/listen-admission-test.md) - an in-process test proves `moq --listen` bounds admission by `listen.timeout` -- [JS redirect keys strip userinfo](/quest/m1/js-reload-userinfo.md) - js/net keys a reconnect endpoint without URL userinfo, matching Rust - [Container reads drop backfill](/quest/m1/read-backfill.md) - the JS container consumer drops groups below its read cursor like Rust - [qmux no-wait opens](/quest/m1/qmux-no-wait.md) - the in-tree @moq/qmux rejects an over-limit create when waitUntilAvailable is false, like Chrome - [TS fidelity and recovery](/quest/m1/ts-reliability/README.md) - both modes have explicit profiles, automated validation, loss tests, and a dedicated guide diff --git a/quest/m1/js-reload-userinfo.md b/quest/m1/js-reload-userinfo.md deleted file mode 100644 index d78f90a9c67..00000000000 --- a/quest/m1/js-reload-userinfo.md +++ /dev/null @@ -1,16 +0,0 @@ -# [XS] JS redirect keys strip URL userinfo - -## Goal - -js/net's reconnect (`js/net/src/connection/reload.ts`) keys an endpoint -without its URL userinfo, as Rust does, so a redirect loop is detected -whatever credentials the URL carries and none of them end up in the key. - -## Plan - -`endpoint()` clears only `search` and `hash` today. Rust's redirect-key rule -from [moq-dev/moq#6162](https://github.com/moq-dev/moq/pull/6162) also drops -userinfo; match it exactly, and test both a userinfo URL and a redirect loop -that differs only in userinfo. - -Public API: none. Wire: none.