From 7b9914086e262a4e3bc78dc46868bba2d866a5d4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 13:14:08 -0700 Subject: [PATCH 1/7] claim quest/m0/ietf-namespace-stream From ea26cde277a309d3a6fa824b737036764bf3716c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 13:28:57 -0700 Subject: [PATCH 2/7] fix(net): fill draft-16+ SUBSCRIBE_NAMESPACE streams A peer that did not send SOLICIT still gets NAMESPACE and NAMESPACE_DONE for each match. Unsolicited PUBLISH_NAMESPACE pushes stay, so that peer hears each namespace twice. Drafts 14 and 15 are unchanged. --- doc/concept/standard.md | 4 +- js/net/src/ietf/connection.ts | 4 + js/net/src/ietf/publisher.test.ts | 52 ++++++++ js/net/src/ietf/publisher.ts | 39 +++--- rs/moq-net/src/ietf/publisher.rs | 208 +++++++++++++++++++++++++----- 5 files changed, 258 insertions(+), 49 deletions(-) diff --git a/doc/concept/standard.md b/doc/concept/standard.md index 9b4730a7953..10ddf3e132e 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -141,6 +141,8 @@ network (it accepts any certificate, so never point it at a remote relay) and `RUST_LOG=info,moq_net=debug` to see the negotiated version. Behavior worth knowing when pointing another implementation at ours: we announce every namespace we can offer unsolicited *and* ask for every prefix we may discover; -set the solicit `SETUP` option to make us wait to be asked. Single-track +set the solicit `SETUP` option to make us wait to be asked. A peer that does not +set that option still gets each match as a `NAMESPACE` on its `SUBSCRIBE_NAMESPACE` +stream on draft-16 and later, so it hears the namespace twice. Single-track `PUBLISH` offers are declined; announce a namespace and serve the resulting `SUBSCRIBE`s instead. diff --git a/js/net/src/ietf/connection.ts b/js/net/src/ietf/connection.ts index 9bf00846513..661e791c332 100644 --- a/js/net/src/ietf/connection.ts +++ b/js/net/src/ietf/connection.ts @@ -212,6 +212,10 @@ export class Connection implements Established { async #run(early: Reader[]): Promise { try { + // Both run together. runPublishNamespaces is a no-op when the peer asked to be + // solicited; otherwise it pushes PUBLISH_NAMESPACE. On draft-16 and later a + // SUBSCRIBE_NAMESPACE stream is filled either way. A NAMESPACE is discovery, + // not a second route. await Promise.all([this.#runBidis(), this.#runUnis(early), this.#publisher.runPublishNamespaces()]); } catch (err) { if (!this.#closed) { diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index 310eb85f945..c3400043f49 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -2317,6 +2317,58 @@ test("draft-20: PUBLISH_DONE waits for a queued group and counts every stream", } }); +/** + * A peer that did not send SOLICIT still gets NAMESPACE on its SUBSCRIBE_NAMESPACE + * stream on draft-16 and later: one for a match that already exists, one announced + * after, then NAMESPACE_DONE when that announcement ends. + */ +test.each([ + ["draft-16", Version.DRAFT_16], + ["draft-18", Version.DRAFT_18], +] as const)( + "a non-SOLICIT %s SUBSCRIBE_NAMESPACE carries NAMESPACE then NAMESPACE_DONE", + async (_, version) => { + const pair = createMockTransportPair(ALPN.DRAFT_19); + const session = new NativeSession(pair.server, version, true); + const { pub, origin } = publisher(pair.server, { requiresSolicitation: false, session }); + const early = publish(origin, Path.from("early-cam")); + + const subscription = await Stream.open(pair.client, { version }); + const accepted = await Stream.accept(pair.server, version); + if (!accepted) throw new Error("missing subscription"); + const run = pub.runSubscribeNamespace( + new SubscribeNamespace({ requestId: 1n, namespace: Path.empty() }), + accepted, + ); + + try { + expect(await subscription.reader.u53()).toBe(RequestOk.id); + await RequestOk.decode(subscription.reader, version); + + expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntry.id); + expect((await SubscribeNamespaceEntry.decode(subscription.reader, version)).suffix).toBe( + Path.from("early-cam"), + ); + + publish(origin, Path.from("late-cam")); + expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntry.id); + expect((await SubscribeNamespaceEntry.decode(subscription.reader, version)).suffix).toBe( + Path.from("late-cam"), + ); + + early.close(); + expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntryDone.id); + expect((await SubscribeNamespaceEntryDone.decode(subscription.reader, version)).suffix).toBe( + Path.from("early-cam"), + ); + } finally { + subscription.close(); + origin.close(); + await run; + } + }, +); + for (const version of [Version.DRAFT_15, Version.DRAFT_19] as const) { for (const declared of [false, true]) { for (const solicited of [false, true]) { diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 941b6ba67a3..4c817c6e25e 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -239,9 +239,10 @@ export class Publisher { #hidden: boolean; // The origin this session serves, borrowed: it outlives the session, and closing the - // session leaves its broadcasts alone. The namespaces are advertised with an unsolicited - // PUBLISH_NAMESPACE (see {@link runPublishNamespaces}), or on request if the peer asked - // for that (see {@link runSubscribeNamespace}). + // session leaves its broadcasts alone. Namespaces go out as an unsolicited + // PUBLISH_NAMESPACE (see {@link runPublishNamespaces}) unless the peer asked to be + // told on request. On draft-16 and later {@link runSubscribeNamespace} also carries + // every match: a NAMESPACE is discovery, not a second route. #advertised: Getter; #publish?: OriginConsumer; @@ -860,12 +861,12 @@ export class Publisher { /** * Handles an incoming SUBSCRIBE_NAMESPACE on a bidi stream. * - * This carries the advertisements when the peer asked to be told on request (MoQ - * Solicit); otherwise {@link runPublishNamespaces} has already announced everything - * visible and repeating it here would leave the peer holding two sources for one - * broadcast, so only the hidden namespaces it may see ride here (MoQ Hidden). - * Draft-16+ streams Namespace entries inline; draft-14/15 predate those messages, so - * each advertisement is a PUBLISH_NAMESPACE request of its own. + * On draft-16 and later every visible match is a NAMESPACE on this stream, whatever + * the peer's SETUP said. A NAMESPACE is discovery, not a route, so a peer that also + * hears the unsolicited PUBLISH_NAMESPACE still gets the match here. Draft-14/15 + * predate those messages, so each advertisement is a PUBLISH_NAMESPACE request, and + * only for what {@link runPublishNamespaces} does not already say (the whole set when + * the peer asked to be told on request, otherwise the hidden remainder). * * @internal */ @@ -903,12 +904,15 @@ export class Publisher { await ok.encode(stream.writer, version); } - // Hidden namespaces are left out unless the peer opted in (MoQ Hidden). Unless the - // peer asked to be told only on request, it has already heard everything visible - // from the empty prefix unasked, so this stream carries only what that hid. + // Hidden namespaces are left out unless the peer opted in (MoQ Hidden). Draft-16 + // and later carry every remaining match. Draft-14/15 only add what the + // unsolicited loop cannot say. + const visible = (covered: Path.Valid) => !this.#hidden || msg.hidden || !hiddenBelow(prefix, covered); const carries = (covered: Path.Valid) => - (!this.#hidden || msg.hidden || !hiddenBelow(prefix, covered)) && - (this.#requiresSolicitation || (this.#hidden && hiddenBelow(Path.empty(), covered))); + visible(covered) && + (legacy + ? this.#requiresSolicitation || (this.#hidden && hiddenBelow(Path.empty(), covered)) + : true); // Inline entries always land, and the receiver treats a repeated NAMESPACE as a // replacement, so repricing one is just sending it again, a new original @@ -989,9 +993,10 @@ export class Publisher { * * The peers that never send SUBSCRIBE_NAMESPACE are exactly the ones expecting a * publisher to announce itself, so announcing is the default. A peer that would - * rather ask says so in its SETUP (MoQ Solicit) and this does nothing, leaving - * {@link runSubscribeNamespace} to carry the advertisements instead. Exactly one of - * the two is live, so the peer never hears a namespace twice. + * rather ask says so in its SETUP (MoQ Solicit) and this does nothing. On draft-16 + * and later {@link runSubscribeNamespace} also carries every match, so a peer that + * did not ask hears each namespace both ways. A NAMESPACE is discovery only, not a + * second route. * * @internal */ diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 30a8faa5fef..b4d680116a4 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -2107,10 +2107,10 @@ where /// expecting a publisher to announce itself, so the default has to be to announce. A /// peer that would rather ask says so with the MoQ Solicit extension /// ([`solicit`](super::solicit)), - /// and then this loop does nothing and - /// [`Self::run_subscribe_namespace_stream`] carries the advertisements instead. - /// Exactly one of the two is live, which is what keeps the peer from hearing a - /// namespace twice. + /// and then this loop does nothing. + /// On draft-16 and later [`Self::run_subscribe_namespace_stream`] also carries every + /// match, so a peer that did not ask hears each namespace both ways. A NAMESPACE is + /// discovery only, not a second route. pub async fn run_publish_namespaces(self) -> Result<(), Error> { if self.requires_solicitation().await { return Ok(()); @@ -2135,8 +2135,10 @@ where /// /// All the announce state is local to this task (mirroring `lite::Publisher`'s /// announce handling): whatever this subscription advertised is withdrawn - /// when its stream ends. It only advertises anything when the peer asked to be told - /// on request; otherwise [`Self::run_publish_namespaces`] has already said it all. + /// when its stream ends. On draft-16 and later every match is a NAMESPACE on + /// this stream, whatever the peer's SETUP said. Draft-14 and 15 predate that + /// message and still answer with PUBLISH_NAMESPACE only for what + /// [`Self::run_publish_namespaces`] does not already say. async fn run_subscribe_namespace_stream( self, mut stream: Stream, @@ -2176,15 +2178,19 @@ where // origin that already opted in (the caller's choice for this peer) keeps them. let origin = origin.discovery(!declared.hidden || msg.hidden || self.origin.includes_hidden()); - // Unless the peer asked to be told only on request, it has already heard what an - // unsolicited PUBLISH_NAMESPACE can say. Repeating it here would leave it holding - // two sources for one namespace, so this stream carries only what that loop hid - // from the empty prefix and this request may see, and otherwise simply stays open - // until the peer is done with it. - let origin = match declared.solicit.unwrap_or(false) { - true => origin, - false if !declared.hidden || self.origin.includes_hidden() => origin.empty(), - false => origin.beyond(&self.origin.clone().discovery(false)), + // Draft-16 and later always fill this stream. A NAMESPACE is discovery, not a + // route, so a peer that also hears the unsolicited PUBLISH_NAMESPACE still gets + // the match here. Draft-14 and 15 answer with PUBLISH_NAMESPACE and only say + // what that loop does not already say: everything when the peer asked to be + // told on request, otherwise the hidden remainder. + let origin = if matches!(self.version, Version::Draft14 | Version::Draft15) { + match declared.solicit.unwrap_or(false) { + true => origin, + false if !declared.hidden || self.origin.includes_hidden() => origin.empty(), + false => origin.beyond(&self.origin.clone().discovery(false)), + } + } else { + origin }; let mut announced = origin.announced(); @@ -4960,6 +4966,7 @@ mod serve_tests { #[cfg(test)] mod tests { use super::*; + use crate::coding::Decode; use crate::lite::test_transport::SinkSession; use crate::model::ProduceTest; use futures::FutureExt; @@ -4973,6 +4980,30 @@ mod tests { writes.windows(needle.len()).filter(|window| *window == needle).count() } + /// NAMESPACE and NAMESPACE_DONE suffixes on a SUBSCRIBE_NAMESPACE stream, in order. + fn namespace_events(bytes: &[u8], version: Version) -> Vec<(u64, String)> { + let mut dec = crate::coding::Decoder::new(bytes, version.into()); + let mut out = Vec::new(); + while !dec.is_empty() { + let id = dec.varint().expect("message type"); + match id { + ietf::RequestOk::ID => { + ietf::RequestOk::decode(&mut dec, version).expect("request ok"); + } + ietf::Namespace::ID => { + let msg = ietf::Namespace::decode(&mut dec, version).expect("namespace"); + out.push((id, msg.suffix.as_str().to_owned())); + } + ietf::NamespaceDone::ID => { + let msg = ietf::NamespaceDone::decode(&mut dec, version).expect("namespace done"); + out.push((id, msg.suffix.as_str().to_owned())); + } + other => panic!("unexpected message {other:#x}"), + } + } + out + } + /// A SETUP slot already filled with what the peer declared. The announce loops block /// on it, so a test that leaves it empty is a test that never advertises. fn declared(solicit: Option) -> peer::PeerSetup { @@ -5747,13 +5778,14 @@ mod tests { } /// A hidden namespace reaches only a subscription that opted in or named its dot - /// segment. With the unsolicited loop live, the subscription stream carries just - /// what that loop hid, so nothing is advertised twice. + /// segment. On draft-16 and later the subscription also repeats every visible + /// namespace the unsolicited loop already sent. A NAMESPACE is discovery, not + /// a second route. #[moq_net_sim::test] async fn hidden_namespaces_need_an_opt_in() { for (solicit, prefix, hidden, cam, stats) in [ - (Some(false), "", false, 1, 0), - (Some(false), "", true, 1, 1), + (Some(false), "", false, 2, 0), + (Some(false), "", true, 2, 1), (Some(false), ".stats", false, 1, 1), (Some(true), "", false, 1, 0), (Some(true), "", true, 1, 1), @@ -5795,7 +5827,20 @@ mod tests { scoped, }) .await; - let expected = usize::from(!declared || hidden || prefix == ".stats"); + // Visible on the subscription: not filtered, opted in, or the prefix + // names the dot segment. The unsolicited loop sends a dot namespace + // only when the peer did not declare MoQ Hidden. Draft-14/15 keep + // the two paths disjoint; draft-16 and later fill the stream too. + let on_stream = !declared || hidden || prefix == ".stats"; + let unsolicited = solicit != Some(true) && !declared; + let legacy = matches!(version, Version::Draft14 | Version::Draft15); + let inline = if legacy { + (solicit == Some(true) && on_stream) + || (solicit != Some(true) && declared && (hidden || prefix == ".stats")) + } else { + on_stream + }; + let expected = usize::from(inline) + usize::from(unsolicited); assert_eq!( occurrences(&log, b"node"), expected, @@ -5808,20 +5853,121 @@ mod tests { } } - /// The regression that made announces solicited in the first place: a namespace sent - /// as both PUBLISH_NAMESPACE and NAMESPACE leaves the peer holding two sources for - /// one broadcast, and whichever arrives second replaces the one the first attached. - /// The peer's SETUP picks which loop carries it, so the other stays quiet and the - /// namespace goes out exactly once either way. + /// On draft-16 and later a peer that did not ask to be solicited hears each + /// namespace twice: an unsolicited PUBLISH_NAMESPACE and a NAMESPACE on the + /// SUBSCRIBE_NAMESPACE stream. Those are two discoveries of one namespace, not + /// two routes. A peer that asked to be solicited hears it only on the stream. + /// Draft-14 and 15 still answer a non-solicit peer only with PUBLISH_NAMESPACE. #[moq_net_sim::test] - async fn each_namespace_is_advertised_exactly_once() { - let (unsolicited, streams) = advertise_both_ways(Some(false)).await; - assert_eq!(unsolicited, 1, "a peer that required nothing is told once"); - assert_eq!(streams, 2, "on its own PUBLISH_NAMESPACE request"); + async fn a_non_solicit_peer_hears_a_namespace_both_ways() { + let (times, streams) = advertise_both_ways(Some(false)).await; + assert_eq!(times, 2, "PUBLISH_NAMESPACE and NAMESPACE"); + assert_eq!(streams, 2, "the subscription plus one PUBLISH_NAMESPACE request"); - let (solicited, streams) = advertise_both_ways(Some(true)).await; - assert_eq!(solicited, 1, "a peer that asked to be told on request is told once"); + let (once, streams) = advertise_both_ways(Some(true)).await; + assert_eq!(once, 1, "a peer that asked to be told on request is told once"); assert_eq!(streams, 1, "inline on the SUBSCRIBE_NAMESPACE stream it asked on"); + + for version in [Version::Draft14, Version::Draft15] { + let log = advertise_with_hidden(Discovery { + version, + solicit: Some(false), + ..Discovery::default() + }) + .await; + assert_eq!( + occurrences(&log, b"cam"), + 1, + "{version:?} still answers only with PUBLISH_NAMESPACE" + ); + } + } + + /// A peer that did not send SOLICIT still gets NAMESPACE on its SUBSCRIBE_NAMESPACE + /// stream on draft-16 and later: one for a match that already exists, one announced + /// after, then NAMESPACE_DONE when that announcement ends. + #[moq_net_sim::test] + async fn a_non_solicit_subscribe_namespace_carries_namespace() { + for version in [Version::Draft16, Version::Draft18] { + let origin = crate::origin::Config::new(crate::Hop::new(1).unwrap()).produce(); + let early = origin.announce("early-cam", crate::origin::Route::default()).unwrap(); + settle().await; + + let session = crate::lite::test_transport::ScriptedSession::per_stream(vec![Vec::new()]); + let log = session.log.clone(); + + let peer_setup = peer::PeerSetup::default(); + peer_setup.set(peer::Peer::default()); + + let publisher = Publisher::new( + crate::time::Clock::sim(), + session.clone(), + origin.consume(), + Control::new(None, false), + None, + peer_setup, + version, + ); + + let stream = Stream::open(&mut session.clone(), version).await.unwrap(); + let msg = ietf::SubscribeNamespace { + request_id: RequestId(1), + namespace: crate::Path::new(""), + hidden: false, + }; + let mut run = std::pin::pin!(publisher.run_subscribe_namespace_stream(stream, msg)); + + for _ in 0..100 { + assert!(futures::poll!(run.as_mut()).is_pending()); + if occurrences(&log, b"early-cam") >= 1 { + break; + } + settle().await; + } + let bytes = log.writes.lock().unwrap().clone(); + assert_eq!( + namespace_events(&bytes, version), + vec![(ietf::Namespace::ID, "early-cam".to_owned())], + "{version:?}: existing match", + ); + + let _late = origin.announce("late-cam", crate::origin::Route::default()).unwrap(); + for _ in 0..100 { + assert!(futures::poll!(run.as_mut()).is_pending()); + if occurrences(&log, b"late-cam") >= 1 { + break; + } + settle().await; + } + let bytes = log.writes.lock().unwrap().clone(); + assert_eq!( + namespace_events(&bytes, version), + vec![ + (ietf::Namespace::ID, "early-cam".to_owned()), + (ietf::Namespace::ID, "late-cam".to_owned()), + ], + "{version:?}: announced later", + ); + + drop(early); + for _ in 0..100 { + assert!(futures::poll!(run.as_mut()).is_pending()); + if occurrences(&log, b"early-cam") >= 2 { + break; + } + settle().await; + } + let bytes = log.writes.lock().unwrap().clone(); + assert_eq!( + namespace_events(&bytes, version), + vec![ + (ietf::Namespace::ID, "early-cam".to_owned()), + (ietf::Namespace::ID, "late-cam".to_owned()), + (ietf::NamespaceDone::ID, "early-cam".to_owned()), + ], + "{version:?}: NAMESPACE_DONE when it ends", + ); + } } /// A peer out of stream credit parks the open. That must not wedge the loop, because From 0d5492281ce4545a737698a0ef5a56893a77b328 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 19:27:42 -0700 Subject: [PATCH 3/7] fix(net): honor SUBSCRIBE_NAMESPACE Subscribe Options Draft-16/17 SUBSCRIBE_NAMESPACE asks for PUBLISH (0x00), NAMESPACE (0x01), or both (0x02). Carry the decoded field to dispatch: 0x00 is refused NOT_SUPPORTED since we never send PUBLISH, 0x01 and 0x02 get NAMESPACE, and any other value is a protocol violation. Draft-18 has no field and always gets NAMESPACE. Align the solicit draft: a namespace heard both ways is one advertisement. Co-Authored-By: Claude Opus 5.5 --- doc/concept/standard.md | 4 +- drafts/draft-lcurley-moq-solicit.md | 5 +- js/net/src/ietf/connection.test.ts | 107 ++++++++++++++++- js/net/src/ietf/connection.ts | 14 ++- js/net/src/ietf/publisher.test.ts | 70 +++++------ js/net/src/ietf/publisher.ts | 20 +++- js/net/src/ietf/subscribe_namespace.ts | 37 ++++-- rs/moq-net/src/ietf/publisher.rs | 130 +++++++++++++++++++-- rs/moq-net/src/ietf/subscribe_namespace.rs | 95 +++++++++++---- rs/moq-net/src/ietf/subscriber.rs | 2 +- 10 files changed, 393 insertions(+), 91 deletions(-) diff --git a/doc/concept/standard.md b/doc/concept/standard.md index 10ddf3e132e..4ab49e90b36 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -143,6 +143,8 @@ knowing when pointing another implementation at ours: we announce every namespace we can offer unsolicited *and* ask for every prefix we may discover; set the solicit `SETUP` option to make us wait to be asked. A peer that does not set that option still gets each match as a `NAMESPACE` on its `SUBSCRIBE_NAMESPACE` -stream on draft-16 and later, so it hears the namespace twice. Single-track +stream on draft-16 and later, so it hears the namespace twice. We never send +`PUBLISH`: on drafts 16 and 17 a `SUBSCRIBE_NAMESPACE` asking only for `PUBLISH` +is refused, and one asking for both gets only `NAMESPACE`. Single-track `PUBLISH` offers are declined; announce a namespace and serve the resulting `SUBSCRIBE`s instead. diff --git a/drafts/draft-lcurley-moq-solicit.md b/drafts/draft-lcurley-moq-solicit.md index 72b4bfc6485..eb96b50dd57 100644 --- a/drafts/draft-lcurley-moq-solicit.md +++ b/drafts/draft-lcurley-moq-solicit.md @@ -103,9 +103,8 @@ This is a MUST NOT rather than a SHOULD NOT because the receiver enforces it. A A relay is the expected user of this declaration, as is any endpoint that asks for what it wants. So is an endpoint that only publishes, which cannot subscribe to anything and therefore has no use for an advertisement of any kind. -An endpoint SHOULD NOT advertise the same namespace both ways on one session. -Whichever arrives second replaces the source the first attached, which at best wastes a stream and at worst leaves the receiver holding two independent advertisements it must reconcile. -Because this declaration decides which of the two an endpoint uses, honoring it also settles that question for the whole session. +An endpoint that did not declare 1 can hear a namespace both ways: unsolicited, and in answer to its own SUBSCRIBE_NAMESPACE. +Both report the same namespace, so the receiver treats them as one advertisement, not two sources. # Enforcement {#enforcement} diff --git a/js/net/src/ietf/connection.test.ts b/js/net/src/ietf/connection.test.ts index 15e8879b4ab..c3402ab2a0d 100644 --- a/js/net/src/ietf/connection.test.ts +++ b/js/net/src/ietf/connection.test.ts @@ -2,13 +2,21 @@ import { expect, spyOn, test } from "bun:test"; import { exchangeSetup } from "../connection/handshake.ts"; import { SessionCode } from "../error.ts"; import { createMockTransportPair } from "../mock.ts"; +import { Producer as OriginProducer } from "../origin.ts"; +import * as Path from "../path.ts"; import { Stream, Writer } from "../stream.ts"; import { Connection } from "./connection.ts"; import { Group } from "./object.ts"; import { SetupOption, SetupOptions } from "./parameters.ts"; -import { RequestError } from "./request.ts"; +import { RequestError, RequestOk } from "./request.ts"; import { Setup } from "./setup.ts"; -import { SUBSCRIBE_TRACKS_ID } from "./subscribe_namespace.ts"; +import { + SUBSCRIBE_TRACKS_ID, + SubscribeNamespace, + SubscribeNamespaceEntry, + SubscribeNamespaceLegacy, + SubscribeOptions, +} from "./subscribe_namespace.ts"; import { ALPN, type IetfVersion, Version } from "./version.ts"; const PADDING = 0x132b3e28n; @@ -149,6 +157,101 @@ test("SUBSCRIBE_TRACKS refuses only its request", async () => { } }); +/** + * A draft-16/17 SUBSCRIBE_NAMESPACE gets NAMESPACE whenever it asks for namespaces, and one + * asking for PUBLISH alone is refused, since we never send PUBLISH. Draft-18 has no + * Subscribe Options and always asks for namespaces. + */ +for (const [version, alpn, options, namespaces] of [ + [Version.DRAFT_16, ALPN.DRAFT_16, SubscribeOptions.PUBLISH, false], + [Version.DRAFT_16, ALPN.DRAFT_16, SubscribeOptions.NAMESPACE, true], + [Version.DRAFT_16, ALPN.DRAFT_16, SubscribeOptions.BOTH, true], + [Version.DRAFT_17, ALPN.DRAFT_17, SubscribeOptions.PUBLISH, false], + [Version.DRAFT_17, ALPN.DRAFT_17, SubscribeOptions.NAMESPACE, true], + [Version.DRAFT_17, ALPN.DRAFT_17, SubscribeOptions.BOTH, true], + [Version.DRAFT_18, ALPN.DRAFT_18, undefined, true], +] as const) { + test(`${alpn} SUBSCRIBE_NAMESPACE with Subscribe Options ${options ?? "absent"}`, async () => { + const pair = createMockTransportPair(alpn); + const control = await Stream.open(pair.server, { version }); + const origin = new OriginProducer(); + origin.createBroadcast(Path.from("room")).announce(); + const connection = new Connection({ + url: new URL("https://example.com"), + quic: pair.server, + control, + maxRequestId: 100n, + version, + client: false, + publish: origin.consume(), + solicit: true, + }); + + try { + const stream = await Stream.open(pair.client, { version }); + if (options === undefined) { + await stream.writer.u53(SubscribeNamespace.id); + await new SubscribeNamespace({ namespace: Path.empty(), requestId: 1n }).encode(stream.writer, version); + } else { + await stream.writer.u53(SubscribeNamespaceLegacy.id); + await new SubscribeNamespaceLegacy({ + namespace: Path.empty(), + requestId: 1n, + subscribeOptions: options, + }).encode(stream.writer, version); + } + + if (namespaces) { + expect(await stream.reader.u53()).toBe(RequestOk.id); + await RequestOk.decode(stream.reader, version); + expect(await stream.reader.u53()).toBe(SubscribeNamespaceEntry.id); + expect((await SubscribeNamespaceEntry.decode(stream.reader, version)).suffix).toBe(Path.from("room")); + } else { + expect(await stream.reader.u53()).toBe(RequestError.id); + const refused = await RequestError.decode(stream.reader, version); + expect(refused.errorCode).toBe(0x3); + expect(refused.requestId).toBe(version === Version.DRAFT_16 ? 1n : undefined); + expect(await stream.reader.done()).toBe(true); + } + } finally { + connection.abort(); + origin.close(); + } + }); +} + +/** Subscribe Options above 0x02 are malformed and close the session. */ +test("an unknown Subscribe Options value closes the session", async () => { + const logged = spyOn(console, "error").mockImplementation(() => void 0); + const version = Version.DRAFT_17; + const pair = createMockTransportPair(ALPN.DRAFT_17); + const control = await Stream.open(pair.server, { version }); + const connection = new Connection({ + url: new URL("https://example.com"), + quic: pair.server, + control, + maxRequestId: 100n, + version, + client: false, + }); + + try { + const stream = await Stream.open(pair.client, { version }); + await stream.writer.u53(SubscribeNamespaceLegacy.id); + await new SubscribeNamespaceLegacy({ + namespace: Path.empty(), + requestId: 1n, + subscribeOptions: 3 as SubscribeOptions, + }).encode(stream.writer, version); + + const info = await pair.client.closed; + expect(info.closeCode).toBe(SessionCode.ProtocolViolation); + } finally { + logged.mockRestore(); + connection.abort(); + } +}); + /** * Padding and group streams that beat the peer's SETUP are held and classified once it * lands. The drafts say to buffer early data; failing the handshake over it broke a peer diff --git a/js/net/src/ietf/connection.ts b/js/net/src/ietf/connection.ts index 661e791c332..937dd3b614b 100644 --- a/js/net/src/ietf/connection.ts +++ b/js/net/src/ietf/connection.ts @@ -19,7 +19,12 @@ import { Publish } from "./publish.ts"; import { PublishNamespace } from "./publish_namespace.ts"; import { Publisher } from "./publisher.ts"; import { Subscribe, SubscribeUpdate } from "./subscribe.ts"; -import { SUBSCRIBE_TRACKS_ID, SubscribeNamespace, SubscribeNamespaceLegacy } from "./subscribe_namespace.ts"; +import { + SUBSCRIBE_TRACKS_ID, + SubscribeNamespace, + SubscribeNamespaceLegacy, + SubscribeOptions, +} from "./subscribe_namespace.ts"; import { Subscriber } from "./subscriber.ts"; import { TrackStatusRequest } from "./track.ts"; import { type IetfVersion, Version, versionName } from "./version.ts"; @@ -261,7 +266,8 @@ export class Connection implements Established { switch (typeId) { // Draft-18 SUBSCRIBE_NAMESPACE (0x50) and the legacy 0x11 message decode - // to the same request_id + namespace; the legacy options field is ignored. + // to the same request_id + namespace. We never send PUBLISH, so a legacy + // 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); await this.#publisher.runSubscribeNamespace(msg, stream); @@ -269,6 +275,10 @@ export class Connection implements Established { } case BigInt(SubscribeNamespaceLegacy.id): { const legacy = await SubscribeNamespaceLegacy.decode(stream.reader, this.#session.version); + if (legacy.subscribeOptions === SubscribeOptions.PUBLISH) { + await this.#publisher.refuseSubscribeNamespace(legacy.requestId, stream); + break; + } const msg = new SubscribeNamespace({ requestId: legacy.requestId, namespace: legacy.namespace, diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index c3400043f49..3404f9eea34 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -2325,49 +2325,41 @@ test("draft-20: PUBLISH_DONE waits for a queued group and counts every stream", test.each([ ["draft-16", Version.DRAFT_16], ["draft-18", Version.DRAFT_18], -] as const)( - "a non-SOLICIT %s SUBSCRIBE_NAMESPACE carries NAMESPACE then NAMESPACE_DONE", - async (_, version) => { - const pair = createMockTransportPair(ALPN.DRAFT_19); - const session = new NativeSession(pair.server, version, true); - const { pub, origin } = publisher(pair.server, { requiresSolicitation: false, session }); - const early = publish(origin, Path.from("early-cam")); - - const subscription = await Stream.open(pair.client, { version }); - const accepted = await Stream.accept(pair.server, version); - if (!accepted) throw new Error("missing subscription"); - const run = pub.runSubscribeNamespace( - new SubscribeNamespace({ requestId: 1n, namespace: Path.empty() }), - accepted, - ); +] as const)("a non-SOLICIT %s SUBSCRIBE_NAMESPACE carries NAMESPACE then NAMESPACE_DONE", async (_, version) => { + const pair = createMockTransportPair(ALPN.DRAFT_19); + const session = new NativeSession(pair.server, version, true); + const { pub, origin } = publisher(pair.server, { requiresSolicitation: false, session }); + const early = publish(origin, Path.from("early-cam")); - try { - expect(await subscription.reader.u53()).toBe(RequestOk.id); - await RequestOk.decode(subscription.reader, version); + const subscription = await Stream.open(pair.client, { version }); + const accepted = await Stream.accept(pair.server, version); + if (!accepted) throw new Error("missing subscription"); + const run = pub.runSubscribeNamespace(new SubscribeNamespace({ requestId: 1n, namespace: Path.empty() }), accepted); - expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntry.id); - expect((await SubscribeNamespaceEntry.decode(subscription.reader, version)).suffix).toBe( - Path.from("early-cam"), - ); + try { + expect(await subscription.reader.u53()).toBe(RequestOk.id); + await RequestOk.decode(subscription.reader, version); - publish(origin, Path.from("late-cam")); - expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntry.id); - expect((await SubscribeNamespaceEntry.decode(subscription.reader, version)).suffix).toBe( - Path.from("late-cam"), - ); + expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntry.id); + expect((await SubscribeNamespaceEntry.decode(subscription.reader, version)).suffix).toBe( + Path.from("early-cam"), + ); - early.close(); - expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntryDone.id); - expect((await SubscribeNamespaceEntryDone.decode(subscription.reader, version)).suffix).toBe( - Path.from("early-cam"), - ); - } finally { - subscription.close(); - origin.close(); - await run; - } - }, -); + publish(origin, Path.from("late-cam")); + expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntry.id); + expect((await SubscribeNamespaceEntry.decode(subscription.reader, version)).suffix).toBe(Path.from("late-cam")); + + early.close(); + expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntryDone.id); + expect((await SubscribeNamespaceEntryDone.decode(subscription.reader, version)).suffix).toBe( + Path.from("early-cam"), + ); + } finally { + subscription.close(); + origin.close(); + await run; + } +}); for (const version of [Version.DRAFT_15, Version.DRAFT_19] as const) { for (const declared of [false, true]) { diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 4c817c6e25e..ce00a0544e4 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -37,6 +37,7 @@ import { type SubscribeNamespace, SubscribeNamespaceEntry, SubscribeNamespaceEntryDone, + SubscribeNamespaceError, SubscribeNamespaceOk, } from "./subscribe_namespace.ts"; import type { TrackStatusRequest } from "./track.ts"; @@ -910,9 +911,7 @@ export class Publisher { const visible = (covered: Path.Valid) => !this.#hidden || msg.hidden || !hiddenBelow(prefix, covered); const carries = (covered: Path.Valid) => visible(covered) && - (legacy - ? this.#requiresSolicitation || (this.#hidden && hiddenBelow(Path.empty(), covered)) - : true); + (legacy ? this.#requiresSolicitation || (this.#hidden && hiddenBelow(Path.empty(), covered)) : true); // Inline entries always land, and the receiver treats a repeated NAMESPACE as a // replacement, so repricing one is just sending it again, a new original @@ -1368,6 +1367,21 @@ export class Publisher { await this.#refuseUnsupported(stream, msg.requestId, "track_status", 0x0f, "TRACK_STATUS is not supported"); } + /** + * Refuses a draft-16/17 SUBSCRIBE_NAMESPACE that asks for PUBLISH alone: we never send PUBLISH. + * + * @internal + */ + async refuseSubscribeNamespace(requestId: bigint, stream: Stream) { + await this.#refuseUnsupported( + stream, + requestId, + "subscribe_namespace", + SubscribeNamespaceError.id, + "SUBSCRIBE_NAMESPACE for PUBLISH is not supported", + ); + } + /** * Refuses an incoming SUBSCRIBE_TRACKS (draft-18+): we never send the PUBLISH it asks for. * The reply carries no Request ID, so the message body is left unread. diff --git a/js/net/src/ietf/subscribe_namespace.ts b/js/net/src/ietf/subscribe_namespace.ts index a36090c910e..9ca8ba0a38c 100644 --- a/js/net/src/ietf/subscribe_namespace.ts +++ b/js/net/src/ietf/subscribe_namespace.ts @@ -1,3 +1,4 @@ +import { ProtocolViolation } from "../error.ts"; import type * as Path from "../path.ts"; import type { Reader, Writer } from "../stream.ts"; import * as Cluster from "./cluster.ts"; @@ -74,30 +75,48 @@ export class SubscribeNamespace { } } +/** + * What a draft-16/17 SUBSCRIBE_NAMESPACE asks for (d16 §9.25, d17 §9.20). + * + * Draft-14/15 have no field and only ever ask for namespaces. Draft-18 dropped it + * when SUBSCRIBE_TRACKS took over the PUBLISH half. + */ +export const SubscribeOptions = { + /** PUBLISH for each matching track. */ + PUBLISH: 0x00, + /** NAMESPACE for each matching namespace. */ + NAMESPACE: 0x01, + /** Both PUBLISH and NAMESPACE. */ + BOTH: 0x02, +} as const; + +/** One of the {@link SubscribeOptions} values. */ +export type SubscribeOptions = (typeof SubscribeOptions)[keyof typeof SubscribeOptions]; + // SUBSCRIBE_NAMESPACE message for draft-14 through draft-17 (type 0x11). // // In v16 this moves from the control stream to its own bidi stream. Draft-16/17 -// carry a Subscribe Options field (NAMESPACE vs TRACKS); draft-17 additionally -// prefixes a Required Request ID delta (removed in draft-18 per #1615). -// Draft-18+ uses SubscribeNamespace. +// carry SubscribeOptions; draft-17 additionally prefixes a Required Request ID +// delta (removed in draft-18 per #1615). Draft-18+ uses SubscribeNamespace. export class SubscribeNamespaceLegacy { static id = 0x11; namespace: Path.Valid; requestId: bigint; - subscribeOptions: number; // v16/v17: default 0x01 (NAMESPACE only) + /** Encoded on v16/v17 only; earlier drafts always mean NAMESPACE. */ + subscribeOptions: SubscribeOptions; /** MoQ Hidden: see {@link SubscribeNamespace.hidden}. */ hidden: boolean; constructor({ namespace, requestId, - subscribeOptions = 1, + subscribeOptions = SubscribeOptions.NAMESPACE, hidden = false, }: { namespace: Path.Valid; requestId: bigint; - subscribeOptions?: number; + subscribeOptions?: SubscribeOptions; hidden?: boolean; }) { this.namespace = namespace; @@ -140,9 +159,11 @@ export class SubscribeNamespaceLegacy { await r.u62(); // required_request_id_delta (draft-17 only, removed in draft-18 per #1615) } const namespace = await Namespace.decode(r); - let subscribeOptions = 1; + let subscribeOptions: SubscribeOptions = SubscribeOptions.NAMESPACE; if (version === Version.DRAFT_16 || version === Version.DRAFT_17) { - subscribeOptions = await r.u53(); + const raw = await r.u53(); + if (raw > SubscribeOptions.BOTH) throw new ProtocolViolation(`invalid Subscribe Options: ${raw}`); + subscribeOptions = raw as SubscribeOptions; } const params = await Parameters.decode(r, version); diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index b4d680116a4..6b4c7796b75 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -517,22 +517,34 @@ where .maybe_boxed() } // Draft-18 SUBSCRIBE_NAMESPACE (0x50) and the legacy 0x11 message decode - // to the same request_id + namespace; the legacy Subscribe Options field - // is ignored (moq-lite never subscribes to tracks). + // to the same request_id + namespace. We never send PUBLISH, so a legacy + // request for PUBLISH alone is refused, and one for both gets only NAMESPACE. ietf::SubscribeNamespace::ID | ietf::SubscribeNamespaceLegacy::ID => { - let msg = if id == ietf::SubscribeNamespace::ID { - ietf::SubscribeNamespace::decode_msg(&mut data, this.version)? + let (msg, options) = if id == ietf::SubscribeNamespace::ID { + let msg = ietf::SubscribeNamespace::decode_msg(&mut data, this.version)?; + (msg, ietf::SubscribeOptions::Namespace) } else { let legacy = ietf::SubscribeNamespaceLegacy::decode_msg(&mut data, this.version)?; - ietf::SubscribeNamespace { + let msg = ietf::SubscribeNamespace { request_id: legacy.request_id, namespace: legacy.namespace, hidden: legacy.hidden, - } + }; + (msg, legacy.subscribe_options) }; if !data.is_empty() { return Err(Error::WrongSize); } + if options == ietf::SubscribeOptions::Publish { + let request_id = msg.request_id; + return Ok(async move { + let reason = "SUBSCRIBE_NAMESPACE for PUBLISH is not supported"; + if let Err(err) = this.reject_namespace_request(stream, Some(request_id), reason).await { + tracing::debug!(%err, "subscribe_namespace refusal failed"); + } + } + .maybe_boxed()); + } tracing::debug!(message = ?msg, "received subscribe_namespace"); async move { if let Err(err) = this.run_subscribe_namespace_stream(stream, msg).await { @@ -551,7 +563,8 @@ where ) => { async move { - if let Err(err) = this.reject_subscribe_tracks(stream).await { + let reason = "SUBSCRIBE_TRACKS is not supported"; + if let Err(err) = this.reject_namespace_request(stream, None, reason).await { tracing::debug!(%err, "subscribe_tracks refusal failed"); } } @@ -1623,14 +1636,21 @@ where Ok(()) } - async fn reject_subscribe_tracks(&self, mut stream: Stream) -> Result<(), Error> { + /// Refuse a SUBSCRIBE_NAMESPACE or SUBSCRIBE_TRACKS that asks for PUBLISH, which we + /// never send. Only draft-16 replies carry the Request ID; draft-17+ replies never do. + async fn reject_namespace_request( + &self, + mut stream: Stream, + request_id: Option, + reason: &str, + ) -> Result<(), Error> { stream.writer.varint(ietf::RequestError::ID).await?; stream .writer .encode(&ietf::RequestError { - request_id: None, + request_id: request_id.filter(|_| self.version == Version::Draft16), error_code: request::to_code(&Error::Unsupported, request::Kind::SubscribeNamespace, self.version), - reason_phrase: "SUBSCRIBE_TRACKS is not supported".into(), + reason_phrase: reason.into(), retry_interval: 0, }) .await?; @@ -3877,6 +3897,96 @@ mod serve_tests { } } + /// A draft-16/17 SUBSCRIBE_NAMESPACE gets NAMESPACE whenever it asks for namespaces, + /// and one asking for PUBLISH alone is refused, since we never send PUBLISH. Draft-18 + /// has no Subscribe Options and always asks for namespaces. + #[moq_net_sim::test] + async fn subscribe_namespace_honors_subscribe_options() { + use ietf::SubscribeOptions::{Both, Namespace, Publish}; + + for (version, options, namespaces) in [ + (Version::Draft16, Some(Publish), false), + (Version::Draft16, Some(Namespace), true), + (Version::Draft16, Some(Both), true), + (Version::Draft17, Some(Publish), false), + (Version::Draft17, Some(Namespace), true), + (Version::Draft17, Some(Both), true), + (Version::Draft18, None, true), + ] { + let h = serve(version); + settle().await; + + let mut body = Vec::new(); + let mut w = Encoder::new(&mut body, version.into()); + let id = match options { + Some(subscribe_options) => { + ietf::SubscribeNamespaceLegacy { + request_id: RequestId(REQUEST_ID), + namespace: crate::Path::new(""), + subscribe_options, + hidden: false, + } + .encode_msg(&mut w, version) + .unwrap(); + ietf::SubscribeNamespaceLegacy::ID + } + None => { + ietf::SubscribeNamespace { + request_id: RequestId(REQUEST_ID), + namespace: crate::Path::new(""), + hidden: false, + } + .encode_msg(&mut w, version) + .unwrap(); + ietf::SubscribeNamespace::ID + } + }; + + let stream = Stream::open(&mut h.session.clone(), version).await.unwrap(); + let mark = h.log.writes.lock().unwrap().len(); + let task = h + .publisher + .clone() + .handle_stream(id, ietf::Body(bytes::Bytes::from(body)), stream) + .unwrap_or_else(|e| panic!("{version}: the request closed the session: {e}")); + let mut task = std::pin::pin!(task); + let mut done = false; + for _ in 0..100 { + done = futures::poll!(task.as_mut()).is_ready(); + if done || occurrences(&h.log, b"room") > 0 { + break; + } + settle().await; + } + + let wire = h.log.writes.lock().unwrap()[mark..].to_vec(); + let mut buf = Decoder::new(&wire, version.into()); + let label = format!("{version} {options:?}"); + if namespaces { + assert!(!done, "{label}: the subscription ended"); + assert_eq!(buf.varint().unwrap(), ietf::RequestOk::ID, "{label}"); + ietf::RequestOk::decode(&mut buf, version).unwrap(); + assert_eq!(buf.varint().unwrap(), ietf::Namespace::ID, "{label}"); + assert_eq!( + ietf::Namespace::decode(&mut buf, version).unwrap().suffix.as_str(), + "room", + "{label}" + ); + } else { + assert!(done, "{label}: the refusal did not finish"); + assert_eq!(buf.varint().unwrap(), ietf::RequestError::ID, "{label}"); + let err = ietf::RequestError::decode(&mut buf, version).unwrap(); + assert_eq!(err.error_code, 0x3, "{label}: NOT_SUPPORTED"); + assert_eq!( + err.request_id, + (version == Version::Draft16).then_some(RequestId(REQUEST_ID)), + "{label}" + ); + } + assert!(buf.is_empty(), "{label}: trailing bytes"); + } + } + /// The draft's canonical current-group join: a Next Object subscription plus a /// StartGroup=1 fill. The published head arrives exactly once, on a fetch stream, /// and the subscription starts past the snapshot, so nothing is duplicated and diff --git a/rs/moq-net/src/ietf/subscribe_namespace.rs b/rs/moq-net/src/ietf/subscribe_namespace.rs index f09eb8f9a55..bbac14a4066 100644 --- a/rs/moq-net/src/ietf/subscribe_namespace.rs +++ b/rs/moq-net/src/ietf/subscribe_namespace.rs @@ -88,18 +88,51 @@ impl Message for SubscribeNamespace<'_> { } } +/// What a draft-16/17 SUBSCRIBE_NAMESPACE asks for (d16 §9.25, d17 §9.20). +/// +/// Draft-14/15 have no field and only ever ask for namespaces. Draft-18 dropped it +/// when SUBSCRIBE_TRACKS took over the PUBLISH half. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum SubscribeOptions { + /// PUBLISH for each matching track (0x00). + Publish, + /// NAMESPACE for each matching namespace (0x01). + Namespace, + /// Both PUBLISH and NAMESPACE (0x02). + Both, +} + +impl SubscribeOptions { + fn encode(self, w: &mut Encoder<'_>) -> Result<(), EncodeError> { + w.varint(match self { + Self::Publish => 0x00u64, + Self::Namespace => 0x01, + Self::Both => 0x02, + }) + } + + fn decode(r: &mut Decoder<'_>) -> Result { + match r.varint()? { + 0x00 => Ok(Self::Publish), + 0x01 => Ok(Self::Namespace), + 0x02 => Ok(Self::Both), + _ => Err(DecodeError::InvalidValue), + } + } +} + /// SUBSCRIBE_NAMESPACE message for draft-14 through draft-17 (type 0x11). /// /// In v16 this moves from the control stream to its own bidirectional stream. -/// Draft-16/17 carry a Subscribe Options field (NAMESPACE vs TRACKS); draft-17 -/// additionally prefixes a Required Request ID delta (removed in draft-18 per -/// #1615). Draft-18+ uses [`SubscribeNamespace`]. +/// Draft-16/17 carry [`SubscribeOptions`]; draft-17 additionally prefixes a +/// Required Request ID delta (removed in draft-18 per #1615). Draft-18+ uses +/// [`SubscribeNamespace`]. #[derive(Clone, Debug)] pub struct SubscribeNamespaceLegacy<'a> { pub request_id: RequestId, pub namespace: Path<'a>, - /// v16/v17: Subscribe Options (default 0x01 = NAMESPACE only). - pub subscribe_options: u64, + /// Encoded on v16/v17 only; earlier drafts always mean [`SubscribeOptions::Namespace`]. + pub subscribe_options: SubscribeOptions, /// MoQ Hidden: see [`SubscribeNamespace::hidden`]. pub hidden: bool, } @@ -117,7 +150,7 @@ impl Message for SubscribeNamespaceLegacy<'_> { } encode_namespace(w, &self.namespace)?; if matches!(version, Version::Draft16 | Version::Draft17) { - w.varint(self.subscribe_options)?; + self.subscribe_options.encode(w)?; } encode_params!(w, version, HIDDEN_PARAM => hidden_param(self.hidden)); Ok(()) @@ -133,8 +166,8 @@ impl Message for SubscribeNamespaceLegacy<'_> { } let namespace = decode_namespace(r)?; let subscribe_options = match version { - Version::Draft16 | Version::Draft17 => r.varint()?, - _ => 0x01, + Version::Draft16 | Version::Draft17 => SubscribeOptions::decode(r)?, + _ => SubscribeOptions::Namespace, }; // The token is ignored: the session's grant is what authorizes the request. @@ -461,7 +494,7 @@ mod tests { let legacy = SubscribeNamespaceLegacy { request_id: RequestId(0), namespace: Path::default(), - subscribe_options: 0x01, + subscribe_options: SubscribeOptions::Namespace, hidden: false, }; assert!(body(&legacy, Version::Draft17).len() > body(&modern, Version::Draft18).len()); @@ -502,7 +535,7 @@ mod tests { let msg = SubscribeNamespaceLegacy { request_id: RequestId(4), namespace: Path::new("example"), - subscribe_options: 0x01, + subscribe_options: SubscribeOptions::Namespace, hidden, }; let mut buf = bytes::Bytes::from(body(&msg, version)); @@ -516,21 +549,39 @@ mod tests { #[test] fn legacy_round_trips() { for version in [Version::Draft16, Version::Draft17] { - let msg = SubscribeNamespaceLegacy { - request_id: RequestId(4), - namespace: Path::new("example/meeting"), - subscribe_options: 0x01, - hidden: false, - }; - let mut buf = bytes::Bytes::from(body(&msg, version)); - let decoded = crate::coding::decode_buf(&mut buf, version, SubscribeNamespaceLegacy::decode_msg).unwrap(); - assert!(buf.is_empty(), "trailing bytes for {version:?}"); - assert_eq!(decoded.request_id, RequestId(4)); - assert_eq!(decoded.namespace.as_str(), "example/meeting"); - assert_eq!(decoded.subscribe_options, 0x01); + for options in [ + SubscribeOptions::Publish, + SubscribeOptions::Namespace, + SubscribeOptions::Both, + ] { + let msg = SubscribeNamespaceLegacy { + request_id: RequestId(4), + namespace: Path::new("example/meeting"), + subscribe_options: options, + hidden: false, + }; + let mut buf = bytes::Bytes::from(body(&msg, version)); + let decoded = + crate::coding::decode_buf(&mut buf, version, SubscribeNamespaceLegacy::decode_msg).unwrap(); + assert!(buf.is_empty(), "trailing bytes for {version:?}"); + assert_eq!(decoded.request_id, RequestId(4)); + assert_eq!(decoded.namespace.as_str(), "example/meeting"); + assert_eq!(decoded.subscribe_options, options); + } } } + /// Subscribe Options above 0x02 are malformed (d16 §9.25). + #[test] + fn legacy_rejects_unknown_subscribe_options() { + // Request ID, empty namespace, Subscribe Options 0x03, no parameters. + let mut buf = bytes::Bytes::from(vec![0x00, 0x00, 0x03, 0x00]); + assert!(matches!( + crate::coding::decode_buf(&mut buf, Version::Draft16, SubscribeNamespaceLegacy::decode_msg), + Err(DecodeError::InvalidValue) + )); + } + #[test] fn rejects_wrong_version() { // The modern 0x50 message only exists in draft-18+. diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index f144d58419d..eb51fb2f5c8 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -862,7 +862,7 @@ where let msg = ietf::SubscribeNamespaceLegacy { request_id, namespace: prefix.clone(), - subscribe_options: 0x01, // NAMESPACE only + subscribe_options: ietf::SubscribeOptions::Namespace, hidden, }; stream.writer.varint(ietf::SubscribeNamespaceLegacy::ID).await?; From e132da84c906d9af981e93936ea121503f96905d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 20:22:05 -0700 Subject: [PATCH 4/7] fix(js): read Subscribe Options at full varint width A value past 2^53 threw RangeError and only reset its stream; read it as u62 so it closes the session as a protocol violation, matching Rust. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ietf/connection.test.ts | 70 +++++++++++++++----------- js/net/src/ietf/subscribe_namespace.ts | 7 +-- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/js/net/src/ietf/connection.test.ts b/js/net/src/ietf/connection.test.ts index c3402ab2a0d..ede0a7a55e2 100644 --- a/js/net/src/ietf/connection.test.ts +++ b/js/net/src/ietf/connection.test.ts @@ -6,8 +6,10 @@ import { Producer as OriginProducer } from "../origin.ts"; import * as Path from "../path.ts"; import { Stream, Writer } from "../stream.ts"; import { Connection } from "./connection.ts"; +import * as Message from "./message.ts"; +import * as Namespace from "./namespace.ts"; import { Group } from "./object.ts"; -import { SetupOption, SetupOptions } from "./parameters.ts"; +import { Parameters, SetupOption, SetupOptions } from "./parameters.ts"; import { RequestError, RequestOk } from "./request.ts"; import { Setup } from "./setup.ts"; import { @@ -220,37 +222,45 @@ for (const [version, alpn, options, namespaces] of [ }); } -/** Subscribe Options above 0x02 are malformed and close the session. */ -test("an unknown Subscribe Options value closes the session", async () => { - const logged = spyOn(console, "error").mockImplementation(() => void 0); - const version = Version.DRAFT_17; - const pair = createMockTransportPair(ALPN.DRAFT_17); - const control = await Stream.open(pair.server, { version }); - const connection = new Connection({ - url: new URL("https://example.com"), - quic: pair.server, - control, - maxRequestId: 100n, - version, - client: false, - }); +/** Subscribe Options above 0x02 are malformed and close the session, even past 2^53. */ +for (const [version, alpn] of [ + [Version.DRAFT_16, ALPN.DRAFT_16], + [Version.DRAFT_17, ALPN.DRAFT_17], +] as const) { + for (const options of [3n, 2n ** 53n]) { + test(`${alpn} Subscribe Options ${options} closes the session`, async () => { + const logged = spyOn(console, "error").mockImplementation(() => void 0); + const pair = createMockTransportPair(alpn); + const control = await Stream.open(pair.server, { version }); + const connection = new Connection({ + url: new URL("https://example.com"), + quic: pair.server, + control, + maxRequestId: 100n, + version, + client: false, + }); - try { - const stream = await Stream.open(pair.client, { version }); - await stream.writer.u53(SubscribeNamespaceLegacy.id); - await new SubscribeNamespaceLegacy({ - namespace: Path.empty(), - requestId: 1n, - subscribeOptions: 3 as SubscribeOptions, - }).encode(stream.writer, version); - - const info = await pair.client.closed; - expect(info.closeCode).toBe(SessionCode.ProtocolViolation); - } finally { - logged.mockRestore(); - connection.abort(); + try { + const stream = await Stream.open(pair.client, { version }); + await stream.writer.u53(SubscribeNamespaceLegacy.id); + await Message.encode(stream.writer, async (w) => { + await w.u62(1n); // Request ID + if (version === Version.DRAFT_17) await w.u62(0n); // Required Request ID delta + await Namespace.encode(w, Path.empty()); + await w.u62(options); + await new Parameters().encode(w, version); + }); + + const info = await pair.client.closed; + expect(info.closeCode).toBe(SessionCode.ProtocolViolation); + } finally { + logged.mockRestore(); + connection.abort(); + } + }); } -}); +} /** * Padding and group streams that beat the peer's SETUP are held and classified once it diff --git a/js/net/src/ietf/subscribe_namespace.ts b/js/net/src/ietf/subscribe_namespace.ts index 9ca8ba0a38c..9bd3d6cf31e 100644 --- a/js/net/src/ietf/subscribe_namespace.ts +++ b/js/net/src/ietf/subscribe_namespace.ts @@ -161,9 +161,10 @@ export class SubscribeNamespaceLegacy { const namespace = await Namespace.decode(r); let subscribeOptions: SubscribeOptions = SubscribeOptions.NAMESPACE; if (version === Version.DRAFT_16 || version === Version.DRAFT_17) { - const raw = await r.u53(); - if (raw > SubscribeOptions.BOTH) throw new ProtocolViolation(`invalid Subscribe Options: ${raw}`); - subscribeOptions = raw as SubscribeOptions; + // Full width, so a value past 2^53 is still a protocol violation. + const raw = await r.u62(); + if (raw > BigInt(SubscribeOptions.BOTH)) throw new ProtocolViolation(`invalid Subscribe Options: ${raw}`); + subscribeOptions = Number(raw) as SubscribeOptions; } const params = await Parameters.decode(r, version); From 5a33713e76fbcb6f403dac0f70990ec704dbf361 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 20:22:25 -0700 Subject: [PATCH 5/7] quest: complete m0/ietf-namespace-stream Co-Authored-By: Claude Opus 5.5 --- quest/m0/README.md | 1 - quest/m0/ietf-namespace-stream.md | 59 ------------------------------- 2 files changed, 60 deletions(-) delete mode 100644 quest/m0/ietf-namespace-stream.md diff --git a/quest/m0/README.md b/quest/m0/README.md index fcfeb086b24..5077aea7d14 100644 --- a/quest/m0/README.md +++ b/quest/m0/README.md @@ -52,7 +52,6 @@ a published `@moq/watch` break. - [Held group wakes](/quest/m0/held-group-wakes.md) - a group held across a route switch is woken when its successor's first timestamp or abort makes it stale, not at the next unrelated append - [Draft-22 LOCATION_FILTER](/quest/m0/ietf-location-filter-22.md) - moqt-22 LOCATION_FILTER carries its type instead of a Length in Rust and JS, so a draft-22 peer reads our Next Object correctly - [moq-transport parameters per draft](/quest/m0/ietf-params-per-draft.md) - every control message accepts exactly the parameters its draft allows, so moqx's draft-16 SUBSCRIBE_NAMESPACE with FORWARD stops failing -- [NAMESPACE on the stream](/quest/m0/ietf-namespace-stream.md) - every draft-16+ SUBSCRIBE_NAMESPACE stream carries NAMESPACE for each match, whatever the peer's SETUP options - [Dialed split horizon](/quest/m0/dial-split-horizon.md) - a session moq-net dials gets a per-connection hop, so routes never echo back to the peer they came from - [No empty d14 prefix](/quest/m0/ietf-d14-root-prefix.md) - draft-14 never sends SUBSCRIBE_NAMESPACE with an empty prefix, and the docs say to scope a d14 link to moxygen - [End of Track Location](/quest/m0/ietf-end-of-track-location.md) - a relay forwards END_OF_TRACK at its upstream's Location, on the last group's stream, instead of re-sending it at the next group's object 0 diff --git a/quest/m0/ietf-namespace-stream.md b/quest/m0/ietf-namespace-stream.md deleted file mode 100644 index 93ad66c5789..00000000000 --- a/quest/m0/ietf-namespace-stream.md +++ /dev/null @@ -1,59 +0,0 @@ -# [S] SUBSCRIBE_NAMESPACE streams carry NAMESPACE - -## Goal - -On draft-16 and later, a SUBSCRIBE_NAMESPACE that asks for namespaces -gets NAMESPACE and NAMESPACE_DONE on its response stream for each matching -namespace, whatever the peer's SETUP options, in `rs/moq-net` and `js/net`. -Today a peer that did not send our SOLICIT option (0x40B5A), which is every -other implementation, gets an empty stream and only the unsolicited -PUBLISH_NAMESPACE pushes. - -## Plan - -Triaged from Fastly's moq-relay-interop report (2026-09-23 run) on -2026-10-07. d18 §6.2 says the publisher "MUST send a NAMESPACE message to -any subscriber that has sent SUBSCRIBE_NAMESPACE" for a matching prefix, -and §10.18 puts them on the response stream. d16 §9.25 says the same -without the MUST. Both drafts let a publisher send PUBLISH_NAMESPACE to any -subscriber, and neither says anything about duplicates. - -On d16 and d17 the message's Subscribe Options choose what the subscriber -wants: PUBLISH (0x00), NAMESPACE (0x01) or both (0x02) (d16 §9.25, d17 -§9.20). d18 moved track requests to SUBSCRIBE_TRACKS, so its -SUBSCRIBE_NAMESPACE always asks for namespaces. Both `rs/moq-net` and -`js/net` decode the field but drop it before dispatch, so the publisher -cannot tell the options apart today. - -Decided 2026-10-07: fill the stream on d16+ whenever NAMESPACE is -requested, and keep the unsolicited pushes to non-SOLICIT peers, so those -peers hear each namespace twice. Carry Subscribe Options through dispatch: -0x01 and 0x02 both ask for namespaces, so both get NAMESPACE; 0x00 gets none. -We send no PUBLISH for a namespace subscription, so decide how the track half -is answered. For 0x00 (tracks only), refusing is the recommendation, since it -fails loud. For 0x02, the recommendation is to answer with namespaces only, -since refusing would drop the NAMESPACE it asked for. A value above 0x02 is -malformed and refused. -Rejected: stopping unsolicited pushes on d16+ (a peer that never subscribes -would learn nothing), and filling only on d18+ (two behaviours for one -message). - -Where it lives: `rs/moq-net/src/ietf/publisher.rs` (around line 2180) -picks `origin.empty()`, or only the hidden remainder, when -`declared.solicit` is unset. Its comment explains the old reason: a peer -hearing both would hold two sources for one namespace. A NAMESPACE is -discovery only, not a route, so that no longer applies; rewrite the comment. -Drafts 14 and 15 keep answering with PUBLISH_NAMESPACE requests, unchanged. -`js/net` mirrors the solicit logic (`js/net/src/ietf/connection.ts`, -`publisher.ts`). - -Update `doc/concept/standard.md` where it describes solicit: a non-SOLICIT -d16+ peer now also gets NAMESPACE on its SUBSCRIBE_NAMESPACE stream, so it -hears each namespace twice. - -Test: a non-SOLICIT d16 and d18 peer's SUBSCRIBE_NAMESPACE receives -NAMESPACE for an existing match and for one announced later, then -NAMESPACE_DONE when it ends. On d16, options 0x00, 0x01, 0x02 and 0x03 each -get the chosen behaviour, in both languages. - -Public API: none. Wire: moq-transport replies move closer to the drafts. From 0e28ffa21a01d22970809b6432f1a8864357f4d8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 20:29:47 -0700 Subject: [PATCH 6/7] docs(drafts): log the solicit both-ways change Co-Authored-By: Claude Opus 5.5 --- drafts/draft-lcurley-moq-solicit.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drafts/draft-lcurley-moq-solicit.md b/drafts/draft-lcurley-moq-solicit.md index eb96b50dd57..0bdb2e00718 100644 --- a/drafts/draft-lcurley-moq-solicit.md +++ b/drafts/draft-lcurley-moq-solicit.md @@ -157,6 +157,12 @@ This document defines only the values 0 and 1; a later extension that needs to s --- back +# Changelog + +## Since draft-lcurley-moq-solicit-00 (in progress) + +- A namespace heard both unsolicited and in answer to SUBSCRIBE_NAMESPACE is one advertisement, replacing the advice not to advertise it both ways. + # Acknowledgments {:numbered="false"} From 00dd4d8b546b7b8f8aedc99be37cb01dcd650348 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 20:34:10 -0700 Subject: [PATCH 7/7] fix(net): word undefined Subscribe Options as our refusal The drafts define only 0x00 through 0x02 and do not call other values malformed, so say closing the session is our policy, in code and docs. Cover it at dispatch in Rust, and say a namespace heard both ways is held until both advertisements are withdrawn. Co-Authored-By: Claude Opus 5.5 --- doc/concept/standard.md | 3 ++- drafts/draft-lcurley-moq-solicit.md | 4 ++-- js/net/src/ietf/connection.test.ts | 2 +- rs/moq-net/src/ietf/publisher.rs | 21 +++++++++++++++++++++ rs/moq-net/src/ietf/subscribe_namespace.rs | 2 +- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/doc/concept/standard.md b/doc/concept/standard.md index 673ca0256dd..fabd517ff39 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -186,6 +186,7 @@ set the solicit `SETUP` option to make us wait to be asked. A peer that does not set that option still gets each match as a `NAMESPACE` on its `SUBSCRIBE_NAMESPACE` stream on draft-16 and later, so it hears the namespace twice. We never send `PUBLISH`: on drafts 16 and 17 a `SUBSCRIBE_NAMESPACE` asking only for `PUBLISH` -is refused, and one asking for both gets only `NAMESPACE`. Single-track +is refused, one asking for both gets only `NAMESPACE`, and any other Subscribe +Options value closes the session. Single-track `PUBLISH` offers are declined; announce a namespace and serve the resulting `SUBSCRIBE`s instead. diff --git a/drafts/draft-lcurley-moq-solicit.md b/drafts/draft-lcurley-moq-solicit.md index 0bdb2e00718..2f6bacbee24 100644 --- a/drafts/draft-lcurley-moq-solicit.md +++ b/drafts/draft-lcurley-moq-solicit.md @@ -104,7 +104,7 @@ A relay is the expected user of this declaration, as is any endpoint that asks f So is an endpoint that only publishes, which cannot subscribe to anything and therefore has no use for an advertisement of any kind. An endpoint that did not declare 1 can hear a namespace both ways: unsolicited, and in answer to its own SUBSCRIBE_NAMESPACE. -Both report the same namespace, so the receiver treats them as one advertisement, not two sources. +Both report the same namespace, so the receiver holds one advertisement, not two sources, until both are withdrawn. # Enforcement {#enforcement} @@ -161,7 +161,7 @@ This document defines only the values 0 and 1; a later extension that needs to s ## Since draft-lcurley-moq-solicit-00 (in progress) -- A namespace heard both unsolicited and in answer to SUBSCRIBE_NAMESPACE is one advertisement, replacing the advice not to advertise it both ways. +- A namespace heard both unsolicited and in answer to SUBSCRIBE_NAMESPACE is one advertisement until both are withdrawn, replacing the advice not to advertise it both ways. # Acknowledgments {:numbered="false"} diff --git a/js/net/src/ietf/connection.test.ts b/js/net/src/ietf/connection.test.ts index ede0a7a55e2..7d49b115453 100644 --- a/js/net/src/ietf/connection.test.ts +++ b/js/net/src/ietf/connection.test.ts @@ -222,7 +222,7 @@ for (const [version, alpn, options, namespaces] of [ }); } -/** Subscribe Options above 0x02 are malformed and close the session, even past 2^53. */ +/** The drafts define only 0x00 through 0x02; any other value closes the session, even past 2^53. */ for (const [version, alpn] of [ [Version.DRAFT_16, ALPN.DRAFT_16], [Version.DRAFT_17, ALPN.DRAFT_17], diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 695ebae8e0b..00e7bbd9858 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -4134,6 +4134,27 @@ mod serve_tests { } assert!(buf.is_empty(), "{label}: trailing bytes"); } + + // Undefined Subscribe Options close the session: dispatch returns an error. + #[rustfmt::skip] + let cases: [(Version, &[u8]); 2] = [ + // Request ID, empty namespace, Subscribe Options 0x03, no parameters. + (Version::Draft16, &[0x2B, 0x00, 0x03, 0x00]), + // Draft-17 adds the Required Request ID delta after the Request ID. + (Version::Draft17, &[0x2B, 0x00, 0x00, 0x03, 0x00]), + ]; + for (version, body) in cases { + let h = serve(version); + let stream = Stream::open(&mut h.session.clone(), version).await.unwrap(); + let body = ietf::Body(bytes::Bytes::copy_from_slice(body)); + assert!( + h.publisher + .clone() + .handle_stream(ietf::SubscribeNamespaceLegacy::ID, body, stream) + .is_err(), + "{version}: Subscribe Options 0x03 kept the session open" + ); + } } /// The draft's canonical current-group join: a Next Object subscription plus a diff --git a/rs/moq-net/src/ietf/subscribe_namespace.rs b/rs/moq-net/src/ietf/subscribe_namespace.rs index bbac14a4066..c5eb3e01db6 100644 --- a/rs/moq-net/src/ietf/subscribe_namespace.rs +++ b/rs/moq-net/src/ietf/subscribe_namespace.rs @@ -571,7 +571,7 @@ mod tests { } } - /// Subscribe Options above 0x02 are malformed (d16 §9.25). + /// The drafts define only 0x00 through 0x02, so we refuse any other value as a protocol violation. #[test] fn legacy_rejects_unknown_subscribe_options() { // Request ID, empty namespace, Subscribe Options 0x03, no parameters.