diff --git a/js/net/src/lite/connection.test.ts b/js/net/src/lite/connection.test.ts index 45c2526f73b..2c70ac2ec34 100644 --- a/js/net/src/lite/connection.test.ts +++ b/js/net/src/lite/connection.test.ts @@ -12,9 +12,17 @@ import { wireOf } from "../wire.ts"; import { Connection, probeLevel } from "./connection.ts"; import { Fetch } from "./fetch.ts"; import { Goaway } from "./goaway.ts"; +import { Group as GroupMessage } from "./group.ts"; import { ProbeLevel, Setup } from "./setup.ts"; import { DataType, StreamId } from "./stream.ts"; -import { decodeSubscribeResponse, Subscribe } from "./subscribe.ts"; +import { + decodeSubscribeResponse, + encodeSubscribeResponse, + Subscribe, + SubscribeEnd, + SubscribeStart, +} from "./subscribe.ts"; +import { TrackInfo, Track as TrackMessage } from "./track.ts"; import { ALPN_04, ALPN_05, ALPN_06, ALPN_07_WIP, Version } from "./version.ts"; /** A transport whose `getStats` behaves as described, or is absent entirely. */ @@ -602,3 +610,51 @@ for (const [alpn, version] of [ } }); } + +// A group stream that breaks the protocol ends the session, so a hostile peer cannot repeat +// it on every stream. +for (const violation of ["a partial group", "a group past the declared end"] as const) { + test(`${violation} closes the session with PROTOCOL_VIOLATION`, async () => { + const version = Version.DRAFT_07; + const pair = createMockTransportPair(ALPN_07_WIP); + const connection = new Connection({ url: new URL("https://relay.example/"), quic: pair.client, version }); + try { + const track = wireOf(connection).consume(Path.from("room")).track("video").subscribe(); + + // The publisher, played by hand: answer TRACK_INFO, start the subscription, and + // leave any other stream the session opens (such as PROBE) open. + let sub: Stream | undefined; + while (!sub) { + const stream = await Stream.accept(pair.server, version); + if (!stream) throw new Error("the subscriber never subscribed"); + const id = await stream.reader.u53(); + if (id === StreamId.Track) { + await TrackMessage.decode(stream.reader, version); + await new TrackInfo({ maxAge: 60_000, timescale: Timescale.MILLI }).encode(stream.writer, version); + stream.close(); + } else if (id === StreamId.Subscribe) { + await Subscribe.decode(stream.reader, version); + sub = stream; + } + } + await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0) }, version); + + let group: GroupMessage; + if (violation === "a partial group") { + // The subscriber never asked to start mid-group. + group = new GroupMessage({ subscribe: 0n, sequence: 0, frameStart: 5 }); + } else { + await encodeSubscribeResponse(sub.writer, { end: new SubscribeEnd(2) }, version); + group = new GroupMessage({ subscribe: 0n, sequence: 2 }); + } + const writer = await Writer.open(pair.server, { version }); + await writer.u53(DataType.Group); + await group.encode(writer, version); + + expect((await pair.client.closed).closeCode).toBe(SessionCode.ProtocolViolation); + track.close(); + } finally { + connection.abort(); + } + }); +} diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 8639b8aa0db..11a9dbf7640 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -1595,6 +1595,12 @@ export class Subscriber { const read = tail.open(group.sequence); try { + // This side never asks for a group from a later frame, so a stream that skips the + // first ones is a peer bug. Read as a whole group, its frames would be misnumbered, + // and resumption and deduplication rely on those positions. + if (group.frameStart !== 0) { + throw new ProtocolViolation(`group ${group.sequence} starts at frame ${group.frameStart}`); + } // The publisher contradicted its own end, which no later group can repair. lite-05 // specified an inclusive end, so its last group lands on it: the write below drops // only that group there. @@ -1621,14 +1627,17 @@ export class Subscriber { scale = timescale.peek(); } - const compress = entry.compress; - if (compress && group.frameStart !== 0) - throw new StreamError(StreamCode.TooFarBehind, { message: "compressed group lacks its prefix" }); - await readFrames(stream, producer, { scale, timed: hasMediaTime(this.version), compress }); + await readFrames(stream, producer, { scale, timed: hasMediaTime(this.version), compress: entry.compress }); producer.close(); stream.stop(StreamCode.Cancel); } catch (err: unknown) { + // A violation ends the session, which the connection does once it escapes; anything + // else ends only this group. + if (err instanceof ProtocolViolation) { + producer.close(err); + throw err; + } const e = await sessionCause(this.#quic, err); producer.close(e); stream.stop(e); diff --git a/js/net/src/lite/tail.test.ts b/js/net/src/lite/tail.test.ts index 160b258914d..88e2f7bed09 100644 --- a/js/net/src/lite/tail.test.ts +++ b/js/net/src/lite/tail.test.ts @@ -319,10 +319,18 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver await respond({ end: new SubscribeEnd(2) }); expect(await reader.finished()).toEqual({ group: 2, frame: 0 }); - await groupStream(subscriber, 2).handled; + const handled = groupStream(subscriber, 2).handled; if (version === Version.DRAFT_05) { + await handled; expect(await settlesWithin(Promise.resolve(reader.closed), 50)).toBe(false); } else { + // The violation escapes to the connection, which closes the session on it. + expect( + await handled.then( + () => undefined, + (err: unknown) => err, + ), + ).toBeInstanceOf(ProtocolViolation); expect(await reader.closed).toBeInstanceOf(ProtocolViolation); } }); diff --git a/quest/m1/release-audit/README.md b/quest/m1/release-audit/README.md index 42a06c9fc67..2c03b3cef97 100644 --- a/quest/m1/release-audit/README.md +++ b/quest/m1/release-audit/README.md @@ -95,7 +95,6 @@ child records whether its behavior is new on main or inherited from release. - [Review HLS admission](/quest/m1/release-audit/hls-admission.md) - resolve the security-sensitive resource-boundary review before the cut - [Bound decoded datagram retention](/quest/m1/release-audit/datagram-memory.md) - shared ownership accounts for retained plain and compressed payloads across tracks - [Decode order](/quest/m1/ts-reliability/decode-order.md) - the exporters' decode clock holds at most the declared reorder depth, so flat timestamps cannot bypass MKV/TS backpressure or cause quadratic draining -- [Refuse partial lite groups in JS](/quest/m1/release-audit/js-partial-group.md) - a nonzero `frameStart` closes the session as a protocol violation, not a renumbered complete group - [Parse QMux records without copying suffixes](/quest/m1/release-audit/qmux-copy.md) - many small frames do not cause quadratic copying - [Release AUTH wake listeners](/quest/m1/release-audit/auth-listeners.md) - session churn does not retain one terminal promise reaction per change - [Report fatal watch ingestion failures](/quest/m1/release-audit/watch-errors.md) - failed audio/video reads reach the existing error output and tear down their resources diff --git a/quest/m1/release-audit/js-partial-group.md b/quest/m1/release-audit/js-partial-group.md deleted file mode 100644 index e666ce3b56c..00000000000 --- a/quest/m1/release-audit/js-partial-group.md +++ /dev/null @@ -1,35 +0,0 @@ -# [S] JS refuses an unrequested partial lite group - -## Goal - -A JS lite subscriber refuses a group stream whose `frameStart` is above 0 as a -protocol violation, closing the session, instead of exposing its first frame -as frame 0. - -## Plan - -P2, inherited from release (lite-06 added the field; JS parses and ignores -it). The 2026-10-22 subscription audit injected a lite-07 group declaring -`frameStart: 5` at main 1f5caa7026; its first frame read back as frame 0. The -group receiver in `js/net/src/lite/subscriber.ts` builds every group from -frame 0 and checks the offset only for compressed groups. A partial group then -looks complete, corrupting the frame positions resumption and deduplication -rely on. - -Decided 2026-10-22: refuse rather than model the offset. A JS subscriber never -asks for a mid-group start, so a nonzero `frameStart` is a peer bug. Raise -`ProtocolViolation`, folding the compressed-group check into the same rule. - -The violation must close the session, as `ProtocolViolation` promises, so a -hostile peer cannot repeat it on every stream (found in review of the planning -PR). Today `runGroup` catches every error, closes the group, resets the -stream, and returns, so nothing reaches the dispatch in `lite/connection.ts`. -The existing check for a group past the track's declared end throws the same -way and is swallowed too; fix both, letting the violation escape `runGroup` -while ordinary group errors stay stream-scoped. Test that each closes the -session. - -[JS ranges](/quest/m1/subscribe-ranges/js.md) carries the offset through the -group model when it adds frame bounds. - -Public API: none. Wire: none. diff --git a/quest/m1/subscribe-ranges/js.md b/quest/m1/subscribe-ranges/js.md index cc6f369af33..691248eb13f 100644 --- a/quest/m1/subscribe-ranges/js.md +++ b/quest/m1/subscribe-ranges/js.md @@ -29,15 +29,11 @@ IETF FETCH dispatch. Resized from [M] to [L] for it. concurrent requests, and cancellation while awaiting a reply. When ranges add frame bounds, the group model carries a lite group's -`frameStart` instead of refusing a nonzero one, as -[partial lite groups](/quest/m1/release-audit/js-partial-group.md) does until -then. Preserve the per-reader group-bound rule for reliable reads, live-edge -selection, subscription updates, and publisher datagrams. +`frameStart` instead of refusing a nonzero one as a protocol violation, as +`lite/subscriber.ts` `runGroup` does until then. Preserve the per-reader +group-bound rule for reliable reads, live-edge selection, subscription +updates, and publisher datagrams. ## Required - [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - the wire this speaks - -## Related - -- [Partial lite groups](/quest/m1/release-audit/js-partial-group.md) - refuses a nonzero `frameStart` until this models it