diff --git a/c/moq/MIGRATE.md b/c/moq/MIGRATE.md index d80ff8da638..6851ca98b44 100644 --- a/c/moq/MIGRATE.md +++ b/c/moq/MIGRATE.md @@ -6,6 +6,10 @@ How to update your code for each breaking change, newest first. The hand-written crate never published as moq-c: a libmoq 0.6 user applies these entries before moving to the generated API. +### Paths with an empty segment are refused ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +`moq_origin_create_broadcast`, `moq_origin_requests`, `moq_origin_announced_broadcast`, and `moq_origin_request` return an error for a path with a leading, trailing, or doubled `/` instead of collapsing it. Pass the canonical path, such as `room/cam`. The generated API refuses the same paths. + ### Session allocator ([#6751](https://github.com/moq-dev/moq/pull/6751)) `moq_session_bandwidth` is `moq_session_allocator`, `moq_bandwidth_reserve` is `moq_allocator_reserve`, and `moq_bandwidth_close` is `moq_allocator_close`, matching `moq_net::bandwidth::Allocator`. `moq_reservation_grant` is `moq_reservation_rate`, matching `moq_net::bandwidth::Reservation::rate()`. Signatures are unchanged. The receive estimate keeps its names (`moq_session_recv_bandwidth`, `moq_bandwidth_consumer_*`). diff --git a/cpp/moq/MIGRATE.md b/cpp/moq/MIGRATE.md index 6ca4ebcf703..c3f53f138ba 100644 --- a/cpp/moq/MIGRATE.md +++ b/cpp/moq/MIGRATE.md @@ -2,6 +2,10 @@ ## Unreleased +### Paths with an empty segment are refused ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +Publishing, serving, or requesting a broadcast at a path with a leading, trailing, or doubled `/` fails instead of collapsing it. Pass the canonical path, such as `room/cam`. + ### First release There is no published C++ package to migrate from. Start with the current headers and examples, and build the wrapper, generated bindings, and native library together. diff --git a/cpp/obs/MIGRATE.md b/cpp/obs/MIGRATE.md index afbac495f82..7c7ba986809 100644 --- a/cpp/obs/MIGRATE.md +++ b/cpp/obs/MIGRATE.md @@ -4,6 +4,10 @@ How to update your OBS setup for each breaking change, newest first. ## Unreleased +### Broadcast names with an empty segment are refused ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +A broadcast name with a leading, trailing, or doubled `/` fails to publish or play instead of being collapsed. Use the canonical name, such as `room/cam`. + Connection state and first-dial failures now arrive through one lifecycle watcher. UI reads no longer poll the Client or Session for failure separately. ### The audio-only and video-only outputs are gone (#6762) diff --git a/dart/moq/MIGRATE.md b/dart/moq/MIGRATE.md index 5cc86e51b4c..6dbf79168f8 100644 --- a/dart/moq/MIGRATE.md +++ b/dart/moq/MIGRATE.md @@ -4,6 +4,10 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Paths with an empty segment are refused ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +Publishing, serving, or requesting a broadcast at a path with a leading, trailing, or doubled `/` throws instead of collapsing it. Pass the canonical path, such as `room/cam`. + ### Request handlers are `requests()` ([#6752](https://github.com/moq-dev/moq/pull/6752)) Rename `dynamic_()` on `OriginProducer`, `BroadcastProducer`, `TrackProducer`, and `TrackRequest` to `requests()`. diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index 49bed2e6454..23d6e26371a 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -29,6 +29,11 @@ carries [publisher epochs](#publisher-epochs). | **Frame** | A sized chunk of bytes. | Object | | **Datagram** | One unreliable frame sent as a QUIC datagram instead of a group. | Datagram | +A broadcast is named by a path: non-empty segments joined by `/`, such as +`room/alice`. A path never starts or ends with `/` or doubles one, so each name +has one spelling, and every API and peer refuses one that does. The empty path +is the root, which prefixes every path. + ## Discovery A session can ask for announcements matching a path prefix. The peer replies diff --git a/doc/concept/standard.md b/doc/concept/standard.md index 780465acb3b..c5de148b361 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -31,7 +31,7 @@ subset drops. What a peer actually observes against this implementation: - **History is fetched in ranges.** In Rust, Object ID gaps must be marked by Prior Object ID Gap properties for a group to be fetchable. A `FETCH` returns a range of groups from the cache, which a relay fills upstream with one `FETCH` per run of missing groups, or on draft 18 the saved prefix of the group a new subscription just joined. The range ends at Largest Object's group and reads it whole, so a `FETCH` of a group still being written waits for it to finish. That departs from draft 22's cap at Largest Object: a cache cannot know an open group's real Largest Object, and a cut group would be cached downstream as complete. On draft 18, `FETCH_OK` End Object 0 covers the entire End Group, matching the request; Rust also accepts the legacy answer naming the next group with Object 0. On draft 22, `FETCH_OK`'s End Location is the requested end, capped at Largest Object's group: a range holding no objects gets an empty fetch stream, and only a start past Largest Object is refused, with `INVALID_RANGE`. A Rust relay retains the upstream Largest Object from `TRACK_STATUS`, so even a fetch-only copy can resolve a draft 22 `LOCATION_FILTER` relative to it; without a publisher report or live feed, it refuses that filter as `NOT_SUPPORTED`. JavaScript publishing refuses every `FETCH`. Datagrams are never fetchable. A Rust reader that only fetches learns the track from `TRACK_STATUS` instead of subscribing, so a finished track stays fetchable; a reader whose publisher refuses `TRACK_STATUS` as `NOT_SUPPORTED` still subscribes. - **Compression.** Rust peers exchange compressed tracks when both offer the [Flate extension](/draft/moq-flate). Other peers receive one shared inflated copy. A publisher that seals the compression declaration makes that fallback unavailable: an incompatible peer is refused, preserving the sealed metadata. JavaScript does not offer the IETF extension. - **Timing.** A track whose `SUBSCRIBE_OK` declares no `TIMESCALE` is untimed. A Rust reader that never subscribed takes the units from `TRACK_STATUS`. -- **Strict SETUP and parameters.** Repeated unknown `SETUP` options, GREASE included, are accepted if well-formed; a repeated known option is rejected. A `GROUP_ORDER` other than Ascending (1) or Descending (2) closes the session. An unknown message parameter, or one defined only for another message, closes the session. Other malformed control input closes the session with the draft's code too, such as a server `SETUP` carrying `PATH` or `AUTHORITY`, a request cut short by FIN, or a second `GOAWAY` on one stream; a first `GOAWAY` on a request stream is legal. +- **Strict SETUP and parameters.** Repeated unknown `SETUP` options, GREASE included, are accepted if well-formed; a repeated known option is rejected. A `GROUP_ORDER` other than Ascending (1) or Descending (2) closes the session. An unknown message parameter, or one defined only for another message, closes the session. Other malformed control input closes the session with the draft's code too, such as a server `SETUP` carrying `PATH` or `AUTHORITY`, a namespace field ending in `/` (it would map to a path with an empty segment), a request cut short by FIN, or a second `GOAWAY` on one stream; a first `GOAWAY` on a request stream is legal. - **One `SETUP` credential**, forwarded to the [auth server](/bin/relay/auth#the-contract) unverified. A second `SETUP` token closes the session. We advertise no token cache, so an alias reference in `SETUP` closes it with `PROTOCOL_VIOLATION`, one on a request with `UNKNOWN_AUTH_TOKEN_ALIAS`, and a registration on a request with `AUTH_TOKEN_CACHE_OVERFLOW`. Where the Rust side authorizes the peer or verifies tokens, a token on a `SUBSCRIBE` or `PUBLISH_NAMESPACE` is verified like an in-band session token and authorizes that one request alone, even where that authorization already covers it; it never widens the session. It is refused with `NOT_SUPPORTED` when nothing verifies it, when it is empty, or when its Token Type is anything but 0 (out of band). A side that does neither, JavaScript included, ignores the token. Rust refuses a request carrying more than one distinct token with `NOT_SUPPORTED`, since it verifies one; JavaScript, which verifies none, ignores them all. A token-authorized `SUBSCRIBE` still stays within the grant the peer gave us. A token-authorized `PUBLISH_NAMESPACE` needs a grant covering everything beneath the namespace, and the tracks we subscribe under it rely on that announce instead of our limit, ending with it. An update renews the token, and a refused renewal ends only that request. Rust and JavaScript send the token a route names (`Route::auth`) on its `PUBLISH_NAMESPACE`, and swapping it renews the request with `REQUEST_UPDATE`; the inline `NAMESPACE` that answers a solicited `SUBSCRIBE_NAMESPACE` carries no token. A token added to a reconnecting connection's auth handle rides every session it makes, so the announcement keeps it across a reconnect; one added to a single session's handle ends with that session. A token on any other request is decoded and grants nothing, and a request repeating one is refused with `MALFORMED_AUTH_TOKEN`. The negotiated [MoQ Auth extension](/concept/moq-lite#authorization) accepts additional session tokens in band. - **Pause delivery.** `FORWARD=0` on a `SUBSCRIBE` or its update drops the subscription, closing its data streams and holding nothing upstream, while the request and its slot stay. `FORWARD=1` subscribes again as a new `SUBSCRIBE` with the same filter would, so nothing carries across the pause: its `REQUEST_OK` names the live edge it joined at. A paused update may change only priority and max age. - **Refused, not fatal.** A legal request this stack does not serve is rejected on its own and the session stays up: the fetch forms above get `NOT_SUPPORTED`. Range Filters get `INVALID_FILTER` on draft 22 because the advertised filter limit is zero. On draft 22, namespaces whose first tuple field is exactly `.` or `.session` get `DOES_NOT_EXIST` before application lookup; other dot prefixes still reach the application. Subscription updates accept priority, negotiated subscriber max age, and delivery timeouts. Rust also carries range changes, keeps bounded subscriptions updatable, renews request tokens, and judges `NEW_GROUP_REQUEST` as a join (see dynamic groups below). On draft 22, widening fills cached history on a separate fetch stream, completing earlier groups and handing the newest head to its live subgroup tail; fills follow the requested group order. Other supported-but-unimplemented preferences end that subscription; a `TRACK_PROPERTY_FILTER` there closes the session with `PROTOCOL_VIOLATION`, since it belongs only to `SUBSCRIBE_TRACKS`. A namespace update may change its prefix; suffixes after the acknowledgement are relative to that new prefix. diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 926eaa10ef5..09ef864b9fc 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -131,6 +131,7 @@ Any broadcasts and subscriptions are transparently proxied by the CDN behind the ## Broadcast A Broadcast is a collection of Tracks named by a path. This corresponds to a MoqTransport's "track namespace". +A path is a sequence of non-empty segments joined by `/` (0x2F), so it never starts or ends with `/` or contains two in a row; the empty path has no segments and prefixes every path. A publisher instance is identified by an Epoch, carried on its routes (see [ANNOUNCE_START](#announce-start)): a path and an Epoch name one Broadcast, and a publisher MUST NOT serve different content under the same pair. A restarted publisher mints a new Epoch rather than reusing the path's old one, while replicas of the same content MAY share one. A route without an Epoch names no instance: nothing says another route serves the same bytes. @@ -1829,6 +1830,8 @@ The `Message Length` describes the payload size on the wire. - A session without a negotiated subprotocol negotiates the version in a draft-14 CLIENT_SETUP and SERVER_SETUP exchange. Implementations apply this to moq-lite-03 through moq-lite-06 too. +- A path never has an empty segment, so a leading, trailing, or doubled `/` is a PROTOCOL_VIOLATION rather than a second spelling of the same path. + - Added the SETUP `Implementation` parameter (0x6): the sender's software as `name/version` product tokens. Earlier versions with a Setup Stream ignore it as an unknown parameter. - Hop IDs span 64 bits; a relay sends 0 in place of one an earlier version's varint cannot carry. diff --git a/go/wrapper/MIGRATE.md b/go/wrapper/MIGRATE.md index 1f47be3393c..32cc37e56f7 100644 --- a/go/wrapper/MIGRATE.md +++ b/go/wrapper/MIGRATE.md @@ -4,6 +4,10 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Paths with an empty segment are refused ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +Publishing, serving, or requesting a broadcast at a path with a leading, trailing, or doubled `/` returns an error instead of collapsing it. Pass the canonical path, such as `room/cam`. + ### Request handlers are `Requests()` ([#6752](https://github.com/moq-dev/moq/pull/6752)) Rename `Dynamic()` on `OriginProducer`, `BroadcastProducer`, `TrackProducer`, and `TrackRequest` to `Requests()`. diff --git a/js/moq-boy/src/element.test.ts b/js/moq-boy/src/element.test.ts new file mode 100644 index 00000000000..9264c361763 --- /dev/null +++ b/js/moq-boy/src/element.test.ts @@ -0,0 +1,52 @@ +import { expect, mock, test } from "bun:test"; + +// The UI renders into a shadow root, which the test runner has no DOM for. +mock.module("solid-js/web", () => ({ render: () => () => {} })); +mock.module("./ui/element.tsx", () => ({ BoyUI: () => null })); + +const registry = new Map(); +globalThis.HTMLElement = class { + attachShadow() { + return {}; + } +} as unknown as typeof HTMLElement; +globalThis.customElements = { + define: (name: string, ctor: CustomElementConstructor) => { + registry.set(name, ctor); + }, + get: (name: string) => registry.get(name), +} as unknown as CustomElementRegistry; + +const { default: MoqBoy } = await import("./element"); + +// Effects rerun in a later task, so each check waits one out. +const settle = () => new Promise((resolve) => setTimeout(resolve, 0)); + +// A refused prefix idles the element rather than discovering games under a default prefix. +for (const attribute of ["prefix", "prefix-game", "prefix-viewer"] as const) { + test(`an invalid ${attribute} idles the element until a valid one arrives`, async () => { + const el = new MoqBoy(); + try { + // Invalid markup, parsed before the element connects. + expect(() => el.attributeChangedCallback(attribute, null, "/boy")).toThrow(); + el.connectedCallback(); + await settle(); + expect(el.connection.enabled.peek()).toBe(false); + + el.attributeChangedCallback(attribute, "/boy", "boy"); + await settle(); + expect(el.connection.enabled.peek()).toBe(true); + + expect(() => el.attributeChangedCallback(attribute, "boy", "boy//x")).toThrow(); + await settle(); + expect(el.connection.enabled.peek()).toBe(false); + + // Removing the attribute restores the default, which is valid. + el.attributeChangedCallback(attribute, "boy//x", null); + await settle(); + expect(el.connection.enabled.peek()).toBe(true); + } finally { + el.disconnectedCallback(); + } + }); +} diff --git a/js/moq-boy/src/element.tsx b/js/moq-boy/src/element.tsx index 26580a43482..5e12f823236 100644 --- a/js/moq-boy/src/element.tsx +++ b/js/moq-boy/src/element.tsx @@ -14,8 +14,9 @@ import { BoyUI } from "./ui/element.tsx"; /** The attributes the element observes. */ const OBSERVED = ["url", "prefix", "prefix-game", "prefix-viewer"] as const; +type Observed = (typeof OBSERVED)[number]; -const DEFAULT_PREFIX = "boy"; +const DEFAULT_PREFIX = Moq.Path.from("boy"); const cleanup = new FinalizationRegistry((signals) => signals.close()); @@ -44,10 +45,15 @@ export default class MoqBoy extends HTMLElement { readonly games = new Moq.Signals.Signal>(new Map()); readonly #signals = new Moq.Signals.Effect(); + // Set when the element is connected to the DOM. + readonly #connected = new Moq.Signals.Signal(false); + // The path attributes holding an invalid path, which idle the element until each is fixed. + readonly #refused = new Moq.Signals.Signal>(new Set()); + // Whether to run: connected, with nothing refused. readonly #enabled = new Moq.Signals.Signal(false); - readonly #prefix = new Moq.Signals.Signal(DEFAULT_PREFIX); - readonly #gamePrefixOverride = new Moq.Signals.Signal(undefined); - readonly #viewerPrefixOverride = new Moq.Signals.Signal(undefined); + readonly #prefix = new Moq.Signals.Signal(DEFAULT_PREFIX); + readonly #gamePrefixOverride = new Moq.Signals.Signal(undefined); + readonly #viewerPrefixOverride = new Moq.Signals.Signal(undefined); readonly #sessions = new Map(); #dispose?: () => void; @@ -55,6 +61,10 @@ export default class MoqBoy extends HTMLElement { super(); cleanup.register(this, this.#signals); + this.#signals.run((effect) => { + this.#enabled.set(effect.get(this.#connected) && effect.get(this.#refused).size === 0); + }); + // One origin, both directions: viewer broadcasts are published into it and the // relay's announced games arrive in it, with no risk of echoing either back. this.connection = new Moq.Connection({ @@ -72,7 +82,7 @@ export default class MoqBoy extends HTMLElement { /** Called by the browser when the element joins a document. */ connectedCallback() { - this.#enabled.set(true); + this.#connected.set(true); // Render the UI into a Shadow DOM (reuse existing on reconnect). const shadow = this.shadowRoot ?? this.attachShadow({ mode: "open" }); @@ -81,29 +91,39 @@ export default class MoqBoy extends HTMLElement { /** Called by the browser when the element leaves its document. */ disconnectedCallback() { - this.#enabled.set(false); + this.#connected.set(false); this.#dispose?.(); this.#dispose = undefined; } /** Called by the browser when an observed attribute changes. */ attributeChangedCallback(name: (typeof OBSERVED)[number], _oldValue: string | null, newValue: string | null) { - switch (name) { - case "url": - this.connection.url.set(newValue ? new URL(newValue) : undefined); - break; - case "prefix": - this.#prefix.set(newValue ?? DEFAULT_PREFIX); - break; - case "prefix-game": - this.#gamePrefixOverride.set(newValue ?? undefined); - break; - case "prefix-viewer": - this.#viewerPrefixOverride.set(newValue ?? undefined); - break; + if (name === "url") { + this.connection.url.set(newValue ? new URL(newValue) : undefined); + return; + } + + // Refused until parsed, so an invalid path leaves the element idle and the error rethrown. + this.#refuse(name, true); + const path = newValue === null ? undefined : Moq.Path.from(newValue); + this.#refuse(name, false); + + if (name === "prefix") { + this.#prefix.set(path ?? DEFAULT_PREFIX); + } else if (name === "prefix-game") { + this.#gamePrefixOverride.set(path); + } else { + this.#viewerPrefixOverride.set(path); } } + #refuse(name: Observed, refused: boolean) { + const next = new Set(this.#refused.peek()); + if (refused) next.add(name); + else next.delete(name); + this.#refused.set(next); + } + /** The relay URL, mirrored by the `url` attribute. */ get url(): URL | undefined { return this.connection.url.peek(); @@ -119,36 +139,38 @@ export default class MoqBoy extends HTMLElement { } set prefixPath(value: string) { - this.#prefix.set(value); + this.#prefix.set(Moq.Path.from(value)); + this.#refuse("prefix", false); } /** The path prefix for game broadcasts, mirrored by the `prefix-game` attribute. */ get prefixGame(): string { - return this.#gamePrefixOverride.peek() ?? `${this.#prefix.peek()}/game`; + return this.#gamePrefixOverride.peek() ?? Moq.Path.from(this.#prefix.peek(), "game"); } set prefixGame(value: string) { - this.#gamePrefixOverride.set(value); + this.#gamePrefixOverride.set(Moq.Path.from(value)); + this.#refuse("prefix-game", false); } /** The path prefix for viewer broadcasts, mirrored by the `prefix-viewer` attribute. */ get prefixViewer(): string { - return this.#viewerPrefixOverride.peek() ?? `${this.#prefix.peek()}/viewer`; + return this.#viewerPrefixOverride.peek() ?? Moq.Path.from(this.#prefix.peek(), "viewer"); } set prefixViewer(value: string) { - this.#viewerPrefixOverride.set(value); + this.#viewerPrefixOverride.set(Moq.Path.from(value)); + this.#refuse("prefix-viewer", false); } #runDiscovery(effect: Moq.Signals.Effect) { const base = effect.get(this.#prefix); - const gamePrefix = effect.get(this.#gamePrefixOverride) ?? `${base}/game`; - const viewerPrefix = effect.get(this.#viewerPrefixOverride) ?? `${base}/viewer`; - const prefix = Moq.Path.from(gamePrefix); + const gamePrefix = effect.get(this.#gamePrefixOverride) ?? Moq.Path.from(base, "game"); + const viewerPrefix = effect.get(this.#viewerPrefixOverride) ?? Moq.Path.from(base, "viewer"); // The origin's stream spans reconnects: entries retract when the session dies and // return when the next one re-announces them, so this loop never needs to restart. - const announced = this.origin.consume().announced(Moq.Path.Pattern.parse("*").rooted(prefix)); + const announced = this.origin.consume().announced(Moq.Path.Pattern.parse("*").rooted(gamePrefix)); effect.cleanup(() => announced.close()); effect.spawn(async () => { diff --git a/js/moq-boy/src/game.ts b/js/moq-boy/src/game.ts index a70a0896ed4..a2020261d1b 100644 --- a/js/moq-boy/src/game.ts +++ b/js/moq-boy/src/game.ts @@ -130,7 +130,7 @@ export class Game { // Video pipeline. this.broadcast = new Watch.Broadcast({ origin: config.origin, - name: Moq.Path.from(`${gamePrefix}/${sessionId}`), + name: Moq.Path.from(gamePrefix, sessionId), enabled: true, }); this.#signals.cleanup(() => this.broadcast.close()); @@ -318,7 +318,7 @@ export class Game { this.viewerId.set(viewerId); const viewerBroadcastPublication = origin.createBroadcast( - Moq.Path.from(`${this.#viewerPrefix}/${this.sessionId}/${viewerId}`), + Moq.Path.from(this.#viewerPrefix, this.sessionId, viewerId), ); const viewerBroadcast = viewerBroadcastPublication.broadcast; viewerBroadcastPublication.announce({ epoch: Moq.Epoch.mint() }); diff --git a/js/net/MIGRATE.md b/js/net/MIGRATE.md index a3ac5bd6ba4..e43e6f21b6a 100644 --- a/js/net/MIGRATE.md +++ b/js/net/MIGRATE.md @@ -4,6 +4,11 @@ How to update your code for each breaking change, newest first. ## Unreleased +### `Path.from` refuses empty segments ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +`Path.from` throws on a component with a leading, trailing, or doubled `/` instead of collapsing it, so a path has one spelling, and `Path.decode` refuses one off the wire. A moq-transport namespace field ending in `/`, which escapes to an empty segment, is refused too. +Pass canonical components: `Path.from("room/cam")` or `Path.from(prefix, "cam")`, where an empty component is the root, rather than ``Path.from(`${prefix}/cam`)`` or `Path.from("room/")`. + ### moq-transport drafts 14 through 17 are removed ([#6753](https://github.com/moq-dev/moq/pull/6753)) The `"moq-transport-14"` through `"moq-transport-17"` version names now throw a `TypeError`, and a peer offering only those versions fails the handshake. @@ -54,7 +59,7 @@ Take the length of `Varint.encodeLeadingOnes(v)`. Broadcast creation and route publication now refuse literal `.` and `..` segments, including segments in the producer's root. Resolve relative references before publishing, or rename broadcasts that used these segments. -Path normalization, relative references, and subscription paths retain their semantics. +Relative references and subscription paths retain their semantics. ### A listing says whether a removed track finished (#6399) diff --git a/js/net/src/ietf/ietf.test.ts b/js/net/src/ietf/ietf.test.ts index d927fe27788..cb4e1b8b693 100644 --- a/js/net/src/ietf/ietf.test.ts +++ b/js/net/src/ietf/ietf.test.ts @@ -435,12 +435,17 @@ test("Namespace: literal backslash round trips", async () => { expect(await encodeNamespace(decoded)).toEqual(tuple); }); -test("Namespace: slashes at tuple part boundaries round trip", async () => { - const tuple = await encodeNamespaceTuple(["/foo", "bar/", "/baz/"]); +test("Namespace: slashes at tuple part boundaries", async () => { + // A leading slash escapes to a non-empty `\` segment. + const tuple = await encodeNamespaceTuple(["/foo", "/bar"]); const decoded = await decodeNamespace(tuple); - - expect(decoded).toBe("\\/foo/bar\\//\\/baz\\/" as Path.Valid); + expect(decoded).toBe("\\/foo/\\/bar" as Path.Valid); expect(await encodeNamespace(decoded)).toEqual(tuple); + + // A trailing slash escapes to an empty segment, which every later path operation assumes away. + for (const fields of [["/foo", "bar/", "/baz/"], ["foo/", "bar"], ["/"], ["a//"]]) { + await expect(decodeNamespace(await encodeNamespaceTuple(fields))).rejects.toThrow(ProtocolViolation); + } }); test("Namespace: Subscribe with empty namespace round trip", async () => { diff --git a/js/net/src/ietf/namespace.test.ts b/js/net/src/ietf/namespace.test.ts index 9e89aefb4c1..fbee61d46ee 100644 --- a/js/net/src/ietf/namespace.test.ts +++ b/js/net/src/ietf/namespace.test.ts @@ -9,7 +9,6 @@ test("reserved namespaces preserve tuple fields and draft boundaries", () => { [[".session"], true], [[".session", "extension"], true], [[".session/extension"], false], - [["", ".session"], false], [[".application"], false], [[".sessionish"], false], ] as [string[], boolean][]) { diff --git a/js/net/src/ietf/namespace.ts b/js/net/src/ietf/namespace.ts index 44a4314346d..4a5d73906ee 100644 --- a/js/net/src/ietf/namespace.ts +++ b/js/net/src/ietf/namespace.ts @@ -22,9 +22,18 @@ function toTuple(namespace: Path.Valid): string[] { return parts; } -/** Convert IETF namespace tuple fields into the escaped moq-net path representation. */ +/** + * Convert IETF namespace tuple fields into the escaped moq-net path representation. + * + * Throws when a field ends in `/`, which escapes to a path with an empty segment. + */ export function fromTuple(parts: string[]): Path.Valid { - return parts.map((part) => part.replaceAll("\\", "\\\\").replaceAll("/", "\\/")).join("/") as Path.Valid; + const escaped = parts.map((part) => part.replaceAll("\\", "\\\\").replaceAll("/", "\\/")).join("/"); + try { + return Path.from(escaped); + } catch (cause) { + throw new ProtocolViolation("namespace field escapes to an empty path segment", { cause }); + } } /** Encode a moq-net namespace as an IETF namespace tuple. */ diff --git a/js/net/src/lite/announce.test.ts b/js/net/src/lite/announce.test.ts index 84d4ce421d4..d3d6e2a4d5a 100644 --- a/js/net/src/lite/announce.test.ts +++ b/js/net/src/lite/announce.test.ts @@ -171,14 +171,14 @@ async function requestRoundTrip(msg: AnnounceRequest, version: Version): Promise // Draft04/05 carry the subscriber's Hop ID so the publisher can skip reflected // announces before they hit the wire. test("AnnounceRequest carries excludeHop on draft-05", async () => { - const got = await requestRoundTrip(new AnnounceRequest(Path.from("room/"), HopSchema.parse(42n)), Version.DRAFT_05); + const got = await requestRoundTrip(new AnnounceRequest(Path.from("room"), HopSchema.parse(42n)), Version.DRAFT_05); expect(got.excludeHop).toBe(HopSchema.parse(42n)); }); // Draft06 dropped the field: the receiver's reflected-announce check catches the same // loops, so a value set locally is simply not sent and decodes as zero. test("AnnounceRequest drops excludeHop on draft-06", async () => { - const msg = new AnnounceRequest(Path.from("room/"), HopSchema.parse(42n)); + const msg = new AnnounceRequest(Path.from("room"), HopSchema.parse(42n)); const got = await requestRoundTrip(msg, Version.DRAFT_06); expect(got.excludeHop).toBe(UNKNOWN_HOP); @@ -190,7 +190,7 @@ test("AnnounceRequest drops excludeHop on draft-06", async () => { // Draft07 carries the hidden opt-in; every earlier version decodes as not opted in. test("AnnounceRequest carries hidden from draft-07", async () => { for (const hidden of [false, true]) { - const msg = new AnnounceRequest(Path.from("room/"), UNKNOWN_HOP, hidden); + const msg = new AnnounceRequest(Path.from("room"), UNKNOWN_HOP, hidden); expect((await requestRoundTrip(msg, Version.DRAFT_07)).hidden).toBe(hidden); expect((await requestRoundTrip(msg, Version.DRAFT_06)).hidden).toBe(false); } diff --git a/js/net/src/path.test.ts b/js/net/src/path.test.ts index 1e9f8e4dc48..49f68048425 100644 --- a/js/net/src/path.test.ts +++ b/js/net/src/path.test.ts @@ -4,16 +4,17 @@ import * as Path from "./path.ts"; /** Brand a literal as a relative reference; the tests feed raw strings on purpose. */ const asRel = (s: string) => s as Path.Relative; -test("Path constructor trims leading and trailing slashes", () => { - expect(Path.from("/foo/bar/")).toBe("foo/bar" as Path.Valid); - expect(Path.from("///foo/bar///")).toBe("foo/bar" as Path.Valid); +test("from refuses empty segments", () => { + for (const raw of ["/foo", "foo/", "/foo/bar/", "foo//bar", "/", "//"]) { + expect(() => Path.from(raw)).toThrow("empty segment"); + expect(() => Path.from("ok", raw)).toThrow("empty segment"); + } expect(Path.from("foo/bar")).toBe("foo/bar" as Path.Valid); }); -test("Path constructor handles empty paths", () => { - expect(Path.from("")).toBe("" as Path.Valid); - expect(Path.from("/")).toBe("" as Path.Valid); - expect(Path.from("///")).toBe("" as Path.Valid); +test("from treats the empty path as the root", () => { + expect(Path.from("")).toBe(Path.empty()); + expect(Path.from()).toBe(Path.empty()); }); test("hasPrefix matches exact paths", () => { @@ -40,12 +41,6 @@ test("hasPrefix handles empty prefix", () => { expect(Path.hasPrefix(Path.empty(), path)).toBe(true); }); -test("hasPrefix ignores trailing slashes in prefix", () => { - const path = Path.from("foo/bar"); - expect(Path.hasPrefix(Path.from("foo/"), path)).toBe(true); - expect(Path.hasPrefix(Path.from("foo/bar/"), path)).toBe(true); -}); - test("stripPrefix strips valid prefixes", () => { const path = Path.from("foo/bar/baz"); @@ -111,49 +106,6 @@ test("join handles multiple joins", () => { expect(path).toBe("api/v1/users/123" as Path.Valid); }); -test("equals checks correctly", () => { - const path1 = Path.from("foo/bar"); - const path2 = Path.from("/foo/bar/"); - const path3 = Path.from("foo/baz"); - - expect(path1 === path2).toBe(true); - expect(path1 === path3).toBe(false); -}); - -test("handles paths with multiple consecutive slashes", () => { - const path = Path.from("foo//bar///baz"); - // Multiple consecutive slashes are collapsed to single slashes - expect(path).toBe("foo/bar/baz" as Path.Valid); -}); - -test("removes multiple slashes comprehensively", () => { - // Test various multiple slash scenarios - expect(Path.from("foo//bar")).toBe("foo/bar" as Path.Valid); - expect(Path.from("foo///bar")).toBe("foo/bar" as Path.Valid); - expect(Path.from("foo////bar")).toBe("foo/bar" as Path.Valid); - - // Multiple occurrences of double slashes - expect(Path.from("foo//bar//baz")).toBe("foo/bar/baz" as Path.Valid); - expect(Path.from("a//b//c//d")).toBe("a/b/c/d" as Path.Valid); - - // Mixed slash counts - expect(Path.from("foo//bar///baz////qux")).toBe("foo/bar/baz/qux" as Path.Valid); - - // With leading and trailing slashes - expect(Path.from("//foo//bar//")).toBe("foo/bar" as Path.Valid); - expect(Path.from("///foo///bar///")).toBe("foo/bar" as Path.Valid); - - // Edge case: only slashes - expect(Path.from("//")).toBe("" as Path.Valid); - expect(Path.from("////")).toBe("" as Path.Valid); - - // Test that operations work correctly with normalized paths - const pathWithSlashes = Path.from("foo//bar///baz"); - expect(Path.hasPrefix(Path.from("foo/bar"), pathWithSlashes)).toBe(true); - expect(Path.stripPrefix(Path.from("foo"), pathWithSlashes)).toBe("bar/baz" as Path.Valid); - expect(Path.join(pathWithSlashes, Path.from("qux"))).toBe("foo/bar/baz/qux" as Path.Valid); -}); - test("handles special characters", () => { const path = Path.from("foo-bar_baz.txt"); expect(path).toBe("foo-bar_baz.txt" as Path.Valid); @@ -171,11 +123,6 @@ test("from handles empty strings in arguments", () => { expect(Path.from("", "foo", "bar", "")).toBe("foo/bar" as Path.Valid); }); -test("from sanitizes multiple arguments with slashes", () => { - expect(Path.from("/foo/", "/bar/", "/baz/")).toBe("foo/bar/baz" as Path.Valid); - expect(Path.from("foo//", "//bar", "baz")).toBe("foo/bar/baz" as Path.Valid); -}); - test("resolve replaces the base name", () => { expect(Path.resolve(Path.from("a/b"), asRel("c"))).toBe(Path.from("a/c")); expect(Path.resolve(Path.from("a/b"), asRel("c/d"))).toBe(Path.from("a/c/d")); @@ -295,7 +242,7 @@ test("normalizeRelative preserves an all-dot reference", () => { test("parts splits a path into its components", () => { expect(Path.parts(Path.from(""))).toEqual([]); expect(Path.parts(Path.from("foo"))).toEqual(["foo"]); - expect(Path.parts(Path.from("/foo//bar/"))).toEqual(["foo", "bar"]); + expect(Path.parts(Path.from("foo/bar"))).toEqual(["foo", "bar"]); }); test("decode enforces the max part count", () => { @@ -308,11 +255,11 @@ test("decode never allocates per slash of a hostile path", () => { // Splitting a path would hold one entry per slash, so decode must count separators and stop at // the limit instead, even for the most slashes MAX_BYTES admits. const slashes = "/".repeat(Path.MAX_BYTES); - const parts = "a/".repeat(Path.MAX_BYTES / 2); + const parts = `${"a/".repeat(Path.MAX_BYTES / 2 - 1)}a`; const split = spyOn(String.prototype, "split"); const indexOf = spyOn(String.prototype, "indexOf"); try { - expect(Path.decode(slashes)).toBe(Path.empty()); + expect(() => Path.decode(slashes)).toThrow("empty segment"); indexOf.mockClear(); let error: unknown; try { @@ -343,8 +290,6 @@ test("encode and decode enforce the max byte length in UTF-8", () => { expect(Path.decode(atLimit)).toBe(atLimit); expect(() => Path.encode(`${atLimit}b` as Path.Valid)).toThrow(); expect(() => Path.decode(`${atLimit}b`)).toThrow(); - // Measured before normalization, so slashes can't pad a short path past the limit. - expect(() => Path.decode(`${"/".repeat(Path.MAX_BYTES)}a`)).toThrow(); // One UTF-16 unit of "é" is two UTF-8 bytes, so half as many units reach the limit. const wide = "é".repeat(Math.floor(Path.MAX_BYTES / 2)) as Path.Valid; diff --git a/js/net/src/path.ts b/js/net/src/path.ts index 5564a8bb8e2..f1002dfa127 100644 --- a/js/net/src/path.ts +++ b/js/net/src/path.ts @@ -4,26 +4,24 @@ * This module provides path-aware operations that respect delimiter boundaries, * preventing issues like "foo" matching "foobar". * - * Paths are automatically trimmed of leading and trailing slashes on creation, - * making all slashes implicit at boundaries. A path names a point in the origin's - * tree from its root; a leading slash never escapes that root. The same type names - * an exact broadcast and the prefix a route or announcement covers, so a broadcast's - * own path is the prefix it announces. See {@link Relative} for `..`-style references. + * A path is a run of non-empty segments separated by `/`, so it has exactly one spelling: + * {@link from} throws on a leading, trailing, or doubled slash rather than collapsing it. + * The empty path is the root, which prefixes every path; a prefix needs no trailing slash. + * A path names a point in the origin's tree from its root. The same type names an exact + * broadcast and the prefix a route or announcement covers, so a broadcast's own path is + * the prefix it announces. See {@link Relative} for `..`-style references. * * {@link Pattern} and {@link Patterns} are re-exported from `@moq/pattern`, the owner of - * the v1 grammar and algebra. Literal path construction and wire decoding retain - * their existing behavior. + * the v1 grammar and algebra. * * @example * ```typescript - * // Creation automatically trims slashes - * const path1 = Path.from("/foo/bar/"); - * const path2 = Path.from("foo/bar"); - * console.log(path1 === path2); // true + * // Multiple arguments are joined with "/", and an empty one is the root + * const path = Path.from("api", "", "v1/users"); + * console.log(path); // "api/v1/users" * - * // Multiple arguments are joined with "/" - * const path3 = Path.from("api", "v1", "users"); - * console.log(path3); // "api/v1/users" + * // A slash at a boundary or a doubled one is an empty segment + * Path.from("/api/v1"); // throws * * // Safe prefix matching * const base = Path.from("api/v1"); @@ -36,7 +34,7 @@ * @module */ -/** A normalized broadcast path. */ +/** A broadcast path: non-empty segments separated by `/`, or the empty root. */ export type Valid = string & { __brand: "Name" }; /** @@ -61,11 +59,18 @@ export const MAX_BYTES = 16384; const utf8 = new TextEncoder(); -/** Build a path from one or more components, joining with "/" and trimming redundant slashes. */ +/** + * Build a path by joining components with "/", where an empty component is the root. + * + * Throws when a component has a leading, trailing, or doubled `/`. + */ export function from(...paths: string[]): Valid { - // Join paths with "/" and then remove leading and trailing slashes, and collapse multiple slashes into one. - const joined = paths.join("/"); - return joined.replace(/\/+/g, "/").replace(/^\/+/, "").replace(/\/+$/, "") as Valid; + for (const path of paths) { + if (path.startsWith("/") || path.endsWith("/") || path.includes("//")) { + throw new Error("path contains an empty segment"); + } + } + return paths.filter((path) => path !== "").join("/") as Valid; } /** Split a path into its slash-separated parts. The empty path has no parts. */ @@ -76,16 +81,10 @@ export function parts(path: Valid): string[] { /** * Validate an untrusted wire string as a path, enforcing {@link MAX_PARTS} and {@link MAX_BYTES}. * - * Throws when the path is too deep or too long; use at wire decode sites. + * Throws when the path has an empty segment or is too deep or too long; use at wire decode sites. */ export function decode(raw: string): Valid { - // Measured before normalization, so redundant slashes can't smuggle a longer string. - // A UTF-16 unit is at least one UTF-8 byte, so a long string fails without encoding it. - if (raw.length > MAX_BYTES || utf8.encode(raw).length > MAX_BYTES) { - throw new Error(`path is longer than ${MAX_BYTES} bytes`); - } - const path = from(raw); - return encode(path); + return encode(from(raw)); } /** @@ -127,7 +126,6 @@ export function check(path: Valid): string | undefined { * ```typescript * const path = Path.from("foo/bar"); * console.log(Path.hasPrefix(Path.from("foo"), path)); // true - * console.log(Path.hasPrefix(Path.from("foo/"), path)); // true (trailing slash ignored) * console.log(Path.hasPrefix(Path.from("fo"), path)); // false * * const path2 = Path.from("foobar"); @@ -163,9 +161,6 @@ export function hasPrefix(prefix: Valid, path: Valid): boolean { * const suffix = Path.stripPrefix(Path.from("foo"), path); * console.log(suffix); // "bar/baz" * - * const suffix2 = Path.stripPrefix(Path.from("foo/"), path); - * console.log(suffix2); // "bar/baz" - * * const noMatch = Path.stripPrefix(Path.from("notfound"), path); * console.log(noMatch); // null * ``` @@ -205,7 +200,7 @@ export function join(path: Valid, other: Valid): Valid { } else if (other === "") { return path; } else { - // Since paths are trimmed, we always need to add a slash + // Neither side has a boundary slash, so exactly one joins them return `${path}/${other}` as Valid; } } diff --git a/js/room/MIGRATE.md b/js/room/MIGRATE.md index 323ee407a1f..fcc8d39410c 100644 --- a/js/room/MIGRATE.md +++ b/js/room/MIGRATE.md @@ -4,6 +4,10 @@ How to update your code for each breaking change, newest first. ## Unreleased +### `claims` refuses an identity with an empty segment ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +`claims(room, identity)` throws on an identity with a leading, trailing, or doubled `/` instead of collapsing it: pass `alice`, not `alice/`. + ### Chat events carry their send time `Chat.Subscriber.recv()` returns `{ value, at }`: `value` is the event `recv()` used to return, and `at` is its frame's timestamp. diff --git a/js/room/src/token.test.ts b/js/room/src/token.test.ts index ce4245c81ef..f75dc0febcd 100644 --- a/js/room/src/token.test.ts +++ b/js/room/src/token.test.ts @@ -9,8 +9,10 @@ test("claims root the token at the room and scope publish to the identity", () = }); }); -test("claims does not double a trailing slash on publish", () => { - expect(claims("meet/demo", "alice/").publish).toEqual(["alice/**"]); +test("claims rejects an identity with an empty segment", () => { + for (const identity of ["alice/", "/alice", "a//b"]) { + expect(() => claims("room", identity)).toThrow("empty segment"); + } }); test("claims accepts a multi-segment identity", () => { @@ -21,10 +23,8 @@ test("claims accepts a multi-segment identity", () => { }); }); -test("claims rejects empty normalized identities", () => { - for (const identity of ["", "/", "///"]) { - expect(() => claims("room", identity)).toThrow(); - } +test("claims rejects an empty identity", () => { + expect(() => claims("room", "")).toThrow("must not be empty"); }); test("claims rejects identities with wildcards", () => { diff --git a/js/room/src/token.ts b/js/room/src/token.ts index 75a64c46049..71e9dd771f8 100644 --- a/js/room/src/token.ts +++ b/js/room/src/token.ts @@ -24,7 +24,7 @@ export type Claims = { * * `root` is the room prefix, `subscribe` is `**` (everything under the room), and * `publish` is `/**` so a participant cannot publish at anyone else's - * paths. Throws on an identity that is empty after normalization or contains `*`, + * paths. Throws on an identity that is empty, has an empty segment, or contains `*`, * since a wildcard would let the participant publish as someone else. */ export function claims(room: string, identity: string): Claims { diff --git a/js/watch/src/msf.ts b/js/watch/src/msf.ts index 6f395ad17ee..f284be10d42 100644 --- a/js/watch/src/msf.ts +++ b/js/watch/src/msf.ts @@ -130,10 +130,12 @@ export function toHang(msf: Msf.Catalog, base?: Path.Valid): Catalog.Root { if (track.namespace !== undefined) { if (base === undefined) throw new Error(`MSF track ${track.name} namespace requires the catalog broadcast path`); - const target = Path.from(track.namespace); - // Normalizing an MSF namespace must not select different content. - if (target !== track.namespace) + let target: Path.Valid; + try { + target = Path.from(track.namespace); + } catch { throw new Error(`MSF track ${track.name} namespace cannot be referenced from the catalog`); + } if (!namesCatalog(target, base)) { const relative = Path.relative(target, base); if (relative === undefined) diff --git a/kt/moq/MIGRATE.md b/kt/moq/MIGRATE.md index 459ae34e936..dc179002b2a 100644 --- a/kt/moq/MIGRATE.md +++ b/kt/moq/MIGRATE.md @@ -4,6 +4,10 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Paths with an empty segment are refused ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +Publishing, serving, or requesting a broadcast at a path with a leading, trailing, or doubled `/` throws instead of collapsing it. Pass the canonical path, such as `room/cam`. + ### Request handlers are `requests()` ([#6752](https://github.com/moq-dev/moq/pull/6752)) Rename `dynamic()` on `OriginProducer`, `BroadcastProducer`, `TrackProducer`, and `TrackRequest` to `requests()`. diff --git a/py/moq-rs/MIGRATE.md b/py/moq-rs/MIGRATE.md index 42d00562300..bba7d801b6e 100644 --- a/py/moq-rs/MIGRATE.md +++ b/py/moq-rs/MIGRATE.md @@ -4,6 +4,10 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Paths with an empty segment are refused ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +Publishing, serving, or requesting a broadcast at a path with a leading, trailing, or doubled `/` raises instead of collapsing it. Pass the canonical path, such as `room/cam`. + ### Request handlers are `requests()` ([#6752](https://github.com/moq-dev/moq/pull/6752)) Rename `dynamic()` on `OriginProducer`, `BroadcastProducer`, `TrackProducer`, and `TrackRequest` to `requests()`. diff --git a/quest/m1/README.md b/quest/m1/README.md index 939bb078e2b..eba86c17ded 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -118,7 +118,6 @@ surface and zero-copy texture import, land before the cut. - [Pre-cut audit findings](/quest/m1/cut-audit/README.md) - fix the crashes, hostile-input costs, and correctness bugs the 2026-10-24 audit of main found - [Cancelled moq-transport requests stop](/quest/m1/ietf-request-cancel.md) - a cancelled request releases its state even when the peer withholds credit - [Datagrams past object 0 never pose as object 0](/quest/m1/datagram-object-drop.md) - a hop or reader that cannot carry an Object ID delivers only object 0 of a moq-transport datagram group -- [Empty segments refused](/quest/m1/empty-segments.md) - `room//cam` fails loud instead of naming `room/cam` - [Cluster routing](/quest/m1/cluster-routing/README.md) - prove shared routing contracts, implement per-origin routes, and connect outbound local clusters - [Routing guide](/quest/m1/routing-guide.md) - concepts and practical outbound federation setup - [moq-c at parity](/quest/m1/c/README.md) - the hand-written `moq-c` ships at the cut with every m1 binding feature moq-ffi gains diff --git a/quest/m1/empty-segments.md b/quest/m1/empty-segments.md deleted file mode 100644 index ff1f3bc4b37..00000000000 --- a/quest/m1/empty-segments.md +++ /dev/null @@ -1,19 +0,0 @@ -# [S] Publication paths refuse empty segments - -## Goal - -A publication path with an empty segment, such as `room//cam`, is refused -in Rust and JS instead of being silently collapsed to `room/cam`. - -## Plan - -[moq-dev/moq#6441](https://github.com/moq-dev/moq/pull/6441) refuses `.` and -`..` segments. `Path` normalization (`rs/moq-net/src/path/mod.rs`) still -collapses empty ones, so two different inputs name the same broadcast. - -Decided 2026-10-23 (maintainer): fail loud. Refuse empty publication segments -before normalization, at the same point #6441 refuses navigation segments, -in Rust and `@moq/net`, with tests for leading, trailing, and doubled -slashes. Start after #6441 merges. - -Public API: inputs that were accepted now fail. Wire: none. diff --git a/rs/moq-auth/src/path.rs b/rs/moq-auth/src/path.rs index b857473106b..8cf0ea68a24 100644 --- a/rs/moq-auth/src/path.rs +++ b/rs/moq-auth/src/path.rs @@ -3,8 +3,9 @@ //! A token's root and a connection's path are literal paths, so overlapping them is //! prefix arithmetic; everything beneath is a pattern from `moq-pattern`, re-exported //! from this crate so a token minting service does not pull in the wire stack. The -//! normalization and boundary rules must stay identical to `moq_net::Path`, which the -//! relay applies to the same strings. +//! boundary rules must stay identical to `moq_net::Path`, which the relay applies to +//! the same strings. `moq_net::Path` refuses the empty segments [`normalize`] drops, so +//! a string spelled with one is refused there rather than read as another path. //! //! Every function below assumes its arguments are already [`normalize`]d. diff --git a/rs/moq-boy/src/main.rs b/rs/moq-boy/src/main.rs index 1c270e377af..deff337f231 100644 --- a/rs/moq-boy/src/main.rs +++ b/rs/moq-boy/src/main.rs @@ -97,6 +97,23 @@ impl Config { anyhow::ensure!(deprecated.is_empty(), "{deprecated}"); Ok(()) } + + /// The game broadcast `name` publishes, and the prefix its viewers publish under. + /// + /// Each piece is parsed on its own and joined, so an empty (root) prefix adds no segment. + fn paths(&self, name: &str) -> Result<(moq_net::Path, moq_net::Path)> { + let name = moq_net::Path::new(name).context("invalid --name")?; + let prefix = moq_net::Path::new(&self.prefix).context("invalid --prefix")?; + let game = match &self.prefix_game { + Some(game) => moq_net::Path::new(game).context("invalid --prefix-game")?, + None => prefix.join(&moq_net::Path::from_static("game"))?, + }; + let viewer = match &self.prefix_viewer { + Some(viewer) => moq_net::Path::new(viewer).context("invalid --prefix-viewer")?, + None => prefix.join(&moq_net::Path::from_static("viewer"))?, + }; + Ok((game.join(&name)?, viewer.join(&name)?)) + } } /// Shared state for a game session, accessible from multiple threads/tasks. @@ -237,13 +254,9 @@ async fn run(config: &Config) -> Result<()> { // Publish origin: the game session broadcast. let publish_origin = moq_tokio::origin::spawn(); - let default_game_prefix = format!("{}/game", config.prefix); - let default_viewer_prefix = format!("{}/viewer", config.prefix); - let game_prefix = config.prefix_game.as_deref().unwrap_or(&default_game_prefix); - let viewer_prefix = config.prefix_viewer.as_deref().unwrap_or(&default_viewer_prefix); + let (broadcast_path, viewer_path) = config.paths(&name)?; // Create the broadcast on the publish origin and announce its path. - let broadcast_path = format!("{game_prefix}/{name}"); let broadcast = publish_origin .create_broadcast(&broadcast_path) .context("failed to create broadcast")?; @@ -255,7 +268,6 @@ async fn run(config: &Config) -> Result<()> { // Consume origin: viewer broadcasts under the viewer prefix. // JS publishes viewer feedback at "{viewer_prefix}/{name}/{viewerId}" - let viewer_path = format!("{viewer_prefix}/{name}"); let consume_origin = moq_tokio::origin::spawn(); let viewer_consumer = consume_origin .scope(&viewer_path, &moq_net::Patterns::from(moq_net::Pattern::all())) @@ -540,6 +552,34 @@ mod tests { assert_eq!(connect.port(), Some(4443)); } + /// An empty prefix is the root, so it adds no segment to the paths built under it. + #[test] + fn empty_prefixes_are_the_root() { + let parse = |args: &[&str]| { + let mut argv = vec!["--rom", "rom/big2small.gb"]; + argv.extend_from_slice(args); + Config::parse_from(&argv.iter().map(std::ffi::OsStr::new).collect::>()).unwrap() + }; + let paths = |config: Config| { + let (game, viewer) = config.paths("tetris").unwrap(); + (game.to_string(), viewer.to_string()) + }; + + assert_eq!( + paths(parse(&[])), + ("boy/game/tetris".into(), "boy/viewer/tetris".into()) + ); + assert_eq!( + paths(parse(&["--prefix", ""])), + ("game/tetris".into(), "viewer/tetris".into()) + ); + assert_eq!( + paths(parse(&["--prefix-game", "", "--prefix-viewer", ""])), + ("tetris".into(), "tetris".into()) + ); + assert!(parse(&["--prefix", "/boy"]).paths("tetris").is_err()); + } + /// The spelling the demo used to pass. It still parses, so the process can name /// `--connect` rather than leave the parser reporting an unexpected argument, but it /// configures nothing and must stop the run. diff --git a/rs/moq-c/MIGRATE.md b/rs/moq-c/MIGRATE.md index 038c7ca1fbf..31737ddda7e 100644 --- a/rs/moq-c/MIGRATE.md +++ b/rs/moq-c/MIGRATE.md @@ -4,6 +4,10 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Paths with an empty segment are refused ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +Every function taking a path, such as `moq_origin_create_broadcast` and `moq_origin_request`, returns an error for a path with a leading, trailing, or doubled `/` instead of collapsing it. Pass the canonical path, such as `room/cam`. + ### Request handlers are named requests ([#6752](https://github.com/moq-dev/moq/pull/6752)) The request-handler functions are renamed to match moq-net's `requests()`; their arguments and callbacks are unchanged: diff --git a/rs/moq-cli/MIGRATE.md b/rs/moq-cli/MIGRATE.md index e1315fb9855..6571f46ad7f 100644 --- a/rs/moq-cli/MIGRATE.md +++ b/rs/moq-cli/MIGRATE.md @@ -4,6 +4,10 @@ How to update your invocation and config for each breaking change, newest first. ## Unreleased +### `--broadcast` refuses empty segments ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +A broadcast name or prefix with a leading, trailing, or doubled `/` is an error instead of being collapsed: pass `--broadcast room/cam`, not `/room/cam/`. + ### Unknown `MOQ_*` environment variables stop startup ([#6633](https://github.com/moq-dev/moq/pull/6633)) A `MOQ_*` variable `moq` does not read, such as a misspelling, a removed name, or one meant for another binary, now stops startup with the variable named instead of being ignored. diff --git a/rs/moq-ffi/MIGRATE.md b/rs/moq-ffi/MIGRATE.md index ee4d98e19a9..5320d3e4706 100644 --- a/rs/moq-ffi/MIGRATE.md +++ b/rs/moq-ffi/MIGRATE.md @@ -5,6 +5,10 @@ These are the UniFFI names; each language wrapper has its own `MIGRATE.md` in it ## Unreleased +### Paths with an empty segment are refused ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +Every method taking a path, such as `MoqOriginProducer::create_broadcast` and `MoqOriginConsumer::request_broadcast`, returns an error for a path with a leading, trailing, or doubled `/` instead of collapsing it. Pass the canonical path, such as `room/cam`. + ### Request handlers are `requests()` ([#6752](https://github.com/moq-dev/moq/pull/6752)) Rename `dynamic()` on `MoqOriginProducer`, `MoqBroadcastProducer`, `MoqTrackProducer`, and `MoqTrackRequest` to `requests()`. diff --git a/rs/moq-mux/src/catalog/msf/consumer.rs b/rs/moq-mux/src/catalog/msf/consumer.rs index b4621445aef..bb9ffb3a9da 100644 --- a/rs/moq-mux/src/catalog/msf/consumer.rs +++ b/rs/moq-mux/src/catalog/msf/consumer.rs @@ -130,10 +130,6 @@ pub(crate) fn from_msf(msf: &moq_msf::Catalog, base: &moq_net: namespace: namespace.clone(), }; let target = moq_net::Path::new(namespace).map_err(|_| unresolvable())?; - // An MSF namespace names content; normalizing it must not select another namespace. - if target.as_str() != namespace { - return Err(unresolvable()); - } if super::names_catalog(&target, base) { return Ok(None); } diff --git a/rs/moq-mux/src/container/ts/programs.rs b/rs/moq-mux/src/container/ts/programs.rs index 433d036f135..2a6fb1e1acb 100644 --- a/rs/moq-mux/src/container/ts/programs.rs +++ b/rs/moq-mux/src/container/ts/programs.rs @@ -452,12 +452,14 @@ impl Programs { /// The broadcast one program of `name` publishes on: `event.hang` becomes `event/2.hang`. fn program_broadcast(name: &str, program: u16) -> String { - match catalog::CatalogFormat::detect(name) { - Some(format) => { - let stem = &name[..name.len() - format.extension().len()]; - format!("{stem}/{program}{}", format.extension()) - } - None => format!("{name}/{program}"), + let (stem, extension) = match catalog::CatalogFormat::detect(name) { + Some(format) => (&name[..name.len() - format.extension().len()], format.extension()), + None => (name, ""), + }; + // A final segment that is only the extension (`room/.hang`) leaves the stem's separator behind. + match stem.strip_suffix('/').unwrap_or(stem) { + "" => format!("{program}{extension}"), + stem => format!("{stem}/{program}{extension}"), } } @@ -501,6 +503,9 @@ mod test { assert_eq!(program_broadcast("event.hang", 2), "event/2.hang"); assert_eq!(program_broadcast("demo/event.msf", 7), "demo/event/7.msf"); assert_eq!(program_broadcast("event", 1), "event/1"); + assert_eq!(program_broadcast("", 1), "1"); + assert_eq!(program_broadcast(".hang", 2), "2.hang"); + assert_eq!(program_broadcast("room/.msf", 2), "room/2.msf"); } /// The first whole PAT in a buffer, as the parser reads it. diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index 508e72fb05e..f5319291da5 100644 --- a/rs/moq-net/MIGRATE.md +++ b/rs/moq-net/MIGRATE.md @@ -4,6 +4,13 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Building a `Path` refuses empty segments ([#6648](https://github.com/moq-dev/moq/pull/6648)) + +`Path::new` and `TryFrom` fail with `path::Invalid::EmptySegment` on a leading, trailing, or doubled `/` instead of collapsing it, so a path has one spelling; the empty path is still the root. Origin entry points report it as `Error::EmptySegment`, and `Path::from_static` panics on it. +Pass a prefix without a trailing `/`: `Path::from_static("room")` covers `room/cam`. Build a path from pieces with `Path::join` rather than `format!`, since an empty piece is the root and adds no segment. +`Path::from_escaped` is gone, and `path::TooLong` is `path::Invalid::TooLong`. `Path::head(n)` returns the path's first `n` segments. +moq-lite decoding refuses a path with an empty segment as a protocol violation, and moq-transport decoding refuses a namespace field ending in `/`, which escapes to one. + ### moq-transport drafts 14 through 17 are removed ([#6753](https://github.com/moq-dev/moq/pull/6753)) `ietf::Version::Draft14` through `Draft17` are gone, and a peer offering only those versions, by ALPN or in the moql SETUP, fails the handshake. @@ -17,8 +24,8 @@ There is one owned `Path` type: a short path is stored inline and a long one sha - `PathOwned`, `Path<'a>`, and `Path<'static>` are all `Path`; `RelativeOwned` and `Relative<'a>` are `Relative`. - `Path::{to_owned, into_owned, borrow}` and the `Relative` equivalents are gone: drop the call, or `clone()` a `&Path` you need to keep. -- `Path::new`, `Relative::new`, `Path::join`, and `Path::resolve` return `Result<_, path::TooLong>`, failing on a path longer than `Path::MAX_BYTES`. `Path::new` copies its argument; use `Path::from_static("room")` for a literal, which borrows it and panics if it is too long. -- `From<&str>`, `From<&String>`, and `From` for `Path` and `Relative` are `TryFrom`, with `path::TooLong` as the error. +- `Path::new`, `Relative::new`, `Path::join`, and `Path::resolve` return `Result<_, path::Invalid>`, failing on a path longer than `Path::MAX_BYTES`. `Path::new` copies its argument; use `Path::from_static("room")` for a literal, which borrows it and panics if it is too long. +- `From<&str>`, `From<&String>`, and `From` for `Path` and `Relative` are `TryFrom`, with `path::Invalid` as the error. - `AsPath` is gone. Origin entry points such as `publish`, `create_broadcast`, `scope`, `mount`, and `routed` take `impl TryInto>`, so `&str`, `String`, `Path`, and `&Path` still work and a too-long string fails with `Error::BoundsExceeded`. Other functions that took `impl AsPath`, such as `stats::Tier::new`, take `impl Into`: pass a `Path` or `&Path`. - `origin::{Producer, Consumer}::absolute` and `announce::Consumer::absolute` return `Result`. - `Path::{has_prefix, strip_prefix, join, relative}` take `&Path`: write `path.has_prefix(&Path::from_static("room"))`. diff --git a/rs/moq-net/fuzz/README.md b/rs/moq-net/fuzz/README.md index 534b6e39762..5a5c75a5ed6 100644 --- a/rs/moq-net/fuzz/README.md +++ b/rs/moq-net/fuzz/README.md @@ -49,6 +49,9 @@ Beyond "does not panic": iteration order differs per instance (moq-lite SETUP). - A lite-07 announce stream our decoder accepts, recompressed by our encoder, resolves to the same announcements. +- Every path decoded from the wire (a moq-lite path, an IETF namespace or Full Track + Name) or resolved from an announce stream parses back to itself under the strict + constructor, and so does each run of its leading segments. - Setup Options a client accepts from its server, a server accepts from its client: only PATH and AUTHORITY are one-sided. - `Path::relative` inverts `Path::resolve`, and never produces a reference that walks diff --git a/rs/moq-net/fuzz/regressions/ietf/draft18-namespace-field-trailing-slash b/rs/moq-net/fuzz/regressions/ietf/draft18-namespace-field-trailing-slash new file mode 100644 index 00000000000..8bd10803afa Binary files /dev/null and b/rs/moq-net/fuzz/regressions/ietf/draft18-namespace-field-trailing-slash differ diff --git a/rs/moq-net/src/discovery.rs b/rs/moq-net/src/discovery.rs index 25641014f31..e19c479d29f 100644 --- a/rs/moq-net/src/discovery.rs +++ b/rs/moq-net/src/discovery.rs @@ -306,9 +306,5 @@ impl Discovery { /// `path` and every prefix above it, up to the root, narrowest first. fn covering(path: &Path) -> Vec { - std::iter::successors(Some(path.as_str()), |path| { - (!path.is_empty()).then(|| path.rfind('/').map_or("", |slash| &path[..slash])) - }) - .map(|prefix| Path::new(prefix).expect("a prefix of a path fits")) - .collect() + (0..=path.parts().count()).rev().filter_map(|n| path.head(n)).collect() } diff --git a/rs/moq-net/src/error.rs b/rs/moq-net/src/error.rs index a1d88afa4c5..013b9351c22 100644 --- a/rs/moq-net/src/error.rs +++ b/rs/moq-net/src/error.rs @@ -377,6 +377,10 @@ pub enum Error { #[error("broadcast path contains a navigation segment")] NavigationSegment, + /// A path was spelled with a leading, trailing, or doubled `/`. + #[error("path contains an empty segment")] + EmptySegment, + /// A duplicate ID was used // The broadcast/track is a duplicate #[error("duplicate")] @@ -652,7 +656,8 @@ impl From<&Error> for SessionError { | Error::WrongSize | Error::BoundsExceeded(_) | Error::InvalidPath(_) - | Error::NavigationSegment => Self::ProtocolViolation, + | Error::NavigationSegment + | Error::EmptySegment => Self::ProtocolViolation, Error::App(app) => Self::App(*app), // A code we did not recognize, so we cannot say which space it came from. // Forwarding it into this one risks landing on a value that IS registered here @@ -717,6 +722,7 @@ impl From<&Error> for StreamError { | Error::BoundsExceeded(_) | Error::InvalidPath(_) | Error::NavigationSegment + | Error::EmptySegment | Error::MalformedTrack => Self::MalformedTrack, Error::App(app) => Self::App(*app), // See the SessionError impl: an unregistered code carries no registry, so diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 9d61e7b21ca..6997c3d5aa7 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -149,6 +149,48 @@ where true } +/// Assert the invariant every path holds once built: its spelling parses back to it under +/// the strict constructor, and so does every run of its leading segments, which is what +/// stats grouping and announce compression derive from it. +fn assert_strict(path: &Path) { + let reparsed = Path::new(path.as_str()).unwrap_or_else(|err| panic!("{path:?} is refused: {err}")); + assert_eq!(reparsed, *path, "{path:?} did not parse back to itself"); + let count = path.parts().count(); + for n in 0..=count { + let head = path.head(n).expect("a path has a head per segment"); + assert!( + Path::new(head.as_str()).is_ok(), + "{path:?} has an invalid head {head:?}" + ); + } + assert!(path.head(count + 1).is_none(), "{path:?} has a head past its segments"); +} + +/// Read the input as a moq-lite path, whatever its kind, and hold it to [`assert_strict`]. +fn lite_path(data: &[u8], version: lite::Version) { + if let Ok((path, _)) = Path::decode_slice(data, version) { + assert_strict(&path); + } +} + +/// Read the input as a namespace tuple and as a Full Track Name, whatever its kind, and +/// hold each path to [`assert_strict`] and the namespace to the wire round trip. +fn ietf_paths(data: &[u8], version: ietf::Version) { + let form: Form = version.into(); + if let Ok(path) = ietf::namespace::decode_namespace(&mut Decoder::new(data, form)) { + assert_strict(&path); + let mut encoded = Vec::new(); + if ietf::namespace::encode_namespace(&mut Encoder::new(&mut encoded, form), &path).is_ok() { + let echo = ietf::namespace::decode_namespace(&mut Decoder::new(&encoded, form)) + .expect("could not decode our own namespace"); + assert_eq!(echo, path, "namespace did not survive a round trip"); + } + } + if let Ok((path, _)) = ietf::namespace::decode_track(&mut Decoder::new(data, form)) { + assert_strict(&path); + } +} + /// [`roundtrip`] for a datagram body, which runs to the end of its buffer. fn datagram(data: &[u8], version: lite::Version) -> bool { // The body doesn't say whether it carries a timestamp or an inflated size, whether the @@ -199,6 +241,7 @@ pub fn lite_wire(data: &[u8]) -> bool { }; let version = LITE_VERSIONS[version]; let kind = kind % LITE_KINDS; + lite_path(rest, version); match kind { 0 => roundtrip::(rest, version), @@ -610,6 +653,11 @@ impl LiteSample { /// Returns whether anything resolved. pub fn announce_stream(data: &[u8]) -> bool { let resolved = decode_announces(data, true); + for announced in &resolved { + if let Announced::Start(suffix, _) = announced { + assert_strict(suffix); + } + } let echo = decode_announces(&encode_announces(&resolved, true), true); assert_eq!(echo, resolved, "compression did not survive a round trip"); !resolved.is_empty() @@ -625,6 +673,7 @@ pub fn ietf_wire(data: &[u8]) -> bool { }; let version = IETF_VERSIONS[version]; let kind = kind % IETF_KINDS; + ietf_paths(rest, version); match kind { 0 => roundtrip::, _>(rest, version), @@ -914,29 +963,26 @@ pub fn path(data: &[u8]) -> bool { }; let (target, base) = text.split_once('\n').unwrap_or((text, "")); - // Only a path over the limit is refused, and the limit applies after normalizing. - let (Ok(target), Ok(base)) = (Path::new(target), Path::new(base)) else { - let normalized = |s: &str| { - s.split('/') - .filter(|part| !part.is_empty()) - .collect::>() - .join("/") - .len() + + // A spelling is refused exactly when it is over the limit or has an empty segment, so + // each path has one spelling. + for s in [target, base] { + let expected = if s.len() > Path::MAX_BYTES { + Err(crate::path::Invalid::TooLong) + } else if !s.is_empty() && s.split('/').any(str::is_empty) { + Err(crate::path::Invalid::EmptySegment) + } else { + Ok(()) }; - assert!( - normalized(target).max(normalized(base)) > Path::MAX_BYTES, - "a path within the limit was refused" - ); + assert_eq!(Path::new(s).map(|_| ()), expected, "{s:?} was judged wrongly"); + } + let (Ok(target), Ok(base)) = (Path::new(target), Path::new(base)) else { return true; }; - // Normalization is a fixed point: re-parsing what `as_str` printed changes nothing, - // or every path that made a round trip through the wire would drift. - assert_eq!( - Path::new(target.as_str()), - Ok(target.clone()), - "normalization is not idempotent" - ); + // A path's own spelling parses back to it, or every path that made a round trip + // through the wire would drift. + assert_strict(&target); assert_eq!(target.parts().collect::>().join("/"), target.as_str()); // A prefix is a prefix whichever way you ask. diff --git a/rs/moq-net/src/ietf/namespace.rs b/rs/moq-net/src/ietf/namespace.rs index f464793fa5e..aa4b0c05c5b 100644 --- a/rs/moq-net/src/ietf/namespace.rs +++ b/rs/moq-net/src/ietf/namespace.rs @@ -1,4 +1,4 @@ -use crate::{Path, coding::*, path::TooLong}; +use crate::{Path, coding::*, path::Invalid}; fn to_tuple(namespace: &Path) -> Vec { let path = namespace.as_str(); @@ -28,7 +28,8 @@ fn to_tuple(namespace: &Path) -> Vec { parts } -fn from_tuple(parts: &[&str]) -> Result { +// A field ending in `/` escapes to a path with an empty segment, so it is refused. +fn from_tuple(parts: &[&str]) -> Result { // Sized for the common case of no escapes, so building it rarely grows. let mut path = String::with_capacity(parts.iter().map(|part| part.len() + 1).sum()); for (i, part) in parts.iter().enumerate() { @@ -44,7 +45,7 @@ fn from_tuple(parts: &[&str]) -> Result { } } - Path::from_escaped(path) + Path::try_from(path) } /// Helper function to encode namespace as tuple of strings @@ -69,7 +70,7 @@ const MAX_BYTES: usize = 4096; /// Decode a namespace tuple, along with the sum of its field lengths. /// /// Every limit here closes the session as a PROTOCOL_VIOLATION (draft-22 section 8.7): -/// more than 32 fields, an empty field, or more than 4096 bytes. +/// more than 32 fields, an empty field, a field ending in `/`, or more than 4096 bytes. fn decode_fields(r: &mut Decoder<'_>) -> Result<(Path, usize), DecodeError> { let count = r.varint()?; @@ -120,7 +121,7 @@ pub(super) fn refusal(namespace: &Path, version: super::Version) -> Option