From f5a5968a983c9a6ab061949c996b565e19649290 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 09:29:53 -0700 Subject: [PATCH 01/13] chore: claim request ID order quest Co-Authored-By: GPT-6 From 88a1503542cf7ec8bf7221ab7847d4a9350c5066 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 10:17:38 -0700 Subject: [PATCH 02/13] fix(moq-net): permanently spend incoming IETF request IDs Co-Authored-By: GPT-6 --- drafts/draft-lcurley-moq-lite.md | 4 ++++ js/net/src/error.test.ts | 5 ++++ js/net/src/error.ts | 2 ++ js/net/src/ietf/adapter.test.ts | 41 +++++++++++++++++++++++--------- js/net/src/ietf/adapter.ts | 13 +++++----- quest/m1/README.md | 1 - quest/m1/request-id-order.md | 25 ------------------- rs/moq-net/src/error.rs | 8 +++++++ rs/moq-net/src/ietf/control.rs | 24 +++++++++++++------ 9 files changed, 72 insertions(+), 51 deletions(-) delete mode 100644 quest/m1/request-id-order.md diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index dc963c4150b..3239df81dcb 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -284,6 +284,8 @@ Sent when terminating the session, via the transport's session close. | ------- | ------------- | ----------- | | 0x3 | PROTOCOL_VIOLATION | The peer violated this specification. | | ------- | ------------- | ----------- | +| 0x4 | INVALID_REQUEST_ID | A moq-transport request ID was reused, decreased, or had the wrong parity; moq-lite has no request IDs. | +| ------- | ------------- | ----------- | | 0x6 | KEY_VALUE_FORMATTING_ERROR | A key-value pair was malformed, or repeated more than allowed. | | ------- | ------------- | ----------- | | 0x7 | TOO_MANY_REQUESTS | The peer held more announcements or subscriptions than the endpoint allows. | @@ -1423,6 +1425,8 @@ The `Message Length` describes the payload size on the wire. ## moq-lite-07 +- Listed the shared INVALID_REQUEST_ID (0x4) session code used by moq-transport drafts 14 through 16. + - SUBSCRIBE_OK `Group` names where delivery starts when the subscription resolves, which need not be the first group sent. It is not a new floor. A subscriber does not wait for a group between the requested floor and it, but a publisher still delivers one that arrives later within Subscriber Max Age. - Corrected the SUBSCRIBE note that offset `Group Start` by 1. Only `Group End` and `Frame End` are offset so 0 can mean absent. `Group Start` is an absolute sequence, and 0 is group 0. - A subscription's range bounds datagrams like groups, and FETCH never returns a datagram. diff --git a/js/net/src/error.test.ts b/js/net/src/error.test.ts index 210d39efa71..c4a291f47fc 100644 --- a/js/net/src/error.test.ts +++ b/js/net/src/error.test.ts @@ -93,6 +93,11 @@ test("the error type names the registry the code came from", () => { expect(Number(stream.code)).toBe(Number(session?.code)); }); +test("invalid request IDs keep their session registry code", () => { + expect(Number(SessionCode.InvalidRequestId)).toBe(0x4); + expect(fromClose({ closeCode: 0x4 })?.code).toBe(SessionCode.InvalidRequestId); +}); + test("the code types reject the other registry", () => { new StreamError(StreamCode.DeliveryTimeout); new SessionError(SessionCode.Unauthorized); diff --git a/js/net/src/error.ts b/js/net/src/error.ts index f13d7dc536d..3789f7f77ea 100644 --- a/js/net/src/error.ts +++ b/js/net/src/error.ts @@ -36,6 +36,8 @@ export const SessionCode = Object.freeze( Unauthorized: 0x2 as SessionCode, /** A protocol rule was broken; the session is unusable. */ ProtocolViolation: 0x3 as SessionCode, + /** A request ID was reused, decreased, or had the wrong parity. */ + InvalidRequestId: 0x4 as SessionCode, /** A key-value pair was malformed or repeated more than allowed. */ KeyValueFormatting: 0x6 as SessionCode, /** The peer went past what the session allows: a request ID past MAX_REQUEST_ID, or too many announcements or subscriptions. */ diff --git a/js/net/src/ietf/adapter.test.ts b/js/net/src/ietf/adapter.test.ts index b23db67212d..7196f0a9409 100644 --- a/js/net/src/ietf/adapter.test.ts +++ b/js/net/src/ietf/adapter.test.ts @@ -376,7 +376,7 @@ test("a second use of an open request id closes the session", async () => { const { pair, peer, running } = await windowed(version, alpn, 2n, false); await trackStatus(peer, version, 0n); await trackStatus(peer, version, 0n); - await expect(running).rejects.toThrow("duplicate request id"); + await expect(running).rejects.toThrow("request id was already spent"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); @@ -470,24 +470,43 @@ test("an update spending an open request id closes the session", async () => { await trackStatus(peer, version, heldId); if (!(await adapter.acceptBi())) throw new Error("no stream"); await update(peer, version, heldId, heldId); - await expect(running).rejects.toThrow("duplicate request id"); + await expect(running).rejects.toThrow("request id was already spent"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } } }); -/** Repeated updates raise the maximum, but the peer still cannot hold more than the window open. */ -test("reused update ids cannot hold more than the window", async () => { +/** Request IDs stay spent after their stream or update retires. */ +test("retired and lower request ids close the session", async () => { for (const [version, alpn] of WINDOW_DRAFTS) { - const { pair, adapter, peer, running } = await windowed(version, alpn, 2n, false); + for (const client of [false, true]) { + const parity = client ? 1n : 0n; + for (const [first, later] of [ + [parity, parity], + [parity + 4n, parity + 2n], + ] as const) { + const { pair, adapter, peer, running } = await windowed(version, alpn, 8n, client); + await trackStatus(peer, version, first); + const stream = await adapter.acceptBi(); + if (!stream) throw new Error("no stream"); + stream.close(); + await trackStatus(peer, version, later); + await expect(running).rejects.toThrow("request id was already spent"); + expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); + } + } + } +}); + +test("an update id stays spent after the update retires", async () => { + for (const [version, alpn] of WINDOW_DRAFTS) { + const { pair, adapter, peer, running } = await windowed(version, alpn, 4n, false); await trackStatus(peer, version, 0n); if (!(await adapter.acceptBi())) throw new Error("no stream"); - for (let i = 0; i < 8; i++) await update(peer, version, 2n, 0n); - await trackStatus(peer, version, 4n); - if (!(await adapter.acceptBi())) throw new Error("no stream"); - await trackStatus(peer, version, 6n); - await expect(running).rejects.toThrow("too many open requests"); - expect((await pair.client.closed).closeCode).toBe(SessionCode.TooManyRequests); + await update(peer, version, 2n, 0n); + await update(peer, version, 2n, 0n); + await expect(running).rejects.toThrow("request id was already spent"); + expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index b5811b928a0..7b1a1e2d8c6 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -9,9 +9,6 @@ import * as Namespace from "./namespace.ts"; import { initialMaxRequestId, MaxRequestId, REQUEST_WINDOW } from "./request.ts"; import { type IetfVersion, Version } from "./version.ts"; -/** Draft-14 to -16: the request ID is not valid for this peer (wrong parity or a duplicate). */ -const INVALID_REQUEST_ID = 0x4; - /** The peer used a request ID the draft closes the session for. */ export class RequestWindowError extends Error { readonly code: number; @@ -149,6 +146,7 @@ export class ControlStreamAdapter implements Session { // by the IDs they spent and goes out as one MAX_REQUEST_ID. #peerWindow: bigint; #peerMax: bigint; + #peerLast: bigint | undefined; #open = new Set(); #retired = 0n; #granting = false; @@ -482,16 +480,17 @@ export class ControlStreamAdapter implements Session { // Even IDs are the client's. `#client` is this side, so the peer's IDs are the other parity. const even = (requestId & 1n) === 0n; if (even === this.#client) { - this.#refuse(INVALID_REQUEST_ID, "request id has the wrong parity"); + this.#refuse(SessionCode.InvalidRequestId, "request id has the wrong parity"); } } #admitHeld(requestId: bigint): void { this.#checkPeer(requestId); - if (this.#open.has(requestId)) { - this.#refuse(INVALID_REQUEST_ID, "duplicate request id"); + if (this.#peerLast !== undefined && requestId <= this.#peerLast) { + this.#refuse(SessionCode.InvalidRequestId, "request id was already spent"); } this.#checkLive(); + this.#peerLast = requestId; this.#open.add(requestId); } @@ -505,7 +504,7 @@ export class ControlStreamAdapter implements Session { this.#retire(); } - /** A peer reusing IDs below the bound still cannot hold more than the window open. */ + /** Enforce the live-request cap independently of the ID bound. */ #checkLive(): void { if (BigInt(this.#open.size) >= this.#peerWindow) { this.#refuse(SessionCode.TooManyRequests, "too many open requests"); diff --git a/quest/m1/README.md b/quest/m1/README.md index 25c59174d55..ba38b5a091b 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -196,7 +196,6 @@ ladders. - [Datagram replay bound](/quest/m1/datagram-replay-bound.md) - a new Rust datagram subscriber starts within its max delay of the newest datagram, not at a minutes-old buffer - [Catalog colour](/quest/m1/color-catalog.md) - the catalog describes a rendition's colour and HDR properties, which the WebGPU HDR renderer reads - [WebGPU HDR](/quest/m1/webgpu-hdr.md) - HDR renditions play as HDR where the browser and display can show it, and tone-map to SDR elsewhere -- [Request ID order](/quest/m1/request-id-order.md) - drafts 14 to 16 refuse a reused or lower Request ID in both languages - [Import catalog drop](/quest/m1/import-catalog-drop.md) - moq-cli import never drops a catalog producer without finishing it - [WebGPU on Safari](/quest/m1/webgpu-safari.md) - the WebGPU renderer is verified on Safari 26 for macOS and iOS - [A/V sync across a break](/quest/m1/watch-break-av-sync.md) - `@moq/watch` never plays pre-break audio out of sync with pre-break video diff --git a/quest/m1/request-id-order.md b/quest/m1/request-id-order.md deleted file mode 100644 index 086701934dd..00000000000 --- a/quest/m1/request-id-order.md +++ /dev/null @@ -1,25 +0,0 @@ -# [S] A reused moq-transport Request ID is refused - -## Goal - -On drafts 14 to 16, `@moq/net` and `moq-net` refuse an incoming request whose -Request ID is not above every ID the peer already used, instead of accepting -any ID below the window's bound. A peer can no longer reuse an ID that named -an earlier request. - -## Plan - -Found while landing [#5011](https://github.com/moq-dev/moq/pull/5011); it -predates it and covers every request type. `js/net/src/ietf/adapter.ts` -deliberately tolerates reuse today ("A peer reusing IDs below the bound still -cannot hold more than the window open"), bounding only how many are open. - -- First confirm what drafts 14 to 16 require of an out-of-order or repeated - Request ID and which session error they name; follow that. -- Keep a high-water mark of the IDs the peer allocates and refuse anything at - or below it, in both languages, beside the existing window checks. -- Tests: a repeated ID and a lower ID are each refused with the drafts' error; - in-order IDs and the MAX_REQUEST_ID grant are unchanged. - -Public API: none. Wire: no format change, but a stricter receiver closes -sessions with peers that reuse IDs today; report it as an interop change. diff --git a/rs/moq-net/src/error.rs b/rs/moq-net/src/error.rs index d9b38c9eaea..a453718b9c1 100644 --- a/rs/moq-net/src/error.rs +++ b/rs/moq-net/src/error.rs @@ -57,6 +57,10 @@ pub enum SessionError { /// A code this version does not recognize, kept verbatim. #[error("unknown code={0}")] Unknown(u32), + + /// A request ID was reused, decreased, or had the wrong parity. + #[error("invalid request id")] + InvalidRequestId, } impl SessionError { @@ -67,6 +71,7 @@ impl SessionError { Self::Internal => 0x1, Self::Unauthorized => 0x2, Self::ProtocolViolation => 0x3, + Self::InvalidRequestId => 0x4, Self::KeyValueFormatting => 0x6, Self::TooManyRequests => 0x7, Self::GoawayTimeout => 0x10, @@ -88,6 +93,7 @@ impl SessionError { 0x1 => Self::Internal, 0x2 => Self::Unauthorized, 0x3 => Self::ProtocolViolation, + 0x4 => Self::InvalidRequestId, 0x6 => Self::KeyValueFormatting, 0x7 => Self::TooManyRequests, 0x10 => Self::GoawayTimeout, @@ -647,6 +653,7 @@ mod tests { SessionError::Internal, SessionError::Unauthorized, SessionError::ProtocolViolation, + SessionError::InvalidRequestId, SessionError::KeyValueFormatting, SessionError::TooManyRequests, SessionError::GoawayTimeout, @@ -665,6 +672,7 @@ mod tests { // The moq-transport codes we reuse must keep moq-transport's values. assert_eq!(SessionError::Unauthorized.to_code(), 0x2); + assert_eq!(SessionError::InvalidRequestId.to_code(), 0x4); assert_eq!(SessionError::TooManyRequests.to_code(), 0x7); assert_eq!(SessionError::GoawayTimeout.to_code(), 0x10); assert_eq!(SessionError::Version.to_code(), 0x15); diff --git a/rs/moq-net/src/ietf/control.rs b/rs/moq-net/src/ietf/control.rs index 3ba504d7e2b..5996f8eed8f 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -30,6 +30,8 @@ struct Incoming { live: u64, /// Requests retired since the last grant. retired: u64, + /// The highest accepted ID, including requests that already retired. + last: Option, } #[derive(Clone)] @@ -60,6 +62,7 @@ impl Control { sent: max, live: 0, retired: 0, + last: None, }); self } @@ -72,17 +75,22 @@ impl Control { /// Admit a request the peer opened, held open until the returned permit drops. /// /// Closes the session with TOO_MANY_REQUESTS when the ID is at or past the - /// MAX_REQUEST_ID we advertised, or the peer reused IDs to hold more than the window. + /// MAX_REQUEST_ID we advertised, or its live requests exceed the window. + /// Reused or decreasing IDs close with INVALID_REQUEST_ID even after retirement. /// `None` when the peer's requests are not flow controlled. pub fn accept(&self, id: RequestId) -> Result, Error> { let mut state = self.state.lock(); let Some(incoming) = &mut state.incoming else { return Ok(None); }; + if incoming.last.is_some_and(|last| id <= last) { + return Err(SessionError::InvalidRequestId.into()); + } if id.0 >= incoming.max || incoming.live >= incoming.window { tracing::warn!(id = %id, max = incoming.max, live = incoming.live, "peer exceeded MAX_REQUEST_ID"); return Err(SessionError::TooManyRequests.into()); } + incoming.last = Some(id); incoming.live += 1; Ok(Some(RequestPermit(self.clone()))) } @@ -200,13 +208,15 @@ mod tests { assert!(control.accept(RequestId(12)).is_err()); } - /// A peer reusing request IDs below the window still cannot hold more than it. + /// An ID remains spent after its request ends, across every request type. #[test] - fn reused_ids_cannot_exceed_the_window() { - let control = Control::new(None, false).with_window(2, false); - let _a = control.accept(RequestId(0)).unwrap(); - let _b = control.accept(RequestId(0)).unwrap(); - assert!(control.accept(RequestId(2)).is_err()); + fn retired_or_lower_request_ids_are_refused() { + for (first, later) in [(0, 0), (4, 2)] { + let control = Control::new(None, false).with_window(8, false); + drop(control.accept(RequestId(first)).unwrap()); + let err = control.accept(RequestId(later)).err().expect("spent ID refused"); + assert_eq!(SessionError::from(&err).to_code(), 0x4); + } } /// Draft-17+ has no MAX_REQUEST_ID: nothing is tracked or granted. From f1ab95176810168b1bb167b99a56197bd02b06cb Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 14:25:41 -0700 Subject: [PATCH 03/13] chore(quest): claim AUTH protocol violations Co-Authored-By: GPT-6 From ec90c5591fc0b778be98e3959692c949e0fc697e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 14:46:56 -0700 Subject: [PATCH 04/13] fix(net): close sessions on malformed AUTH messages Co-Authored-By: GPT-6 --- drafts/draft-lcurley-moq-auth.md | 5 ++ drafts/draft-lcurley-moq-lite.md | 3 + js/net/src/auth.test.ts | 122 +++++++++++++++++++++++++++++- js/net/src/auth_session.ts | 53 ++++++++++--- js/net/src/ietf/auth.ts | 1 + js/net/src/ietf/connection.ts | 6 ++ js/net/src/lite/auth.ts | 1 + js/net/src/lite/connection.ts | 11 ++- rs/moq-net/src/auth.rs | 13 ++++ rs/moq-net/src/ietf/auth.rs | 47 ++++++++++-- rs/moq-net/src/ietf/session.rs | 124 ++++++++++++++++++++++++++++++- rs/moq-net/src/lite/publisher.rs | 18 ++++- rs/moq-net/src/lite/session.rs | 62 +++++++++++----- 13 files changed, 423 insertions(+), 43 deletions(-) diff --git a/drafts/draft-lcurley-moq-auth.md b/drafts/draft-lcurley-moq-auth.md index af2674cfdbf..7726cdb1935 100644 --- a/drafts/draft-lcurley-moq-auth.md +++ b/drafts/draft-lcurley-moq-auth.md @@ -89,6 +89,9 @@ After an AUTH_ERROR the acceptor closes its side of the stream. The presenter withdraws a token by closing or resetting its side of the stream, and the acceptor then closes its own. An acceptor that closes its side without an AUTH_ERROR ends the grant without a reason. +A receiver MUST close the session with PROTOCOL_VIOLATION if an AUTH message fails to decode, has trailing body bytes, or has an unexpected type for its direction. +A presenter sends exactly one AUTH on its request stream; any additional message is a PROTOCOL_VIOLATION. + ## Scope {#scope} An endpoint's scope is the union of the grants of its open Auth requests. @@ -162,6 +165,7 @@ A namespace prefix the presenter may subscribe to and discover. **Expires**: The number of milliseconds until the grant lapses, or 0 for never. +Expires MUST NOT exceed 2^53 - 1 milliseconds; a larger value is a PROTOCOL_VIOLATION. The acceptor revokes a lapsed grant with AUTH_ERROR, and the presenter uses Expires to present a replacement token in time. ## AUTH_ERROR {#auth-error} @@ -179,6 +183,7 @@ AUTH_ERROR Message { ~~~ **Error Code**: +A code MUST fit in 32 bits; a larger value is a PROTOCOL_VIOLATION. A code from the REQUEST_ERROR registry ({{moqt}}): UNAUTHORIZED, MALFORMED_AUTH_TOKEN, EXPIRED_AUTH_TOKEN, or NOT_SUPPORTED ({{prefixes}}). **Reason Phrase**: diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index cd636a8f271..9efa05a72b9 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -1447,6 +1447,9 @@ A pattern that breaks any of these rules is a PROTOCOL_VIOLATION. A grant covers a request when one of its patterns matches the path. +An endpoint MUST close the session with PROTOCOL_VIOLATION when an AUTH stream message fails to decode, has trailing body bytes, or has an unexpected type for its direction. +The opener sends exactly one AUTH; any additional message on its side is a PROTOCOL_VIOLATION. + ## AUTH_ERROR {#auth-error} AUTH_ERROR refuses a token, or revokes it after an AUTH_OK. diff --git a/js/net/src/auth.test.ts b/js/net/src/auth.test.ts index 44dec229d1c..46384cd4156 100644 --- a/js/net/src/auth.test.ts +++ b/js/net/src/auth.test.ts @@ -3,13 +3,16 @@ import type { Getter } from "@moq/signals"; import { type Grant, type Issued, Unsupported } from "./auth.ts"; import { accept as acceptSession, connect as connectSession, type Established } from "./connection/index.ts"; import { SessionCode, SessionError, StreamCode, toStreamCode } from "./error.ts"; +import { AuthMessage as IetfAuthMessage, IetfAuthWire } from "./ietf/auth.ts"; import * as Ietf from "./ietf/index.ts"; -import { LiteAuthWire } from "./lite/auth.ts"; +import * as IetfMessage from "./ietf/message.ts"; +import { AuthMessage as LiteAuthMessage, LiteAuthWire } from "./lite/auth.ts"; import * as Lite from "./lite/index.ts"; +import * as LiteMessage from "./lite/message.ts"; import { createMockTransportPair, type MockTransport } from "./mock.ts"; import { Producer as OriginProducer } from "./origin.ts"; import * as Path from "./path.ts"; -import { Writer } from "./stream.ts"; +import { type Stream, Writer } from "./stream.ts"; import { withTimeout } from "./util/timeout.ts"; import { wireOf } from "./wire.ts"; @@ -394,3 +397,118 @@ test("a revoked grant resets its subscriptions with UNAUTHORIZED", async () => { server.abort(); } }); + +// Capture the acceptor's actual AUTH writer after a valid setup grant. The following +// bytes travel through the same connection dispatcher and presenter as a real peer. +describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_17, Ietf.ALPN.DRAFT_22])("AUTH violations %s", (protocol) => { + test("a second presenter message closes the session", async () => { + const streams: Stream[] = []; + const prototype = protocol === Lite.ALPN_07_WIP ? LiteAuthWire.prototype : IetfAuthWire.prototype; + const original = prototype.present; + const spy = spyOn(prototype, "present").mockImplementation(async function ( + this: LiteAuthWire & IetfAuthWire, + token, + ) { + const stream = await original.call(this, token); + streams.push(stream); + return stream; + }); + let pair: Pair | undefined; + try { + pair = await connect({ protocol }); + await waitFor(pair.client.auth.grant, (g) => g !== undefined); + await waitFor(pair.server.auth.grant, (g) => g !== undefined); + await streams[0]?.writer.u53(0); + expect((await withTimeout(pair.transport.closed, 100, "second AUTH did not close")).closeCode).toBe( + SessionCode.ProtocolViolation, + ); + } finally { + spy.mockRestore(); + pair?.client.abort(); + pair?.server.abort(); + } + }); + + test("a malformed presenter AUTH closes the session", async () => { + const pair = await connect({ protocol }); + await waitFor(pair.client.auth.grant, (g) => g !== undefined); + await waitFor(pair.server.auth.grant, (g) => g !== undefined); + const lite = protocol === Lite.ALPN_07_WIP; + const prototype = lite ? LiteAuthMessage.prototype : IetfAuthMessage.prototype; + const spy = spyOn(prototype, "encode").mockImplementation(async (writer: Writer) => { + const body = async (w: Writer) => { + if (!lite) await w.u62(2n); + await w.u53(2); + }; + if (lite) await LiteMessage.encode(writer, body); + else await IetfMessage.encode(writer, body, IetfAuthMessage.id); + }); + const added = pair.client.auth.add("bad").catch(() => undefined); + try { + expect((await withTimeout(pair.transport.closed, 100, "malformed AUTH did not close")).closeCode).toBe( + SessionCode.ProtocolViolation, + ); + } finally { + spy.mockRestore(); + pair.client.abort(); + pair.server.abort(); + await added; + } + }); + + for (const fault of [ + "unknown reply", + "oversized code", + "truncated error", + "extra body", + "oversized expiry", + ] as const) { + test(`${fault} closes the session`, async () => { + const streams: Stream[] = []; + const lite = protocol === Lite.ALPN_07_WIP; + const prototype = lite ? LiteAuthWire.prototype : IetfAuthWire.prototype; + const original = prototype.grant; + const spy = spyOn(prototype, "grant").mockImplementation(async function ( + this: LiteAuthWire & IetfAuthWire, + stream, + value, + ) { + streams.push(stream); + await original.call(this, stream, value); + }); + let pair: Pair | undefined; + try { + pair = await connect({ protocol }); + await waitFor(pair.client.auth.grant, (g) => g !== undefined); + await waitFor(pair.server.auth.grant, (g) => g !== undefined); + const stream = streams[0]; + if (!stream) throw new Error("setup AUTH writer missing"); + if (fault === "unknown reply") { + await stream.writer.u53(99); + } else { + const body = async (writer: Writer) => { + if (fault === "oversized expiry") { + await writer.u53(0); + await writer.u53(0); + await writer.u62(1n << 53n); + } else { + await writer.u62(fault === "oversized code" ? 1n << 32n : 1n); + if (fault !== "truncated error") await writer.string(""); + if (fault === "extra body") await writer.u53(0); + } + }; + const id = fault === "oversized expiry" ? (lite ? 0 : 0x40b62) : lite ? 1 : 0x40b63; + if (lite) await LiteMessage.encode(stream.writer, body, { id }); + else await IetfMessage.encode(stream.writer, body, id); + } + expect((await withTimeout(pair.transport.closed, 100, "malformed AUTH did not close")).closeCode).toBe( + SessionCode.ProtocolViolation, + ); + } finally { + spy.mockRestore(); + pair?.client.abort(); + pair?.server.abort(); + } + }); + } +}); diff --git a/js/net/src/auth_session.ts b/js/net/src/auth_session.ts index ac9c3824c65..c790fbb90ef 100644 --- a/js/net/src/auth_session.ts +++ b/js/net/src/auth_session.ts @@ -1,4 +1,4 @@ -import { type Dispose, type Getter, Signal } from "@moq/signals"; +import { type Dispose, type Getter, Once, Signal } from "@moq/signals"; import { type Auth as AuthApi, type Grant, @@ -9,7 +9,7 @@ import { type Token, Unsupported, } from "./auth.ts"; -import { closeReason, error, SessionCode, StreamCode, StreamError } from "./error.ts"; +import { closeReason, error, ProtocolViolation, SessionCode, SessionError, StreamCode, StreamError } from "./error.ts"; import * as Path from "./path.ts"; import type { Stream } from "./stream.ts"; @@ -154,10 +154,15 @@ class IssuedGrant implements Issued { this.#stream = stream; this.#wire = wire; // The presenter withdraws by closing or cancelling its side. - this.closed = stream.reader.closed.then( - () => null, - (err: unknown) => (err instanceof StreamError && err.code === StreamCode.Cancel ? null : error(err)), - ); + this.closed = stream.reader + .done() + .then((done) => { + if (!done) throw new ProtocolViolation("additional message on an AUTH stream"); + }) + .then( + () => null, + (err: unknown) => (err instanceof StreamError && err.code === StreamCode.Cancel ? null : error(err)), + ); } #write(write: () => Promise) { @@ -237,6 +242,8 @@ export interface AuthSessionProps { * @internal */ export class AuthSession implements AuthApi { + /** The peer sent a malformed AUTH message; the owning connection closes the session. */ + readonly violated = new Once(); #wire?: AuthWire; #peerGrant: Grant; @@ -297,7 +304,19 @@ export class AuthSession implements AuthApi { const wire = this.#wire; if (!wire) throw new Error("auth not negotiated"); - const token = await wire.accept(stream); + let token: Uint8Array; + try { + token = await wire.accept(stream); + } catch (err) { + if ( + err instanceof StreamError || + err instanceof SessionError || + err instanceof Unsupported || + (typeof err === "object" && err !== null && "source" in err && err.source === "session") + ) + throw err; + throw new ProtocolViolation("malformed AUTH", { cause: err }); + } await this.#decided; if (this.#acceptor === "undecided") this.#acceptor = "default"; @@ -314,8 +333,9 @@ export class AuthSession implements AuthApi { issued.update(this.#peerGrant); } - await issued.closed; + const ended = await issued.closed; issued.close(); + if (ended instanceof ProtocolViolation) throw ended; } /** End the session: fail every pending token and close the requests. */ @@ -344,7 +364,19 @@ export class AuthSession implements AuthApi { if (token.withdrawn) throw cancel(); for (;;) { - const reply = await wire.read(stream); + let reply: WireReply | undefined; + try { + reply = await wire.read(stream); + } catch (err) { + if ( + err instanceof StreamError || + err instanceof SessionError || + err instanceof Unsupported || + (typeof err === "object" && err !== null && "source" in err && err.source === "session") + ) + throw err; + throw new ProtocolViolation("malformed AUTH reply", { cause: err }); + } if (!reply) { // The peer ended the grant without revoking it, or closed without ever // answering. Close our side too, which is what settles its Issued.closed. @@ -376,6 +408,9 @@ export class AuthSession implements AuthApi { result = new Unsupported(); } else { result = error(err); + if (result instanceof ProtocolViolation && !this.#closed && this.violated.peek() === undefined) { + this.violated.set(result); + } if (!this.#closed) console.warn("auth token ended", result); } } diff --git a/js/net/src/ietf/auth.ts b/js/net/src/ietf/auth.ts index 672b19db5e0..61a2cc58533 100644 --- a/js/net/src/ietf/auth.ts +++ b/js/net/src/ietf/auth.ts @@ -195,6 +195,7 @@ export class AuthError { guard(version); return Message.decode(r, async (rd) => { const code = await rd.u53(); + if (code > 0xffffffff) throw new Error("AUTH_ERROR code exceeds 32 bits"); const reason = await rd.string(); if (new TextEncoder().encode(reason).byteLength > MAX_REASON) { throw new Error("AUTH_ERROR reason exceeds 8,192 bytes"); diff --git a/js/net/src/ietf/connection.ts b/js/net/src/ietf/connection.ts index 37beef5614d..23a9d99e611 100644 --- a/js/net/src/ietf/connection.ts +++ b/js/net/src/ietf/connection.ts @@ -199,6 +199,12 @@ export class Connection implements Established { }); this.#client = client; + this.#auth.violated.subscribe((err) => { + if (!err) return; + this.#quic.close({ closeCode: SessionCode.ProtocolViolation, reason: err.message }); + this.abort(); + }); + this.#publisher = new Publisher({ quic: this.#quic, session: this.#session, diff --git a/js/net/src/lite/auth.ts b/js/net/src/lite/auth.ts index 47b7d99cf6e..335ce3bdf42 100644 --- a/js/net/src/lite/auth.ts +++ b/js/net/src/lite/auth.ts @@ -127,6 +127,7 @@ export class AuthError { static async #decode(r: Reader): Promise { const code = await r.u53(); + if (code > 0xffffffff) throw new Error("AUTH_ERROR code exceeds 32 bits"); const reason = await r.string(); if (new TextEncoder().encode(reason).byteLength > MAX_REASON) { throw new Error("AUTH_ERROR reason exceeds 8,192 bytes"); diff --git a/js/net/src/lite/connection.ts b/js/net/src/lite/connection.ts index 51b96cec6ea..7584b5dbc00 100644 --- a/js/net/src/lite/connection.ts +++ b/js/net/src/lite/connection.ts @@ -159,6 +159,12 @@ export class Connection implements Established { subscribe: new Path.Patterns(publish ? [Path.Pattern.all()] : []), }, }); + this.#auth.violated.subscribe((err) => { + if (!err) return; + this.#quic.close({ closeCode: SessionCode.ProtocolViolation, reason: err.message }); + this.abort(); + }); + this.#publisher = new Publisher(this.#quic, this.#version, this.hop, publish, { grant: this.#auth.grant, ready: this.#auth.setupAnswered(), @@ -299,7 +305,10 @@ export class Connection implements Established { // A protocol violation on one stream is the peer breaking the session. // Resetting that stream leaves it free to repeat the violation; a duplicate // GOAWAY is the one this dispatcher raises. - if (err instanceof ProtocolViolation) this.abort(); + if (err instanceof ProtocolViolation) { + this.#quic.close({ closeCode: SessionCode.ProtocolViolation, reason: err.message }); + this.abort(); + } }) .finally(() => { stream.writer.close(); diff --git a/rs/moq-net/src/auth.rs b/rs/moq-net/src/auth.rs index db5028d0766..167e9318ca4 100644 --- a/rs/moq-net/src/auth.rs +++ b/rs/moq-net/src/auth.rs @@ -30,6 +30,19 @@ use bytes::Bytes; use crate::{Error, Patterns, Result, SessionError, time::Instant}; +/// Classify malformed AUTH messages separately from transport and local failures. +pub(crate) fn protocol_error(err: Error) -> Error { + match err { + Error::Decode(_) + | Error::BoundsExceeded(_) + | Error::WrongSize + | Error::TooManyParameters + | Error::UnexpectedMessage + | Error::UnexpectedStream => Error::ProtocolViolation, + err => err, + } +} + /// `ready!` for a `kio::Shared` poll, which yields a guard rather than a result. macro_rules! ready_or { ($e:expr) => { diff --git a/rs/moq-net/src/ietf/auth.rs b/rs/moq-net/src/ietf/auth.rs index 8f5fc3dc17c..e35d6a4c379 100644 --- a/rs/moq-net/src/ietf/auth.rs +++ b/rs/moq-net/src/ietf/auth.rs @@ -140,7 +140,8 @@ impl Message for AuthOk { let subscribe = decode_prefixes(r)?; let expires = match r.varint()? { 0 => None, - ms => Some(Duration::from_millis(ms)), + ms if ms <= MAX_EXPIRES_MS => Some(Duration::from_millis(ms)), + _ => return Err(DecodeError::InvalidValue), }; Ok(Self { publish, @@ -177,6 +178,9 @@ impl Message for AuthError { fn decode_msg(r: &mut Decoder<'_>, version: Version) -> Result { check_version(version)?; let code = r.varint()?; + if code > u32::MAX as u64 { + return Err(DecodeError::InvalidValue); + } let len = r.varint()?; if len > MAX_REASON as u64 { return Err(DecodeError::InvalidValue); @@ -232,7 +236,8 @@ pub(super) async fn run_present( peer_setup: peer::PeerSetup, version: Version, going_away: crate::goaway::GoingAway, -) where +) -> Result<(), Error> +where S: crate::transport::poll::Boxable, { if !peer_setup.get().await.auth { @@ -241,7 +246,19 @@ pub(super) async fn run_present( } let mut tasks = crate::util::TaskSet::owned(); - while let Some((id, token)) = tasks.drive(|waiter| handle.poll_opening(waiter)).await { + let violation: kio::Shared> = Default::default(); + while let Some((id, token)) = tasks + .drive(|waiter| { + if let Poll::Ready(mut err) = violation.poll(waiter, |err| match err.is_some() { + true => Poll::Ready(()), + false => Poll::Pending, + }) { + return Poll::Ready(Err(err.take().unwrap())); + } + handle.poll_opening(waiter).map(Ok) + }) + .await? + { let present = Present { runtime: runtime.clone(), session: session.clone(), @@ -251,12 +268,18 @@ pub(super) async fn run_present( id, }; let going_away = going_away.clone(); + let violation = violation.clone(); tasks.push(async move { // After a GOAWAY the peer must not see new requests. let err = match going_away.is_set() { true => Error::GoingAway, false => present.run(token).await, }; + let err = crate::auth::protocol_error(err); + if matches!(err, Error::ProtocolViolation) { + violation.lock().get_or_insert(err.clone()); + } + match &err { Error::Cancel | Error::Unsupported | Error::Transport(_) | Error::Session(_) => { tracing::debug!(%err, "auth token ended") @@ -404,7 +427,7 @@ impl Serve { mut stream: Stream, msg: Auth, version: Version, - ) { + ) -> Result<(), Error> { let serving = Serving::new(self.handle.clone()); let issue = &serving.issue; let mut default = None; @@ -423,7 +446,7 @@ impl Serve { if stream.writer.encode_message(&reply).await.is_ok() { let _ = stream.writer.close().await; } - return; + return Ok(()); } // Re-sent whenever a new limit changes it. None => default = Some(crate::auth::DefaultGrant::new(self.handle.clone(), self.peer_grant)), @@ -441,7 +464,12 @@ impl Serve { err } }; - serving.end(err); + let err = crate::auth::protocol_error(err); + serving.end(err.clone()); + if matches!(err, Error::ProtocolViolation) { + return Err(err); + } + Ok(()) } } @@ -462,7 +490,12 @@ async fn serve_issue( loop { let next = kio::wait(|waiter| { let mut cx = std::task::Context::from_waker(waiter.waker()); - if let Poll::Ready(res) = stream.reader.poll_closed(&mut cx) { + if let Poll::Ready(res) = stream.reader.poll_read_chunk(&mut cx, 1).map(|res| { + res.and_then(|chunk| match chunk { + None => Ok(()), + Some(_) => Err(Error::ProtocolViolation), + }) + }) { return Poll::Ready(Next::Withdrawn(res)); } if let Some(default) = default.as_deref_mut() diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index e5f9dfb6a97..24dfacb5196 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -487,7 +487,7 @@ where ))); let mut datagrams = std::pin::pin!(err_only(run_datagrams(session.clone(), subscriber.clone()))); let mut goaway_recv = std::pin::pin!(err_only(goaway_recv)); - let mut present = std::pin::pin!(present); + let mut present = std::pin::pin!(err_only(present)); let mut enforce = std::pin::pin!(err_only(enforce)); let mut setup = std::pin::pin!(setup); // Unsolicited PUBLISH_NAMESPACE unless the peer requires solicitation; @@ -532,8 +532,10 @@ where if let Poll::Ready(err) = waiter.poll_future(goaway_recv.as_mut()) { return Poll::Ready(Err(err)); } - // Presenting tokens never ends the session. - let _ = waiter.poll_future(present.as_mut()); + // Malformed replies are session-fatal, unlike token refusals. + if let Poll::Ready(err) = waiter.poll_future(present.as_mut()) { + return Poll::Ready(Err(err)); + } if let Poll::Ready(err) = waiter.poll_future(enforce.as_mut()) { return Poll::Ready(Err(err)); } @@ -1122,6 +1124,7 @@ where let declared = subscriber.solicit().await; let mut tasks = TaskSet::owned(); + let violation: kio::Shared> = Default::default(); // Each AUTH serve task settles `Issued::closed` from whatever the auth handle // holds when it drops, and the first reason wins. So the error that ends the // session has to reach the handle while `tasks` is still alive, or every grant @@ -1136,6 +1139,12 @@ where loop { let mut stream = tasks .drive(|waiter| { + if let std::task::Poll::Ready(mut err) = violation.poll(waiter, |err| match err.is_some() { + true => std::task::Poll::Ready(()), + false => std::task::Poll::Pending, + }) { + return std::task::Poll::Ready(Err(err.take().unwrap())); + } let mut cx = waiter.context(); Stream::poll_accept(&mut accept, version, &mut cx) }) @@ -1146,6 +1155,12 @@ where let mut hdr_id: Option = None; let header = tasks .drive(|waiter| { + if let std::task::Poll::Ready(mut err) = violation.poll(waiter, |err| match err.is_some() { + true => std::task::Poll::Ready(()), + false => std::task::Poll::Pending, + }) { + return std::task::Poll::Ready(Err(err.take().unwrap())); + } let mut cx = waiter.context(); let id = match hdr_id { Some(id) => id, @@ -1204,7 +1219,13 @@ where if !data.is_empty() { return Err(Error::WrongSize); } - tasks.push(serve.clone().run(stream, msg, version)); + let serve = serve.clone(); + let violation = violation.clone(); + tasks.push(async move { + if let Err(err) = serve.run(stream, msg, version).await { + violation.lock().get_or_insert(err); + } + }); } _ => { tracing::warn!(id, "unexpected bidi stream type"); @@ -1324,6 +1345,101 @@ mod tests { use super::*; use crate::model::ProduceTest; + #[moq_net_sim::test] + async fn malformed_auth_replies_close_the_session() { + for version in [Version::Draft17, Version::Draft22] { + for (id, body) in [ + (99, vec![]), + (auth::AuthError::ID, vec![0xc0, 0, 0, 1, 0, 0, 0, 0, 0]), + (auth::AuthError::ID, vec![1]), + (auth::AuthError::ID, vec![1, 0, 0]), + (auth::AuthOk::ID, vec![0, 0, 0xc0, 0x20, 0, 0, 0, 0, 0, 0]), + ] { + let mut script = Vec::new(); + let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); + encoder.varint(id).unwrap(); + encoder.u16(body.len() as u16); + script.extend(body); + let session = crate::lite::test_transport::ScriptedSession::per_stream(vec![script]); + let log = session.log.clone(); + let (driver, _, _) = start(Config { + runtime: crate::time::Clock::sim(), + session, + setup: None, + request_id_max: None, + limits: Default::default(), + client: true, + publish: None, + subscribe: None, + peer_hop: None, + cost: None, + version, + path: None, + authority: None, + peer_setup_stream: None, + peer_declared: Some(peer::Peer { + auth: true, + ..Default::default() + }), + auth: crate::auth::Handle::new(true), + early_unis: Vec::new(), + }) + .unwrap(); + let err = moq_net_sim::timeout(std::time::Duration::from_secs(10), driver) + .await + .expect("the session outlived a malformed AUTH reply") + .expect_err("malformed AUTH is fatal"); + assert!(is_protocol_violation(&err), "{err:?}"); + assert_eq!(log.closes()[0].0, SessionError::ProtocolViolation.to_code()); + } + } + } + + #[moq_net_sim::test] + async fn malformed_presenter_auth_closes_the_session() { + for version in [Version::Draft17, Version::Draft22] { + for body in [vec![0, 2], vec![0, 0, 0]] { + let mut script = Vec::new(); + let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); + encoder.varint(auth::Auth::ID).unwrap(); + encoder.u16(2); + script.extend(body); + let session = + crate::lite::test_transport::ScriptedSession::new(Vec::new()).with_incoming_bidis(vec![script]); + let log = session.log.clone(); + let (driver, _, _) = start(Config { + runtime: crate::time::Clock::sim(), + session, + setup: None, + request_id_max: None, + limits: Default::default(), + client: false, + publish: None, + subscribe: None, + peer_hop: None, + cost: None, + version, + path: None, + authority: None, + peer_setup_stream: None, + peer_declared: Some(peer::Peer { + auth: true, + ..Default::default() + }), + auth: crate::auth::Handle::new(true), + early_unis: Vec::new(), + }) + .unwrap(); + let err = moq_net_sim::timeout(std::time::Duration::from_secs(10), driver) + .await + .expect("malformed presenter AUTH was ignored") + .expect_err("malformed AUTH is fatal"); + assert!(is_protocol_violation(&err), "{err:?}"); + assert_eq!(log.closes()[0].0, SessionError::ProtocolViolation.to_code()); + } + } + } + fn occurrences(log: &crate::lite::test_transport::Log, needle: &[u8]) -> usize { let writes = log.writes.lock().unwrap(); writes.windows(needle.len()).filter(|window| *window == needle).count() diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 0bc24118f7f..970888e397a 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -417,7 +417,7 @@ impl AuthServe { } fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { - let res = ready!(self.poll_serve(waiter)); + let res = ready!(self.poll_serve(waiter)).map_err(crate::auth::protocol_error); let err = match res { Ok(()) => Error::Cancel, Err(err) => { @@ -434,7 +434,14 @@ impl AuthServe { } }; if let Some(serving) = &self.serving { - serving.end(err); + serving.end(err.clone()); + } + if matches!(err, Error::ProtocolViolation) { + self.shared.auth.close(err.clone()); + self.shared + .session + .clone() + .close(SessionError::ProtocolViolation.to_code(), &err.to_string()); } Poll::Ready(Ok(())) } @@ -479,7 +486,12 @@ impl AuthServe { // The presenter withdrew the token, by FIN or by a cancelling reset, or its // stream died. - if let Poll::Ready(res) = stream.reader.poll_closed(&mut cx) { + if let Poll::Ready(res) = stream.reader.poll_read_chunk(&mut cx, 1).map(|res| { + res.and_then(|chunk| match chunk { + None => Ok(()), + Some(_) => Err(Error::ProtocolViolation), + }) + }) { match res { Ok(()) | Err(Error::Stream(crate::StreamError::Cancel)) => {} Err(err) => return Poll::Ready(Err(err)), diff --git a/rs/moq-net/src/lite/session.rs b/rs/moq-net/src/lite/session.rs index a40940f4834..0af3ef146d3 100644 --- a/rs/moq-net/src/lite/session.rs +++ b/rs/moq-net/src/lite/session.rs @@ -546,8 +546,8 @@ impl kio::Task for PresentToken { // A peer that predates AUTH resets a stream type it does not know, and one // that takes no tokens in band refuses the same way: either way, before any // reply, this token earns nothing here. - Error::Stream(_) | Error::Decode(crate::DecodeError::Short) if !answered => Error::Unsupported, - err => err, + Error::Stream(_) if !answered => Error::Unsupported, + err => crate::auth::protocol_error(err), }; match &err { Error::Cancel | Error::Unsupported | Error::Transport(_) | Error::Session(_) => { @@ -604,7 +604,7 @@ impl PresentToken { // The peer ended the grant without revoking it, or closed without // ever answering (mapped to unsupported by the caller). Ok(None) if *answered => return Poll::Ready(Error::Cancel), - Ok(None) => return Poll::Ready(Error::Decode(crate::DecodeError::Short)), + Ok(None) => return Poll::Ready(Error::Unsupported), Err(err) => return Poll::Ready(err), }; match reply { @@ -885,21 +885,20 @@ mod tests { use crate::coding::Encode; use crate::lite::test_transport::ScriptedSession; - /// An AUTH_ERROR code the session registry cannot hold is the peer breaking the - /// protocol: it closes the session rather than surfacing a code the peer never sent. #[moq_net_sim::test] - async fn an_auth_error_code_past_u32_closes_the_session() { - let refused = crate::lite::AuthReply::Error(crate::lite::AuthError { - code: u64::from(u32::MAX) + 1, - reason: String::new(), - }); - let mut script = Vec::new(); - refused - .encode( - &mut crate::coding::Encoder::new(&mut script, Version::Lite07.into()), - Version::Lite07, - ) - .unwrap(); + async fn malformed_auth_replies_close_the_session() { + // Unknown reply, invalid code, truncated error, and trailing body bytes. + for script in [ + vec![99], + vec![1, 9, 0xc0, 0, 0, 1, 0, 0, 0, 0, 0], + vec![1, 1, 1], + vec![1, 3, 1, 0, 0], + ] { + malformed_auth_reply(script).await; + } + } + + async fn malformed_auth_reply(script: Vec) { // The setup token's AUTH stream is the first one the session opens. let transport = ScriptedSession::per_stream(vec![script]); let log = transport.log.clone(); @@ -931,6 +930,35 @@ mod tests { assert_eq!(log.closes()[0].0, SessionError::ProtocolViolation.to_code()); } + #[moq_net_sim::test] + async fn malformed_presenter_auth_closes_the_session() { + for script in [vec![7, 1, 2], vec![7, 1, 0, 0]] { + let transport = ScriptedSession::new(Vec::new()).with_incoming_bidis(vec![script]); + let log = transport.log.clone(); + let start = start(Config { + runtime: crate::time::Clock::sim(), + client: false, + limits: Default::default(), + session: transport, + setup_stream: None, + publish: None, + subscribe: None, + peer_hop: None, + version: Version::Lite07, + our_setup: Setup::default(), + peer_setup: None, + auth: crate::auth::Handle::new(true), + }) + .unwrap(); + let mut driver = start.driver; + let _ = driver.poll(&kio::Waiter::noop()); + assert_eq!( + log.closes().first().map(|close| close.0), + Some(SessionError::ProtocolViolation.to_code()) + ); + } + } + #[moq_net_sim::test] async fn accept_setup_does_not_wait_on_an_early_stream_header() { let version = Version::Lite05; From def07267d2afc59fda225466565738bc6268b594 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 14:58:29 -0700 Subject: [PATCH 05/13] quest: retain AUTH request-ID validation dependency Co-Authored-By: GPT-6 --- quest/m1/auth/violations.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/quest/m1/auth/violations.md b/quest/m1/auth/violations.md index 28ab20f7695..4325ebef555 100644 --- a/quest/m1/auth/violations.md +++ b/quest/m1/auth/violations.md @@ -39,3 +39,5 @@ regression test per case, and check the lite and IETF draft text agrees. Run `just test interop --all`. Public API: none. Wire: none beyond draft wording. + +**Draft progress:** [#5279](https://github.com/moq-dev/moq/pull/5279) implements malformed AUTH decoding, unexpected and extra messages, expiry/code bounds, and session-fatal propagation in Rust and JS. The quest remains open for AUTH Request-ID ordering and reuse, which must use the common validator being implemented in [#5235](https://github.com/moq-dev/moq/pull/5235), rather than introduce a competing AUTH-only validator. From 97776e4b47a28817e18b7ddb94a96a58a340986d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 17:52:38 -0700 Subject: [PATCH 06/13] fix(net): require the next incoming IETF request ID Co-Authored-By: GPT-6 Astra --- drafts/draft-lcurley-moq-lite.md | 2 +- js/net/src/error.ts | 2 +- js/net/src/ietf/adapter.test.ts | 46 ++++++++++++++++++------- js/net/src/ietf/adapter.ts | 9 ++--- rs/moq-net/src/error.rs | 2 +- rs/moq-net/src/ietf/control.rs | 59 ++++++++++++++++++++++++++------ 6 files changed, 91 insertions(+), 29 deletions(-) diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index bc4c208f683..e9cedb117fe 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -284,7 +284,7 @@ Sent when terminating the session, via the transport's session close. | ------- | ------------- | ----------- | | 0x3 | PROTOCOL_VIOLATION | The peer violated this specification. | | ------- | ------------- | ----------- | -| 0x4 | INVALID_REQUEST_ID | A moq-transport request ID was reused, decreased, or had the wrong parity; moq-lite has no request IDs. | +| 0x4 | INVALID_REQUEST_ID | A moq-transport request ID was reused, skipped, decreased, or had the wrong parity; moq-lite has no request IDs. | | ------- | ------------- | ----------- | | 0x6 | KEY_VALUE_FORMATTING_ERROR | A key-value pair was malformed, or repeated more than allowed. | | ------- | ------------- | ----------- | diff --git a/js/net/src/error.ts b/js/net/src/error.ts index 3920c056ded..c0e7d6ea8eb 100644 --- a/js/net/src/error.ts +++ b/js/net/src/error.ts @@ -36,7 +36,7 @@ export const SessionCode = Object.freeze( Unauthorized: 0x2 as SessionCode, /** A protocol rule was broken; the session is unusable. */ ProtocolViolation: 0x3 as SessionCode, - /** A request ID was reused, decreased, or had the wrong parity. */ + /** A request ID was reused, skipped, decreased, or had the wrong parity. */ InvalidRequestId: 0x4 as SessionCode, /** A key-value pair was malformed or repeated more than allowed. */ KeyValueFormatting: 0x6 as SessionCode, diff --git a/js/net/src/ietf/adapter.test.ts b/js/net/src/ietf/adapter.test.ts index 7196f0a9409..a67717bfd0d 100644 --- a/js/net/src/ietf/adapter.test.ts +++ b/js/net/src/ietf/adapter.test.ts @@ -373,10 +373,10 @@ test("a request id with the wrong parity closes the session", async () => { test("a second use of an open request id closes the session", async () => { for (const [version, alpn] of WINDOW_DRAFTS) { - const { pair, peer, running } = await windowed(version, alpn, 2n, false); + const { pair, peer, running } = await windowed(version, alpn, 1n, false); await trackStatus(peer, version, 0n); await trackStatus(peer, version, 0n); - await expect(running).rejects.toThrow("request id was already spent"); + await expect(running).rejects.toThrow("request id is not next in sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); @@ -416,9 +416,9 @@ test("drafts 14 to 16 route an update to its second request id", async () => { for (const [version, alpn] of WINDOW_DRAFTS) { const { adapter, peer, running } = await windowed(version, alpn, 4n, false); const state = settled(running); - const target = await subscribe(adapter, peer, version, 2n); - await update(peer, version, 6n, 2n); - expect(await updated(target, version)).toBe(2n); + const target = await subscribe(adapter, peer, version, 0n); + await update(peer, version, 2n, 0n); + expect(await updated(target, version)).toBe(0n); expect(await state()).toBeUndefined(); } }); @@ -470,7 +470,7 @@ test("an update spending an open request id closes the session", async () => { await trackStatus(peer, version, heldId); if (!(await adapter.acceptBi())) throw new Error("no stream"); await update(peer, version, heldId, heldId); - await expect(running).rejects.toThrow("request id was already spent"); + await expect(running).rejects.toThrow("request id is not next in sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } } @@ -486,12 +486,14 @@ test("retired and lower request ids close the session", async () => { [parity + 4n, parity + 2n], ] as const) { const { pair, adapter, peer, running } = await windowed(version, alpn, 8n, client); - await trackStatus(peer, version, first); - const stream = await adapter.acceptBi(); - if (!stream) throw new Error("no stream"); - stream.close(); + for (let id = parity; id <= first; id += 2n) { + await trackStatus(peer, version, id); + const stream = await adapter.acceptBi(); + if (!stream) throw new Error("no stream"); + stream.close(); + } await trackStatus(peer, version, later); - await expect(running).rejects.toThrow("request id was already spent"); + await expect(running).rejects.toThrow("request id is not next in sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } } @@ -505,7 +507,7 @@ test("an update id stays spent after the update retires", async () => { if (!(await adapter.acceptBi())) throw new Error("no stream"); await update(peer, version, 2n, 0n); await update(peer, version, 2n, 0n); - await expect(running).rejects.toThrow("request id was already spent"); + await expect(running).rejects.toThrow("request id is not next in sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); @@ -543,3 +545,23 @@ test("more requests than the setup window complete on one session", async () => await reading; } }); + +// Both requests and updates allocate IDs, even when an update's target is gone. +test("skipped request and update ids close the session", async () => { + for (const [version, alpn] of WINDOW_DRAFTS) { + for (const client of [false, true]) { + for (const prefix of [0n, 1n]) { + for (const isUpdate of [false, true]) { + const { pair, peer, running } = await windowed(version, alpn, 8n, client); + const first = client ? 1n : 0n; + if (prefix) await trackStatus(peer, version, first); + const skipped = first + 2n * prefix + 2n; + if (isUpdate) await update(peer, version, skipped, 100n); + else await trackStatus(peer, version, skipped); + await expect(running).rejects.toThrow("request id is not next in sequence"); + expect((await pair.client.closed).closeCode).toBe(SessionCode.InvalidRequestId); + } + } + } + } +}); diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index 7b1a1e2d8c6..0a082903a63 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -146,7 +146,7 @@ export class ControlStreamAdapter implements Session { // by the IDs they spent and goes out as one MAX_REQUEST_ID. #peerWindow: bigint; #peerMax: bigint; - #peerLast: bigint | undefined; + #peerNext: bigint; #open = new Set(); #retired = 0n; #granting = false; @@ -177,6 +177,7 @@ export class ControlStreamAdapter implements Session { this.#maxRequestId = maxRequestId; this.#peerWindow = window; this.#peerMax = initialMaxRequestId(!client, window); + this.#peerNext = client ? 1n : 0n; this.#requestId = client ? 0n : 1n; this.#client = client; } @@ -486,11 +487,11 @@ export class ControlStreamAdapter implements Session { #admitHeld(requestId: bigint): void { this.#checkPeer(requestId); - if (this.#peerLast !== undefined && requestId <= this.#peerLast) { - this.#refuse(SessionCode.InvalidRequestId, "request id was already spent"); + if (requestId !== this.#peerNext) { + this.#refuse(SessionCode.InvalidRequestId, "request id is not next in sequence"); } this.#checkLive(); - this.#peerLast = requestId; + this.#peerNext += 2n; this.#open.add(requestId); } diff --git a/rs/moq-net/src/error.rs b/rs/moq-net/src/error.rs index 7e0199cd042..b438349c30f 100644 --- a/rs/moq-net/src/error.rs +++ b/rs/moq-net/src/error.rs @@ -58,7 +58,7 @@ pub enum SessionError { #[error("unknown code={0}")] Unknown(u32), - /// A request ID was reused, decreased, or had the wrong parity. + /// A request ID was reused, skipped, decreased, or had the wrong parity. #[error("invalid request id")] InvalidRequestId, } diff --git a/rs/moq-net/src/ietf/control.rs b/rs/moq-net/src/ietf/control.rs index 5996f8eed8f..552d998c066 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -30,8 +30,8 @@ struct Incoming { live: u64, /// Requests retired since the last grant. retired: u64, - /// The highest accepted ID, including requests that already retired. - last: Option, + /// The next ID the peer must use, including requests that already retired. + next: RequestId, } #[derive(Clone)] @@ -62,7 +62,7 @@ impl Control { sent: max, live: 0, retired: 0, - last: None, + next: RequestId(u64::from(client)), }); self } @@ -76,21 +76,24 @@ impl Control { /// /// Closes the session with TOO_MANY_REQUESTS when the ID is at or past the /// MAX_REQUEST_ID we advertised, or its live requests exceed the window. - /// Reused or decreasing IDs close with INVALID_REQUEST_ID even after retirement. + /// Non-sequential IDs close with INVALID_REQUEST_ID even after retirement. /// `None` when the peer's requests are not flow controlled. pub fn accept(&self, id: RequestId) -> Result, Error> { let mut state = self.state.lock(); let Some(incoming) = &mut state.incoming else { return Ok(None); }; - if incoming.last.is_some_and(|last| id <= last) { + if id.0 >= incoming.max { + tracing::warn!(id = %id, max = incoming.max, live = incoming.live, "peer exceeded MAX_REQUEST_ID"); + return Err(SessionError::TooManyRequests.into()); + } + if id != incoming.next { return Err(SessionError::InvalidRequestId.into()); } - if id.0 >= incoming.max || incoming.live >= incoming.window { - tracing::warn!(id = %id, max = incoming.max, live = incoming.live, "peer exceeded MAX_REQUEST_ID"); + if incoming.live >= incoming.window { return Err(SessionError::TooManyRequests.into()); } - incoming.last = Some(id); + incoming.next.increment(); incoming.live += 1; Ok(Some(RequestPermit(self.clone()))) } @@ -211,14 +214,50 @@ mod tests { /// An ID remains spent after its request ends, across every request type. #[test] fn retired_or_lower_request_ids_are_refused() { - for (first, later) in [(0, 0), (4, 2)] { + for later in [0, 2] { let control = Control::new(None, false).with_window(8, false); - drop(control.accept(RequestId(first)).unwrap()); + for id in [0, 2, 4] { + drop(control.accept(RequestId(id)).unwrap()); + } let err = control.accept(RequestId(later)).err().expect("spent ID refused"); assert_eq!(SessionError::from(&err).to_code(), 0x4); } } + #[test] + fn a_duplicate_is_invalid_even_when_the_window_is_full() { + for client in [false, true] { + let control = Control::new(None, client).with_window(1, client); + let id = RequestId(u64::from(client)); + let _held = control.accept(id).unwrap(); + assert!(matches!( + control.accept(id), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } + } + + /// The first ID and every subsequent request must be exactly the next in sequence. + #[test] + fn skipped_and_wrong_parity_request_ids_are_refused() { + for client in [false, true] { + let first = u64::from(client); + for prefix in [0, 1, 2] { + for gap in [2, 4, 1] { + let control = Control::new(None, client).with_window(8, client); + for index in 0..prefix { + drop(control.accept(RequestId(first + 2 * index)).unwrap()); + } + let err = control + .accept(RequestId(first + 2 * prefix + gap)) + .err() + .expect("non-sequential ID refused"); + assert_eq!(SessionError::from(&err), SessionError::InvalidRequestId); + } + } + } + } + /// Draft-17+ has no MAX_REQUEST_ID: nothing is tracked or granted. #[test] fn unlimited_without_a_window() { From 90b84903e4d5d30db487225073d0aed8290e9990 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 18:08:12 -0700 Subject: [PATCH 07/13] quest: retain draft-16 request ordering decision Co-Authored-By: GPT-6 Astra --- quest/m1/README.md | 1 + quest/m1/request-id-order.md | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 quest/m1/request-id-order.md diff --git a/quest/m1/README.md b/quest/m1/README.md index f20331f5418..2dcf40041ca 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -209,6 +209,7 @@ bench all require its crate. - [Datagram replay bound](/quest/m1/datagram-replay-bound.md) - a new Rust datagram subscriber starts within its max delay of the newest datagram, not at a minutes-old buffer - [Catalog colour](/quest/m1/color-catalog.md) - the catalog describes a rendition's colour and HDR properties, which the WebGPU HDR renderer reads - [WebGPU HDR](/quest/m1/webgpu-hdr.md) - HDR renditions play as HDR where the browser and display can show it, and tone-map to SDR elsewhere +- [Request ID order](/quest/m1/request-id-order.md) - refuse spent IDs while preserving valid draft-16 stream reordering - [Import catalog drop](/quest/m1/import-catalog-drop.md) - moq-cli import never drops a catalog producer without finishing it - [WebGPU on Safari](/quest/m1/webgpu-safari.md) - the WebGPU renderer is verified on Safari 26 for macOS and iOS - [A/V sync across a break](/quest/m1/watch-break-av-sync.md) - `@moq/watch` never plays pre-break audio out of sync with pre-break video diff --git a/quest/m1/request-id-order.md b/quest/m1/request-id-order.md new file mode 100644 index 00000000000..af42b590f68 --- /dev/null +++ b/quest/m1/request-id-order.md @@ -0,0 +1,40 @@ +# [S] A reused moq-transport Request ID is refused + +## Goal + +On drafts 14 to 16, `@moq/net` and `moq-net` refuse invalid incoming Request +IDs, including IDs whose requests already retired, without rejecting valid +independent-stream reordering. In-order requests and MAX_REQUEST_ID grants +continue to work. + +## Plan + +[PR #5235](https://github.com/moq-dev/moq/pull/5235) implements exact-next +admission in both languages. Regressions cover repeated, decreasing, +skipped, wrong-parity and retired IDs, including updates and both peer +roles. Request-limit errors remain distinct from invalid-ID errors. + +Section 9.1 of drafts 14 to 16 starts at the peer's parity and increments +by two for each request. Draft 16 section 9.25 also puts SUBSCRIBE_NAMESPACE +on an independent bidi stream with that same Request ID space. Its arrival +can reorder against the control stream; one arrival-order counter does not +establish sender order across those streams. + +Settle the draft-16 compatibility policy before completing this quest: + +- Literal exact-next admission across all streams follows section 9.1 but + can reject transport reordering. +- Exact order for drafts 14/15, with bounded spent-ID tracking for draft 16, + preserves reordering. Recommended direction; define the lifetime/window + bound before implementing it. Retired IDs must never become reusable. +- Buffer draft-16 gaps until the missing requests arrive. This preserves + order but can delay refusing a genuinely skipped ID. + +Test control/native arrival reordering deterministically, as well as +retired-ID reuse and long-lived namespace requests while control requests +retire. Do not add retries or timing allowances. Run `just check` and +`just test interop --all`. + +Public API: matching named InvalidRequestId session errors in Rust and JS. +Wire: unchanged framing, stricter refusal with existing error 0x4; report +any draft-16 acceptance policy explicitly. From 0afe789fa2990f30ccd6b1c321f4fce2751a59f3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 18:41:58 -0700 Subject: [PATCH 08/13] fix(net): allow bounded draft-16 request ID reordering Follow upstream receiver guidance while retaining strict draft-14/15 order. Remember only missing IDs backed by unreturned window credits, and benchmark both arrival orders across sessions and window sizes. Co-Authored-By: GPT-6 Astra --- .github/workflows/nightly.yml | 4 ++ drafts/draft-lcurley-moq-lite.md | 2 +- js/justfile | 2 +- js/net/bench/request-ids.ts | 63 ++++++++++++++++++++++ js/net/src/error.ts | 2 +- js/net/src/ietf/adapter.test.ts | 41 +++++++++++--- js/net/src/ietf/adapter.ts | 18 +++++-- quest/m1/README.md | 1 - quest/m1/request-id-order.md | 40 -------------- rs/moq-net/Cargo.toml | 5 ++ rs/moq-net/benches/request-ids.rs | 36 +++++++++++++ rs/moq-net/src/error.rs | 2 +- rs/moq-net/src/fuzz.rs | 5 ++ rs/moq-net/src/ietf/adapter.rs | 2 +- rs/moq-net/src/ietf/control.rs | 89 +++++++++++++++++++++++++++---- rs/moq-net/src/ietf/mod.rs | 10 ++++ rs/moq-net/src/ietf/session.rs | 2 +- 17 files changed, 257 insertions(+), 67 deletions(-) create mode 100644 js/net/bench/request-ids.ts delete mode 100644 quest/m1/request-id-order.md create mode 100644 rs/moq-net/benches/request-ids.rs diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index df96ff12ef4..65cdd7900b3 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -97,6 +97,10 @@ jobs: if: ${{ !cancelled() }} run: nix develop --command just js bench broadcasts + - name: JS request ID admission benchmark + if: ${{ !cancelled() }} + run: nix develop --command just js bench request-ids + # A route above several interest heads lands once per head, so its re-price # should expose any slope over heads times received routes. - name: JS origin announce forwarding benchmark diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index e9cedb117fe..19962696295 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -284,7 +284,7 @@ Sent when terminating the session, via the transport's session close. | ------- | ------------- | ----------- | | 0x3 | PROTOCOL_VIOLATION | The peer violated this specification. | | ------- | ------------- | ----------- | -| 0x4 | INVALID_REQUEST_ID | A moq-transport request ID was reused, skipped, decreased, or had the wrong parity; moq-lite has no request IDs. | +| 0x4 | INVALID_REQUEST_ID | A moq-transport request ID was reused or violated that draft’s order or parity rules; moq-lite has no request IDs. | | ------- | ------------- | ----------- | | 0x6 | KEY_VALUE_FORMATTING_ERROR | A key-value pair was malformed, or repeated more than allowed. | | ------- | ------------- | ----------- | diff --git a/js/justfile b/js/justfile index 68f09ae8526..aa59127c628 100644 --- a/js/justfile +++ b/js/justfile @@ -47,7 +47,7 @@ test: bun test common/declarations.test.ts common/deps.test.ts common/worklet.test.ts bun run --filter='*' --elide-lines=0 test -# Run one net benchmark: broadcasts, forward, reader, frames, track, varint, or lite-varint. +# Run one net benchmark: broadcasts, forward, reader, frames, track, varint, lite-varint, or request-ids. bench name: bun install --frozen-lockfile bun net/bench/{{ name }}.ts diff --git a/js/net/bench/request-ids.ts b/js/net/bench/request-ids.ts new file mode 100644 index 00000000000..157486d0ae4 --- /dev/null +++ b/js/net/bench/request-ids.ts @@ -0,0 +1,63 @@ +// Request-ID admission cost across independent sessions and reordered windows. +import { ControlStreamAdapter } from "../src/ietf/adapter.ts"; +import { Version } from "../src/ietf/version.ts"; +import { Stream } from "../src/stream.ts"; + +const debug = console.debug; +console.debug = () => undefined; +try { + console.log("mode,sessions,window,admit_us,accepted"); + for (const reversed of [false, true]) { + for (const sessions of [1, 8, 32]) { + for (const window of [8, 32, 128]) { + const ids = Array.from({ length: window }, (_, id) => BigInt(id * 2)); + if (reversed) ids.reverse(); + let elapsed = 0; + let accepted = 0; + for (let round = 0; round < 64; round++) { + const fixtures = Array.from({ length: sessions }, () => { + const stream = new Stream({ + readable: new ReadableStream(), + writable: new WritableStream(), + version: Version.DRAFT_16, + }); + const quic = { close() {} } as unknown as WebTransport; + return { + stream, + adapter: new ControlStreamAdapter( + quic, + stream, + Version.DRAFT_16, + 0n, + false, + BigInt(window), + ), + }; + }); + const start = performance.now(); + for (const { adapter } of fixtures) { + for (const id of ids) { + try { + adapter.admitRequest(id); + accepted++; + } catch { + /* Count refusals in baseline comparisons. */ + } + } + } + elapsed += performance.now() - start; + for (const { stream, adapter } of fixtures) { + adapter.close(); + stream.close(); + } + await Promise.resolve(); + } + console.log( + `${reversed ? "reordered" : "ordered"},${sessions},${window},${((elapsed * 1000) / 64).toFixed(2)},${accepted}`, + ); + } + } + } +} finally { + console.debug = debug; +} diff --git a/js/net/src/error.ts b/js/net/src/error.ts index c0e7d6ea8eb..55bdde66258 100644 --- a/js/net/src/error.ts +++ b/js/net/src/error.ts @@ -36,7 +36,7 @@ export const SessionCode = Object.freeze( Unauthorized: 0x2 as SessionCode, /** A protocol rule was broken; the session is unusable. */ ProtocolViolation: 0x3 as SessionCode, - /** A request ID was reused, skipped, decreased, or had the wrong parity. */ + /** A request ID was reused or violated the negotiated draft’s order or parity rules. */ InvalidRequestId: 0x4 as SessionCode, /** A key-value pair was malformed or repeated more than allowed. */ KeyValueFormatting: 0x6 as SessionCode, diff --git a/js/net/src/ietf/adapter.test.ts b/js/net/src/ietf/adapter.test.ts index a67717bfd0d..63df9df603a 100644 --- a/js/net/src/ietf/adapter.test.ts +++ b/js/net/src/ietf/adapter.test.ts @@ -376,7 +376,7 @@ test("a second use of an open request id closes the session", async () => { const { pair, peer, running } = await windowed(version, alpn, 1n, false); await trackStatus(peer, version, 0n); await trackStatus(peer, version, 0n); - await expect(running).rejects.toThrow("request id is not next in sequence"); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); @@ -470,7 +470,7 @@ test("an update spending an open request id closes the session", async () => { await trackStatus(peer, version, heldId); if (!(await adapter.acceptBi())) throw new Error("no stream"); await update(peer, version, heldId, heldId); - await expect(running).rejects.toThrow("request id is not next in sequence"); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } } @@ -493,7 +493,7 @@ test("retired and lower request ids close the session", async () => { stream.close(); } await trackStatus(peer, version, later); - await expect(running).rejects.toThrow("request id is not next in sequence"); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } } @@ -507,7 +507,7 @@ test("an update id stays spent after the update retires", async () => { if (!(await adapter.acceptBi())) throw new Error("no stream"); await update(peer, version, 2n, 0n); await update(peer, version, 2n, 0n); - await expect(running).rejects.toThrow("request id is not next in sequence"); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); @@ -548,7 +548,7 @@ test("more requests than the setup window complete on one session", async () => // Both requests and updates allocate IDs, even when an update's target is gone. test("skipped request and update ids close the session", async () => { - for (const [version, alpn] of WINDOW_DRAFTS) { + for (const [version, alpn] of WINDOW_DRAFTS.filter(([version]) => version !== Version.DRAFT_16)) { for (const client of [false, true]) { for (const prefix of [0n, 1n]) { for (const isUpdate of [false, true]) { @@ -558,10 +558,39 @@ test("skipped request and update ids close the session", async () => { const skipped = first + 2n * prefix + 2n; if (isUpdate) await update(peer, version, skipped, 100n); else await trackStatus(peer, version, skipped); - await expect(running).rejects.toThrow("request id is not next in sequence"); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); expect((await pair.client.closed).closeCode).toBe(SessionCode.InvalidRequestId); } } } } }); + +// SUBSCRIBE_NAMESPACE uses an independent bidi stream in draft 16, sharing the ID space. +test("draft 16 accepts reordered native and control IDs but never reuses them", async () => { + for (const client of [false, true]) { + for (const nativeFirst of [false, true]) { + const version = Version.DRAFT_16; + const { pair, adapter, peer, running } = await windowed(version, ALPN.DRAFT_16, 8n, client); + void running.catch(() => undefined); + const first = client ? 1n : 0n; + const native = nativeFirst ? first + 2n : first; + const control = nativeFirst ? first : first + 2n; + try { + if (nativeFirst) adapter.admitRequest(native); + await trackStatus(peer, version, control); + const stream = await adapter.acceptBi(); + if (!stream) throw new Error("no reordered control request"); + if (!nativeFirst) adapter.admitRequest(native); + stream.close(); + adapter.releaseRequest(native); + await trackStatus(peer, version, first); + await expect(running).rejects.toThrow("request id was already spent"); + expect((await pair.client.closed).closeCode).toBe(SessionCode.InvalidRequestId); + } finally { + peer.close(); + adapter.close(); + } + } + } +}); diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index 0a082903a63..aae1f27814f 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -147,6 +147,9 @@ export class ControlStreamAdapter implements Session { #peerWindow: bigint; #peerMax: bigint; #peerNext: bigint; + // Draft 16 reorders across streams. Every missing ID retains an unreturned window + // credit, so this set is bounded by the window rather than the session lifetime. + #peerMissing: Set | undefined; #open = new Set(); #retired = 0n; #granting = false; @@ -178,6 +181,7 @@ export class ControlStreamAdapter implements Session { this.#peerWindow = window; this.#peerMax = initialMaxRequestId(!client, window); this.#peerNext = client ? 1n : 0n; + this.#peerMissing = version === Version.DRAFT_16 ? new Set() : undefined; this.#requestId = client ? 0n : 1n; this.#client = client; } @@ -487,11 +491,19 @@ export class ControlStreamAdapter implements Session { #admitHeld(requestId: bigint): void { this.#checkPeer(requestId); - if (requestId !== this.#peerNext) { - this.#refuse(SessionCode.InvalidRequestId, "request id is not next in sequence"); + const unused = + requestId < this.#peerNext + ? this.#peerMissing?.has(requestId) + : requestId === this.#peerNext || this.#peerMissing !== undefined; + if (!unused) { + this.#refuse(SessionCode.InvalidRequestId, "request id was already spent or out of sequence"); } this.#checkLive(); - this.#peerNext += 2n; + if (this.#peerMissing) { + for (let skipped = this.#peerNext; skipped < requestId; skipped += 2n) this.#peerMissing.add(skipped); + this.#peerMissing.delete(requestId); + } + if (requestId >= this.#peerNext) this.#peerNext = requestId + 2n; this.#open.add(requestId); } diff --git a/quest/m1/README.md b/quest/m1/README.md index 2dcf40041ca..f20331f5418 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -209,7 +209,6 @@ bench all require its crate. - [Datagram replay bound](/quest/m1/datagram-replay-bound.md) - a new Rust datagram subscriber starts within its max delay of the newest datagram, not at a minutes-old buffer - [Catalog colour](/quest/m1/color-catalog.md) - the catalog describes a rendition's colour and HDR properties, which the WebGPU HDR renderer reads - [WebGPU HDR](/quest/m1/webgpu-hdr.md) - HDR renditions play as HDR where the browser and display can show it, and tone-map to SDR elsewhere -- [Request ID order](/quest/m1/request-id-order.md) - refuse spent IDs while preserving valid draft-16 stream reordering - [Import catalog drop](/quest/m1/import-catalog-drop.md) - moq-cli import never drops a catalog producer without finishing it - [WebGPU on Safari](/quest/m1/webgpu-safari.md) - the WebGPU renderer is verified on Safari 26 for macOS and iOS - [A/V sync across a break](/quest/m1/watch-break-av-sync.md) - `@moq/watch` never plays pre-break audio out of sync with pre-break video diff --git a/quest/m1/request-id-order.md b/quest/m1/request-id-order.md deleted file mode 100644 index af42b590f68..00000000000 --- a/quest/m1/request-id-order.md +++ /dev/null @@ -1,40 +0,0 @@ -# [S] A reused moq-transport Request ID is refused - -## Goal - -On drafts 14 to 16, `@moq/net` and `moq-net` refuse invalid incoming Request -IDs, including IDs whose requests already retired, without rejecting valid -independent-stream reordering. In-order requests and MAX_REQUEST_ID grants -continue to work. - -## Plan - -[PR #5235](https://github.com/moq-dev/moq/pull/5235) implements exact-next -admission in both languages. Regressions cover repeated, decreasing, -skipped, wrong-parity and retired IDs, including updates and both peer -roles. Request-limit errors remain distinct from invalid-ID errors. - -Section 9.1 of drafts 14 to 16 starts at the peer's parity and increments -by two for each request. Draft 16 section 9.25 also puts SUBSCRIBE_NAMESPACE -on an independent bidi stream with that same Request ID space. Its arrival -can reorder against the control stream; one arrival-order counter does not -establish sender order across those streams. - -Settle the draft-16 compatibility policy before completing this quest: - -- Literal exact-next admission across all streams follows section 9.1 but - can reject transport reordering. -- Exact order for drafts 14/15, with bounded spent-ID tracking for draft 16, - preserves reordering. Recommended direction; define the lifetime/window - bound before implementing it. Retired IDs must never become reusable. -- Buffer draft-16 gaps until the missing requests arrive. This preserves - order but can delay refusing a genuinely skipped ID. - -Test control/native arrival reordering deterministically, as well as -retired-ID reuse and long-lived namespace requests while control requests -retire. Do not add retries or timing allowances. Run `just check` and -`just test interop --all`. - -Public API: matching named InvalidRequestId session errors in Rust and JS. -Wire: unchanged framing, stricter refusal with existing error 0x4; report -any draft-16 acceptance policy explicitly. diff --git a/rs/moq-net/Cargo.toml b/rs/moq-net/Cargo.toml index 0aaecfb0ed5..0d9d0cef71a 100644 --- a/rs/moq-net/Cargo.toml +++ b/rs/moq-net/Cargo.toml @@ -121,3 +121,8 @@ harness = false [[bench]] name = "session" harness = false + +[[bench]] +name = "request-ids" +harness = false +required-features = ["fuzz"] diff --git a/rs/moq-net/benches/request-ids.rs b/rs/moq-net/benches/request-ids.rs new file mode 100644 index 00000000000..5d1031dbe92 --- /dev/null +++ b/rs/moq-net/benches/request-ids.rs @@ -0,0 +1,36 @@ +//! Request-ID admission cost across independent sessions and reordered windows. +use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; +use std::{hint::black_box, time::Duration}; + +fn request_ids(c: &mut Criterion) { + let mut group = c.benchmark_group("request-ids"); + group.sample_size(20); + group.warm_up_time(Duration::from_millis(200)); + group.measurement_time(Duration::from_secs(1)); + for reversed in [false, true] { + for sessions in [1, 8, 32] { + for window in [8, 32, 128] { + let mut ids: Vec<_> = (0..window).map(|id| id * 2).collect(); + if reversed { + ids.reverse(); + } + let mode = if reversed { "reordered" } else { "ordered" }; + println!( + "admitted,{mode},{sessions},{window},{}", + moq_net::fuzz::request_ids(window, &ids) * sessions + ); + group.bench_function(BenchmarkId::new(mode, format!("{sessions}x{window}")), |b| { + b.iter(|| { + let accepted: usize = (0..sessions) + .map(|_| moq_net::fuzz::request_ids(window, black_box(&ids))) + .sum(); + black_box(accepted) + }); + }); + } + } + } + group.finish(); +} +criterion_group!(benches, request_ids); +criterion_main!(benches); diff --git a/rs/moq-net/src/error.rs b/rs/moq-net/src/error.rs index b438349c30f..5293cc39949 100644 --- a/rs/moq-net/src/error.rs +++ b/rs/moq-net/src/error.rs @@ -58,7 +58,7 @@ pub enum SessionError { #[error("unknown code={0}")] Unknown(u32), - /// A request ID was reused, skipped, decreased, or had the wrong parity. + /// A request ID was reused or violated the negotiated draft’s order or parity rules. #[error("invalid request id")] InvalidRequestId, } diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index fbf13059ad0..8f42dde6004 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -1171,6 +1171,11 @@ impl crate::transport::Error for NoError { } } +/// Admit a draft-16 window for the request-ID benchmark, returning the accepted count. +pub fn request_ids(window: u64, ids: &[u64]) -> usize { + ietf::request_ids(window, ids) +} + #[cfg(test)] mod tests { use super::*; diff --git a/rs/moq-net/src/ietf/adapter.rs b/rs/moq-net/src/ietf/adapter.rs index 7734c6b83d0..db1eb75de2e 100644 --- a/rs/moq-net/src/ietf/adapter.rs +++ b/rs/moq-net/src/ietf/adapter.rs @@ -1848,7 +1848,7 @@ mod tests { let mut session = ScriptedSession::new(script); let (_, recv) = session.open_bi().await.unwrap(); // We are the server, so the client's request IDs are even. - let control = Control::new(None, false).with_window(window, false); + let control = Control::new(None, false).with_window(window, false, VERSION); let adapter = ControlStreamAdapter::new(session, control, VERSION); let log = Log::default(); let writer = Writer::new(SinkSend::new(log.clone()), VERSION); diff --git a/rs/moq-net/src/ietf/control.rs b/rs/moq-net/src/ietf/control.rs index 552d998c066..6561bb054b0 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -1,6 +1,9 @@ -use std::task::Poll; +use std::{collections::HashSet, task::Poll}; -use crate::{Error, SessionError, ietf::RequestId}; +use crate::{ + Error, SessionError, + ietf::{RequestId, Version}, +}; /// The MAX_REQUEST_ID that first admits `window` requests from a peer. /// @@ -30,8 +33,11 @@ struct Incoming { live: u64, /// Requests retired since the last grant. retired: u64, - /// The next ID the peer must use, including requests that already retired. + /// One past the highest accepted ID, preserving IDs whose requests retired. next: RequestId, + /// Draft 16 can reorder across streams. Missing IDs retain unreturned window credits, + /// so remembering only these holes is bounded by the request window, not session age. + missing: Option>, } #[derive(Clone)] @@ -54,7 +60,7 @@ impl Control { /// /// `client` is our own role, as passed to [`Self::new`]; the window starts at what /// [`initial_max_request_id`] advertised in our SETUP. - pub fn with_window(self, window: u64, client: bool) -> Self { + pub fn with_window(self, window: u64, client: bool, version: Version) -> Self { let max = initial_max_request_id(window, !client); self.state.lock().incoming = Some(Incoming { window, @@ -63,6 +69,7 @@ impl Control { live: 0, retired: 0, next: RequestId(u64::from(client)), + missing: (version == Version::Draft16).then(HashSet::new), }); self } @@ -76,7 +83,8 @@ impl Control { /// /// Closes the session with TOO_MANY_REQUESTS when the ID is at or past the /// MAX_REQUEST_ID we advertised, or its live requests exceed the window. - /// Non-sequential IDs close with INVALID_REQUEST_ID even after retirement. + /// Drafts 14/15 require the next ID; draft 16 permits cross-stream reordering. + /// Used IDs close with INVALID_REQUEST_ID even after retirement. /// `None` when the peer's requests are not flow controlled. pub fn accept(&self, id: RequestId) -> Result, Error> { let mut state = self.state.lock(); @@ -87,13 +95,24 @@ impl Control { tracing::warn!(id = %id, max = incoming.max, live = incoming.live, "peer exceeded MAX_REQUEST_ID"); return Err(SessionError::TooManyRequests.into()); } - if id != incoming.next { + let unused = if id < incoming.next { + incoming.missing.as_ref().is_some_and(|missing| missing.contains(&id)) + } else { + id == incoming.next || incoming.missing.is_some() + }; + if id.0 % 2 != incoming.next.0 % 2 || !unused { return Err(SessionError::InvalidRequestId.into()); } if incoming.live >= incoming.window { return Err(SessionError::TooManyRequests.into()); } - incoming.next.increment(); + if let Some(missing) = &mut incoming.missing { + for skipped in (incoming.next.0..id.0).step_by(2) { + missing.insert(RequestId(skipped)); + } + missing.remove(&id); + } + incoming.next.0 = incoming.next.0.max(id.0 + 2); incoming.live += 1; Ok(Some(RequestPermit(self.clone()))) } @@ -185,7 +204,7 @@ mod tests { #[test] fn window_refills_as_requests_retire() { // We are the server, so the client's IDs are even: 0, 2, 4, 6. - let control = Control::new(None, false).with_window(4, false); + let control = Control::new(None, false).with_window(4, false, Version::Draft15); assert_eq!(initial_max_request_id(4, true), 8); let mut open: Vec<_> = (0..4) @@ -215,7 +234,7 @@ mod tests { #[test] fn retired_or_lower_request_ids_are_refused() { for later in [0, 2] { - let control = Control::new(None, false).with_window(8, false); + let control = Control::new(None, false).with_window(8, false, Version::Draft15); for id in [0, 2, 4] { drop(control.accept(RequestId(id)).unwrap()); } @@ -227,7 +246,7 @@ mod tests { #[test] fn a_duplicate_is_invalid_even_when_the_window_is_full() { for client in [false, true] { - let control = Control::new(None, client).with_window(1, client); + let control = Control::new(None, client).with_window(1, client, Version::Draft15); let id = RequestId(u64::from(client)); let _held = control.accept(id).unwrap(); assert!(matches!( @@ -244,7 +263,7 @@ mod tests { let first = u64::from(client); for prefix in [0, 1, 2] { for gap in [2, 4, 1] { - let control = Control::new(None, client).with_window(8, client); + let control = Control::new(None, client).with_window(8, client, Version::Draft15); for index in 0..prefix { drop(control.accept(RequestId(first + 2 * index)).unwrap()); } @@ -258,6 +277,54 @@ mod tests { } } + #[test] + fn draft16_independent_streams_can_arrive_out_of_order() { + for client in [false, true] { + let control = Control::new(None, client).with_window(8, client, Version::Draft16); + let first = u64::from(client); + for id in [first + 2, first, first + 6, first + 4] { + drop(control.accept(RequestId(id)).expect("independent stream arrival order")); + } + for id in [first, first + 2, first + 4, first + 6] { + assert!(matches!( + control.accept(RequestId(id)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } + } + } + + #[test] + fn draft16_remembers_only_missing_ids_while_other_requests_retire() { + let control = Control::new(None, false).with_window(4, false, Version::Draft16); + for id in 1..1024 { + drop(control.accept(RequestId(id * 2)).unwrap()); + let state = control.state.lock(); + let missing = state.incoming.as_ref().unwrap().missing.as_ref().unwrap(); + assert_eq!(missing.len(), 1); + assert!(missing.contains(&RequestId(0))); + } + drop(control.accept(RequestId(0)).unwrap()); + assert!( + control + .state + .lock() + .incoming + .as_ref() + .unwrap() + .missing + .as_ref() + .unwrap() + .is_empty() + ); + for id in [0, 2, 2046] { + assert!(matches!( + control.accept(RequestId(id)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } + } + /// Draft-17+ has no MAX_REQUEST_ID: nothing is tracked or granted. #[test] fn unlimited_without_a_window() { diff --git a/rs/moq-net/src/ietf/mod.rs b/rs/moq-net/src/ietf/mod.rs index 9ed57e1918b..9446fd6ad38 100644 --- a/rs/moq-net/src/ietf/mod.rs +++ b/rs/moq-net/src/ietf/mod.rs @@ -59,3 +59,13 @@ pub use subscribe_namespace::*; // includes PublishBlocked use subscriber::*; pub use track::*; pub use version::Version; + +#[cfg(any(test, feature = "fuzz"))] +pub(crate) fn request_ids(window: u64, ids: &[u64]) -> usize { + let control = Control::new(None, false).with_window(window, false, Version::Draft16); + let held: Vec<_> = ids + .iter() + .filter_map(|id| control.accept(RequestId(*id)).ok()) + .collect(); + held.len() +} diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index e5f9dfb6a97..7e6cae38318 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -207,7 +207,7 @@ where session.close(SessionError::from(&err).to_code(), "setup stream required"); return Err(err); }; - let control = Control::new(request_id_max, client).with_window(limits.requests(), client); + let control = Control::new(request_id_max, client).with_window(limits.requests(), client, version); let adapter = ControlStreamAdapter::new(session.clone(), control.clone(), version); // No AUTH on these drafts, but a limit still reaches both halves. From eee8e9e41db29914e617762b979b19e217a092c9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 20:55:41 -0700 Subject: [PATCH 09/13] fix(net): share modern request ID history across AUTH and requests Reject reused and wrong-parity IDs across modern request streams and the implemented update readers in Rust and JavaScript. Coalesce spent ranges without rejecting valid stream reordering or forgetting retired IDs. Keep the provisional history-budget choice in the quest and draft PR. Co-Authored-By: GPT-6 Astra --- drafts/draft-lcurley-moq-auth.md | 3 +- js/net/bench/request-ids.ts | 40 +++++++++ js/net/src/auth.test.ts | 89 +++++++++++++++++++ js/net/src/goaway-requests.test.ts | 3 + js/net/src/ietf/adapter.ts | 17 +++- js/net/src/ietf/auth.ts | 20 ++--- js/net/src/ietf/connection.test.ts | 5 ++ js/net/src/ietf/connection.ts | 24 +++-- js/net/src/ietf/publisher.test.ts | 5 +- js/net/src/ietf/publisher.ts | 2 +- js/net/src/ietf/request.ts | 40 +++++++++ js/net/src/ietf/request_ids.test.ts | 49 +++++++++++ js/net/src/ietf/subscriber.ts | 1 + js/net/src/integration.test.ts | 3 +- quest/m1/auth/violations.md | 10 ++- rs/moq-net/benches/request-ids.rs | 32 ++++++- rs/moq-net/src/fuzz.rs | 5 ++ rs/moq-net/src/ietf/control.rs | 127 ++++++++++++++++++++++++++- rs/moq-net/src/ietf/mod.rs | 6 ++ rs/moq-net/src/ietf/publisher.rs | 10 ++- rs/moq-net/src/ietf/session.rs | 107 ++++++++++++++++++++++ rs/moq-net/src/ietf/subscriber.rs | 34 ++++++- rs/moq-net/src/session.rs | 4 +- rs/moq-net/tests/subscribe_tracks.rs | 2 +- 24 files changed, 603 insertions(+), 35 deletions(-) create mode 100644 js/net/src/ietf/request_ids.test.ts diff --git a/drafts/draft-lcurley-moq-auth.md b/drafts/draft-lcurley-moq-auth.md index 7726cdb1935..c202c7cccf7 100644 --- a/drafts/draft-lcurley-moq-auth.md +++ b/drafts/draft-lcurley-moq-auth.md @@ -135,7 +135,8 @@ AUTH Message { ~~~ **Request ID**: -The request's identifier, allocated like that of any other request ({{moqt}}). +The request's identifier, allocated from the same space as every other request ({{moqt}}). +A duplicate or wrong-parity Request ID closes the session with INVALID_REQUEST_ID, including an ID from an AUTH that has ended. **Token**: The credential to verify, opaque to this extension. diff --git a/js/net/bench/request-ids.ts b/js/net/bench/request-ids.ts index 157486d0ae4..360b880c494 100644 --- a/js/net/bench/request-ids.ts +++ b/js/net/bench/request-ids.ts @@ -61,3 +61,43 @@ try { } finally { console.debug = debug; } + +// Modern streams have no MAX_REQUEST_ID window. Sweep history fragmentation separately +// from session fanout, including reverse insertion into a fragmented history. +const { RequestIds } = await import("../src/ietf/request.ts"); +// The old modern path admitted every ID without tracking it. +const baseline = process.argv.includes("--baseline"); +console.log("modern_mode,sessions,ranges,admit_us,accepted,duplicates_refused"); +for (const fragmented of [false, true]) { + for (const sessions of [1, 8, 32]) { + for (const ranges of [8, 32, 128]) { + const ids = Array.from({ length: ranges }, (_, i) => BigInt(i * (fragmented ? 4 : 2))); + if (fragmented) ids.reverse(); + let elapsed = 0; + let accepted = 0; + let duplicates = 0; + for (let round = 0; round < 64; round++) { + const receivers = Array.from({ length: sessions }, () => + baseline ? { accept(_id: bigint) {} } : new RequestIds(false), + ); + const start = performance.now(); + for (const receiver of receivers) + for (const id of ids) { + receiver.accept(id); + accepted++; + } + elapsed += performance.now() - start; + for (const receiver of receivers) { + try { + receiver.accept(ids[0] ?? 0n); + } catch { + duplicates++; + } + } + } + console.log( + `modern-${fragmented ? "fragmented" : "contiguous"},${sessions},${ranges},${((elapsed * 1000) / 64).toFixed(2)},${accepted},${duplicates}`, + ); + } + } +} diff --git a/js/net/src/auth.test.ts b/js/net/src/auth.test.ts index 46384cd4156..1c49f19630d 100644 --- a/js/net/src/auth.test.ts +++ b/js/net/src/auth.test.ts @@ -6,6 +6,8 @@ import { SessionCode, SessionError, StreamCode, toStreamCode } from "./error.ts" import { AuthMessage as IetfAuthMessage, IetfAuthWire } from "./ietf/auth.ts"; import * as Ietf from "./ietf/index.ts"; import * as IetfMessage from "./ietf/message.ts"; +import { TrackStatusRequest } from "./ietf/track.ts"; +import { Version as IetfVersion } from "./ietf/version.ts"; import { AuthMessage as LiteAuthMessage, LiteAuthWire } from "./lite/auth.ts"; import * as Lite from "./lite/index.ts"; import * as LiteMessage from "./lite/message.ts"; @@ -512,3 +514,90 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_17, Ietf.ALPN.DRAFT_22])("AUTH }); } }); + +// AUTH shares its ID space with every other request from the same peer. +describe.each([Ietf.ALPN.DRAFT_17, Ietf.ALPN.DRAFT_22])("AUTH request IDs %s", (protocol) => { + for (const client of [true, false]) { + for (const fault of ["reused", "wrong parity"] as const) { + test(`${client ? "client" : "server"} ${fault} closes the session`, async () => { + const pair = await connect({ protocol }); + await waitFor(pair.client.auth.grant, (g) => g !== undefined); + await waitFor(pair.server.auth.grant, (g) => g !== undefined); + const original = IetfAuthMessage.prototype.encode; + const spy = spyOn(IetfAuthMessage.prototype, "encode").mockImplementation(async function ( + this: IetfAuthMessage, + writer, + version, + ) { + this.requestId = fault === "reused" ? (client ? 0n : 1n) : client ? 1n : 0n; + await original.call(this, writer, version); + }); + const added = (client ? pair.client : pair.server).auth.add("bad ID").catch(() => undefined); + try { + expect( + (await withTimeout(pair.transport.closed, 100, "invalid AUTH ID left session open")).closeCode, + ).toBe(SessionCode.InvalidRequestId); + } finally { + spy.mockRestore(); + pair.client.abort(); + pair.server.abort(); + await added; + } + }); + } + } +}); + +for (const [protocol, version] of [ + [Ietf.ALPN.DRAFT_17, IetfVersion.DRAFT_17], + [Ietf.ALPN.DRAFT_22, IetfVersion.DRAFT_22], +] as const) { + test(`TRACK_STATUS cannot reuse setup AUTH ID on ${protocol}`, async () => { + const pair = await connect({ protocol }); + await waitFor(pair.client.auth.grant, (g) => g !== undefined); + try { + const raw = await pair.transport.createBidirectionalStream(); + const writer = new Writer(raw.writable, version); + await writer.u53(TrackStatusRequest.id); + await new TrackStatusRequest({ + requestId: 0n, + trackNamespace: Path.from("test"), + trackName: "video", + }).encode(writer, version); + expect( + (await withTimeout(pair.transport.closed, 100, "cross-request reuse left session open")).closeCode, + ).toBe(SessionCode.InvalidRequestId); + } finally { + pair.client.abort(); + pair.server.abort(); + } + }); +} + +for (const protocol of [Ietf.ALPN.DRAFT_17, Ietf.ALPN.DRAFT_22]) { + test(`AUTH accepts reordered IDs on ${protocol}`, async () => { + const pair = await connect({ protocol }); + await waitFor(pair.client.auth.grant, (g) => g !== undefined); + await waitFor(pair.server.auth.grant, (g) => g !== undefined); + const original = IetfAuthMessage.prototype.encode; + const spy = spyOn(IetfAuthMessage.prototype, "encode").mockImplementation(async function ( + this: IetfAuthMessage, + writer, + version, + ) { + // Swap the next two independently opened request IDs, preserving endpoint parity. + this.requestId = this.requestId < 4n ? this.requestId + 2n : this.requestId - 2n; + await original.call(this, writer, version); + }); + try { + for (const endpoint of [pair.client, pair.server]) { + await expect(endpoint.auth.add("first")).rejects.toBeInstanceOf(Unsupported); + await expect(endpoint.auth.add("second")).rejects.toBeInstanceOf(Unsupported); + } + } finally { + spy.mockRestore(); + pair.client.abort(); + pair.server.abort(); + } + }); +} diff --git a/js/net/src/goaway-requests.test.ts b/js/net/src/goaway-requests.test.ts index 29b56adf5a4..7580166ed67 100644 --- a/js/net/src/goaway-requests.test.ts +++ b/js/net/src/goaway-requests.test.ts @@ -258,6 +258,9 @@ test("announcing and subscribing after GOAWAY still open streams, on the adapter nextRequestId() { return Promise.resolve(0n); }, + admitRequest() { + throw new Error("unexpected incoming request"); + }, close() {}, }; diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index aae1f27814f..b19061fd8a6 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -1,12 +1,12 @@ import { Once } from "@moq/signals"; import { Mutex } from "async-mutex"; import type { Drain } from "../connection/goaway.ts"; -import { error, ProtocolViolation, SessionCode } from "../error.ts"; +import { error, ProtocolViolation, SessionCode, SessionError } from "../error.ts"; import { Reader, Stream, type Writer } from "../stream.ts"; import * as Varint from "../varint.ts"; import { GoAway } from "./goaway.ts"; import * as Namespace from "./namespace.ts"; -import { initialMaxRequestId, MaxRequestId, REQUEST_WINDOW } from "./request.ts"; +import { initialMaxRequestId, MaxRequestId, REQUEST_WINDOW, RequestIds } from "./request.ts"; import { type IetfVersion, Version } from "./version.ts"; /** The peer used a request ID the draft closes the session for. */ @@ -29,6 +29,7 @@ export interface Session { openNativeBi?(): Promise; acceptBi(): Promise; nextRequestId(): Promise; + admitRequest(id: bigint): void; close(): void; readonly version: IetfVersion; } @@ -42,12 +43,24 @@ export class NativeSession implements Session { // moq-transport reserves even request IDs for the client and odd for the server, // so the two peers' ID spaces never overlap. #requestId: bigint; + #received: RequestIds; readonly version: IetfVersion; constructor(quic: WebTransport, version: IetfVersion, client: boolean) { this.#quic = quic; this.version = version; this.#requestId = client ? 0n : 1n; + this.#received = new RequestIds(client); + } + + /** Validate all incoming modern requests against the same lifetime history. */ + admitRequest(id: bigint): void { + try { + this.#received.accept(id); + } catch (err) { + if (err instanceof SessionError) this.#quic.close({ closeCode: err.code, reason: err.message }); + throw err; + } } async openBi(): Promise { diff --git a/js/net/src/ietf/auth.ts b/js/net/src/ietf/auth.ts index 61a2cc58533..4bb93e941de 100644 --- a/js/net/src/ietf/auth.ts +++ b/js/net/src/ietf/auth.ts @@ -3,6 +3,7 @@ import type { AuthWire, WireGrant, WireReply } from "../auth_session.ts"; import { SessionCode, SessionError } from "../error.ts"; import * as Path from "../path.ts"; import type { Reader, Stream, Writer } from "../stream.ts"; +import type { Session } from "./adapter.ts"; import * as Message from "./message.ts"; import * as Namespace from "./namespace.ts"; import { SetupOption, type SetupOptions } from "./parameters.ts"; @@ -213,24 +214,18 @@ export class AuthError { /** The moq-transport binding of the token lifecycle. @internal */ export class IetfAuthWire implements AuthWire { - #openBi: () => Promise; - #nextRequestId: () => Promise; + #session: Session; #version: IetfVersion; - constructor(props: { - openBi: () => Promise; - nextRequestId: () => Promise; - version: IetfVersion; - }) { - this.#openBi = props.openBi; - this.#nextRequestId = props.nextRequestId; - this.#version = props.version; + constructor(session: Session) { + this.#session = session; + this.#version = session.version; } async present(token: Uint8Array): Promise { - const requestId = await this.#nextRequestId(); + const requestId = await this.#session.nextRequestId(); if (requestId === undefined) throw new Error("no request id available"); - const stream = await this.#openBi(); + const stream = await this.#session.openBi(); await new AuthMessage(requestId, token).encode(stream.writer, this.#version); return stream; } @@ -255,6 +250,7 @@ export class IetfAuthWire implements AuthWire { /** The type id is already consumed by the dispatcher. */ async accept(stream: Stream): Promise { const msg = await AuthMessage.decode(stream.reader, this.#version); + this.#session.admitRequest(msg.requestId); return msg.token; } diff --git a/js/net/src/ietf/connection.test.ts b/js/net/src/ietf/connection.test.ts index 306b9f7b2a4..bea75304c13 100644 --- a/js/net/src/ietf/connection.test.ts +++ b/js/net/src/ietf/connection.test.ts @@ -149,6 +149,11 @@ test("SUBSCRIBE_TRACKS refuses only its request", async () => { for (let i = 0; i < 2; i++) { const stream = await Stream.open(pair.client, { version }); await stream.writer.u62(BigInt(SUBSCRIBE_TRACKS_ID)); + await Message.encode(stream.writer, async (writer) => { + await writer.u62(BigInt(i * 2)); + await Namespace.encode(writer, Path.from("")); + await writer.u53(0); + }); expect(await stream.reader.u53()).toBe(RequestError.id); const refused = await RequestError.decode(stream.reader, version); expect(refused.errorCode).toBe(0x3); diff --git a/js/net/src/ietf/connection.ts b/js/net/src/ietf/connection.ts index 23a9d99e611..0ff435c6c84 100644 --- a/js/net/src/ietf/connection.ts +++ b/js/net/src/ietf/connection.ts @@ -17,6 +17,7 @@ import { AuthMessage, supported as authSupported, IetfAuthWire } from "./auth.ts import * as Cluster from "./cluster.ts"; import { Fetch, FetchHeader } from "./fetch.ts"; import { GoAway } from "./goaway.ts"; +import * as Message from "./message.ts"; import { Group } from "./object.ts"; import { Publish } from "./publish.ts"; import { PublishNamespace } from "./publish_namespace.ts"; @@ -184,14 +185,7 @@ export class Connection implements Established { // since we consume on demand, and subscribing to whatever we publish. const session = this.#session; this.#auth = new AuthSession({ - wire: - auth && authSupported(version) - ? new IetfAuthWire({ - openBi: async () => session.openBi(), - nextRequestId: () => session.nextRequestId(), - version, - }) - : undefined, + wire: auth && authSupported(version) ? new IetfAuthWire(session) : undefined, peerGrant: { publish: new Path.Patterns([Path.Pattern.all()]), subscribe: new Path.Patterns(publish ? [Path.Pattern.all()] : []), @@ -344,11 +338,13 @@ export class Connection implements Established { // request for PUBLISH alone is refused, and one for both gets only NAMESPACE. case BigInt(SubscribeNamespace.id): { const msg = await SubscribeNamespace.decode(stream.reader, this.#session.version); + if (this.#session instanceof NativeSession) this.#session.admitRequest(msg.requestId); await this.#publisher.runSubscribeNamespace(msg, stream); break; } case BigInt(SubscribeNamespaceLegacy.id): { const legacy = await SubscribeNamespaceLegacy.decode(stream.reader, this.#session.version); + if (this.#session instanceof NativeSession) this.#session.admitRequest(legacy.requestId); // Draft-16 carries this on its own stream, so the control adapter never sees the ID. // Draft-14 and 15 already admitted it off the control stream. A refused request // still spends its ID, so it is admitted and released like any other. @@ -381,6 +377,7 @@ export class Connection implements Established { // Publisher handles incoming requests case BigInt(Subscribe.id): { const msg = await Subscribe.decode(stream.reader, this.#session.version); + if (this.#session instanceof NativeSession) this.#session.admitRequest(msg.requestId); await this.#publisher.runSubscribe(msg, stream); break; } @@ -389,16 +386,24 @@ export class Connection implements Established { if (this.#session.version < Version.DRAFT_18) { throw new ProtocolViolation("SUBSCRIBE_TRACKS before draft-18"); } + const requestId = await Message.decode(stream.reader, async (reader) => { + const id = await reader.u62(); + await reader.readAll(); + return id; + }); + this.#session.admitRequest(requestId); await this.#publisher.runSubscribeTracks(stream); break; } case BigInt(TrackStatusRequest.id): { const msg = await TrackStatusRequest.decode(stream.reader, this.#session.version); + if (this.#session instanceof NativeSession) this.#session.admitRequest(msg.requestId); await this.#publisher.runTrackStatusRequest(msg, stream); break; } case BigInt(Fetch.id): { const msg = await Fetch.decode(stream.reader, this.#session.version); + if (this.#session instanceof NativeSession) this.#session.admitRequest(msg.requestId); await this.#publisher.runFetch(msg, stream); break; } @@ -411,6 +416,8 @@ export class Connection implements Established { Cluster.negotiated(this.#cluster), ); + if (this.#session instanceof NativeSession) this.#session.admitRequest(msg.requestId); + // We always declare that advertisements to us must be solicited (MoQ // Solicit), and writing the option at all proves the peer implements the // extension, whichever value it chose. It also cannot have advertised @@ -441,6 +448,7 @@ export class Connection implements Established { } case BigInt(Publish.id): { const msg = await Publish.decode(stream.reader, this.#session.version); + if (this.#session instanceof NativeSession) this.#session.admitRequest(msg.requestId); await this.#subscriber.runPublish(msg, stream); break; } diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index ad639c1d97b..328f97e988d 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -550,6 +550,7 @@ test("a failed stream open does not kill the announce loop", async () => { version: inner.version, acceptBi: () => inner.acceptBi(), nextRequestId: () => inner.nextRequestId(), + admitRequest: (id) => inner.admitRequest(id), close: () => inner.close(), openBi: () => { if (failures-- > 0) throw new Error("no stream credit"); @@ -595,6 +596,7 @@ test("a namespace refused once is retried without anything else changing", async version: inner.version, acceptBi: () => inner.acceptBi(), nextRequestId: () => inner.nextRequestId(), + admitRequest: (id) => inner.admitRequest(id), close: () => inner.close(), openBi: () => { if (failures-- > 0) throw new Error("no stream credit"); @@ -631,6 +633,7 @@ test("a solicited legacy advertisement refused once is retried", async () => { version: inner.version, acceptBi: () => inner.acceptBi(), nextRequestId: () => inner.nextRequestId(), + admitRequest: (id) => inner.admitRequest(id), close: () => inner.close(), openBi: () => { if (failures-- > 0) throw new Error("no stream credit"); @@ -1615,7 +1618,7 @@ function fixture(version: IetfVersion = V20): { close: () => void; } { const pair = createMockTransportPair(ALPNS[version]); - const session = new NativeSession(pair.server, version, true); + const session = new NativeSession(pair.server, version, false); const { pub, origin } = publisher(pair.server, { session }); const broadcast = publish(origin, Path.from("test")); const uni = pair.client.incomingUnidirectionalStreams.getReader() as ReadableStreamDefaultReader< diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 265c2d38da7..1e44f31b35a 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -506,7 +506,7 @@ export class Publisher { continue; } const params = await Message.decode(stream.reader, async (r) => { - await r.u62(); + this.#session.admitRequest(await r.u62()); if (version === Version.DRAFT_17) await r.u62(); return Parameters.decode(r, version, "request-update"); }); diff --git a/js/net/src/ietf/request.ts b/js/net/src/ietf/request.ts index f705ef17203..d42e1e764a9 100644 --- a/js/net/src/ietf/request.ts +++ b/js/net/src/ietf/request.ts @@ -1,3 +1,4 @@ +import { SessionCode, SessionError } from "../error.ts"; import type { Reader, Writer } from "../stream.ts"; import * as Message from "./message.ts"; import { Parameters } from "./parameters.ts"; @@ -23,6 +24,45 @@ export function initialMaxRequestId(peerClient: boolean, window: bigint = REQUES return window * 2n + (peerClient ? 0n : 1n); } +/** Permanently spent modern request IDs, coalesced so contiguous traffic retains one range. @internal */ +export class RequestIds { + #parity: bigint; + #ranges: { start: bigint; end: bigint }[] = []; + #limit: number; + constructor(client: boolean, limit = Number(REQUEST_WINDOW)) { + this.#parity = client ? 1n : 0n; + this.#limit = limit; + } + /** Admit one peer ID regardless of request type or stream arrival order. */ + accept(id: bigint): void { + if (id < 0n || id >= 1n << 62n || id % 2n !== this.#parity) + throw new SessionError(SessionCode.InvalidRequestId); + let low = 0; + let high = this.#ranges.length; + while (low < high) { + const mid = (low + high) >>> 1; + const range = this.#ranges[mid]; + if (range && range.start <= id) low = mid + 1; + else high = mid; + } + const previous = this.#ranges[low - 1]; + const next = this.#ranges[low]; + if (previous && id <= previous.end) throw new SessionError(SessionCode.InvalidRequestId); + const right = next && next.start === id + 2n; + if (previous && previous.end === id - 2n) { + previous.end = right ? next.end : id; + if (right) this.#ranges.splice(low, 1); + } else if (right) { + next.start = id; + } else { + // Evicting an old range would make its spent IDs usable again. + if (this.#ranges.length >= this.#limit) + throw new SessionError(SessionCode.Internal, { reason: "request ID history exhausted" }); + this.#ranges.splice(low, 0, { start: id, end: id }); + } + } +} + export class MaxRequestId { static id = 0x15; diff --git a/js/net/src/ietf/request_ids.test.ts b/js/net/src/ietf/request_ids.test.ts new file mode 100644 index 00000000000..c26b5d81199 --- /dev/null +++ b/js/net/src/ietf/request_ids.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, test } from "bun:test"; +import { SessionCode, SessionError } from "../error.ts"; +import { RequestIds } from "./request.ts"; + +function code(ids: RequestIds, id: bigint): SessionCode | undefined { + try { + ids.accept(id); + } catch (err) { + if (err instanceof SessionError) return err.code; + throw err; + } + return undefined; +} + +describe("modern request ID history", () => { + test.each([true, false])("allows reordering but permanently spends IDs, local client=%s", (client) => { + const first = client ? 1n : 0n; + const ids = new RequestIds(client); + for (const offset of [4n, 0n, 2n, 8n, 6n]) ids.accept(first + offset); + for (const offset of [0n, 2n, 4n, 6n, 8n]) expect(code(ids, first + offset)).toBe(SessionCode.InvalidRequestId); + expect(code(ids, first ^ 1n)).toBe(SessionCode.InvalidRequestId); + }); + test("contiguous traffic and reverse arrival use one range", () => { + const forward = new RequestIds(false, 1); + const reverse = new RequestIds(false, 1); + for (let i = 0n; i < 4096n; i++) { + forward.accept(i * 2n); + reverse.accept((4095n - i) * 2n); + } + expect(code(forward, 0n)).toBe(SessionCode.InvalidRequestId); + expect(code(reverse, 8190n)).toBe(SessionCode.InvalidRequestId); + }); + test("bridging ranges frees capacity without forgetting IDs", () => { + const ids = new RequestIds(false, 2); + ids.accept(0n); + ids.accept(4n); + expect(code(ids, 8n)).toBe(SessionCode.Internal); + ids.accept(2n); + ids.accept(8n); + for (const id of [0n, 2n, 4n, 8n]) expect(code(ids, id)).toBe(SessionCode.InvalidRequestId); + }); + test("a huge sparse ID does not allocate its intervening gap", () => { + const ids = new RequestIds(false, 2); + ids.accept((1n << 62n) - 2n); + ids.accept(0n); + expect(code(ids, (1n << 62n) - 2n)).toBe(SessionCode.InvalidRequestId); + expect(code(ids, 1n << 62n)).toBe(SessionCode.InvalidRequestId); + }); +}); diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 1fd518f6876..ae0460abdc5 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -1051,6 +1051,7 @@ export class Subscriber { } const update = await PublishNamespaceUpdate.decode(stream.reader, version); + this.#session.admitRequest(update.requestId); // The parameters exist only on a session that negotiated the extension; // anywhere else they are the peer's violation. diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index a8f0a7987a7..e6b05fe413b 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -1224,7 +1224,8 @@ test("integration: ietf fetch group is unsupported", async () => { // subscription already on the session keeps going. test("integration: ietf SUBSCRIBE_TRACKS is refused per request", async () => { // SUBSCRIBE_TRACKS, Length, Request ID, Track Namespace Prefix ("room"), no parameters. - const subscribeTracks = new Uint8Array([0x51, 0x00, 0x08, 0x01, 0x01, 0x04, 0x72, 0x6f, 0x6f, 0x6d, 0x00]); + // A fresh even client ID, distinct from the connection's setup/discovery/subscription requests. + const subscribeTracks = new Uint8Array([0x51, 0x00, 0x08, 0x3e, 0x01, 0x04, 0x72, 0x6f, 0x6f, 0x6d, 0x00]); for (const protocol of [ Ietf.ALPN.DRAFT_18, diff --git a/quest/m1/auth/violations.md b/quest/m1/auth/violations.md index 4325ebef555..1c293aa16f9 100644 --- a/quest/m1/auth/violations.md +++ b/quest/m1/auth/violations.md @@ -40,4 +40,12 @@ regression test per case, and check the lite and IETF draft text agrees. Run Public API: none. Wire: none beyond draft wording. -**Draft progress:** [#5279](https://github.com/moq-dev/moq/pull/5279) implements malformed AUTH decoding, unexpected and extra messages, expiry/code bounds, and session-fatal propagation in Rust and JS. The quest remains open for AUTH Request-ID ordering and reuse, which must use the common validator being implemented in [#5235](https://github.com/moq-dev/moq/pull/5235), rather than introduce a competing AUTH-only validator. +**Draft progress:** [#5279](https://github.com/moq-dev/moq/pull/5279) implements malformed AUTH decoding, unexpected and extra messages, expiry/code bounds, and session-fatal propagation in Rust and JS. It composes [#5235](https://github.com/moq-dev/moq/pull/5235) for legacy request admission and adds one modern lifetime history shared by AUTH, other initial requests, and REQUEST_UPDATE. Duplicate and wrong-parity IDs close with INVALID_REQUEST_ID; reordered streams remain valid. + +The remaining decision is the resource bound for fragmented modern request-ID history. The implementation coalesces adjacent IDs and provisionally limits disjoint ranges to the existing request capacity (twice the announce plus subscription caps; 220,000 by default in both languages), closing with INTERNAL_ERROR when exhausted. Modern drafts no longer assign 0x7 to TOO_MANY_REQUESTS. Keep this quest until the history policy is settled: + +- **Recommended:** use the existing capacity, as implemented, retaining every spent ID until the session ends. +- Expose a separate configurable history budget, adding a public API surface that must match both languages and bindings. +- Keep unbounded compressed history, preserving arbitrary fragmentation but allowing memory to grow with adversarial or abandoned requests. + +Evicting old history or enforcing arrival order would allow reuse or reject reordered streams, respectively, and does not satisfy the lifetime contract. diff --git a/rs/moq-net/benches/request-ids.rs b/rs/moq-net/benches/request-ids.rs index 5d1031dbe92..d003bb4cd41 100644 --- a/rs/moq-net/benches/request-ids.rs +++ b/rs/moq-net/benches/request-ids.rs @@ -32,5 +32,35 @@ fn request_ids(c: &mut Criterion) { } group.finish(); } -criterion_group!(benches, request_ids); +fn request_history(c: &mut Criterion) { + let mut group = c.benchmark_group("modern-request-ids"); + group.sample_size(20); + group.warm_up_time(Duration::from_millis(200)); + group.measurement_time(Duration::from_secs(1)); + for fragmented in [false, true] { + for sessions in [1, 8, 32] { + for ranges in [8, 32, 128] { + let mut ids: Vec<_> = (0..ranges).map(|id| id * if fragmented { 4 } else { 2 }).collect(); + if fragmented { + ids.reverse(); + } + let mode = if fragmented { "fragmented" } else { "contiguous" }; + println!( + "admitted,{mode},{sessions},{ranges},{}", + moq_net::fuzz::request_history(&ids) * sessions + ); + group.bench_function(BenchmarkId::new(mode, format!("{sessions}x{ranges}")), |b| { + b.iter(|| { + let accepted: usize = (0..sessions) + .map(|_| moq_net::fuzz::request_history(black_box(&ids))) + .sum(); + black_box(accepted) + }); + }); + } + } + } + group.finish(); +} +criterion_group!(benches, request_ids, request_history); criterion_main!(benches); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 8f42dde6004..5bb0e5b8350 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -1171,6 +1171,11 @@ impl crate::transport::Error for NoError { } } +/// Admit modern request IDs for the history benchmark, returning the accepted count. +pub fn request_history(ids: &[u64]) -> usize { + ietf::request_history(ids) +} + /// Admit a draft-16 window for the request-ID benchmark, returning the accepted count. pub fn request_ids(window: u64, ids: &[u64]) -> usize { ietf::request_ids(window, ids) diff --git a/rs/moq-net/src/ietf/control.rs b/rs/moq-net/src/ietf/control.rs index 6561bb054b0..373ab2f4dd5 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -1,4 +1,7 @@ -use std::{collections::HashSet, task::Poll}; +use std::{ + collections::{BTreeMap, HashSet}, + task::Poll, +}; use crate::{ Error, SessionError, @@ -19,6 +22,8 @@ struct ControlState { request_id_max: Option, /// The peer's requests, flow controlled on drafts 14 to 16 only. incoming: Option, + /// Permanently spent modern request IDs, compressed across adjacent IDs. + modern: Ids, } /// The MAX_REQUEST_ID window we grant the peer. @@ -40,6 +45,41 @@ struct Incoming { missing: Option>, } +/// Modern request streams can reorder, so a high-water mark alone would reject valid peers. +struct Ids { + parity: u64, + ranges: BTreeMap, + limit: u64, +} + +impl Ids { + fn accept(&mut self, id: RequestId) -> Result<(), Error> { + let id = id.0; + if id >= 1 << 62 || id % 2 != self.parity { + return Err(SessionError::InvalidRequestId.into()); + } + let previous = self.ranges.range(..=id).next_back().map(|(&start, &end)| (start, end)); + if previous.is_some_and(|(_, end)| id <= end) { + return Err(SessionError::InvalidRequestId.into()); + } + let next = self.ranges.range(id..).next().map(|(&start, &end)| (start, end)); + let left = previous.filter(|(_, end)| end + 2 == id); + let right = next.filter(|(start, _)| *start == id + 2); + // Never evict history: doing so would make a retired ID usable again. + if left.is_none() && right.is_none() && self.ranges.len() as u64 >= self.limit { + tracing::warn!(limit = self.limit, "request ID history exhausted"); + return Err(SessionError::Internal.into()); + } + let start = left.map_or(id, |(start, _)| start); + let end = right.map_or(id, |(_, end)| end); + if let Some((start, _)) = right { + self.ranges.remove(&start); + } + self.ranges.insert(start, end); + Ok(()) + } +} + #[derive(Clone)] pub(super) struct Control { state: kio::Shared, @@ -52,6 +92,11 @@ impl Control { request_id_next: if client { RequestId(0) } else { RequestId(1) }, request_id_max, incoming: None, + modern: Ids { + parity: u64::from(client), + ranges: BTreeMap::new(), + limit: crate::session::Limits::default().requests(), + }, }), } } @@ -74,6 +119,11 @@ impl Control { self } + /// Bound fragmented modern ID history using the session's existing request capacity. + pub fn history_limit(&self, limit: u64) { + self.state.lock().modern.limit = limit; + } + pub fn max_request_id(&self, max: RequestId) { // Mutating through the guard wakes any `next_request_id` waiting on the limit. self.state.lock().request_id_max = Some(max); @@ -89,6 +139,7 @@ impl Control { pub fn accept(&self, id: RequestId) -> Result, Error> { let mut state = self.state.lock(); let Some(incoming) = &mut state.incoming else { + state.modern.accept(id)?; return Ok(None); }; if id.0 >= incoming.max { @@ -325,11 +376,81 @@ mod tests { } } - /// Draft-17+ has no MAX_REQUEST_ID: nothing is tracked or granted. + #[test] + fn modern_requests_reject_reuse_and_wrong_parity() { + for client in [false, true] { + let control = Control::new(None, client); + let first = u64::from(client); + for id in [first + 4, first, first + 2] { + assert!(control.accept(RequestId(id)).is_ok()); + } + for id in [first, first + 2, first + 4, first ^ 1] { + assert!( + matches!( + control.accept(RequestId(id)), + Err(Error::Session(SessionError::InvalidRequestId)) + ), + "accepted spent or wrong-parity ID {id}" + ); + } + } + } + + #[test] + fn modern_history_coalesces_and_never_forgets() { + for reversed in [false, true] { + let control = Control::new(None, false); + control.history_limit(1); + for n in 0..4096 { + let id = if reversed { 4095 - n } else { n } * 2; + assert!(control.accept(RequestId(id)).is_ok()); + } + assert_eq!(control.state.lock().modern.ranges.len(), 1); + assert!(matches!( + control.accept(RequestId(0)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } + let control = Control::new(None, false); + control.history_limit(2); + control.accept(RequestId(0)).unwrap(); + control.accept(RequestId(4)).unwrap(); + assert!(matches!( + control.accept(RequestId(8)), + Err(Error::Session(SessionError::Internal)) + )); + control.accept(RequestId(2)).unwrap(); + control.accept(RequestId(8)).unwrap(); + for id in [0, 2, 4, 8] { + assert!(matches!( + control.accept(RequestId(id)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } + } + + #[test] + fn modern_sparse_ids_do_not_allocate_the_gap() { + let control = Control::new(None, false); + control.history_limit(2); + control.accept(RequestId((1 << 62) - 2)).unwrap(); + control.accept(RequestId(0)).unwrap(); + assert_eq!(control.state.lock().modern.ranges.len(), 2); + assert!(matches!( + control.accept(RequestId((1 << 62) - 2)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + assert!(matches!( + control.accept(RequestId(1 << 62)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } + + /// Draft-17+ spends IDs without granting a MAX_REQUEST_ID window. #[test] fn unlimited_without_a_window() { let control = Control::new(None, true); - assert!(control.accept(RequestId(u64::MAX)).unwrap().is_none()); + assert!(control.accept(RequestId(1)).unwrap().is_none()); assert_eq!(granted(&control), None); } } diff --git a/rs/moq-net/src/ietf/mod.rs b/rs/moq-net/src/ietf/mod.rs index 9446fd6ad38..50f2fcdce83 100644 --- a/rs/moq-net/src/ietf/mod.rs +++ b/rs/moq-net/src/ietf/mod.rs @@ -69,3 +69,9 @@ pub(crate) fn request_ids(window: u64, ids: &[u64]) -> usize { .collect(); held.len() } + +#[cfg(any(test, feature = "fuzz"))] +pub(crate) fn request_history(ids: &[u64]) -> usize { + let control = Control::new(None, false); + ids.iter().filter(|id| control.accept(RequestId(**id)).is_ok()).count() +} diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 8fb4fff3b03..394470d33bb 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -948,7 +948,15 @@ where .reader .poll_decode_maybe::(&mut cx) { - Poll::Ready(Ok(Some(update))) => return Poll::Ready(Ok(Some(update))), + Poll::Ready(Ok(Some(update))) => { + if let Err(err) = self.control.accept(update.request_id) { + session + .clone() + .close(crate::SessionError::from(&err).to_code(), "invalid request ID"); + return Poll::Ready(Err(err)); + } + return Poll::Ready(Ok(Some(update))); + } Poll::Ready(Ok(None)) if !super::request_stream::fin_cancels(self.version) => { *finished = true } diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index be401ca2e67..c153f6417ce 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -385,6 +385,7 @@ where }; let control = Control::new(None, client); + control.history_limit(limits.requests()); // Only the dialing side fails loud on a publication outside its grant: a // server's publish origin is everything the peer may read, not what it // intends to push. @@ -1182,6 +1183,22 @@ where Err(err) => return Err(err), }; + if !matches!(version, Version::Draft14 | Version::Draft15 | Version::Draft16) + && matches!( + id, + ietf::Subscribe::ID + | ietf::Fetch::ID | ietf::SubscribeNamespace::ID + | ietf::SubscribeNamespaceLegacy::ID + | ietf::SUBSCRIBE_TRACKS_ID + | ietf::TrackStatus::ID + | ietf::Publish::ID + | ietf::PublishNamespace::ID + | auth::Auth::ID + ) { + let request_id = RequestId::decode(&mut data.decoder(version), version)?; + publisher.control.accept(request_id)?; + } + match id { // Draft-16 moved SUBSCRIBE_NAMESPACE to its own stream, past the control stream // that admits every other request, but it still takes a request ID from the @@ -1345,6 +1362,96 @@ mod tests { use super::*; use crate::model::ProduceTest; + #[moq_net_sim::test] + async fn auth_ids_share_the_modern_request_history() { + for version in [ + Version::Draft17, + Version::Draft18, + Version::Draft19, + Version::Draft20, + Version::Draft21, + Version::Draft22, + ] { + for client in [false, true] { + let first = u64::from(client); + for requests in [ + vec![(true, first), (true, first)], + vec![(true, first), (false, first)], + vec![(false, first), (true, first)], + vec![(true, first ^ 1)], + ] { + let incoming = requests + .into_iter() + .map(|(is_auth, id)| { + let mut body = Vec::new(); + let mut encoder = crate::coding::Encoder::new(&mut body, version.into()); + let kind = if is_auth { + auth::Auth { + request_id: RequestId(id), + token: Default::default(), + } + .encode_msg(&mut encoder, version) + .unwrap(); + auth::Auth::ID + } else { + ietf::TrackStatus { + request_id: RequestId(id), + track_namespace: crate::Path::from("test"), + track_name: "video".into(), + properties_wanted: true, + } + .encode_msg(&mut encoder, version) + .unwrap(); + ietf::TrackStatus::ID + }; + let mut script = Vec::new(); + let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); + encoder.varint(kind).unwrap(); + encoder.u16(body.len() as u16); + script.extend(body); + script + }) + .collect(); + let session = + crate::lite::test_transport::ScriptedSession::new(Vec::new()).with_incoming_bidis(incoming); + let log = session.log.clone(); + let (driver, _, _) = start(Config { + runtime: crate::time::Clock::sim(), + session, + setup: None, + request_id_max: None, + limits: Default::default(), + client, + publish: None, + subscribe: None, + peer_hop: None, + cost: None, + version, + path: None, + authority: None, + peer_setup_stream: None, + peer_declared: Some(peer::Peer { + auth: true, + ..Default::default() + }), + auth: crate::auth::Handle::new(true), + early_unis: Vec::new(), + }) + .unwrap(); + let err = moq_net_sim::timeout(std::time::Duration::from_secs(10), driver) + .await + .expect("invalid AUTH ID left the session open") + .expect_err("invalid request ID must be fatal"); + assert!( + matches!(err, Error::Session(SessionError::InvalidRequestId)), + "{version:?} client={client}: {err:?}" + ); + assert_eq!(log.closes()[0].0, SessionError::InvalidRequestId.to_code()); + } + } + } + } + #[moq_net_sim::test] async fn malformed_auth_replies_close_the_session() { for version in [Version::Draft17, Version::Draft22] { diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 3584105058d..f7bba154153 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -1348,6 +1348,12 @@ where } let msg = ietf::PublishNamespaceUpdate::decode_msg(&mut data, self.version)?; + if let Err(err) = self.control.accept(msg.request_id) { + self.session + .clone() + .close(crate::SessionError::from(&err).to_code(), "invalid request ID"); + return Err(err); + } // Junk inside the declared size would otherwise be applied silently, which // is the one decode path that skipped the check the others make. if !data.is_empty() { @@ -6840,7 +6846,7 @@ mod tests { crate::time::Clock::sim(), session.clone(), origin, - Control::new(None, false), + Control::new(None, true), None, peer::PeerSetup::default(), self_origin, @@ -6918,6 +6924,32 @@ mod tests { ) } + /// Namespace updates spend IDs from the same lifetime history as initial requests. + #[moq_net_sim::test] + async fn namespace_update_cannot_reuse_a_spent_request_id() { + let self_origin = crate::Hop::new(5).unwrap(); + let peer = peer_9(); + let (clean, _) = clean_and_looped(); + let script = publish_namespace_updates(std::slice::from_ref(&clean)).await; + let (mut subscriber, _consumer, mut stream, _driver) = update_harness(self_origin, &peer, &clean, script).await; + let log = subscriber.session.log.clone(); + // The initial request is already retired; its ID remains permanently spent. + assert!(subscriber.control.accept(RequestId(3)).unwrap().is_none()); + let mut attached = true; + let err = subscriber + .run_publish_namespace_updates( + &mut stream, + &crate::Path::new("room/host"), + Some(clean), + peer, + &mut attached, + ) + .await + .expect_err("a repeated update ID must close the session"); + assert!(matches!(err, Error::Session(crate::SessionError::InvalidRequestId))); + assert_eq!(log.closes()[0].0, crate::SessionError::InvalidRequestId.to_code()); + } + /// A reflected update detaches the route but MUST NOT end the stream. Updates ride /// the stream that already carries the advertisement, so closing it strands the /// namespace even when the peer's path goes clean again. It is also not ours to diff --git a/rs/moq-net/src/session.rs b/rs/moq-net/src/session.rs index 4303a0baf58..7102133a33c 100644 --- a/rs/moq-net/src/session.rs +++ b/rs/moq-net/src/session.rs @@ -671,7 +671,9 @@ impl Drop for Withdrawing { /// /// On moq-transport drafts 14 to 16 the caps also size the request window, which counts /// every request (FETCH, TRACK_STATUS and SUBSCRIBE_UPDATE included), so very low caps -/// can starve it. +/// can starve it. Modern drafts retain compressed request-ID history to reject reuse +/// after a request ends. At most twice the sum of these caps may be disjoint ID ranges; +/// exhausting that local history closes with `INTERNAL_ERROR`, without forgetting IDs. #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[non_exhaustive] pub struct Limits { diff --git a/rs/moq-net/tests/subscribe_tracks.rs b/rs/moq-net/tests/subscribe_tracks.rs index af34c42403e..5efce043069 100644 --- a/rs/moq-net/tests/subscribe_tracks.rs +++ b/rs/moq-net/tests/subscribe_tracks.rs @@ -20,7 +20,7 @@ const TIMEOUT: Duration = Duration::from_secs(10); const SUBSCRIBE_TRACKS: &[u8] = &[ 0x51, // SUBSCRIBE_TRACKS 0x00, 0x08, // Length - 0x01, // Request ID + 0x3f, // Fresh server-parity Request ID, after the existing subscription. 0x01, 0x04, b'r', b'o', b'o', b'm', // Track Namespace Prefix 0x00, // Number of Parameters ]; From c30f2f72e208c8b87e47cc7ba0f94b9afa015650 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 12 Oct 2026 08:09:23 -0700 Subject: [PATCH 10/13] fix(net): share the draft-16 skipped-run tracker with draft-17+ request IDs Co-Authored-By: Claude Opus 5.5 --- quest/m1/auth/violations.md | 8 ++++---- rs/moq-net/src/ietf/control.rs | 12 +----------- rs/moq-net/src/ietf/session.rs | 3 ++- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/quest/m1/auth/violations.md b/quest/m1/auth/violations.md index 62c1ca58148..f78d58a57e2 100644 --- a/quest/m1/auth/violations.md +++ b/quest/m1/auth/violations.md @@ -41,12 +41,12 @@ regression test per case, and check the lite and IETF draft text agrees. Run Public API: none. Wire: none beyond draft wording. -**Draft progress:** [#5279](https://github.com/moq-dev/moq/pull/5279) implements malformed AUTH decoding, unexpected and extra messages, expiry/code bounds, and session-fatal propagation in Rust and JS. It composes [#5235](https://github.com/moq-dev/moq/pull/5235) for legacy request admission and adds one modern lifetime history shared by AUTH, other initial requests, and REQUEST_UPDATE. Duplicate and wrong-parity IDs close with INVALID_REQUEST_ID; reordered streams remain valid. +**Draft progress:** [#5279](https://github.com/moq-dev/moq/pull/5279) implements malformed AUTH decoding, unexpected and extra messages, expiry/code bounds, and session-fatal propagation in Rust and JS. Draft-17+ requests, AUTH, and REQUEST_UPDATE share one lifetime history, reusing the draft-16 skipped-run tracker from [#5235](https://github.com/moq-dev/moq/pull/5235). Duplicate and wrong-parity IDs close with INVALID_REQUEST_ID; reordered streams remain valid. -The remaining decision is the resource bound for fragmented modern request-ID history. The implementation coalesces adjacent IDs and provisionally limits disjoint ranges to the existing request capacity (twice the announce plus subscription caps; 220,000 by default in both languages), closing with INTERNAL_ERROR when exhausted. Modern drafts no longer assign 0x7 to TOO_MANY_REQUESTS. Keep this quest until the history policy is settled: +The remaining decision is the bound on draft-17+ skipped runs, which no MAX_REQUEST_ID window limits. The implementation caps them at the existing request capacity (twice the announce plus subscription caps; 220,000 by default in both languages), closing with INTERNAL_ERROR when exhausted. Modern drafts no longer assign 0x7 to TOO_MANY_REQUESTS. Keep this quest until the policy is settled: - **Recommended:** use the existing capacity, as implemented, retaining every spent ID until the session ends. - Expose a separate configurable history budget, adding a public API surface that must match both languages and bindings. -- Keep unbounded compressed history, preserving arbitrary fragmentation but allowing memory to grow with adversarial or abandoned requests. +- Keep unbounded history, allowing memory to grow with adversarial or abandoned requests. -Evicting old history or enforcing arrival order would allow reuse or reject reordered streams, respectively, and does not satisfy the lifetime contract. +Forgetting old runs or enforcing arrival order would allow reuse or reject reordered streams, respectively, and does not satisfy the lifetime contract. diff --git a/rs/moq-net/src/ietf/control.rs b/rs/moq-net/src/ietf/control.rs index 4cd4d0a0eee..cfbb01ad176 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -375,17 +375,7 @@ mod tests { assert_eq!(missing.iter().collect::>(), [(&0, &2)]); } drop(control.accept(RequestId(0)).unwrap()); - assert!( - control - .state - .lock() - .incoming - .as_ref() - .unwrap() - .spent - .missing - .is_empty() - ); + assert!(control.state.lock().incoming.as_ref().unwrap().spent.missing.is_empty()); for id in [0, 2, 2046] { assert!(matches!( control.accept(RequestId(id)), diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index d02b35c001e..96e952f0ee2 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -1273,7 +1273,8 @@ impl Bidi { && matches!( id, ietf::Subscribe::ID - | ietf::Fetch::ID | ietf::SubscribeNamespace::ID + | ietf::Fetch::ID + | ietf::SubscribeNamespace::ID | ietf::SubscribeNamespaceLegacy::ID | ietf::SUBSCRIBE_TRACKS_ID | ietf::TrackStatus::ID From 73ee7088a43e9fa0c0e18436bad9faf5501b9de9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 12 Oct 2026 08:59:03 -0700 Subject: [PATCH 11/13] fix(net): refuse request IDs past 2^62 and close on truncated Lite AUTH replies Draft-17+ varints carry 64 bits, so the largest IDs overflowed the spent-ID tracker's next ID. A Lite AUTH reply cut off inside its frame was treated as an unsupported peer again after the merge with main; it is malformed. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ietf/adapter.ts | 3 ++- js/net/src/ietf/request_ids.test.ts | 4 +++- rs/moq-net/src/ietf/control.rs | 15 ++++++++++----- rs/moq-net/src/lite/session.rs | 22 +++++++++++++++++++--- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index ef40ebde839..8298f1fb0e4 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -189,7 +189,8 @@ export class SpentIds { /** Spend `id`, throwing the session error a reused, wrong-parity, or unrecordable ID closes with. */ spend(id: bigint): void { - const unused = id >= this.#next || this.#missing.has(id); + // No peer reaches 2^62 requests; refusing past it matches Rust, whose `next` would overflow. + const unused = id < 1n << 62n && (id >= this.#next || this.#missing.has(id)); if (id % 2n !== this.#next % 2n || !unused) throw new SessionError(SessionCode.InvalidRequestId); const splits = id < this.#next ? this.#missing.splits(id) : id > this.#next; if (splits && this.#missing.size >= this.#limit) { diff --git a/js/net/src/ietf/request_ids.test.ts b/js/net/src/ietf/request_ids.test.ts index 3496f90ac96..4c9f59554ca 100644 --- a/js/net/src/ietf/request_ids.test.ts +++ b/js/net/src/ietf/request_ids.test.ts @@ -45,6 +45,8 @@ describe("draft-17+ request ID history", () => { const ids = new SpentIds(0n, 1); ids.spend((1n << 62n) - 2n); ids.spend(0n); - expect(code(ids, (1n << 62n) - 2n)).toBe(SessionCode.InvalidRequestId); + for (const id of [(1n << 62n) - 2n, 1n << 62n, (1n << 64n) - 2n]) { + expect(code(ids, id)).toBe(SessionCode.InvalidRequestId); + } }); }); diff --git a/rs/moq-net/src/ietf/control.rs b/rs/moq-net/src/ietf/control.rs index cfbb01ad176..c4a9bf0fed3 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -31,8 +31,11 @@ impl Spent { } /// Whether `id` has the peer's parity and was never spent. + /// + /// No peer reaches 2^62 requests, and refusing the IDs past it keeps `next` from + /// overflowing on a draft-17+ varint, which can carry a full 64 bits. fn unused(&self, id: u64) -> bool { - id % 2 == self.next % 2 && (id >= self.next || self.run(id).is_some()) + id < 1 << 62 && id % 2 == self.next % 2 && (id >= self.next || self.run(id).is_some()) } /// Whether spending `id` would record one more run. @@ -473,10 +476,12 @@ mod tests { control.accept(RequestId(0)).unwrap(); let runs: Vec<_> = control.state.lock().modern.missing.clone().into_iter().collect(); assert_eq!(runs, [(2, top)]); - assert!(matches!( - control.accept(RequestId(top)), - Err(Error::Session(SessionError::InvalidRequestId)) - )); + for id in [top, 1 << 62, u64::MAX - 1] { + assert!(matches!( + control.accept(RequestId(id)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } } /// Draft-17+ has no MAX_REQUEST_ID: nothing is granted. diff --git a/rs/moq-net/src/lite/session.rs b/rs/moq-net/src/lite/session.rs index dc495015398..476ccaa6b8b 100644 --- a/rs/moq-net/src/lite/session.rs +++ b/rs/moq-net/src/lite/session.rs @@ -546,8 +546,9 @@ impl kio::Task for PresentToken { // A peer that predates AUTH resets a stream type it does not know, and one // that takes no tokens in band refuses the same way: either way, before any // reply, this token earns nothing here. - Error::Stream(_) | Error::Decode(crate::DecodeError::Short) if !answered => Error::Unsupported, - // A malformed reply is the peer breaking the session protocol, not refusing a token. + Error::Stream(_) if !answered => Error::Unsupported, + // A malformed reply, truncated ones included, is the peer breaking the session + // protocol, not refusing a token. err => crate::auth::protocol_error(err), }; match &err { @@ -990,6 +991,18 @@ mod tests { assert_malformed_reply_closes(script).await; } + /// A reply cut off inside its declared size fails to decode, unlike a peer that + /// resets before answering. + #[moq_net_sim::test] + async fn truncated_auth_reply_closes_the_session() { + let mut script = Vec::new(); + let mut w = crate::coding::Encoder::new(&mut script, Version::Lite07.into()); + w.varint(0).unwrap(); + w.varint(4).unwrap(); + w.varint(0).unwrap(); + assert_session_closes(ScriptedSession::per_stream_eof(vec![script])).await; + } + #[moq_net_sim::test] async fn malformed_auth_error_reasons_close_the_session() { for reason in [vec![0xff], vec![b'a'; 8193]] { @@ -1005,7 +1018,10 @@ mod tests { } async fn assert_malformed_reply_closes(script: Vec) { - let transport = ScriptedSession::per_stream(vec![script]); + assert_session_closes(ScriptedSession::per_stream(vec![script])).await; + } + + async fn assert_session_closes(transport: ScriptedSession) { let log = transport.log.clone(); let start = start(Config { runtime: crate::time::Clock::sim(), From c64db18b01f728490569d3d6f053aa68f5d277f6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 12 Oct 2026 12:30:19 -0700 Subject: [PATCH 12/13] quest: complete AUTH protocol violations The maintainer chose the derived request-ID history bound, settling the last open decision. Co-Authored-By: Claude Opus 5.5 --- quest/m1/auth/README.md | 1 - quest/m1/auth/violations.md | 52 -------------------------------- quest/m1/lite-malformed-close.md | 4 +-- 3 files changed, 1 insertion(+), 56 deletions(-) delete mode 100644 quest/m1/auth/violations.md diff --git a/quest/m1/auth/README.md b/quest/m1/auth/README.md index e5f120c0d75..eb44f36f93d 100644 --- a/quest/m1/auth/README.md +++ b/quest/m1/auth/README.md @@ -99,7 +99,6 @@ because `main` assigned 0x3A to NOT_FETCHABLE first. ## Required - [WebSocket refusal](/quest/m1/auth/ws-unauthorized.md) - the relay serves WebSocket through moq-tokio, so a refused token closes the session as Unauthorized, as QUIC does -- [AUTH violations](/quest/m1/auth/violations.md) - every AUTH protocol violation closes the session in Rust and JS, lite and IETF - [Relay tokens](/quest/m1/auth/relay-refresh.md) - the relay verifies tokens sent in band, unions their grants, and cancels only work that loses access - [Request-token decode](/quest/m1/auth/request-token-decode.md) - an diff --git a/quest/m1/auth/violations.md b/quest/m1/auth/violations.md deleted file mode 100644 index f78d58a57e2..00000000000 --- a/quest/m1/auth/violations.md +++ /dev/null @@ -1,52 +0,0 @@ -# [M] AUTH protocol violations close the session everywhere - -## Goal - -Every AUTH stream message that the lite or IETF drafts call a -PROTOCOL_VIOLATION closes the session in Rust and `@moq/net` alike, instead -of ending only its token. Examples: an AUTH_ERROR code past `u32`, an -out-of-range IETF AUTH_OK expiry, or an AUTH or AUTH_ERROR that fails to -decode. The lite AUTH_OK pattern and `Expires` decode gap, plus invalid UTF-8 -and oversized AUTH_ERROR reasons, belong to -[Malformed grant](https://github.com/moq-dev/moq/pull/5256). - -## Plan - -AUTH endings (#4550) closes the session only -on an explicit `ProtocolViolation` in Rust lite. These gaps remain: - -- JS reports the exact oversized code but doesn't close the session. Let the - JS connection close on an `AuthSession` protocol violation. -- An out-of-range IETF AUTH_OK expiry still ends only its token. -- Other AUTH_ERROR framing or decode failures end only the token; invalid - UTF-8 and oversized reasons already close the session. -- On the acceptor side, a presenter's AUTH that fails to decode only aborts - its stream (Rust lite `AuthServe`, JS `#runBidis`), so a peer can repeat - malformed AUTH streams without closing the session. -- Both IETF AUTH acceptors (Rust `Serve::run`, JS `IetfAuthWire.accept`) - discard the decoded Request ID, so a reused ID is accepted. Validate it like - every other request, where a duplicate is session-fatal. -- An AUTH reply of an unknown type (neither AUTH_OK nor AUTH_ERROR) fails - the type dispatch before either decoder runs, and ends only the token. -- A second AUTH on an open IETF AUTH stream is never read: after the first, - Rust `Serve::run` and JS `AuthSession.serve` wait only for the stream to - close. Treat it like a repeated PUBLISH_NAMESPACE, a duplicate request. - -This list comes from review, not a full audit. Walk every AUTH read path on -both sides, both protocols, and both languages, and fold in anything missing. - -Close the session on each, in both languages and on both protocols, with one -regression test per case, and check the lite and IETF draft text agrees. Run -`just test interop --all`. - -Public API: none. Wire: none beyond draft wording. - -**Draft progress:** [#5279](https://github.com/moq-dev/moq/pull/5279) implements malformed AUTH decoding, unexpected and extra messages, expiry/code bounds, and session-fatal propagation in Rust and JS. Draft-17+ requests, AUTH, and REQUEST_UPDATE share one lifetime history, reusing the draft-16 skipped-run tracker from [#5235](https://github.com/moq-dev/moq/pull/5235). Duplicate and wrong-parity IDs close with INVALID_REQUEST_ID; reordered streams remain valid. - -The remaining decision is the bound on draft-17+ skipped runs, which no MAX_REQUEST_ID window limits. The implementation caps them at the existing request capacity (twice the announce plus subscription caps; 220,000 by default in both languages), closing with INTERNAL_ERROR when exhausted. Modern drafts no longer assign 0x7 to TOO_MANY_REQUESTS. Keep this quest until the policy is settled: - -- **Recommended:** use the existing capacity, as implemented, retaining every spent ID until the session ends. -- Expose a separate configurable history budget, adding a public API surface that must match both languages and bindings. -- Keep unbounded history, allowing memory to grow with adversarial or abandoned requests. - -Forgetting old runs or enforcing arrival order would allow reuse or reject reordered streams, respectively, and does not satisfy the lifetime contract. diff --git a/quest/m1/lite-malformed-close.md b/quest/m1/lite-malformed-close.md index baadda12811..d21893f0fe4 100644 --- a/quest/m1/lite-malformed-close.md +++ b/quest/m1/lite-malformed-close.md @@ -51,8 +51,7 @@ Rust-to-Rust and Rust-to-JS sessions never hit these; third-party peers do. it. Prefer one shared close path over per-handler fixes. - One test per rule per language, driving raw bytes like the runner does, including a malformed SUBSCRIBE_UPDATE and a malformed PROBE report. - Share the session-close helper from - [AUTH protocol violations](/quest/m1/auth/violations.md) if it lands first. + Share the session-close path that malformed AUTH messages already use. Public API: none. Wire: none; the behavior now matches the draft. @@ -65,4 +64,3 @@ Public API: none. Wire: none; the behavior now matches the draft. ## Related - [Malformed moq-transport input](/quest/m2/ietf-malformed-close.md) - the same rule for draft-18 and draft-21 -- [AUTH protocol violations](/quest/m1/auth/violations.md) - the AUTH-only slice, in progress From 146fdf87c4d27eed77f1075c801bfbb2858a1c17 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 12 Oct 2026 13:03:57 -0700 Subject: [PATCH 13/13] drafts(lite): record the session-fatal AUTH stream rules in the changelog Co-Authored-By: Claude Opus 5.5 --- drafts/draft-lcurley-moq-lite.md | 1 + 1 file changed, 1 insertion(+) diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 9b7b3cf1320..9caf4125ea8 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -1569,6 +1569,7 @@ The `Message Length` describes the payload size on the wire. - Added the AUTH_TOKEN_CACHE_OVERFLOW (0x13) and UNKNOWN_AUTH_TOKEN_ALIAS (0x17) session codes, with moq-transport's meaning. - Limited AUTH_OK Expires to 2^53 - 1 milliseconds, with an out-of-range value closing the session with PROTOCOL_VIOLATION. - Invalid UTF-8 or an oversized AUTH_ERROR reason closes the session with PROTOCOL_VIOLATION. +- An Auth Stream message that fails to decode, has trailing body bytes, or has an unexpected type, or a second AUTH from the opener, closes the session with PROTOCOL_VIOLATION. - Added NOT_SUPPORTED (0x30) to session errors; unsupported Auth Stream tokens and grants use AUTH_ERROR before or after a grant. - Added the Auth Stream (0x7) with AUTH, AUTH_OK, and AUTH_ERROR: either endpoint presents a token on its own stream and learns the [path patterns](#path-pattern) it may publish and subscribe to. The union of a session's open grants is its scope. A shrink withdraws what it no longer covers, and an announcement outside the scope closes the session with UNAUTHORIZED. A peer without the stream resets it.