diff --git a/doc/concept/standard.md b/doc/concept/standard.md index 1fdab9340ba..260533efe9d 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -28,8 +28,8 @@ subset drops. What a peer actually observes against this implementation: - **Pull.** Subscribers ask. Single-track `PUBLISH` offers are declined; announce a namespace and serve the resulting subscriptions. Announcements go out unsolicited, and we also ask for every prefix we may discover. The solicit `SETUP` option makes us wait to be asked. Without it, a draft 16+ peer also gets each match as a `NAMESPACE` on its `SUBSCRIBE_NAMESPACE` stream, so it hears the namespace twice. We never send `PUBLISH`: on drafts 16 and 17 a `SUBSCRIBE_NAMESPACE` asking only for `PUBLISH` is refused, one asking for both gets only `NAMESPACE`, and any other Subscribe Options value closes the session. - **History is one group.** On drafts 14 through 19 a `FETCH` returns one whole group from the cache, or the saved prefix of the group a new subscription just joined. A range of groups is refused, as is any `FETCH` on draft 20 and later. JavaScript publishing refuses every `FETCH`. Datagrams are never fetchable. - **Timing.** A track whose `SUBSCRIBE_OK` declares no `TIMESCALE` is untimed, as is every track on drafts 14 through 16. A reader that never subscribed reads a standalone fetch untimed. -- **Strict SETUP and parameters.** Repeated unknown `SETUP` options, GREASE included, are accepted if well-formed; a repeated known option is rejected. On draft 17 and later, a `GROUP_ORDER` other than Ascending (1) or Descending (2) closes the session. -- **One credential per session**, carried in `SETUP` and forwarded to the [auth server](/bin/relay/auth#the-contract) unverified. A token attached to an individual request is ignored. An alias reference, a second token, or a parameter the negotiated draft does not define closes the session. +- **Strict SETUP and parameters.** Repeated unknown `SETUP` options, GREASE included, are accepted if well-formed; a repeated known option is rejected. On draft 17 and later, a `GROUP_ORDER` other than Ascending (1) or Descending (2) closes the session. Message parameters follow the negotiated draft's lists: drafts 14 and 15 ignore an unknown or misplaced parameter, draft 16 ignores one defined only for another message but closes the session on an unknown one, and draft 17 and later close it on both. +- **One credential per session**, carried in `SETUP` and forwarded to the [auth server](/bin/relay/auth#the-contract) unverified. A token attached to an individual request is ignored. An alias reference or a second token closes the session. - **Refused, not fatal.** A legal request this stack does not serve is rejected on its own and the session stays up: `FORWARD=0`, range filters, `TRACK_STATUS`, `SUBSCRIBE_TRACKS`, and the fetch forms above. On draft 19 and later, and in Rust on drafts 14 through 16, a subscription update may change only priority; any other update ends that subscription. - **Datagrams** are a single normal object at object 0, forwarded without renumbering. Anything else is dropped, and a malformed one closes the session. Rust and JavaScript both carry them on every draft. - **Priority.** Higher is served first. The IETF default of 128 is this stack's 127, and a track that never sets one is 127. diff --git a/js/net/src/ietf/cluster.test.ts b/js/net/src/ietf/cluster.test.ts index 83a0cc33310..a4c2626c465 100644 --- a/js/net/src/ietf/cluster.test.ts +++ b/js/net/src/ietf/cluster.test.ts @@ -4,6 +4,8 @@ import { type Hop, HopSchema, UNKNOWN_HOP } from "../hop.ts"; import * as Path from "../path.ts"; import { Reader, Writer } from "../stream.ts"; import * as Cluster from "./cluster.ts"; +import * as Message from "./message.ts"; +import * as Namespace from "./namespace.ts"; import { Parameters, SetupOption, SetupOptions } from "./parameters.ts"; import { PublishNamespace, PublishNamespaceUpdate } from "./publish_namespace.ts"; import { SubscribeNamespaceEntry } from "./subscribe_namespace.ts"; @@ -229,6 +231,26 @@ test("Cluster: PUBLISH_NAMESPACE carries the parameters only once negotiated", a await expect(PublishNamespace.decode(reader(await encode(bare)), VERSION, true)).rejects.toThrow(ProtocolViolation); }); +test("Cluster: a negotiated PUBLISH_NAMESPACE may carry AUTHORIZATION_TOKEN", async () => { + // The advertisement shares its decode with NAMESPACE, whose list has no token. A + // PUBLISH_NAMESPACE's own list does, so the token must not close the session. + const advert = { hops: hops(7n, 9n), cost: 5n }; + const withToken = { + async encode(w: Writer, version: IetfVersion) { + await Message.encode(w, async (wr) => { + await wr.u62(1n); + await Namespace.encode(wr, Path.from("alice.hang")); + const params = Cluster.intoParams(advert); + params.bytes.set(0x03n, new Uint8Array([1, 2, 3])); // AUTHORIZATION_TOKEN + await params.encode(wr, version); + }); + }, + }; + + const decoded = await PublishNamespace.decode(reader(await encode(withToken)), VERSION, true); + expect(decoded.cluster).toEqual(advert); +}); + test("Cluster: every malformed PUBLISH_NAMESPACE update is a protocol violation", async () => { const valid = await encode(new PublishNamespaceUpdate({ requestId: 1n, update: { cost: 2n } })); const trailing = new Uint8Array(valid.byteLength + 1); diff --git a/js/net/src/ietf/cluster.ts b/js/net/src/ietf/cluster.ts index 2b791363469..0af006eb97b 100644 --- a/js/net/src/ietf/cluster.ts +++ b/js/net/src/ietf/cluster.ts @@ -204,7 +204,8 @@ function encodeHops(hops: Hop[]): Uint8Array { } /** - * Read the parameters a negotiated session puts on every advertisement. + * Read the parameters a negotiated session puts on every advertisement, checked against + * the list of the message that carries them (PUBLISH_NAMESPACE also allows a token). * * The parameter block is mandatory there, so a message that ends before it (the base form) * or one whose block does not parse is the peer's violation, same as a block that parses but @@ -212,10 +213,14 @@ function encodeHops(hops: Hop[]): Uint8Array { * * @internal */ -export async function decodeParams(r: Reader, version: IetfVersion): Promise { +export async function decodeParams( + r: Reader, + version: IetfVersion, + message: "namespace" | "publish-namespace", +): Promise { let params: Parameters; try { - params = await Parameters.decode(r, version); + params = await Parameters.decode(r, version, message); } catch (err) { throw new ProtocolViolation(reason(err), { cause: err }); } diff --git a/js/net/src/ietf/fetch.ts b/js/net/src/ietf/fetch.ts index e0a5159873f..3db0412c98a 100644 --- a/js/net/src/ietf/fetch.ts +++ b/js/net/src/ietf/fetch.ts @@ -89,7 +89,7 @@ export class Fetch { } } - const params = await Parameters.decode(r, version); + const params = await Parameters.decode(r, version, "fetch"); if (params.rangeFilters && !hasRangeFilters(version)) { throw new Error("Range Filters need draft-19"); } diff --git a/js/net/src/ietf/ietf.test.ts b/js/net/src/ietf/ietf.test.ts index 9090c7a5c23..d16c7b5bee2 100644 --- a/js/net/src/ietf/ietf.test.ts +++ b/js/net/src/ietf/ietf.test.ts @@ -1,4 +1,5 @@ import { expect, test } from "bun:test"; +import { ProtocolViolation } from "../error.ts"; import * as Path from "../path.ts"; import { type Cursor, Reader, Writer } from "../stream.ts"; import { Timescale, Timestamp } from "../time.ts"; @@ -1559,6 +1560,73 @@ test("SubscribeNamespace: draft-18 omits subscribe options", async () => { } }); +// FORWARD is legal on SUBSCRIBE_NAMESPACE through draft-17. A value other than 0 or 1 +// still closes the session. Draft-18 dropped it from this message. +test("SubscribeNamespace: FORWARD follows the draft", async () => { + const legacy = (version: IetfVersion, value: number) => { + const body = [0x01]; + if (version === Version.DRAFT_17) body.push(0x00); + body.push(0x00, 0x01, 0x01, 0x10, value); + return framed(body); + }; + + for (const version of [Version.DRAFT_16, Version.DRAFT_17] as const) { + const decoded = await decodeVersioned( + legacy(version, 0), + SubscribeNamespace.SubscribeNamespaceLegacy.decode, + version, + ); + expect(decoded.requestId).toBe(1n); + await expect( + decodeVersioned(legacy(version, 2), SubscribeNamespace.SubscribeNamespaceLegacy.decode, version), + ).rejects.toThrow(ProtocolViolation); + } + + await expect( + decodeVersioned( + framed([0x01, 0x00, 0x01, 0x10, 0x00]), + SubscribeNamespace.SubscribeNamespace.decode, + Version.DRAFT_18, + ), + ).rejects.toThrow(ProtocolViolation); +}); + +// EXPIRES is a draft-16 message parameter, but not on SUBSCRIBE. Draft-16 ignores it and +// still reads the parameter after it. Draft-18 closes the session. +test("Parameters: a known parameter on the wrong message", async () => { + const block = new Uint8Array([0x02, 0x08, 0x05, 0x18, 0x07]); + const ignored = await Parameters.decode( + new Reader(undefined, block, Version.DRAFT_16), + Version.DRAFT_16, + "subscribe", + ); + expect(ignored.subscriberPriority).toBe(7); + expect(ignored.expires).toBeUndefined(); + + await expect( + Parameters.decode(new Reader(undefined, block, Version.DRAFT_18), Version.DRAFT_18, "subscribe"), + ).rejects.toThrow(ProtocolViolation); +}); + +// TRACK_NAMESPACE_PREFIX updates a SUBSCRIBE_NAMESPACE or SUBSCRIBE_TRACKS only, so a +// subscription's REQUEST_UPDATE carrying it closes the session instead of being acked. +test("Parameters: TRACK_NAMESPACE_PREFIX is not a subscription update parameter", async () => { + // One parameter: TRACK_NAMESPACE_PREFIX (0x34), a one-field namespace "a". + const block = new Uint8Array([0x01, 0x34, 0x01, 0x01, 0x61]); + await expect( + Parameters.decode(new Reader(undefined, block, Version.DRAFT_18), Version.DRAFT_18, "request-update"), + ).rejects.toThrow(ProtocolViolation); +}); + +test("Subscribe: SUBGROUP_DELIVERY_TIMEOUT follows its draft", async () => { + const body = framed([...TRACK_HEAD, 0x01, 0x06, 0x00]); + await expect(decodeVersioned(body, Subscribe.Subscribe.decode, Version.DRAFT_16)).rejects.toThrow( + ProtocolViolation, + ); + const decoded = await decodeVersioned(body, Subscribe.Subscribe.decode, Version.DRAFT_18); + expect(decoded.trackName).toBe("video"); +}); + test("Group: draft-18 sets FIRST_OBJECT bit, draft-17 does not", async () => { const makeGroup = () => new Group({ diff --git a/js/net/src/ietf/parameters.ts b/js/net/src/ietf/parameters.ts index d45fd772e75..339fc392aea 100644 --- a/js/net/src/ietf/parameters.ts +++ b/js/net/src/ietf/parameters.ts @@ -226,6 +226,126 @@ const MSG_PARAM_SUBSCRIPTION_FILTER = 0x21n; const MSG_PARAM_FILL_PARAMETERS = 0x23n; /// HOP_PATH, from the MoQ Cluster extension. See `cluster.ts`. const MSG_PARAM_HOP_PATH = 0x40b57n; +/// ACTIVE_COUNT, from the MoQ Active Count extension. +const MSG_PARAM_ACTIVE_COUNT = 0x40b66n; + +/// Message parameter ids defined in draft-16. A known id on the wrong message is ignored. +const DRAFT16_MESSAGE_PARAMS: readonly bigint[] = [0x02n, 0x03n, 0x08n, 0x09n, 0x10n, 0x20n, 0x21n, 0x22n, 0x32n]; + +/** Which control message the parameter block belongs to. Omitted keeps the unfiltered decode. */ +type ControlMessage = + | "subscribe" + | "subscribe-ok" + | "subscribe-update" + | "subscribe-namespace" + | "publish" + | "publish-namespace" + | "fetch" + | "request-ok" + | "request-update" + | "namespace"; + +function draft20(version: IetfVersion): boolean { + return version >= Version.DRAFT_20; +} + +/** True when this draft's definition of `message` includes `id`. */ +function allows(message: ControlMessage, version: IetfVersion, id: bigint): boolean { + const d15 = version === Version.DRAFT_15; + const d16 = version === Version.DRAFT_16; + const d17 = version === Version.DRAFT_17; + const from16 = version >= Version.DRAFT_16; + const from17 = version >= Version.DRAFT_17; + const from18 = version >= Version.DRAFT_18; + const from19 = version >= Version.DRAFT_19; + const legacyForward = d15 || d16 || d17; + const auth = id === MSG_PARAM_AUTHORIZATION_TOKEN; + const forward = id === MSG_PARAM_FORWARD; + const priority = id === MSG_PARAM_SUBSCRIBER_PRIORITY; + const filter = id === MSG_PARAM_SUBSCRIPTION_FILTER; + const order = id === MSG_PARAM_GROUP_ORDER; + const expires = id === MSG_PARAM_EXPIRES; + const largest = id === MSG_PARAM_LARGEST_OBJECT; + const objectTimeout = id === MSG_PARAM_DELIVERY_TIMEOUT; + const subgroupTimeout = id === MSG_PARAM_SUBGROUP_DELIVERY_TIMEOUT && from18; + const range = id >= 0x25n && id <= 0x28n && from19; + const trackRange = id === MSG_PARAM_TRACK_PROPERTY_FILTER && from19; + const fill = id === MSG_PARAM_FILL_PARAMETERS && draft20(version); + const include = id === MSG_PARAM_INCLUDE_PROPERTIES && draft20(version); + const newGroup = id === MSG_PARAM_NEW_GROUP_REQUEST && from16; + + switch (message) { + case "subscribe": + return ( + objectTimeout || + auth || + (id === MSG_PARAM_MAX_CACHE_DURATION && from17) || + subgroupTimeout || + forward || + priority || + filter || + order || + fill || + range || + newGroup || + include + ); + case "subscribe-ok": + return (id === MSG_PARAM_MAX_CACHE_DURATION && d15) || expires || largest || (order && d15); + case "subscribe-update": + case "request-update": + return ( + objectTimeout || + auth || + subgroupTimeout || + forward || + priority || + filter || + fill || + range || + trackRange || + newGroup + ); + case "subscribe-namespace": + return auth || (forward && legacyForward) || id === MSG_PARAM_HIDDEN; + case "publish": + return ( + (objectTimeout && (d15 || draft20(version))) || + auth || + (subgroupTimeout && draft20(version)) || + expires || + largest || + forward || + (priority && draft20(version)) || + (filter && draft20(version)) || + (order && (d15 || draft20(version))) + ); + case "publish-namespace": + return auth || id === MSG_PARAM_HOP_PATH || id === MSG_PARAM_ROUTE_COST; + case "fetch": + return ( + auth || + (id === MSG_PARAM_FILL_TIMEOUT && from18) || + priority || + (filter && draft20(version)) || + order || + range || + include + ); + case "request-ok": + return expires || largest || id === MSG_PARAM_ACTIVE_COUNT; + case "namespace": + return id === MSG_PARAM_HOP_PATH || id === MSG_PARAM_ROUTE_COST; + } +} + +async function skipKvp(r: Reader, id: bigint): Promise { + if (id % 2n === 0n) { + await r.u62(); + } else { + await r.read(await r.u53()); + } +} /// The object Range Filters (draft-19): SUBGROUP, OBJECTID, PRIORITY and OBJECT_PROPERTY. /// Each is length prefixed whatever the parity of its id. @@ -254,6 +374,7 @@ function getMessageParamKind(id: bigint): MessageParamKind { case MSG_PARAM_EXPIRES: case MSG_PARAM_ROUTE_COST: case MSG_PARAM_HIDDEN: + case MSG_PARAM_ACTIVE_COUNT: return "varint"; case MSG_PARAM_PUBLISHER_PRIORITY: case MSG_PARAM_SUBSCRIBER_PRIORITY: @@ -574,7 +695,16 @@ export class Parameters { } } - static async decode(r: Reader, version: IetfVersion): Promise { + /** + * Decode a parameter block. + * + * `message`, when set, is the allow-list for that control message. A parameter the + * draft defines for a different message is ignored through draft-16 and closes the + * session from draft-17 on. An id the draft does not define at all closes from + * draft-16 on. Draft-14 and draft-15 ignore anything the message does not list. + * Omitting `message` keeps the previous decode, which stores every id. + */ + static async decode(r: Reader, version: IetfVersion, message?: ControlMessage): Promise { const count = await r.u53(); const params = new Parameters(); @@ -591,12 +721,29 @@ export class Parameters { prevType = id; } + if (message !== undefined && !allows(message, version, id)) { + // Draft-17 on has no length on a parameter value, so an unlisted id cannot be skipped. + const ignore = + version === Version.DRAFT_14 || + version === Version.DRAFT_15 || + (version === Version.DRAFT_16 && DRAFT16_MESSAGE_PARAMS.includes(id)); + if (!ignore) { + throw new ProtocolViolation(`message parameter ${id} is not defined for ${message}`); + } + await skipKvp(r, id); + continue; + } + if (version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16) { if (id % 2n === 0n) { if (params.vars.has(id)) { throw new Error(`duplicate message parameter id: ${id.toString()}`); } const varint = await r.u62(); + // A bool is a varint here. Only 0 and 1 are legal once the message lists it. + if (message !== undefined && id === MSG_PARAM_FORWARD && varint !== 0n && varint !== 1n) { + throw new ProtocolViolation(`invalid message parameter value: ${id}`); + } params.vars.set(id, varint); } else { const size = await r.u53(); @@ -640,9 +787,14 @@ export class Parameters { params.vars.set(id, BigInt(value)); break; } - case "bool": - params.vars.set(id, (await r.bool()) ? 1n : 0n); + case "bool": { + const value = await r.u8(); + if (value !== 0 && value !== 1) { + throw new ProtocolViolation(`invalid message parameter value: ${id}`); + } + params.vars.set(id, BigInt(value)); break; + } case "location": { // Two bare varints from draft-17 on; see the matching comment in encode. const groupId = await r.u62(); diff --git a/js/net/src/ietf/publish.ts b/js/net/src/ietf/publish.ts index dbe73ecf45d..19b1c94cd63 100644 --- a/js/net/src/ietf/publish.ts +++ b/js/net/src/ietf/publish.ts @@ -133,10 +133,10 @@ export class Publish { }); } // v15+: parameters followed by Track Properties (draft-17+) - const params = await Parameters.decode(r, version); + const params = await Parameters.decode(r, version, "publish"); const properties = await Properties.decode(r, version); - // GROUP_ORDER is only legal here through draft-15, but keep accepting it so a peer that - // still sends it doesn't have its session torn down over a hint. + // GROUP_ORDER is legal here in draft-15 and again from draft-20. Draft-16 ignores it. + // Draft-17 through draft-19 close the session. const groupOrder = properties.groupOrder ?? params.groupOrder ?? 0x02; const forward = params.forward ?? true; const largest = params.largest; diff --git a/js/net/src/ietf/publish_namespace.ts b/js/net/src/ietf/publish_namespace.ts index 1be3e59c458..ad0da3ec601 100644 --- a/js/net/src/ietf/publish_namespace.ts +++ b/js/net/src/ietf/publish_namespace.ts @@ -62,11 +62,11 @@ export class PublishNamespace { } const trackNamespace = await Namespace.decode(r); if (negotiated) { - const cluster = await Cluster.decodeParams(r, version); + const cluster = await Cluster.decodeParams(r, version, "publish-namespace"); return new PublishNamespace({ requestId, trackNamespace, cluster }); } - await Parameters.decode(r, version); // ignore parameters + await Parameters.decode(r, version, "publish-namespace"); return new PublishNamespace({ requestId, trackNamespace }); } } @@ -134,7 +134,7 @@ export class PublishNamespaceUpdate { if (version === Version.DRAFT_17) { await r.u62(); // required_request_id_delta (draft-17 only, removed in draft-18 per #1615) } - const params = await Parameters.decode(r, version); + const params = await Parameters.decode(r, version, "publish-namespace"); return new PublishNamespaceUpdate({ requestId, update: Cluster.updateFromParams(params) }); } } diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 54193f51f02..e8035c66f3b 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -480,7 +480,7 @@ export class Publisher { const params = await Message.decode(stream.reader, async (r) => { await r.u62(); if (version === Version.DRAFT_17) await r.u62(); - return Parameters.decode(r, version); + return Parameters.decode(r, version, "request-update"); }); const unsupported = params.forward === false || diff --git a/js/net/src/ietf/request.ts b/js/net/src/ietf/request.ts index 9b9f86da846..f705ef17203 100644 --- a/js/net/src/ietf/request.ts +++ b/js/net/src/ietf/request.ts @@ -105,7 +105,7 @@ export class RequestOk { version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16 ? await r.u62() : undefined; - const parameters = await Parameters.decode(r, version); + const parameters = await Parameters.decode(r, version, "request-ok"); await Properties.decode(r, version); return new RequestOk({ requestId, parameters }); } diff --git a/js/net/src/ietf/subscribe.ts b/js/net/src/ietf/subscribe.ts index fcd48f42607..73d218463ae 100644 --- a/js/net/src/ietf/subscribe.ts +++ b/js/net/src/ietf/subscribe.ts @@ -150,7 +150,7 @@ export class Subscribe { return new Subscribe({ requestId, trackNamespace, trackName, subscriberPriority, filter, forward }); } // v15+: fields are in parameters - const params = await Parameters.decode(r, version); + const params = await Parameters.decode(r, version, "subscribe"); const subscriberPriority = params.subscriberPriority ?? 128; let groupOrder = params.groupOrder ?? GROUP_ORDER; if (groupOrder > 2) { @@ -306,7 +306,7 @@ export class SubscribeOk { // v15+: parameters followed by Track Properties (draft-17+). LARGEST_OBJECT is // required on every draft once the track has content, so rejecting it would tear // down a session over a parameter compliant publishers must send. - const params = await Parameters.decode(r, version); + const params = await Parameters.decode(r, version, "subscribe-ok"); largest = params.largest; properties = await Properties.decode(r, version); if (version === Version.DRAFT_15) properties.maxCacheDuration = params.maxCacheDuration; @@ -413,7 +413,7 @@ export class SubscribeUpdate { } else if (version === Version.DRAFT_15 || version === Version.DRAFT_16) { const requestId = await r.u62(); await r.u62(); // subscription_request_id - await Parameters.decode(r, version); + await Parameters.decode(r, version, "subscribe-update"); return new SubscribeUpdate({ requestId }); } else { // v17+: REQUEST_UPDATE @@ -421,7 +421,7 @@ export class SubscribeUpdate { if (version === Version.DRAFT_17) { await r.u62(); // required_request_id_delta (draft-17 only, removed in draft-18 per #1615) } - await Parameters.decode(r, version); + await Parameters.decode(r, version, "subscribe-update"); return new SubscribeUpdate({ requestId }); } } diff --git a/js/net/src/ietf/subscribe_namespace.ts b/js/net/src/ietf/subscribe_namespace.ts index 9bd3d6cf31e..b20fcef7cff 100644 --- a/js/net/src/ietf/subscribe_namespace.ts +++ b/js/net/src/ietf/subscribe_namespace.ts @@ -69,7 +69,7 @@ export class SubscribeNamespace { } const requestId = await r.u62(); const namespace = await Namespace.decode(r); - const params = await Parameters.decode(r, version); + const params = await Parameters.decode(r, version, "subscribe-namespace"); return new SubscribeNamespace({ namespace, requestId, hidden: params.hidden }); } @@ -166,7 +166,7 @@ export class SubscribeNamespaceLegacy { if (raw > BigInt(SubscribeOptions.BOTH)) throw new ProtocolViolation(`invalid Subscribe Options: ${raw}`); subscribeOptions = Number(raw) as SubscribeOptions; } - const params = await Parameters.decode(r, version); + const params = await Parameters.decode(r, version, "subscribe-namespace"); return new SubscribeNamespaceLegacy({ namespace, requestId, subscribeOptions, hidden: params.hidden }); } @@ -318,7 +318,7 @@ export class SubscribeNamespaceEntry { const suffix = await Namespace.decode(r); if (!negotiated) return new SubscribeNamespaceEntry({ suffix }); - return new SubscribeNamespaceEntry({ suffix, cluster: await Cluster.decodeParams(r, version) }); + return new SubscribeNamespaceEntry({ suffix, cluster: await Cluster.decodeParams(r, version, "namespace") }); } } diff --git a/quest/m0/README.md b/quest/m0/README.md index 848bc443c19..3c92f5b917c 100644 --- a/quest/m0/README.md +++ b/quest/m0/README.md @@ -54,10 +54,8 @@ 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 - [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 - [End of Group status](/quest/m0/ietf-end-of-group-status.md) - an End of Group status on a stream whose header already marks the group's end is accepted, so imquic's last object per group arrives -- [Subgroup at object 0](/quest/m0/ietf-first-object-zero.md) - a draft-18 subgroup with FIRST_OBJECT clear that starts at object 0 is read whole - [Prefix route fronts](/quest/m0/prefix-route-fronts.md) - a prefix route cannot be made to mint one front per requested path - [TRACK stream demand](/quest/m0/track-stream-demand.md) - a lite-05+ subscribe shows the publisher one `used` edge, not a flap between TRACK and SUBSCRIBE - [Idle fronts](/quest/m0/idle-fronts.md) - a front nobody has read for the linger ends with its per-path state, so a standing claim stops accumulating fronts and sources diff --git a/quest/m0/ietf-first-object-zero.md b/quest/m0/ietf-first-object-zero.md deleted file mode 100644 index 701c72f4980..00000000000 --- a/quest/m0/ietf-first-object-zero.md +++ /dev/null @@ -1,32 +0,0 @@ -# [XS] A subgroup starting at object 0 is whole - -## Goal - -A draft-18+ subgroup stream with FIRST_OBJECT clear whose first Object ID -is 0 is read as a whole group, in `rs/moq-net`, instead of being dropped as -"a group with no head". A clear bit with any other first ID is still dropped -as it is today. - -## Plan - -Triaged from Fastly's moq-relay-interop report (2026-09-23 run, item 7) on -2026-10-07. d18 §2.2 says the original publisher MUST set FIRST_OBJECT when -it opens a subgroup, and a relay MUST keep it set, so the probe that left it -clear was the one out of spec. The first Object ID is absolute whatever -the bit says (§11.4.2), and object IDs in our model start at 0, so ID 0 is -the head either way. - -Decided 2026-10-07: accept it anyway, to be lenient toward publishers that -are out of spec. Rejected: keeping the drop and documenting it. - -Where it lives: `rs/moq-net/src/ietf/subscriber.rs` (around line 2611) -drops the stream before reading any object. `next_object_id` already -refuses a first ID other than the group's start (0 for a whole group), so -check whether letting a clear-bit stream through to it is enough before -adding a second peek. Check whether `js/net` drops these streams at all; it -has no matching check today. - -Test: a d18 stream with FIRST_OBJECT clear and first ID 0 delivers its -group; one with first ID 3 is still dropped. - -Public API: none. Wire: none. diff --git a/quest/m0/ietf-params-per-draft.md b/quest/m0/ietf-params-per-draft.md deleted file mode 100644 index fd81987dca6..00000000000 --- a/quest/m0/ietf-params-per-draft.md +++ /dev/null @@ -1,60 +0,0 @@ -# [M] moq-transport parameters follow each draft's lists - -## Goal - -Every control message accepts exactly the parameters its draft allows, in -`rs/moq-net` and `js/net`, for each supported draft (14 through 22). A -parameter the draft defines for that message is decoded or skipped. Drafts -14 and 15 ignore unknown and misplaced parameters alike; draft-16 closes the -session with PROTOCOL_VIOLATION on an unknown key but ignores a misplaced -one; draft-17 and later close it on both. The case seen in the wild: -moqx's draft-16 SUBSCRIBE_NAMESPACE carries FORWARD (0x10), which we refuse, -and moqx redials in a loop. - -## Plan - -Triaged from Fastly's moq-relay-interop report (run of 2026-09-23, build -7ee2b02) on 2026-10-07. Facts from the draft text: - -- Unknown message parameter: d14 and d15 §9.2 say "Receivers ignore - unrecognized parameters"; d16 §9.2, d17 §9.3, d18 §10.2 and d22 §9.20 - close with PROTOCOL_VIOLATION. -- A known parameter in a message it is not defined for: d14, d15 and d16 - (§9.2.2) say "it MUST be ignored"; d17 §9.3.1, d18 §10.2.1 and d22 - §9.20.1 close the session. -- d16 allows FORWARD on SUBSCRIBE, REQUEST_UPDATE, PUBLISH, PUBLISH_OK and - SUBSCRIBE_NAMESPACE. d18 drops it from SUBSCRIBE_NAMESPACE (moved to - SUBSCRIBE_TRACKS). - -Decided 2026-10-07: audit every message against every draft rather than -patch FORWARD alone, since the report's EXPIRES case (fixed in #4195) and -this one are the same class. - -Where it lives: the `decode_params!` macro (`rs/moq-net/src/ietf/parameters.rs`, -around line 473) returns `InvalidValue` for any key outside a message's list. -Its call sites are in `fetch.rs`, `publish_namespace.rs`, `publish.rs`, -`subscribe_namespace.rs`, `request_stream.rs`, `request.rs` and -`subscribe.rs`. `Parameters::skip` ignores unknown keys, but only d14 -SUBSCRIBE, FETCH and PUBLISH use it; the legacy d14 to d17 decodes of -SUBSCRIBE_NAMESPACE, PUBLISH_NAMESPACE and the `request*.rs` messages go -through `decode_params!`, so they refuse unknown keys on d14 and d15 today -(`test_param_unknown_rejected` asserts that). Those are gaps. - -Accepting FORWARD=0 on a d16 SUBSCRIBE_NAMESPACE can be accept-and-ignore: -it only sets FORWARD on the PUBLISH messages it triggers, and we send none -for that subscription (Subscribe Options 0x01). - -Coordinate with [moq-transport request codes](/quest/m2/ietf-request-codes.md), -which accepts the delivery-timeout parameters on REQUEST_UPDATE; whichever -lands second drops the overlap. - -Tests: one codec test per gap the audit finds, plus one per rule above -(unknown and misplaced, on each side of the d16 and d17 boundaries). Mirror -in `js/net/src/ietf` in the same PR. `doc/concept/standard.md` says an -undefined parameter always closes the session; correct it. - -Public API: none. Wire: none new; decoding moves closer to the drafts. - -## Related - -- [Malformed moq-transport input](/quest/m2/ietf-malformed-close.md) - owns the other session-closing cases diff --git a/rs/moq-net/src/ietf/fetch.rs b/rs/moq-net/src/ietf/fetch.rs index b256878b75a..6793193ad6b 100644 --- a/rs/moq-net/src/ietf/fetch.rs +++ b/rs/moq-net/src/ietf/fetch.rs @@ -204,13 +204,13 @@ impl Message for Fetch<'_> { let fetch_type = FetchType::decode(buf, version)?; decode_params!(buf, version, 0x03 => _authorization_token: Vec, - 0x0A => fill_timeout: Option, + 0x0A => fill_timeout: Option where !matches!(version, Version::Draft15 | Version::Draft16 | Version::Draft17), 0x20 => subscriber_priority: Option, 0x22 => group_order: Option, - 0x25 => subgroup_filter: Vec, - 0x26 => object_id_filter: Vec, - 0x27 => priority_filter: Vec, - 0x28 => object_property_filter: Vec, + 0x25 => subgroup_filter: Vec where has_range_filters(version), + 0x26 => object_id_filter: Vec where has_range_filters(version), + 0x27 => priority_filter: Vec where has_range_filters(version), + 0x28 => object_property_filter: Vec where has_range_filters(version), ); let range_filters = [ subgroup_filter, @@ -221,13 +221,6 @@ impl Message for Fetch<'_> { .iter() .any(|filter| !filter.is_empty()); - // An unknown message parameter is a protocol violation, so each stays rejected - // on the drafts that predate it: FILL_TIMEOUT arrived in draft-18. - let has_fill_timeout = !matches!(version, Version::Draft15 | Version::Draft16 | Version::Draft17); - if (fill_timeout.is_some() && !has_fill_timeout) || (range_filters && !has_range_filters(version)) { - return Err(DecodeError::InvalidValue); - } - ( fetch_type, subscriber_priority, @@ -358,11 +351,12 @@ impl Message for FetchOk { _ => { let end_of_track = buf.bool()?; let end_location = Location::decode(buf, version)?; - // GROUP_ORDER isn't legal here, but keep accepting it so a peer that still sends - // it doesn't have its session torn down over a hint. + // MAX_CACHE_DURATION and GROUP_ORDER are legal on FETCH_OK only in draft-15. + // Draft-16 still knows GROUP_ORDER, so one here is ignored. From draft-17 + // it closes the session. decode_params!(buf, version, - 0x04 => max_cache_duration: Option, - 0x22 => group_order: Option, + 0x04 => max_cache_duration: Option where version == Version::Draft15, + 0x22 => group_order: Option where version == Version::Draft15, ); // The timescale is read but not surfaced yet: a fetched object without its // own units arrives untimed. diff --git a/rs/moq-net/src/ietf/parameters.rs b/rs/moq-net/src/ietf/parameters.rs index fe8cfff794b..3a73d2e51f7 100644 --- a/rs/moq-net/src/ietf/parameters.rs +++ b/rs/moq-net/src/ietf/parameters.rs @@ -415,6 +415,43 @@ impl Param for Opaque { } } +/// Message parameter ids defined in draft-16. Sorted for `binary_search`. +/// +/// A known id on a message that does not list it is ignored. An id outside this set +/// is unknown and closes the session. +const DRAFT16_MESSAGE_PARAMS: &[u64] = &[0x02, 0x03, 0x08, 0x09, 0x10, 0x20, 0x21, 0x22, 0x32]; + +fn skip_kvp(r: &mut Decoder<'_>, key: u64) -> Result<(), DecodeError> { + if key.is_multiple_of(2) { + r.varint()?; + } else { + let value = r.bytes()?; + if value.len() > MAX_KVP_VALUE_LEN { + return Err(DecodeError::BoundsExceeded); + } + } + Ok(()) +} + +/// Consumes a parameter the message does not list, when that draft says to ignore it. +/// +/// Draft-14 and draft-15 ignore an unrecognized parameter, including one defined for a +/// different message. Draft-16 ignores a known parameter on the wrong message and closes +/// on an unknown id. From draft-17 on both close the session, and a parameter value has +/// no length to skip by, so this returns false and the caller fails the message. +pub(crate) fn skip_unlisted(r: &mut Decoder<'_>, version: Version, key: u64) -> Result { + let ignore = match version { + Version::Draft14 | Version::Draft15 => true, + Version::Draft16 => DRAFT16_MESSAGE_PARAMS.binary_search(&key).is_ok(), + _ => false, + }; + if !ignore { + return Ok(false); + } + skip_kvp(r, key)?; + Ok(true) +} + /// Encode message parameters with compile-time sorted keys. /// /// Keys must be listed in ascending order (enforced at compile time). @@ -472,7 +509,10 @@ macro_rules! encode_params { /// optional parameters (defaults to `None` when absent) and bare types like `u8` /// for parameters where `T::default()` is an acceptable fallback. /// -/// Unknown parameters cause `DecodeError::InvalidValue`. +/// A `where` gate takes the key off the list on versions where the expression is false. +/// What remains unlisted is ignored on draft-14 and draft-15, ignored on draft-16 when +/// the id is a message parameter of that draft used on the wrong message, and a +/// `DecodeError::InvalidValue` otherwise (unknown id, or any unlisted id from draft-17 on). /// Duplicate parameters cause `DecodeError::Duplicate`, unless the type allows a repeat /// (see [`Param::param_repeat`]), such as `Vec`. /// @@ -485,7 +525,7 @@ macro_rules! encode_params { /// let subscriber_priority = subscriber_priority.unwrap_or(128); /// ``` macro_rules! decode_params { - ($r:expr, $version:expr, $($key:expr => $name:ident: $ty:ty),* $(,)?) => { + ($r:expr, $version:expr, $($key:expr => $name:ident: $ty:ty $(where $gate:expr)?),* $(,)?) => { #[allow(unused)] const _: () = { let _keys: &[u64] = &[$($key),*]; @@ -519,8 +559,10 @@ macro_rules! decode_params { // An if-chain rather than a `match`, so a key can be a named constant: // the macro captures it as an expression, which is not a legal pattern. + // A false `where` gate falls through, so the draft's ignore-or-close rule applies + // instead of validating a value the message is not allowed to carry. $( - if _key == $key { + if _key == $key $( && ($gate) )? { let _value = <$ty as $crate::ietf::Param>::param_decode($r, _version)?; $name = Some(match $name.take() { None => _value, @@ -529,6 +571,9 @@ macro_rules! decode_params { continue; } )* + if $crate::ietf::parameters::skip_unlisted($r, _version, _key)? { + continue; + } return Err($crate::coding::DecodeError::InvalidValue); } } @@ -1053,7 +1098,8 @@ mod tests { #[test] fn test_param_unknown_rejected() { - // Manually encode one param at key 0x10, try to decode expecting key 0x20 + // 0x3E is not a message parameter in any of these drafts. Draft-14 and draft-15 + // ignore it. Draft-16 on closes the session. for version in [ Version::Draft14, Version::Draft15, @@ -1064,19 +1110,54 @@ mod tests { let mut buf = Vec::new(); let mut w = Encoder::new(&mut buf, version.into()); w.varint(1).unwrap(); - w.varint(0x10u64).unwrap(); - true.param_encode(&mut w, version).unwrap(); + w.varint(0x3Eu64).unwrap(); + 1u64.param_encode(&mut w, version).unwrap(); let mut bytes = Decoder::new(&buf, version.into()); - let result: Result<(), DecodeError> = (|| { + let result: Result, DecodeError> = (|| { decode_params!(&mut bytes, version, 0x20 => val: Option); - let _ = val; - Ok(()) + Ok(val) })(); - assert!( - matches!(result, Err(DecodeError::InvalidValue)), - "expected InvalidValue for unknown param in {version}" - ); + match version { + Version::Draft14 | Version::Draft15 => { + assert_eq!(result.unwrap(), None, "{version} ignores an unrecognized parameter"); + assert!(bytes.is_empty(), "{version}"); + } + _ => assert!( + matches!(result, Err(DecodeError::InvalidValue)), + "expected InvalidValue for unknown param in {version}" + ), + } + } + } + + /// EXPIRES (0x08) is a draft-16 message parameter, but not for a message whose list is + /// only SUBSCRIBER_PRIORITY. Draft-16 ignores it and still reads the parameter after it. + /// Draft-18 closes the session on the same bytes. + #[test] + fn known_param_on_the_wrong_message() { + for version in [Version::Draft16, Version::Draft18] { + let mut buf = Vec::new(); + let mut w = Encoder::new(&mut buf, version.into()); + w.varint(2).unwrap(); + w.varint(0x08u64).unwrap(); + 5u64.param_encode(&mut w, version).unwrap(); + w.varint(0x18u64).unwrap(); // delta from 0x08 to 0x20 + 7u8.param_encode(&mut w, version).unwrap(); + + let mut bytes = Decoder::new(&buf, version.into()); + let result: Result, DecodeError> = (|| { + decode_params!(&mut bytes, version, 0x20 => val: Option); + Ok(val) + })(); + match version { + Version::Draft16 => { + assert_eq!(result.unwrap(), Some(7)); + assert!(bytes.is_empty()); + } + Version::Draft18 => assert!(matches!(result, Err(DecodeError::InvalidValue))), + _ => unreachable!(), + } } } diff --git a/rs/moq-net/src/ietf/publish.rs b/rs/moq-net/src/ietf/publish.rs index 4778ea4b569..843ddfedf4e 100644 --- a/rs/moq-net/src/ietf/publish.rs +++ b/rs/moq-net/src/ietf/publish.rs @@ -330,36 +330,26 @@ impl Message for Publish<'_> { }) } _ => { - // GROUP_ORDER is only legal here through draft-15, but keep accepting it so a - // peer that still sends it doesn't have its session torn down over a hint. + // GROUP_ORDER is legal on PUBLISH in draft-15 and again from draft-20, when + // the subscription parameters moved here. Draft-16 still knows the id, so one + // on this message is ignored. Draft-17 through draft-19 close the session. // - // Draft-20 moved the subscription parameters into PUBLISH, so they have to - // parse here even though reverse publishing is unsupported: an unlisted - // parameter fails the whole message, which would kill the session instead of - // letting the request reach its NOT_SUPPORTED response. + // The draft-20 parameters have to parse even though reverse publishing is + // unsupported: an unlisted parameter fails the whole message, which would + // kill the session instead of letting the request reach NOT_SUPPORTED. + // OBJECT_DELIVERY_TIMEOUT is also legal in draft-15. The values are dropped. decode_params!(r, version, - 0x02 => object_delivery_timeout: Option, + 0x02 => _object_delivery_timeout: Option where version == Version::Draft15 || Filter::is_draft20(version), 0x03 => _authorization_token: Vec, - 0x06 => subgroup_delivery_timeout: Option, + 0x06 => _subgroup_delivery_timeout: Option where Filter::is_draft20(version), 0x08 => _expires: Option, 0x09 => largest_location: Option, 0x10 => forward: Option, - 0x20 => subscriber_priority: Option, - 0x21 => filter: Option, - 0x22 => group_order: Option, + 0x20 => _subscriber_priority: Option where Filter::is_draft20(version), + 0x21 => _filter: Option where Filter::is_draft20(version), + 0x22 => group_order: Option where version == Version::Draft15 || Filter::is_draft20(version), ); - // The values are dropped: we refuse the PUBLISH itself, so the subscription - // settings it proposes never take effect. They still have to be consumed. - let subscription_params = [ - object_delivery_timeout.is_some(), - subgroup_delivery_timeout.is_some(), - subscriber_priority.is_some(), - filter.is_some(), - ]; - if subscription_params.contains(&true) && !Filter::is_draft20(version) { - return Err(DecodeError::InvalidValue); - } let mut properties = Properties::decode(r, version)?; properties.group_order = properties.group_order.or(group_order); @@ -416,11 +406,18 @@ impl Message for PublishOk { // PUBLISH and REQUEST_UPDATE now, so a PUBLISH_OK carries none of them. _ if Filter::is_draft20(version) => encode_params!(w, version,), _ => { + // GROUP_ORDER left PUBLISH_OK in draft-19. Sending it there is a parameter + // on the wrong message, which closes the session. + let group_order = matches!( + version, + Version::Draft15 | Version::Draft16 | Version::Draft17 | Version::Draft18 + ) + .then_some(self.group_order); encode_params!(w, version, 0x10 => self.forward, 0x20 => self.subscriber_priority, 0x21 => self.filter, - 0x22 => self.group_order, + 0x22 => group_order, ); } } @@ -454,13 +451,20 @@ impl Message for PublishOk { }) } _ => { - // EXPIRES is ignored, as in SUBSCRIBE_OK. + // EXPIRES is ignored, as in SUBSCRIBE_OK. Delivery timeouts and + // NEW_GROUP_REQUEST are legal on the drafts that list them and dropped: + // nothing here acts on a publisher's timeout. Draft-20 moved the + // subscription parameters off PUBLISH_OK, and draft-19 already dropped + // GROUP_ORDER. decode_params!(r, version, + 0x02 => _delivery_timeout: Option where !Filter::is_draft20(version), + 0x06 => _subgroup_delivery_timeout: Option where matches!(version, Version::Draft18 | Version::Draft19), 0x08 => _expires: Option, - 0x10 => forward: Option, - 0x20 => subscriber_priority: Option, - 0x21 => filter: Option, - 0x22 => group_order: Option, + 0x10 => forward: Option where !Filter::is_draft20(version), + 0x20 => subscriber_priority: Option where !Filter::is_draft20(version), + 0x21 => filter: Option where !Filter::is_draft20(version), + 0x22 => group_order: Option where matches!(version, Version::Draft15 | Version::Draft16 | Version::Draft17 | Version::Draft18), + 0x32 => _new_group_request: Option where matches!(version, Version::Draft16 | Version::Draft17 | Version::Draft18 | Version::Draft19), ); let forward = forward.unwrap_or(true); @@ -698,6 +702,58 @@ mod tests { assert_eq!(decoded.subscriber_priority, 128); } + fn publish_with_param(version: Version, key: u64, value: u64) -> Vec { + let mut buf = Vec::new(); + let w = &mut Encoder::new(&mut buf, version.into()); + RequestId(1).encode(w, version).unwrap(); + if version == Version::Draft17 { + w.varint(0).unwrap(); + } + encode_namespace(w, &Path::new("t")).unwrap(); + w.string("v").unwrap(); + w.varint(1).unwrap(); + w.varint(1).unwrap(); + w.varint(key).unwrap(); + w.varint(value).unwrap(); + buf + } + + fn publish_ok_with_param(version: Version, key: u64, value: u64) -> Vec { + let mut buf = Vec::new(); + let w = &mut Encoder::new(&mut buf, version.into()); + if matches!(version, Version::Draft14 | Version::Draft15 | Version::Draft16) { + RequestId(1).encode(w, version).unwrap(); + } + w.varint(1).unwrap(); + w.varint(key).unwrap(); + w.varint(value).unwrap(); + buf + } + + /// SUBSCRIBER_PRIORITY is known in draft-16 but not defined for PUBLISH until draft-20. + /// OBJECT_DELIVERY_TIMEOUT is legal on PUBLISH again from draft-20. + #[test] + fn publish_parameters_follow_the_draft() { + decode_message::(&publish_with_param(Version::Draft16, 0x20, 1), Version::Draft16) + .expect("draft-16 ignores SUBSCRIBER_PRIORITY"); + assert!(decode_message::(&publish_with_param(Version::Draft18, 0x20, 1), Version::Draft18).is_err()); + decode_message::(&publish_with_param(Version::Draft20, 0x02, 1), Version::Draft20) + .expect("draft-20 accepts OBJECT_DELIVERY_TIMEOUT"); + } + + /// PUBLISH_OK carries the delivery timeout through draft-19, and NEW_GROUP_REQUEST from + /// draft-16 through draft-19. Draft-20 no longer carries FORWARD. + #[test] + fn publish_ok_parameters_follow_the_draft() { + decode_message::(&publish_ok_with_param(Version::Draft19, 0x02, 1), Version::Draft19) + .expect("draft-19 accepts DELIVERY_TIMEOUT"); + decode_message::(&publish_ok_with_param(Version::Draft16, 0x32, 1), Version::Draft16) + .expect("draft-16 accepts NEW_GROUP_REQUEST"); + assert!( + decode_message::(&publish_ok_with_param(Version::Draft20, 0x10, 0), Version::Draft20).is_err() + ); + } + #[test] fn test_publish_done_registered_statuses_all_versions() { for version in [ diff --git a/rs/moq-net/src/ietf/request_stream.rs b/rs/moq-net/src/ietf/request_stream.rs index 2fd42913af1..de820911170 100644 --- a/rs/moq-net/src/ietf/request_stream.rs +++ b/rs/moq-net/src/ietf/request_stream.rs @@ -134,17 +134,17 @@ impl Update { decode_params!(data, version, 0x02 => object_timeout: Option, 0x03 => token: Vec, - 0x06 => subgroup_timeout: Option, + 0x06 => subgroup_timeout: Option where !matches!(version, Version::Draft14 | Version::Draft15 | Version::Draft16 | Version::Draft17), 0x10 => forward: Option, 0x20 => priority: Option, 0x21 => filter: Option, - 0x23 => fill: Option, - 0x25 => subgroup_filter: Vec, - 0x26 => object_filter: Vec, - 0x27 => priority_filter: Vec, - 0x28 => property_filter: Vec, - 0x29 => track_filter: Vec, - 0x32 => new_group: Option, + 0x23 => fill: Option where Filter::is_draft20(version), + 0x25 => subgroup_filter: Vec where super::subscribe::has_range_filters(version), + 0x26 => object_filter: Vec where super::subscribe::has_range_filters(version), + 0x27 => priority_filter: Vec where super::subscribe::has_range_filters(version), + 0x28 => property_filter: Vec where super::subscribe::has_range_filters(version), + 0x29 => track_filter: Vec where super::subscribe::has_range_filters(version), + 0x32 => new_group: Option where !matches!(version, Version::Draft14 | Version::Draft15), ); if !data.is_empty() { return Err(DecodeError::InvalidValue); @@ -179,3 +179,23 @@ impl Decode for Update { Self::decode_body(&mut data, version).map_err(DecodeError::complete) } } + +#[cfg(test)] +mod tests { + use super::*; + + /// TRACK_NAMESPACE_PREFIX (0x34) updates a SUBSCRIBE_NAMESPACE or SUBSCRIBE_TRACKS only, + /// so on a subscription's REQUEST_UPDATE it closes the session instead of being acked. + #[test] + fn namespace_prefix_on_subscription_update_closes() { + // Request ID 2, one parameter: TRACK_NAMESPACE_PREFIX, a one-field namespace "a". + let mut buf = bytes::Bytes::from_static(&[0x02, 0x01, 0x34, 0x01, 0x01, b'a']); + assert!(crate::coding::decode_buf(&mut buf, Version::Draft18, Update::decode_body).is_err()); + + // The same update with only a priority is accepted. + let mut buf = bytes::Bytes::from_static(&[0x02, 0x01, 0x20, 0x05]); + let update = crate::coding::decode_buf(&mut buf, Version::Draft18, Update::decode_body).unwrap(); + assert_eq!(update.priority, Some(5)); + assert!(!update.unsupported); + } +} diff --git a/rs/moq-net/src/ietf/subscribe.rs b/rs/moq-net/src/ietf/subscribe.rs index 19d4e764420..b1184b29888 100644 --- a/rs/moq-net/src/ietf/subscribe.rs +++ b/rs/moq-net/src/ietf/subscribe.rs @@ -91,19 +91,21 @@ impl Message for Subscribe<'_> { decode_params!(r, version, 0x02 => _object_delivery_timeout: Option, 0x03 => _authorization_token: Vec, - 0x04 => rendezvous_timeout: Option, - 0x06 => _subgroup_delivery_timeout: Option, + // 0x04 is MAX_CACHE_DURATION in draft-15 (a publisher parameter) and not a + // message parameter at all in draft-16. RENDEZVOUS_TIMEOUT arrives in draft-17. + 0x04 => rendezvous_timeout: Option where !matches!(version, Version::Draft15 | Version::Draft16), + 0x06 => _subgroup_delivery_timeout: Option where !matches!(version, Version::Draft14 | Version::Draft15 | Version::Draft16 | Version::Draft17), 0x10 => forward: Option, 0x20 => subscriber_priority: Option, 0x21 => filter: Option, 0x22 => group_order: Option, - 0x23 => fill: Option, - 0x25 => subgroup_filter: Vec, - 0x26 => object_id_filter: Vec, - 0x27 => priority_filter: Vec, - 0x28 => object_property_filter: Vec, - 0x32 => new_group_request: Option, - 0x35 => include_properties: Option, + 0x23 => fill: Option where Filter::is_draft20(version), + 0x25 => subgroup_filter: Vec where has_range_filters(version), + 0x26 => object_id_filter: Vec where has_range_filters(version), + 0x27 => priority_filter: Vec where has_range_filters(version), + 0x28 => object_property_filter: Vec where has_range_filters(version), + 0x32 => _new_group_request: Option where has_new_group_request(version), + 0x35 => include_properties: Option where Filter::is_draft20(version), ); let range_filters = [ @@ -115,25 +117,9 @@ impl Message for Subscribe<'_> { .iter() .any(|filter| !filter.is_empty()); - // An unknown message parameter is a protocol violation, so each stays rejected - // on the drafts that predate it rather than being quietly tolerated. - if ((fill.is_some() || include_properties.is_some()) && !Filter::is_draft20(version)) - || (range_filters && !has_range_filters(version)) - || (new_group_request.is_some() && !has_new_group_request(version)) - { - return Err(DecodeError::InvalidValue); - } - // Defaults to 1, so an absent parameter means the subscriber wants them. let properties_wanted = include_properties.unwrap_or(true); - // RENDEZVOUS_TIMEOUT arrived in draft-17; 0x04 means MAX_CACHE_DURATION in - // draft-15, which is a publisher parameter with no business in a SUBSCRIBE. - // An unknown message parameter is a protocol violation, so reject it there. - if rendezvous_timeout.is_some() && matches!(version, Version::Draft15 | Version::Draft16) { - return Err(DecodeError::InvalidValue); - } - // The value is deliberately dropped: we always answer a SUBSCRIBE with what is // published right now, which is the shorter timeout the draft lets a relay pick. // We still have to parse it, or the parameter alone would kill the session. @@ -293,16 +279,16 @@ impl Message for SubscribeOk { properties.max_cache_duration = Parameters::skip(r)?.map(std::time::Duration::from_millis); } _ => { - // GROUP_ORDER is only legal here through draft-15, but keep accepting it so a - // peer that still sends it doesn't have its session torn down over a hint. - // LARGEST_OBJECT is required on every draft once the track has content, so - // rejecting it would tear down a session over a parameter compliant - // publishers must send. EXPIRES is ignored, as on draft-14. + // GROUP_ORDER and MAX_CACHE_DURATION are legal here only in draft-15. Draft-16 + // still knows GROUP_ORDER, so one on this message is ignored. From draft-17 + // it closes the session. LARGEST_OBJECT is required once the track has + // content, so rejecting it would tear down a session over a parameter + // compliant publishers must send. EXPIRES is ignored, as on draft-14. decode_params!(r, version, - 0x04 => max_cache_duration: Option, + 0x04 => max_cache_duration: Option where version == Version::Draft15, 0x08 => _expires: Option, 0x09 => largest: Option, - 0x22 => group_order: Option, + 0x22 => group_order: Option where version == Version::Draft15, ); properties = Properties::decode(r, version)?; properties.group_order = properties.group_order.or(group_order); @@ -464,18 +450,12 @@ impl Message for SubscribeUpdate { decode_params!(r, version, 0x02 => _object_delivery_timeout: Option, 0x03 => _authorization_token: Vec, - 0x06 => _subgroup_delivery_timeout: Option, 0x10 => forward: Option, 0x20 => subscriber_priority: Option, 0x21 => _filter: Option, - 0x32 => new_group_request: Option, + 0x32 => _new_group_request: Option where has_new_group_request(version), ); - // NEW_GROUP_REQUEST arrived in draft-16. - if new_group_request.is_some() && !has_new_group_request(version) { - return Err(DecodeError::InvalidValue); - } - let subscriber_priority = subscriber_priority.unwrap_or(128); let forward = forward.unwrap_or(true); @@ -494,41 +474,24 @@ impl Message for SubscribeUpdate { if matches!(version, Version::Draft17) { let _required_request_id_delta = r.varint()?; } - // Nothing reads an update's Range Filters yet; they are consumed so a legal - // update does not fail the session. TRACK_PROPERTY_FILTER (0x29) is legal - // only on an update to SUBSCRIBE_TRACKS, which the message alone can't tell. + // Nothing reads an update's FILL_PARAMETERS or Range Filters yet; they are + // consumed so a legal update does not fail the session. decode_params!(r, version, 0x02 => _object_delivery_timeout: Option, 0x03 => _authorization_token: Vec, - 0x06 => _subgroup_delivery_timeout: Option, + 0x06 => _subgroup_delivery_timeout: Option where !matches!(version, Version::Draft14 | Version::Draft15 | Version::Draft16 | Version::Draft17), 0x10 => forward: Option, 0x20 => subscriber_priority: Option, 0x21 => _filter: Option, - 0x23 => fill: Option, - 0x25 => subgroup_filter: Vec, - 0x26 => object_id_filter: Vec, - 0x27 => priority_filter: Vec, - 0x28 => object_property_filter: Vec, - 0x29 => track_property_filter: Vec, + 0x23 => _fill: Option where Filter::is_draft20(version), + 0x25 => _subgroup_filter: Vec where has_range_filters(version), + 0x26 => _object_id_filter: Vec where has_range_filters(version), + 0x27 => _priority_filter: Vec where has_range_filters(version), + 0x28 => _object_property_filter: Vec where has_range_filters(version), + 0x29 => _track_property_filter: Vec where has_range_filters(version), 0x32 => _new_group_request: Option, ); - let range_filters = [ - subgroup_filter, - object_id_filter, - priority_filter, - object_property_filter, - track_property_filter, - ] - .iter() - .any(|filter| !filter.is_empty()); - - // FILL_PARAMETERS and the Range Filters postdate draft-17, so an earlier peer - // sending one is still the protocol violation it was. - if (fill.is_some() && !Filter::is_draft20(version)) || (range_filters && !has_range_filters(version)) { - return Err(DecodeError::InvalidValue); - } - let subscriber_priority = subscriber_priority.unwrap_or(128); let forward = forward.unwrap_or(true); @@ -645,14 +608,15 @@ mod tests { } } - /// 0x04 only means RENDEZVOUS_TIMEOUT from draft-17 on; in draft-15 it is - /// MAX_CACHE_DURATION, a publisher parameter that has no business in a SUBSCRIBE. + /// 0x04 is MAX_CACHE_DURATION in draft-15, defined for other messages, so a SUBSCRIBE + /// that carries it is ignored. Draft-16 dropped the id, which makes it unknown. #[test] - fn rendezvous_timeout_is_rejected_before_draft17() { - for version in [Version::Draft15, Version::Draft16] { - let encoded = subscribe_with_rendezvous(0, version); - decode_message::(&encoded, version).expect_err(&format!("{version} must reject parameter 0x04")); - } + fn cache_duration_on_subscribe_follows_the_draft() { + let draft15 = subscribe_with_rendezvous(0, Version::Draft15); + decode_message::(&draft15, Version::Draft15).expect("draft-15 ignores MAX_CACHE_DURATION"); + + let draft16 = subscribe_with_rendezvous(0, Version::Draft16); + decode_message::(&draft16, Version::Draft16).expect_err("draft-16 rejects unknown parameter 0x04"); } /// The first message parameter key on an encoded SUBSCRIBE, or `None` when it carries no @@ -1244,10 +1208,10 @@ mod tests { assert_eq!(decoded.properties.group_order, Some(GroupOrder::Descending)); } - /// We stopped sending the parameter, but a peer still sending it shouldn't lose its - /// session over a hint we ignore anyway. + /// GROUP_ORDER left SUBSCRIBE_OK after draft-15. Draft-18 closes the session when a + /// known parameter shows up on a message that does not define it. #[test] - fn test_subscribe_ok_v18_accepts_group_order_parameter() { + fn test_subscribe_ok_v18_rejects_group_order_parameter() { #[rustfmt::skip] let bytes = vec![ 42, // track alias @@ -1256,9 +1220,7 @@ mod tests { 0x02, // descending ]; - let decoded: SubscribeOk = decode_message(&bytes, Version::Draft18).unwrap(); - assert_eq!(decoded.track_alias, 42); - assert_eq!(decoded.properties.group_order, Some(GroupOrder::Descending)); + assert!(decode_message::(&bytes, Version::Draft18).is_err()); } /// Draft-18 removes the `required_request_id_delta` field (#1615), so the @@ -1431,13 +1393,21 @@ mod tests { } } + /// Draft-15 ignores a parameter it does not know. From draft-16 on, a parameter from + /// a later draft is unknown, which closes the session. + #[test] + fn subscribe_ignores_unrecognized_parameters_in_draft15() { + let body = subscribe_body(&[0x01, 0x32, 0x00]); + decode_message::(&body, Version::Draft15).expect("draft-15 ignores NEW_GROUP_REQUEST"); + } + /// A parameter from a later draft is still an unknown parameter, which the draft makes /// a protocol violation. #[test] fn subscribe_refuses_parameters_before_their_draft() { for (version, params) in [ - // NEW_GROUP_REQUEST arrived in draft-16. - (Version::Draft15, &[0x01, 0x32, 0x00][..]), + // SUBGROUP_DELIVERY_TIMEOUT arrived in draft-18, and 0x06 is not a draft-16 id. + (Version::Draft16, &[0x01, 0x06, 0x00][..]), // The Range Filters arrived in draft-19. (Version::Draft18, &[0x01, 0x25, 0x00][..]), // INCLUDE_PROPERTIES arrived in draft-20. @@ -1468,6 +1438,26 @@ mod tests { let decoded: SubscribeUpdate = decode_message(&body, Version::Draft20).unwrap(); assert_eq!(decoded.request_id, RequestId(2)); } + + /// SUBGROUP_DELIVERY_TIMEOUT (0x06) is not a draft-16 parameter. Draft-18 defines it + /// for SUBSCRIBE, and the value is dropped. + #[test] + fn subgroup_delivery_timeout_follows_its_draft() { + let head: &[u8] = &[ + 0x01, 0x01, 0x04, b'l', b'i', b'v', b'e', 0x05, b'v', b'i', b'd', b'e', b'o', + ]; + let body = [head, &[0x01, 0x06, 0x00][..]].concat(); + assert!(decode_message::(&body, Version::Draft16).is_err()); + decode_message::(&body, Version::Draft18).expect("draft-18 accepts SUBGROUP_DELIVERY_TIMEOUT"); + + // GROUP_ORDER (0x22) is known in draft-16 but not defined for REQUEST_UPDATE. + // Request ID, Subscription Request ID, one parameter, absolute key, varint value. + let update16: &[u8] = &[0x02, 0x0A, 0x01, 0x22, 0x02]; + decode_message::(update16, Version::Draft16).expect("draft-16 ignores GROUP_ORDER"); + // Draft-18 has no Subscription Request ID. The value is a uint8. + let update18: &[u8] = &[0x02, 0x01, 0x22, 0x02]; + assert!(decode_message::(update18, Version::Draft18).is_err()); + } } #[cfg(test)] diff --git a/rs/moq-net/src/ietf/subscribe_namespace.rs b/rs/moq-net/src/ietf/subscribe_namespace.rs index c5eb3e01db6..6bf2ff68a54 100644 --- a/rs/moq-net/src/ietf/subscribe_namespace.rs +++ b/rs/moq-net/src/ietf/subscribe_namespace.rs @@ -171,8 +171,13 @@ impl Message for SubscribeNamespaceLegacy<'_> { }; // The token is ignored: the session's grant is what authorizes the request. + // FORWARD is legal here in draft-15 through draft-17. The value is checked (only + // 0 or 1) and then dropped: a namespace subscription has no objects to forward. + // Draft-14 has no such parameter, so an unlisted id is ignored. Draft-18 moved + // FORWARD onto SUBSCRIBE_TRACKS and closes if it shows up on the new message. decode_params!(r, version, 0x03 => _authorization_token: Vec, + 0x10 => _forward: Option where matches!(version, Version::Draft15 | Version::Draft16 | Version::Draft17), HIDDEN_PARAM => hidden: Option, ); @@ -472,6 +477,45 @@ mod tests { ); } + /// FORWARD (0x10) is legal on SUBSCRIBE_NAMESPACE through draft-17. A value other + /// than 0 or 1 still closes the session. Draft-18 dropped it from this message. + #[test] + fn forward_follows_the_draft() { + fn with_forward(version: Version, value: u64) -> Vec { + let mut buf = Vec::new(); + let w = &mut Encoder::new(&mut buf, version.into()); + RequestId(1).encode(w, version).unwrap(); + if version == Version::Draft17 { + w.varint(0).unwrap(); + } + encode_namespace(w, &Path::default()).unwrap(); + if matches!(version, Version::Draft16 | Version::Draft17) { + w.varint(1).unwrap(); + } + // Count, absolute key, then the raw varint. encode_params! needs a Result. + w.varint(1).unwrap(); + w.varint(0x10).unwrap(); + w.varint(value).unwrap(); + buf + } + + for version in [Version::Draft15, Version::Draft16, Version::Draft17] { + let mut buf = bytes::Bytes::from(with_forward(version, 0)); + crate::coding::decode_buf(&mut buf, version, SubscribeNamespaceLegacy::decode_msg) + .unwrap_or_else(|err| panic!("{version} FORWARD=0: {err}")); + assert!(buf.is_empty(), "{version}"); + + let mut buf = bytes::Bytes::from(with_forward(version, 2)); + assert!( + crate::coding::decode_buf(&mut buf, version, SubscribeNamespaceLegacy::decode_msg).is_err(), + "{version} FORWARD=2" + ); + } + + let mut buf = bytes::Bytes::from(with_forward(Version::Draft18, 0)); + assert!(crate::coding::decode_buf(&mut buf, Version::Draft18, SubscribeNamespace::decode_msg).is_err()); + } + #[test] fn message_ids() { // 0x11 through draft-17 (legacy), renumbered to 0x50 in draft-18 (#1542).