diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index ccdd5ed442c..b4d25c53146 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -30,9 +30,10 @@ carries [publisher epochs](#publisher-epochs). | **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. +`room/alice`. A path never starts or ends with `/` or doubles one, and a `\` +only escapes a `\` or `/` (how a moq-transport namespace field holding one is +spelled), so each name has one spelling, and every API and peer refuses any +other. The empty path is the root, which prefixes every path. ## Discovery diff --git a/drafts/draft-lcurley-moq-hang.md b/drafts/draft-lcurley-moq-hang.md index 260b9f7b3a1..63cf879ea4a 100644 --- a/drafts/draft-lcurley-moq-hang.md +++ b/drafts/draft-lcurley-moq-hang.md @@ -670,6 +670,7 @@ By default a rendition's track lives in the same broadcast that served the catal The `broadcast` field overrides that, naming a different broadcast that publishes the track. The value is a relative path, resolved against the path of the broadcast that served the catalog. +It is spelled like a path {{moql}}, so a consumer MUST reject a catalog whose reference has a leading, trailing, or doubled `/`, or a `\` before anything but `\` or `/`. It uses relative reference resolution ({{!RFC3986, Section 5.2}}): a non-empty reference replaces the catalog broadcast's last path segment before applying `.` and `..` segments. For example, `./source` in a catalog served by `room/transcode` resolves to `room/source`, while `.` resolves to `room`. An empty reference resolves to the catalog broadcast itself. @@ -786,7 +787,7 @@ The optional catalog `stats` section names the publisher's snapshot track in the A publisher snapshot covers only renditions that publisher writes, omitting references to another broadcast. The optional catalog `echo` section solicits feedback: `{ "path": "viewers" }`. -The path is relative to the broadcast serving the catalog, with the same URL-style resolution and root containment as {{field-broadcast}}. +The path is relative to the broadcast serving the catalog, with the same spelling, URL-style resolution, and root containment as {{field-broadcast}}. For example, from `room/live`, `viewers` resolves to `room/viewers`, and `live/viewers` resolves to `room/live/viewers`. A consumer MUST reject a catalog whose echo path resolves above its connection root. After reading the soliciting catalog, a viewer MAY announce one broadcast under that prefix at an application-assigned name plus `.echo`, for example `room/viewers/alice.echo`. @@ -1125,6 +1126,7 @@ This document has no IANA actions. ## moq-hang-04 {:numbered="false"} +- A `broadcast` reference and an echo `path` are spelled like a path: one with an empty segment or a lone `\` refuses the catalog instead of collapsing. - Removed `catalog.json.z` and the data track `compression` field: compression is a track property the transport carries ({{compression}}), so each compressed track is one name, and the catalog, `stats` and `echo` snapshots, HLS record tracks, and JSON data tracks SHOULD be compressed. - Replaced the root `archive` section and its per-track timelines with an optional, per-rendition `hls` field on video and audio ({{hls}}). Each record is a segment's first `group` and `pts` in milliseconds, with an optional `duration` where zero marks a stop; frame positions, `end`, `keyframe`, `timescale`, `replay`, `replayEpoch`, `store`, and `version` are gone. Each rendition numbers its own segments, and an edge maps a rendition's record `n` to its segment `n`. The record track is named `.hls` and is compressed ({{compression}}). - A reader refuses a JSON frame in which an object repeats a member name. diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 3c1dcce1448..5c793324592 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 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 `\` (0x5C) escapes the `\` or `/` that follows it, as a MoqTransport namespace field holding either is spelled; a path with any other `\` is a PROTOCOL_VIOLATION. 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. @@ -1766,6 +1767,8 @@ The `Message Length` describes the payload size on the wire. - 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. +- A `\` in a path escapes only `\` or `/`; any other `\` is a PROTOCOL_VIOLATION rather than a second spelling of the same namespace field. + - Added the SETUP `Implementation` parameter (0x6): the sender's software as `name/version` product tokens. - Hop IDs span 64 bits. diff --git a/js/auth/src/cli.ts b/js/auth/src/cli.ts index 0fbcdfc48ed..55b8f984ccb 100755 --- a/js/auth/src/cli.ts +++ b/js/auth/src/cli.ts @@ -6,6 +6,7 @@ import { base64url } from "jose"; import type { Algorithm } from "./algorithm.ts"; import { authorize, ClaimsSchema, type Scope, ScopeSchema } from "./claims.ts"; import { Key } from "./key.ts"; +import * as Path from "./path.ts"; import { encodeGrants } from "./wire.ts"; const program = new Command(); @@ -35,6 +36,8 @@ program ...(options.publish && { publish: options.publish }), ...(options.subscribe && { subscribe: options.subscribe }), }); + // A scope root `Key.sign` would refuse could never sign anything beneath it. + Path.validate(scope.root); key = { ...key, scope }; } diff --git a/js/auth/src/key.test.ts b/js/auth/src/key.test.ts index 9874be7b2d6..dab5d06945d 100644 --- a/js/auth/src/key.test.ts +++ b/js/auth/src/key.test.ts @@ -835,7 +835,7 @@ test("key scope treats absolute and rooted grants alike", async () => { const scoped: Key = { ...Key.parse(encodeJwk(testKey)), scope: { root: "project", publish: ["live/**"] } }; await expect(Key.sign(scoped, { root: "", publish: ["project/live/room/**"] })).resolves.toBeString(); - await expect(Key.sign(scoped, { root: "/project/live/", publish: ["room/**"] })).resolves.toBeString(); + await expect(Key.sign(scoped, { root: "project/live", publish: ["room/**"] })).resolves.toBeString(); // Segment-aware: "live" must not cover "lively". await expect(Key.sign(scoped, { root: "project", publish: ["lively/**"] })).rejects.toThrow("exceed the key scope"); // A root above the scope does not widen it. @@ -889,3 +889,13 @@ test("key IDs share Rust's grammar and 128-character limit", async () => { } await expect(Key.generate("HS256", "a".repeat(129))).rejects.toThrow(); }); + +test("sign refuses a root no path can spell", async () => { + const key = Key.parse(encodeJwk(testKey)); + for (const root of ["/room", "room/", "a//b", "a\\b", "a\\", "a".repeat(4097)]) { + await expect(Key.sign(key, { root, publish: ["**"] })).rejects.toThrow("invalid root"); + } + for (const root of ["", "room", "a\\\\b", "a\\/b"]) { + await expect(Key.sign(key, { root, publish: ["**"] })).resolves.toBeString(); + } +}); diff --git a/js/auth/src/key.ts b/js/auth/src/key.ts index 626e5090995..502b56aeb0a 100644 --- a/js/auth/src/key.ts +++ b/js/auth/src/key.ts @@ -3,6 +3,7 @@ import * as z from "@zod/mini"; import * as jose from "jose"; import { type Algorithm, AlgorithmSchema } from "./algorithm.ts"; import { type Claims, ClaimsSchema, ScopeSchema, scopeAllows } from "./claims.ts"; +import * as Path from "./path.ts"; import { encodeGrants } from "./wire.ts"; /** @@ -192,6 +193,10 @@ function parse(jwk: string): Key { * * `iat` is written only when the claims carry one, so an unset field stays off the * wire rather than being stamped with the current time. + * + * Throws on a root spelled with an empty segment, a `\` escaping anything but `\` or + * `/`, or more than 4096 bytes. Verifying stays lenient on the empty segments, so tokens + * minted before stay valid. */ async function sign(key: Key, claims: z.input): Promise { ensureOperationSupported(key, "sign"); @@ -204,6 +209,7 @@ async function sign(key: Key, claims: z.input): Promise new Error(`invalid root ${JSON.stringify(root)} ${reason}`); + if (utf8.encode(root).length > MAX_BYTES) throw invalid(`is longer than ${MAX_BYTES} bytes`); + if (root.startsWith("/") || root.endsWith("/") || root.includes("//")) throw invalid("contains an empty segment"); + if (/\\(?![\\/])/.test(root.replaceAll("\\\\", ""))) { + throw invalid("contains a `\\` that escapes neither `\\` nor `/`"); + } +} + /** * Trim leading and trailing slashes and collapse consecutive ones, so all slashes * are implicit at boundaries and `/foo//bar/` is the same path as `foo/bar`. diff --git a/js/hang/src/catalog/path.ts b/js/hang/src/catalog/path.ts index 8785197bc2f..e812d057ab9 100644 --- a/js/hang/src/catalog/path.ts +++ b/js/hang/src/catalog/path.ts @@ -1,15 +1,25 @@ import { Epoch, Path } from "@moq/net"; import * as z from "@zod/mini"; +function isRelative(text: string): boolean { + try { + Path.normalizeRelative(text); + return true; + } catch { + return false; + } +} + /** * Zod schema for a relative broadcast reference stored in a catalog (a rendition's - * `broadcast` field, e.g. "./source"). Normalizes the input the same way the Rust - * `path::Relative` type does so JS and Rust agree byte-for-byte after deserialization. + * `broadcast` field, e.g. "./source"). Refuses and normalizes the input the same way the Rust + * `path::Relative` type does so JS and Rust agree byte-for-byte after deserialization: a + * leading, trailing, or doubled `/` or a lone `\` refuses the catalog. * Resolve it against the catalog broadcast's own path with `Path.tryResolve`, which returns * `undefined` for a reference that walks above the root: hang requires rejecting such a catalog. */ export const RelativeBroadcastSchema: z.ZodMiniType = z.pipe( - z.string(), + z.string().check(z.refine(isRelative, "expected a relative broadcast path")), z.transform(Path.normalizeRelative), ); diff --git a/js/hang/src/catalog/root.test.ts b/js/hang/src/catalog/root.test.ts index 73237758936..b02e125593b 100644 --- a/js/hang/src/catalog/root.test.ts +++ b/js/hang/src/catalog/root.test.ts @@ -31,7 +31,7 @@ test("rendition broadcast reference is parsed and normalized", () => { video: { renditions: { video: { - broadcast: "././source/", + broadcast: "././source", codec: "avc1.64001f", container: { kind: "legacy" }, }, @@ -40,8 +40,14 @@ test("rendition broadcast reference is parsed and normalized", () => { }; const parsed = RootSchema.parse(catalog); if (!parsed.video || !("renditions" in parsed.video)) throw new Error("missing video section"); - // Normalized like Rust path::Relative: redundant `.` and empty segments are dropped. + // Normalized like Rust path::Relative: redundant `.` segments are dropped. expect(parsed.video.renditions.video?.broadcast).toBe("source" as RelativeBroadcast); + + // Spelled like a path, so an empty segment or a lone backslash refuses the catalog. + for (const broadcast of ["./source/", "/source", "a//b", "a\\b"]) { + const video = { broadcast, codec: "avc1.64001f", container: { kind: "legacy" } }; + expect(RootSchema.safeParse({ video: { renditions: { video } } }).success).toBe(false); + } }); // Mirrors Rust's `reference_epochs_roundtrip_in_every_section`. diff --git a/js/net/src/ietf/namespace.test.ts b/js/net/src/ietf/namespace.test.ts index 12eab9058d8..9af758294f3 100644 --- a/js/net/src/ietf/namespace.test.ts +++ b/js/net/src/ietf/namespace.test.ts @@ -42,6 +42,26 @@ test("a full track name counts field bytes and is refused before writing", async ); }); +test("every path, `\\` and `/` included, returns from its namespace tuple unchanged", async () => { + let texts = [""]; + for (let length = 1; length <= 8; length++) { + texts = texts.flatMap((text) => ["a", "/", "\\"].map((c) => text + c)); + for (const text of texts) { + let path: Path.Valid; + try { + path = Path.from(text); + } catch { + continue; + } + expect(await roundTrip((w) => encode(w, path), decode)).toBe(path); + } + } +}); + +test("a lone backslash in a field is escaped on the way in", () => { + expect(fromTuple(["foo\\bar/baz", "qux"])).toBe(Path.from("foo\\\\bar\\/baz/qux")); +}); + /** Write with `write`, then read the bytes back with `read`. */ async function roundTrip(write: (w: Writer) => Promise, read: (r: Reader) => Promise): Promise { const bytes: number[] = []; diff --git a/js/net/src/ietf/namespace.ts b/js/net/src/ietf/namespace.ts index 8ef9d6c07a1..23bf26f2fc9 100644 --- a/js/net/src/ietf/namespace.ts +++ b/js/net/src/ietf/namespace.ts @@ -4,7 +4,11 @@ import type { Reader, Writer } from "../stream.ts"; import { decodeUtf8 } from "../util/utf8.ts"; import { type IetfVersion, Version } from "./version.ts"; -/** Convert the escaped moq-net path representation into IETF namespace tuple fields. */ +/** + * Convert the escaped moq-net path representation into IETF namespace tuple fields. + * + * Every `\` in a valid path escapes the `\` or `/` after it. + */ function toTuple(namespace: Path.Valid): string[] { if (namespace === "") return []; @@ -13,7 +17,7 @@ function toTuple(namespace: Path.Valid): string[] { const ch = namespace[i]; if (ch === "/") { parts.push(""); - } else if (ch === "\\" && (namespace[i + 1] === "/" || namespace[i + 1] === "\\")) { + } else if (ch === "\\") { parts[parts.length - 1] += namespace[++i]; } else { parts[parts.length - 1] += ch; @@ -25,8 +29,10 @@ function toTuple(namespace: Path.Valid): string[] { /** * 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, or when the - * separators and escapes push the path past {@link Path.MAX_BYTES}. + * Every `\` and `/` in a field is escaped, the only spelling a path accepts, so every path + * returns from its own tuple unchanged. Throws when a field ends in `/`, which escapes to a + * path with an empty segment, or when the separators and escapes push the path past + * {@link Path.MAX_BYTES}. */ export function fromTuple(parts: string[]): Path.Valid { const escaped = parts.map((part) => part.replaceAll("\\", "\\\\").replaceAll("/", "\\/")).join("/"); diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 38bfe57a772..a3ea4dcd304 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -39,9 +39,19 @@ export function groupBounds(groups: Groups = {}): { start: number; end?: number /** * The announce-interest prefix a scope needs on a prefix-shaped wire: its literal head. * The peer echoes every suffix beneath it, and the caller filters what arrives. + * + * A head cut at an escaped `/`, such as `a\` from `a\/*`, ends in half an escape, so it + * falls back to its parent. */ export function scopeHead(scope: Path.Pattern): Path.Valid { - return Path.from(scope.head); + let head = scope.head; + for (;;) { + try { + return Path.from(head); + } catch { + head = head.slice(0, Math.max(head.lastIndexOf("/"), 0)); + } + } } /** diff --git a/js/net/src/lite/announce.test.ts b/js/net/src/lite/announce.test.ts index d3d6e2a4d5a..a721a5005f2 100644 --- a/js/net/src/lite/announce.test.ts +++ b/js/net/src/lite/announce.test.ts @@ -390,6 +390,17 @@ test("AnnounceHistory rejects a keep longer than the base", () => { expect(() => history.update({ id: 0n, hops: [], hopBase: { distance: 1n, keep: 2 } })).toThrow(ProtocolViolation); }); +test("AnnounceHistory keeps segments split at an escaped slash", () => { + const history = new AnnounceHistory(); + history.start({ suffix: Path.from("a\\/b"), hops: [] }); + // The kept `a\` is half an escape, which the rest completes or nothing does. + const { suffix } = history.start({ suffix: Path.from("c"), hops: [], pathBase: { distance: 1n, keep: 1 } }); + expect(suffix).toBe(Path.from("a\\/c")); + expect(() => history.start({ suffix: Path.empty(), hops: [], pathBase: { distance: 1n, keep: 1 } })).toThrow( + ProtocolViolation, + ); +}); + test("AnnounceHistory rejects a resolved path past the byte limit", () => { const history = new AnnounceHistory(); history.start({ suffix: Path.from("x".repeat(Path.MAX_BYTES - 2)), hops: [] }); diff --git a/js/net/src/lite/announce.ts b/js/net/src/lite/announce.ts index e621de20026..2a4771c0652 100644 --- a/js/net/src/lite/announce.ts +++ b/js/net/src/lite/announce.ts @@ -406,7 +406,14 @@ export class AnnounceHistory { const head = Path.parts(this.#base(msg.pathBase).suffix); const keep = msg.pathBase.keep; if (keep > head.length) throw new ProtocolViolation(`announce keeps ${keep} of ${head.length} segments`); - suffix = Path.join(Path.from(...head.slice(0, keep)), suffix); + // Joined as text before parsing: a kept segment may end in the `\` of an escaped `/`. + const kept = head.slice(0, keep); + if (suffix !== Path.empty()) kept.push(suffix); + try { + suffix = Path.from(kept.join("/")); + } catch (cause) { + throw new ProtocolViolation("announce keeps half an escape", { cause }); + } const error = Path.check(suffix); if (error) throw new ProtocolViolation(error); } diff --git a/js/net/src/origin.test.ts b/js/net/src/origin.test.ts index c9bdb656877..11eea46cc14 100644 --- a/js/net/src/origin.test.ts +++ b/js/net/src/origin.test.ts @@ -167,6 +167,19 @@ test("a scoped route remains visible when an exact local path is outside the sco origin.close(); }); +test("a scope cut at an escaped slash discovers through the nearest valid ancestor", async () => { + const origin = new Producer(); + const path = Path.from("room/a\\/b"); + const broadcast = publish(origin, path); + const live = await announcedView(origin, Path.Pattern.parse("room/a\\/*")); + await settle(); + + expect([...live.peek().keys()]).toEqual([path]); + + broadcast.close(); + origin.close(); +}); + test("a published broadcast resolves by path", async () => { const origin = new Producer(); const consumer = origin.consume(); diff --git a/js/net/src/path.test.ts b/js/net/src/path.test.ts index 49f68048425..4091263c6fd 100644 --- a/js/net/src/path.test.ts +++ b/js/net/src/path.test.ts @@ -12,6 +12,27 @@ test("from refuses empty segments", () => { expect(Path.from("foo/bar")).toBe("foo/bar" as Path.Valid); }); +test("from refuses a backslash that escapes neither a backslash nor a slash", () => { + for (const raw of ["\\", "a\\", "\\a", "a\\b", "a\\\\\\b", "a\\.b/c"]) { + expect(() => Path.from(raw)).toThrow("escapes neither"); + expect(() => Path.normalizeRelative(raw)).toThrow("escapes neither"); + } + // An escape takes exactly the byte after it, so runs of backslashes pair up. + for (const raw of ["\\\\", "a\\/b", "\\/a", "a\\\\/b", "a\\\\\\/b"]) { + expect(Path.from(raw)).toBe(raw as Path.Valid); + } + // A component ending in half an escape is refused even though the join would complete it. + expect(() => Path.from("a\\", "b")).toThrow("escapes neither"); +}); + +test("resolve refuses a stranded escape", () => { + // Segments split at an escaped `/` too, so popping past one strands its `\`. + const base = Path.from("a\\/b/c"); + expect(() => Path.resolve(base, asRel(".."))).toThrow("escapes neither"); + expect(Path.tryResolve(base, asRel(".."))).toBeUndefined(); + expect(Path.resolve(base, asRel("d"))).toBe(Path.from("a\\/b/d")); +}); + test("from treats the empty path as the root", () => { expect(Path.from("")).toBe(Path.empty()); expect(Path.from()).toBe(Path.empty()); @@ -231,12 +252,19 @@ test("tryResolve distinguishes the root from an escape", () => { test("normalizeRelative preserves an all-dot reference", () => { expect(Path.normalizeRelative("")).toBe(asRel("")); expect(Path.normalizeRelative(".")).toBe(asRel(".")); - expect(Path.normalizeRelative("././")).toBe(asRel(".")); + expect(Path.normalizeRelative("./.")).toBe(asRel(".")); expect(Path.normalizeRelative("./foo")).toBe(asRel("foo")); - expect(Path.normalizeRelative("foo//bar")).toBe(asRel("foo/bar")); expect(Path.normalizeRelative("foo/./bar")).toBe(asRel("foo/bar")); - expect(Path.normalizeRelative("/foo/")).toBe(asRel("foo")); expect(Path.normalizeRelative("../foo")).toBe(asRel("../foo")); + // Dropping a `.` cannot strand the `\` of an escaped `/` before it. + expect(() => Path.normalizeRelative("a\\/.")).toThrow("escapes neither"); + expect(Path.normalizeRelative("a\\\\/.")).toBe(asRel("a\\\\")); +}); + +test("normalizeRelative refuses empty segments", () => { + for (const raw of ["/", "/foo", "foo/", "./", "foo//bar", "./foo/", "../"]) { + expect(() => Path.normalizeRelative(raw)).toThrow("empty segment"); + } }); test("parts splits a path into its components", () => { diff --git a/js/net/src/path.ts b/js/net/src/path.ts index 5cd36815cb6..82f5dde7eff 100644 --- a/js/net/src/path.ts +++ b/js/net/src/path.ts @@ -6,6 +6,8 @@ * * 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. + * A `\` escapes the `\` or `/` after it, which is how a moq-transport namespace field holding + * either one is spelled, and any other `\` is refused. Segments still split at every `/`. * 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 @@ -67,17 +69,29 @@ const utf8 = new TextEncoder(); /** * Build a path by joining components with "/", where an empty component is the root. * - * Throws when a component has a leading, trailing, or doubled `/`. + * Throws when a component has a leading, trailing, or doubled `/`, or a `\` before anything + * but `\` or `/`. */ export function from(...paths: string[]): Valid { for (const path of paths) { - if (path.startsWith("/") || path.endsWith("/") || path.includes("//")) { - throw new Error("path contains an empty segment"); - } + const error = spelling(path); + if (error) throw new Error(error); } return paths.filter((path) => path !== "").join("/") as Valid; } +// Why `path` is not the one spelling of a path, or undefined when it is. +function spelling(path: string): string | undefined { + if (path.startsWith("/") || path.endsWith("/") || path.includes("//")) { + return "path contains an empty segment"; + } + // Pairs of `\` are escaped backslashes, so any `\` left must escape a `/`. + if (path.includes("\\") && /\\(?!\/)/.test(path.replaceAll("\\\\", ""))) { + return "path contains a `\\` that escapes neither `\\` nor `/`"; + } + return undefined; +} + /** Split a path into its slash-separated parts. The empty path has no parts. */ export function parts(path: Valid): string[] { return path === "" ? [] : path.split("/"); @@ -216,20 +230,24 @@ export function empty(): Valid { } /** - * Normalize a relative path reference: trim leading/trailing slashes, drop empty - * segments, and drop redundant `.` segments. A reference made only of `.` segments - * normalizes to `.` because it names the base's parent, while empty names the base. - * `..` is preserved and only interpreted by {@link resolve}. - * - * Mirrors the Rust `path::Relative::new` normalization, so JS and Rust agree - * byte-for-byte on the stored form. Two callers comparing normalized strings can - * detect equivalent references while preserving the distinction between `""` and `"."`. + * Parse a relative path reference and drop its redundant `.` segments. It is spelled like + * a path, so this throws on a leading, trailing, or doubled `/` or a lone `\` rather than + * collapsing it. A reference made only of `.` segments normalizes to `.` because it names + * the base's parent, while empty names the base. `..` is preserved and only interpreted + * by {@link resolve}. + * + * Mirrors the Rust `path::Relative::new`, so JS and Rust agree byte-for-byte on the stored + * form. Two callers comparing normalized strings can detect equivalent references while + * preserving the distinction between `""` and `"."`. */ export function normalizeRelative(rel: string): Relative { + from(rel); const raw = rel.split("/"); - const normalized = raw.filter((s) => s !== "" && s !== ".").join("/"); + if (!raw.includes(".")) return rel as Relative; - return (normalized === "" && raw.includes(".") ? "." : normalized) as Relative; + const normalized = raw.filter((s) => s !== ".").join("/"); + // Dropping a `.` after an escaped `/` strands its `\`, so the result is checked again. + return from(normalized === "" ? "." : normalized) as string as Relative; } /** @@ -237,9 +255,12 @@ export function normalizeRelative(rel: string): Relative { * * A non-empty reference replaces the last segment of the base, matching relative URL * resolution. `..` segments then pop another segment; other segments are appended. - * `.` and empty segments are no-ops. Excess `..` once the base is empty is also a no-op + * `.` segments are no-ops. Excess `..` once the base is empty is also a no-op * (subsequent named segments still append). An empty `rel` returns the base unchanged. * + * Throws when the result ends a segment in a lone `\` because resolution dropped the + * escaped `/` after it. + * * Mirrors the Rust `Path::resolve`, used by hang catalogs to express * cross-broadcast track references (a rendition's `broadcast` field). * @@ -257,7 +278,7 @@ export function resolve(base: Valid, rel: Relative): Valid { segments.pop(); for (const seg of rel.split("/")) { - if (seg === "" || seg === ".") { + if (seg === ".") { continue; } if (seg === "..") { @@ -267,11 +288,12 @@ export function resolve(base: Valid, rel: Relative): Valid { } } - return segments.join("/") as Valid; + return from(segments.join("/")); } /** - * Resolve a relative path, returning `undefined` if it escapes above the root. + * Resolve a relative path, returning `undefined` if it escapes above the root or + * {@link resolve} would throw. * * Unlike {@link resolve}, this distinguishes a valid reference to the empty root * path from excess `..` segments. Use it for untrusted catalog references that @@ -284,7 +306,7 @@ export function tryResolve(base: Valid, rel: Relative): Valid | undefined { segments.pop(); for (const seg of rel.split("/")) { - if (seg === "" || seg === ".") { + if (seg === ".") { continue; } if (seg === "..") { @@ -294,7 +316,8 @@ export function tryResolve(base: Valid, rel: Relative): Valid | undefined { } } - return segments.join("/") as Valid; + const resolved = segments.join("/"); + return spelling(resolved) === undefined ? (resolved as Valid) : undefined; } /** diff --git a/js/room/src/path.test.ts b/js/room/src/path.test.ts index dd0e705c2ec..2cd99047641 100644 --- a/js/room/src/path.test.ts +++ b/js/room/src/path.test.ts @@ -23,6 +23,9 @@ test("parse rejects a path with no kind or no identity", () => { expect(parse(p("alice/chat"))).toBeUndefined(); expect(parse(p("camera"))).toBeUndefined(); expect(parse(p(""))).toBeUndefined(); + // A peer's escaped `/` is not the separator before a kind. + expect(parse(p("alice\\/camera"))).toBeUndefined(); + expect(parse(p("a\\/b/camera"))).toEqual({ identity: p("a\\/b"), kind: "camera" }); }); test("kindFromSegment and isKind", () => { diff --git a/js/room/src/path.ts b/js/room/src/path.ts index 0edbf4a9afb..17981461754 100644 --- a/js/room/src/path.ts +++ b/js/room/src/path.ts @@ -47,7 +47,8 @@ export function kindFromSegment(segment: string): Kind | undefined { * Split a room-relative broadcast path into identity and kind. * * The last segment is the kind; everything before it is the identity. Returns - * `undefined` when there is no identity, or the last segment is not a kind. + * `undefined` when there is no identity, the last segment is not a kind, or the `/` + * before it is escaped. */ export function parse(path: Path.Valid): Parsed | undefined { const parts = Path.parts(path); @@ -56,7 +57,12 @@ export function parse(path: Path.Valid): Parsed | undefined { const kind = kindFromSegment(parts[parts.length - 1]); if (!kind) return undefined; - return { identity: Path.from(...parts.slice(0, -1)), kind }; + try { + // Only an identity ending in half an escape is refused, when that `/` was escaped. + return { identity: Path.from(path.slice(0, path.lastIndexOf("/"))), kind }; + } catch { + return undefined; + } } /** The broadcast path a participant publishes for `kind`. */ diff --git a/quest/m1/api-audit/README.md b/quest/m1/api-audit/README.md index 11766b5445d..f625a23c2cd 100644 --- a/quest/m1/api-audit/README.md +++ b/quest/m1/api-audit/README.md @@ -76,7 +76,6 @@ Rejected in planning: - [Two broadcast resolutions](/quest/m1/api-audit/resolve/README.md) - Rust, JS, and the bindings resolve a broadcast with `request(path, epoch)` or `follow(path, epoch)`, and every racy two-step caller is gone - [Bindings default to zero max delay](/quest/m1/api-audit/binding-max-delay.md) - a no-argument subscription skips superseded groups in every binding, as Rust, JS, and Python do, and no subscriber limit is asked for explicitly - [Demand is wanted, everywhere](/quest/m1/api-audit/wanted.md) - every `used`/`unused` pair becomes `wanted()` and `wanted_changed()`, and unused ones go -- [One strict path rule](/quest/m1/api-audit/strict-paths.md) - `Relative` refuses empty segments, `Path` refuses non-canonical escapes, the error is `path::Error`, and auth mints only strict paths - [QMux datagrams obey or refuse their policy](/quest/m1/api-audit/qmux-datagrams.md) - `Session.datagrams` is the DOM interface, its high-water mark is live, and the inert settings throw - [No constructible internal handles](/quest/m1/api-audit/internal-constructors.md) - `Rendition`, `Lister`, `Arrivals`, and e2ee's generations get private constructors, and a lint rule stops new ones - [moq-congestion's first-publish names](/quest/m1/api-audit/congestion-names.md) - `bbr3::Config`, `cubic::Config`, `Metrics`, `Factory`, and no `into_any` diff --git a/quest/m1/api-audit/strict-paths.md b/quest/m1/api-audit/strict-paths.md deleted file mode 100644 index 49f46b95665..00000000000 --- a/quest/m1/api-audit/strict-paths.md +++ /dev/null @@ -1,47 +0,0 @@ -# [M] One strict path rule, minted and parsed - -## Goal - -`path::Relative` refuses empty segments as `Path::new` does, `Path` refuses a -non-canonical escape, the path error is `path::Error`, and moq-auth and @moq/auth mint only roots and paths that parse -strictly, so a token can never grant a path no peer can spell. - -## Plan - -Follows [moq-dev/moq#6648](https://github.com/moq-dev/moq/pull/6648), which -made `Path::new` strict and introduced `path::Invalid { TooLong, EmptySegment }`. - -Decided 2026-10-27 (maintainer): - -- `path::Relative` refuses a leading, trailing, or doubled `/`, so both path - types share one rule. -- Rename `path::Invalid` to `path::Error`, same variants: the module's error, - named the way `encode::Config` is. -- #6648's other choices stay: relay grant-field path errors are `Forbidden`, - and `Path::from_static` panics on an invalid literal. -- moq-auth and @moq/auth refuse to mint a root or path that would not parse - strictly, in Rust and JS alike. - -Decided 2026-10-27 (maintainer), from -[moq-dev/moq#6927](https://github.com/moq-dev/moq/pull/6927): the moq-transport -namespace mapping must be symmetric, so `Path` refuses a non-canonical escape on -every wire rather than only at the IETF boundary. - -- Today `to_tuple` (`rs/moq-net/src/ietf/namespace.rs`, and `toTuple` in - `js/net/src/ietf/namespace.ts`) keeps a lone `\x` as `\x`, while `from_tuple` - escapes every backslash, so the path returns as `\\x`. Both spellings are - valid paths today, and one tuple field can decode back to only one of them. -- `Path` (and `Relative`) accept a backslash only before `\` or `/`, a new - `path::Error` variant otherwise; `from_tuple`'s escape-every-backslash form - stays canonical. -- Add a round-trip property test in Rust and JS: `from_tuple(to_tuple(p)) == p` - for every valid path. - -Public API: breaking for moq-net (`path::Invalid` becomes `path::Error`, -`Relative::new` fails on empty segments, and both refuse a non-canonical -escape), moq-auth, and @moq/auth. Mirror the -names in JS and the bindings. Wire: none for moq-lite, but the hang catalog -carries `Relative` in JSON (`broadcast`, `echo.path`), so a catalog spelling one -with a leading, trailing, or doubled `/` stops parsing. Our publishers never -emit those forms. @moq/net's `Path.normalizeRelative` and the hang draft's -`broadcast` field adopt the same rule. diff --git a/rs/moq-auth/src/error.rs b/rs/moq-auth/src/error.rs index cd1d3655c4f..60e3f608f7d 100644 --- a/rs/moq-auth/src/error.rs +++ b/rs/moq-auth/src/error.rs @@ -88,6 +88,10 @@ pub enum Error { #[error("no publish or subscribe allowed; token is useless")] UselessToken, + /// A root being minted into a token or key scope is not spelled as a path must be. + #[error("invalid root {0}")] + InvalidRoot(String), + /// The path does not overlap the token's root. #[error("path `{0}` does not overlap the token root")] RootMismatch(String), diff --git a/rs/moq-auth/src/key.rs b/rs/moq-auth/src/key.rs index bce12e15c50..8794f1e4a11 100644 --- a/rs/moq-auth/src/key.rs +++ b/rs/moq-auth/src/key.rs @@ -633,11 +633,16 @@ impl Key { } /// Sign the claims with this key, returning the encoded token. + /// + /// Fails with [`Error::InvalidRoot`](crate::Error::InvalidRoot) on a root spelled with an + /// empty segment, a `\` escaping anything but `\` or `/`, or more than 4096 bytes. + /// Verifying stays lenient on the empty segments, so tokens minted before stay valid. pub fn sign(&self, payload: &Claims) -> crate::Result { if !self.operations.contains(&KeyOperation::Sign) { return Err(KeyError::SignUnsupported.into()); } + crate::path::validate(&payload.root)?; payload.validate()?; self.validate_scope(payload)?; @@ -657,8 +662,9 @@ impl Key { /// Derive a key with an authorization scope attached, capping what its tokens may grant. /// /// The scope is validated here, and it is the only way to set one, so a key can never carry a - /// scope that permits nothing. + /// scope that permits nothing, nor a root [`sign`](Self::sign) would refuse. pub fn with_scope(mut self, scope: crate::Scope) -> crate::Result { + crate::path::validate(&scope.root)?; scope.validate()?; self.jwk.scope = Some(scope); Ok(self) @@ -945,6 +951,40 @@ mod tests { assert!(matches!(scoped.verify(&forged), Err(crate::Error::ScopeExceeded))); } + /// A token or key scope never names a root no peer can spell, though one already minted + /// with boundary slashes still verifies. + #[test] + fn test_key_mints_only_strict_roots() { + let key = create_test_key(); + let claims = |root: &str| Claims { + root: root.into(), + publish: patterns(&["**"]), + ..Default::default() + }; + let scope = |root: &str| crate::Scope { + root: root.into(), + publish: patterns(&["**"]), + subscribe: patterns(&[]), + }; + + for root in ["/room", "room/", "a//b", r"a\b"] { + assert!( + matches!(key.sign(&claims(root)), Err(crate::Error::InvalidRoot(_))), + "{root:?}" + ); + assert!( + matches!(key.clone().with_scope(scope(root)), Err(crate::Error::InvalidRoot(_))), + "{root:?}" + ); + } + + let legacy = sign_raw(&key, serde_json::json!({"root": "/room/", "publish": ["**"]})); + assert_eq!( + key.verify(&legacy).unwrap().authorize("room").unwrap().publish, + patterns(&["**"]) + ); + } + /// A key's crypto material is derived once and cached, so the fields it was derived from must /// stay fixed. Changing the algorithm means building a new key, which derives fresh material. #[test] diff --git a/rs/moq-auth/src/path.rs b/rs/moq-auth/src/path.rs index 8cf0ea68a24..ba9c2cbb98b 100644 --- a/rs/moq-auth/src/path.rs +++ b/rs/moq-auth/src/path.rs @@ -5,9 +5,33 @@ //! from this crate so a token minting service does not pull in the wire stack. The //! 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. +//! a string spelled with one is refused there rather than read as another path. Minting +//! checks a root with [`validate`] instead, so a token never names a path no peer can spell. //! -//! Every function below assumes its arguments are already [`normalize`]d. +//! Every function below but [`validate`] assumes its arguments are already [`normalize`]d. + +/// The most bytes a path may hold, matching `moq_net::Path::MAX_BYTES`. +const MAX_BYTES: usize = 4096; + +/// Refuse a root `moq_net::Path::new` would: one longer than 4096 bytes, with a leading, +/// trailing, or doubled `/`, or with a `\` before anything but `\` or `/`. +pub fn validate(root: &str) -> crate::Result<()> { + let invalid = |reason: &str| Err(crate::Error::InvalidRoot(format!("{root:?} {reason}"))); + if root.len() > MAX_BYTES { + return invalid("is longer than 4096 bytes"); + } + if !root.is_empty() && (root.starts_with('/') || root.ends_with('/') || root.contains("//")) { + return invalid("contains an empty segment"); + } + let mut rest = root.as_bytes(); + while let Some(i) = rest.iter().position(|&b| b == b'\\') { + match rest.get(i + 1) { + Some(b'\\' | b'/') => rest = &rest[i + 2..], + _ => return invalid(r"contains a `\` that escapes neither `\` nor `/`"), + } + } + Ok(()) +} /// Trim leading and trailing slashes and collapse consecutive ones, so all /// slashes are implicit at boundaries and `/foo//bar/` == `foo/bar`. @@ -46,6 +70,18 @@ mod tests { assert_eq!(normalize("//foo///bar//"), "foo/bar"); } + #[test] + fn test_validate() { + for root in ["", "foo", "foo/bar", r"a\\b", r"a\/b", ".hidden/x"] { + assert!(validate(root).is_ok(), "{root:?}"); + } + let long = "a".repeat(MAX_BYTES + 1); + for root in ["/", "/foo", "foo/", "foo//bar", r"a\b", r"a\", long.as_str()] { + assert!(matches!(validate(root), Err(crate::Error::InvalidRoot(_))), "{root:?}"); + } + assert!(validate(&long[1..]).is_ok()); + } + #[test] fn test_strip_prefix() { assert_eq!(strip_prefix("foo/bar", ""), Some("foo/bar")); diff --git a/rs/moq-ffi/src/consumer.rs b/rs/moq-ffi/src/consumer.rs index 574e0e6f02c..1e6f3903cd7 100644 --- a/rs/moq-ffi/src/consumer.rs +++ b/rs/moq-ffi/src/consumer.rs @@ -237,8 +237,7 @@ impl MoqBroadcastConsumer { ) -> Result { self.driver .watch(async { - // Normalize before testing emptiness: this is a caller-supplied string, and one made only - // of slashes normalizes to the empty reference, which names this broadcast. + // A caller-supplied string, refused unless it is spelled as a catalog's reference must be. let reference = reference .map(moq_net::path::Relative::new) .transpose() diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index e42f1728355..5e181929271 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -3338,11 +3338,10 @@ async fn resolve_rejects_a_reference_without_an_origin() { let consumer = MoqBroadcastConsumer::new(broadcast.consume()); // An absent or empty reference still names this broadcast, so it needs no origin. A reference - // made only of slashes normalizes to the empty one, so it must be treated the same rather than - // looking like a cross-broadcast reference. + // made only of slashes is refused rather than read as the empty one. consumer.resolve(referencing(None, None)).await.unwrap(); consumer.resolve(referencing(Some(""), None)).await.unwrap(); - consumer.resolve(referencing(Some("/"), None)).await.unwrap(); + assert!(consumer.resolve(referencing(Some("/"), None)).await.is_err()); // Reported in normalized form, matching `EscapingBroadcast` and moq-c: it names what the // resolver actually tried to reach, not the caller's spelling of it. diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index 6bcfb713078..eede46d4122 100644 --- a/rs/moq-net/MIGRATE.md +++ b/rs/moq-net/MIGRATE.md @@ -22,14 +22,14 @@ A group a subscription or fetch handed out now keeps its track wanted until the ### Paths fit moq-transport's 4096-byte namespace ([#6927](https://github.com/moq-dev/moq/pull/6927)) -`Path::MAX_BYTES` is 4096, moq-transport's namespace limit, so every path fits every wire and building a longer one fails with `path::Invalid::TooLong`. Keep broadcast paths within 4096 bytes. +`Path::MAX_BYTES` is 4096, moq-transport's namespace limit, so every path fits every wire and building a longer one fails with `path::Error::TooLong`. Keep broadcast paths within 4096 bytes. A moq-transport namespace whose escaped path would exceed it is refused when decoded, and a SUBSCRIBE, TRACK\_STATUS, FETCH, or PUBLISH whose namespace and track name exceed 4096 bytes together fails that request with `Error::Encode` instead of being sent. ### 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. +`Path::new` and `TryFrom` fail with `path::Error::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. +`Path::from_escaped` is gone, and `path::TooLong` is `path::Error::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)) @@ -45,8 +45,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::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. +- `Path::new`, `Relative::new`, `Path::join`, and `Path::resolve` return `Result<_, path::Error>`, 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::Error` 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/src/error.rs b/rs/moq-net/src/error.rs index 013b9351c22..502558ff355 100644 --- a/rs/moq-net/src/error.rs +++ b/rs/moq-net/src/error.rs @@ -381,6 +381,10 @@ pub enum Error { #[error("path contains an empty segment")] EmptySegment, + /// A path was spelled with a `\` before anything but `\` or `/`. + #[error(r"path contains a `\` that escapes neither `\` nor `/`")] + InvalidEscape, + /// A duplicate ID was used // The broadcast/track is a duplicate #[error("duplicate")] @@ -657,7 +661,8 @@ impl From<&Error> for SessionError { | Error::BoundsExceeded(_) | Error::InvalidPath(_) | Error::NavigationSegment - | Error::EmptySegment => Self::ProtocolViolation, + | Error::EmptySegment + | Error::InvalidEscape => 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 @@ -723,6 +728,7 @@ impl From<&Error> for StreamError { | Error::InvalidPath(_) | Error::NavigationSegment | Error::EmptySegment + | Error::InvalidEscape | 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 86018d5e628..5fc3ea8dd11 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -168,17 +168,18 @@ where /// 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. +/// stats grouping and announce compression derive from it. Only a run ending at an +/// escaped `/` has no head, since it would end in half an escape. 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:?}" - ); + let cut = path.parts().take(n).collect::>().join("/"); + match path.head(n) { + Some(head) => assert_eq!(Path::new(&cut), Ok(head), "{path:?} has a wrong head at {n}"), + None => assert!(cut.ends_with('\\'), "{path:?} has no head at {n}"), + } } assert!(path.head(count + 1).is_none(), "{path:?} has a head past its segments"); } @@ -1048,13 +1049,15 @@ pub fn path(data: &[u8]) -> bool { let (target, base) = text.split_once('\n').unwrap_or((text, "")); - // A spelling is refused exactly when it is over the limit or has an empty segment, so - // each path has one spelling. + // A spelling is refused exactly when it is over the limit, has an empty segment, or has + // a `\` escaping anything but `\` or `/`, so each path has one spelling. for s in [target, base] { let expected = if s.len() > Path::MAX_BYTES { - Err(crate::path::Invalid::TooLong) + Err(crate::path::Error::TooLong) } else if !s.is_empty() && s.split('/').any(str::is_empty) { - Err(crate::path::Invalid::EmptySegment) + Err(crate::path::Error::EmptySegment) + } else if s.replace(r"\\", "").replace(r"\/", "").contains('\\') { + Err(crate::path::Error::InvalidEscape) } else { Ok(()) }; @@ -1107,9 +1110,24 @@ pub fn path(data: &[u8]) -> bool { // Resolving arbitrary references must stay inside the clamped/unclamped contract: // `try_resolve` only refuses by walking above the root, so whenever it answers, it // answers the same as `resolve`. - let rel = Relative::new(base.as_str()).expect("a reference no longer than its path fits"); - if let Some(resolved) = target.try_resolve(&rel) { - assert_eq!(Ok(resolved), target.resolve(&rel), "try_resolve disagreed with resolve"); + match Relative::new(base.as_str()) { + Ok(rel) => { + if let Some(resolved) = target.try_resolve(&rel) { + assert_eq!(Ok(resolved), target.resolve(&rel), "try_resolve disagreed with resolve"); + } + } + // Only dropping a `.` segment can make a valid path an invalid reference, by stranding + // the `\` of the escaped `/` before it. + Err(err) => assert_eq!(err, crate::path::Error::InvalidEscape, "{base:?} is no reference"), + } + + // The moq-transport namespace tuple is the same path back, so the mapping is symmetric. + let mut tuple = Vec::new(); + let form = Form::LeadingOnes; + if ietf::namespace::encode_namespace(&mut Encoder::new(&mut tuple, form), &target).is_ok() { + let decoded = ietf::namespace::decode_namespace(&mut Decoder::new(&tuple, form)) + .expect("could not decode our own namespace"); + assert_eq!(decoded, target, "path did not survive the namespace tuple"); } // The wire form is the same path back, whenever the path is expressible at all. diff --git a/rs/moq-net/src/ietf/namespace.rs b/rs/moq-net/src/ietf/namespace.rs index 084c4fa49c6..2de3c63f3fc 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::Invalid}; +use crate::{Path, coding::*}; /// The escaped text of each tuple field in `path`, split at every `/` no escape covers. /// @@ -26,8 +26,8 @@ fn escaped_fields(path: &str) -> impl Iterator { }) } -/// A field's bytes with its `\/` and `\\` escapes undone, in pieces. Any other -/// backslash stays, as written. +/// A field's bytes with its escapes undone, in pieces. Every `\` in a valid path escapes +/// the `\` or `/` after it. fn unescape(field: &str) -> impl Iterator { let mut rest = field.as_bytes(); // Where to look for the next escape: never at the byte an escape just produced. @@ -36,18 +36,13 @@ fn unescape(field: &str) -> impl Iterator { if rest.is_empty() { return None; } - let mut i = from; - while i + 1 < rest.len() { - if rest[i] == b'\\' && matches!(rest[i + 1], b'/' | b'\\') { - let piece = &rest[..i]; - rest = &rest[i + 1..]; - from = 1; - return Some(piece); - } - // Another backslash keeps the byte after it too. - i += if rest[i] == b'\\' { 2 } else { 1 }; - } - Some(std::mem::take(&mut rest)) + let Some(i) = rest[from..].iter().position(|&b| b == b'\\') else { + return Some(std::mem::take(&mut rest)); + }; + let piece = &rest[..from + i]; + rest = &rest[from + i + 1..]; + from = 1; + Some(piece) }) } @@ -55,13 +50,15 @@ fn unescaped_len(field: &str) -> usize { unescape(field).map(<[u8]>::len).sum() } -// A field ending in `/` escapes to a path with an empty segment, so it is refused, as is a -// namespace whose separators and escapes push its path past `Path::MAX_BYTES`. -fn from_tuple(parts: &[&str]) -> Result { +// Every `\` and `/` in a field is escaped, the only spelling a path accepts, so every path +// returns from its own tuple unchanged. A field ending in `/` escapes to a path with an empty +// segment, so it is refused, as is a namespace whose separators and escapes push its path +// past `Path::MAX_BYTES`. +fn from_tuple(parts: &[&str]) -> Result { // Joined on the stack, which `arrayvec` leaves uninitialized, so the path's own copy // is the only allocation, and a short path makes none. let mut path = arrayvec::ArrayVec::::new(); - let mut push = |b| path.try_push(b).map_err(|_| Invalid::TooLong); + let mut push = |b| path.try_push(b).map_err(|_| crate::path::Error::TooLong); for (i, part) in parts.iter().enumerate() { if i > 0 { push(b'/')?; @@ -297,6 +294,24 @@ mod tests { assert_eq!(encode_path(&decoded), tuple); } + /// Every valid path, `\` and `/` included, encodes to a tuple that decodes back to it, so + /// a path crossing the IETF boundary keeps its one spelling. + #[test] + fn every_path_round_trips() { + let mut texts = vec![String::new()]; + for _ in 0..10 { + texts = texts + .iter() + .flat_map(|text| ['a', '/', '\\'].map(|c| format!("{text}{c}"))) + .collect(); + for text in &texts { + if let Ok(path) = Path::new(text) { + assert_eq!(decode_ns(&encode_path(&path)), path, "{text:?}"); + } + } + } + } + #[test] fn slashes_at_part_boundaries() { // A leading `/` escapes to a non-empty `\` segment. diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 62e6c8aaada..04dbc7c587c 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -9256,7 +9256,7 @@ mod serve_tests { assert_eq!(buf.varint().unwrap(), ietf::RequestError::ID, "{version}"); let code = ietf::RequestError::decode(&mut buf, version).unwrap().error_code; let expected = request::to_code( - &Error::from(crate::path::Invalid::TooLong), + &Error::from(crate::path::Error::TooLong), request::Kind::Subscribe, version, ); diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index ffcb6815210..016640442b2 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -1866,11 +1866,7 @@ impl Default for OriginScope { /// The announce-interest prefixes that cover a pattern scope on a prefix-only /// wire: each member's literal head, minus heads another already covers. pub(crate) fn interest_prefixes(allowed: &Patterns) -> Vec { - // A head too long for a path covers none, so it asks for nothing. - let mut heads: Vec = allowed - .iter() - .filter_map(|pattern| Path::new(pattern.head()).ok()) - .collect(); + let mut heads: Vec = allowed.iter().filter_map(|pattern| covering(pattern.head())).collect(); heads.sort(); heads.dedup(); let covered = heads.clone(); @@ -1878,6 +1874,20 @@ pub(crate) fn interest_prefixes(allowed: &Patterns) -> Vec { heads } +/// The nearest path at or above a pattern head. +/// +/// A head cut at an escaped `/`, such as `a\` from `a\/*`, ends in half an escape, so it +/// falls back to its parent. A head too long for a path covers none, so it asks for nothing. +fn covering(mut head: &str) -> Option { + loop { + match Path::new(head) { + Ok(path) => return Some(path), + Err(crate::path::Error::InvalidEscape) => head = head.rsplit_once('/').map_or("", |(parent, _)| parent), + Err(_) => return None, + } + } +} + /// Which routes beneath a hidden segment an announce cursor reports. /// /// A route is hidden when a segment below the cursor's requested prefix (its @@ -8914,6 +8924,14 @@ mod tests { assert_eq!(replacement.captures, None); } + #[test] + fn interest_prefixes_fall_back_above_an_escaped_slash() { + let heads = |pattern: &str| interest_prefixes(&Patterns::from(pattern.parse::().unwrap())); + assert_eq!(heads(r"room/a\/*"), [Path::from_static("room")]); + assert_eq!(heads(r"a\/b\/*"), [Path::empty()]); + assert_eq!(heads(r"a\/b/*"), [Path::from_static(r"a\/b")]); + } + #[moq_clock::test] #[cfg(feature = "async")] async fn follow_resolves_once_announced() { diff --git a/rs/moq-net/src/path/mod.rs b/rs/moq-net/src/path/mod.rs index 4b52a80d1dc..44d1050126e 100644 --- a/rs/moq-net/src/path/mod.rs +++ b/rs/moq-net/src/path/mod.rs @@ -35,14 +35,7 @@ const _: () = assert!(size_of::() == INLINE + 2); const _: () = assert!(Path::MAX_BYTES <= u16::MAX as usize); impl Repr { - fn copy(s: &str) -> Result { - if s.len() > Path::MAX_BYTES { - return Err(Invalid::TooLong); - } - Ok(Self::copy_bounded(s)) - } - - // `copy` for a string already known to fit `MAX_BYTES`. + // A copy of a string already known to fit `MAX_BYTES`. fn copy_bounded(s: &str) -> Self { debug_assert!(s.len() <= Path::MAX_BYTES); if s.len() <= INLINE { @@ -63,7 +56,8 @@ impl Repr { /// Why a string is not a valid [`Path`] or [`Relative`]. #[derive(Debug, Copy, Clone, Eq, PartialEq, thiserror::Error)] -pub enum Invalid { +#[non_exhaustive] +pub enum Error { /// Longer than [`Path::MAX_BYTES`]. #[error("path is longer than {} bytes", Path::MAX_BYTES)] TooLong, @@ -71,19 +65,41 @@ pub enum Invalid { /// A leading, trailing, or doubled `/`, which would give one path several spellings. #[error("path contains an empty segment")] EmptySegment, + + /// A `\` before anything but `\` or `/`, which would give one namespace field several spellings. + #[error(r"path contains a `\` that escapes neither `\` nor `/`")] + InvalidEscape, } // A path is refused rather than normalized, so each one has exactly one spelling. -fn validate(s: &str) -> Result<(), Invalid> { +fn validate(s: &str) -> Result<(), Error> { if s.len() > Path::MAX_BYTES { - return Err(Invalid::TooLong); + return Err(Error::TooLong); } if !s.is_empty() && (s.starts_with('/') || s.ends_with('/') || s.contains("//")) { - return Err(Invalid::EmptySegment); + return Err(Error::EmptySegment); + } + validate_escapes(s) +} + +// A `\` escapes the byte after it, which must be `\` or `/`: the IETF namespace mapping writes +// a field's `\` and `/` that way and nothing else, so any other spelling could not round-trip. +fn validate_escapes(s: &str) -> Result<(), Error> { + let mut rest = s.as_bytes(); + while let Some(i) = rest.iter().position(|&b| b == b'\\') { + match rest.get(i + 1) { + Some(b'\\' | b'/') => rest = &rest[i + 2..], + _ => return Err(Error::InvalidEscape), + } } Ok(()) } +// Whether the byte at `end` is escaped, so a valid path cut there splits an escape in two. +fn is_escaped(s: &str, end: usize) -> bool { + s.as_bytes()[..end].iter().rev().take_while(|&&b| b == b'\\').count() % 2 == 1 +} + /// A broadcast path that provides safe prefix matching operations. /// /// This type wraps a string but provides path-aware operations that respect @@ -91,6 +107,8 @@ fn validate(s: &str) -> Result<(), Invalid> { /// /// A path is a sequence of non-empty segments joined by `/`, so it has one spelling: /// [`Path::new`] refuses a leading, trailing, or doubled `/` rather than collapsing it. +/// A `\` escapes the `\` or `/` after it, which is how a moq-transport namespace field holding +/// either one is spelled, and any other `\` is refused. Segments still split at every `/`. /// A path is at most [`Path::MAX_BYTES`] long. The empty path has no segments; it is the /// origin's root and prefixes every other path. The same type names an exact broadcast /// and the prefix a route or announcement covers, so a broadcast's own path is the prefix @@ -114,7 +132,7 @@ fn validate(s: &str) -> Result<(), Invalid> { /// /// let joined = base.join(&Path::from_static("users"))?; /// assert_eq!(joined.as_str(), "api/v1/users"); -/// # Ok::<(), moq_net::path::Invalid>(()) +/// # Ok::<(), moq_net::path::Error>(()) /// ``` #[derive(Clone)] pub struct Path(Repr); @@ -136,9 +154,10 @@ impl Path { /// Create a new Path by copying a string slice. /// /// The empty string is the root path. Anything else must be non-empty segments joined - /// by `/`: a leading, trailing, or doubled `/` fails with [`Invalid::EmptySegment`], and - /// a string longer than [`Path::MAX_BYTES`] with [`Invalid::TooLong`]. - pub fn new(s: &str) -> Result { + /// by `/`: a leading, trailing, or doubled `/` fails with [`Error::EmptySegment`], a `\` + /// before anything but `\` or `/` with [`Error::InvalidEscape`], and a string longer + /// than [`Path::MAX_BYTES`] with [`Error::TooLong`]. + pub fn new(s: &str) -> Result { validate(s)?; Ok(Self(Repr::copy_bounded(s))) } @@ -222,7 +241,8 @@ impl Path { Some(self.slice_from(n)) } - /// The first `n` segments, or `None` if the path has fewer. + /// The first `n` segments, or `None` if the path has fewer or the `/` after them is + /// escaped, since no path ends in half an escape. /// /// # Examples /// ``` @@ -232,6 +252,9 @@ impl Path { /// assert_eq!(path.head(2), Some(Path::from_static("foo/bar"))); /// assert_eq!(path.head(0), Some(Path::empty())); /// assert_eq!(path.head(4), None); + /// + /// // `foo\` would end in a lone `\`. + /// assert_eq!(Path::from_static(r"foo\/bar").head(1), None); /// ``` pub fn head(&self, n: usize) -> Option { if n == 0 { @@ -243,7 +266,10 @@ impl Path { .map(|(i, _)| i) .chain((!s.is_empty()).then_some(s.len())) .nth(n - 1)?; - // A run of whole segments of a valid path is itself valid. + if is_escaped(s, end) { + return None; + } + // Otherwise a run of whole segments of a valid path is itself valid. Some(match &self.0 { Repr::Static(s) => Path(Repr::Static(&s[..end])), _ => Path(Repr::copy_bounded(&s[..end])), @@ -314,9 +340,9 @@ impl Path { /// /// // The empty path is the identity. /// assert_eq!(base.join(&Path::empty())?.as_str(), "foo"); - /// # Ok::<(), moq_net::path::Invalid>(()) + /// # Ok::<(), moq_net::path::Error>(()) /// ``` - pub fn join(&self, other: &Path) -> Result { + pub fn join(&self, other: &Path) -> Result { if self.is_empty() { Ok(other.clone()) } else if other.is_empty() { @@ -333,10 +359,10 @@ impl Path { } // `a/b` for two non-empty valid paths, built in place. - pub(crate) fn concat(a: &str, b: &str) -> Result { + pub(crate) fn concat(a: &str, b: &str) -> Result { let len = a.len() + 1 + b.len(); if len > Self::MAX_BYTES { - return Err(Invalid::TooLong); + return Err(Error::TooLong); } if len <= INLINE { let mut buf = [0; INLINE]; @@ -364,10 +390,11 @@ impl Path { /// Excess `..` is a no-op once the base is empty (subsequent named segments still append). /// An empty `rel` returns this path unchanged. /// - /// [`Relative::new`] strips empty and redundant `.` segments, but preserves a lone `.` - /// so it can reference the base's parent. + /// [`Relative::new`] strips redundant `.` segments, but preserves a lone `.` so it can + /// reference the base's parent. /// - /// Fails if the result is longer than [`Path::MAX_BYTES`]. + /// Fails if the result is longer than [`Path::MAX_BYTES`], or ends a segment in a lone `\` + /// because resolution dropped the escaped `/` after it. /// /// # Examples /// ``` @@ -377,9 +404,9 @@ impl Path { /// assert_eq!(base.resolve(&Relative::new("./d")?)?.as_str(), "a/b/d"); /// assert_eq!(base.resolve(&Relative::new(".")?)?.as_str(), "a/b"); /// assert_eq!(base.resolve(&Relative::new("../d")?)?.as_str(), "a/d"); - /// # Ok::<(), moq_net::path::Invalid>(()) + /// # Ok::<(), moq_net::path::Error>(()) /// ``` - pub fn resolve(&self, rel: &Relative) -> Result { + pub fn resolve(&self, rel: &Relative) -> Result { if rel.is_empty() { return Ok(self.clone()); } @@ -397,11 +424,12 @@ impl Path { } } - Ok(Path(Repr::copy(&segments.join("/"))?)) + // Splitting at an escaped `/` and dropping a segment can strand its `\`. + Path::new(&segments.join("/")) } - /// Resolve a [`Relative`], returning `None` if it escapes above the root or the - /// result is longer than [`Path::MAX_BYTES`]. + /// Resolve a [`Relative`], returning `None` if it escapes above the root or + /// [`Path::resolve`] would fail. /// /// Unlike [`Path::resolve`], this distinguishes a valid reference to the empty root /// path from excess `..` segments. Use it when an untrusted relative reference must @@ -424,7 +452,7 @@ impl Path { } } - Repr::copy(&segments.join("/")).ok().map(Path) + Path::new(&segments.join("/")).ok() } /// Express this path relative to `base`: the inverse of [`Path::resolve`]. @@ -561,17 +589,17 @@ impl serde::Serialize for Path { } impl TryFrom<&str> for Path { - type Error = Invalid; + type Error = Error; - fn try_from(s: &str) -> Result { + fn try_from(s: &str) -> Result { Self::new(s) } } impl TryFrom<&String> for Path { - type Error = Invalid; + type Error = Error; - fn try_from(s: &String) -> Result { + fn try_from(s: &String) -> Result { Self::new(s) } } @@ -589,9 +617,9 @@ impl Default for Path { } impl TryFrom for Path { - type Error = Invalid; + type Error = Error; - fn try_from(s: String) -> Result { + fn try_from(s: String) -> Result { // `Arc` can't adopt a `String`'s buffer, so this copies like `new`. Self::new(&s) } @@ -609,20 +637,21 @@ impl Display for Path { } } -impl From for DecodeError { - fn from(err: Invalid) -> Self { +impl From for DecodeError { + fn from(err: Error) -> Self { match err { - Invalid::TooLong => DecodeError::BoundsExceeded, - Invalid::EmptySegment => DecodeError::InvalidValue, + Error::TooLong => DecodeError::BoundsExceeded, + Error::EmptySegment | Error::InvalidEscape => DecodeError::InvalidValue, } } } -impl From for crate::Error { - fn from(err: Invalid) -> Self { +impl From for crate::Error { + fn from(err: Error) -> Self { match err { - Invalid::TooLong => crate::Error::BoundsExceeded(crate::BoundsExceeded), - Invalid::EmptySegment => crate::Error::EmptySegment, + Error::TooLong => crate::Error::BoundsExceeded(crate::BoundsExceeded), + Error::EmptySegment => crate::Error::EmptySegment, + Error::InvalidEscape => crate::Error::InvalidEscape, } } } @@ -690,10 +719,10 @@ impl<'a, V> Decode<'a, V> for Patterns { /// frames. It does serialize via serde for off-wire use (e.g. as a field inside a catalog /// JSON payload, which itself travels as a track). /// -/// Normalization on creation: leading/trailing slashes are trimmed, consecutive internal -/// slashes collapse to one, and redundant `.` segments are stripped. A reference made only -/// of `.` segments normalizes to `.` rather than empty because `.` resolves to the base's -/// parent while empty resolves to the base itself. `..` is preserved for resolve time. +/// It is spelled like a [`Path`], so a leading, trailing, or doubled `/` or a lone `\` is +/// refused rather than collapsed. Redundant `.` segments are stripped on creation. A reference +/// made only of `.` segments normalizes to `.` rather than empty because `.` resolves to the +/// base's parent while empty resolves to the base itself. `..` is preserved for resolve time. /// /// # Examples /// ``` @@ -705,7 +734,10 @@ impl<'a, V> Decode<'a, V> for Patterns { /// // Redundant `.` segments are stripped on creation. /// assert_eq!(Relative::new("./a/./b")?.as_str(), "a/b"); /// assert_eq!(Relative::new(".")?.as_str(), "."); -/// # Ok::<(), moq_net::path::Invalid>(()) +/// +/// // An empty segment is refused, not collapsed. +/// assert!(Relative::new("./a/").is_err()); +/// # Ok::<(), moq_net::path::Error>(()) /// ``` #[derive(Debug, PartialEq, Eq, Hash, Clone, Default)] pub struct Relative(Path); @@ -719,17 +751,15 @@ impl serde::Serialize for Relative { impl Relative { /// Create a new `Relative` from a string slice. /// - /// Leading and trailing slashes are trimmed, consecutive internal slashes collapse to one, - /// and redundant `.` segments are stripped. See the type-level doc for the full rules. - /// Fails with [`Invalid::TooLong`] if the normalized reference is longer than [`Path::MAX_BYTES`]. - pub fn new(s: &str) -> Result { - let trimmed = s.trim_start_matches('/').trim_end_matches('/'); - - if needs_normalize_relative(trimmed) { - Ok(Self(Path(Repr::copy(&normalize_relative_segments(trimmed))?))) - } else { - Ok(Self(Path(Repr::copy(trimmed)?))) + /// Fails like [`Path::new`] on a string that is not a valid path, and strips redundant + /// `.` segments from one that is. See the type-level doc for the full rules. + pub fn new(s: &str) -> Result { + let path = Path::new(s)?; + if !path.parts().any(|seg| seg == ".") { + return Ok(Self(path)); } + // Dropping a `.` after an escaped `/` strands its `\`, so the result is checked again. + Ok(Self(Path::new(&normalize_relative_segments(s))?)) } /// The normalized path as a string slice. @@ -754,45 +784,33 @@ impl Relative { } impl TryFrom<&str> for Relative { - type Error = Invalid; + type Error = Error; - fn try_from(s: &str) -> Result { + fn try_from(s: &str) -> Result { Self::new(s) } } impl TryFrom<&String> for Relative { - type Error = Invalid; + type Error = Error; - fn try_from(s: &String) -> Result { + fn try_from(s: &String) -> Result { Self::new(s) } } impl TryFrom for Relative { - type Error = Invalid; + type Error = Error; - fn try_from(s: String) -> Result { + fn try_from(s: String) -> Result { Self::new(&s) } } -fn needs_normalize_relative(trimmed: &str) -> bool { - trimmed.split('/').any(|seg| seg.is_empty() || seg == ".") -} - -fn normalize_relative_segments(trimmed: &str) -> String { - let segments = trimmed - .split('/') - .filter(|seg| !seg.is_empty() && *seg != ".") - .collect::>() - .join("/"); - - if segments.is_empty() && trimmed.split('/').any(|seg| seg == ".") { - ".".to_string() - } else { - segments - } +// A valid path with at least one `.` segment, without its redundant ones. +fn normalize_relative_segments(s: &str) -> String { + let segments = s.split('/').filter(|seg| *seg != ".").collect::>().join("/"); + if segments.is_empty() { ".".to_string() } else { segments } } impl AsRef for Relative { @@ -828,10 +846,10 @@ mod tests { #[test] fn test_refuses_empty_segments() { for s in ["/", "//", "/foo", "foo/", "/foo/", "foo//bar", "foo/bar/", "a///b"] { - assert_eq!(Path::new(s), Err(Invalid::EmptySegment), "accepted {s:?}"); + assert_eq!(Path::new(s), Err(Error::EmptySegment), "accepted {s:?}"); assert_eq!( Path::try_from(s.to_string()), - Err(Invalid::EmptySegment), + Err(Error::EmptySegment), "accepted owned {s:?}" ); } @@ -847,6 +865,19 @@ mod tests { } } + #[test] + fn test_refuses_invalid_escapes() { + for s in [r"\", r"a\", r"\a", r"a\b", r"a\\\b", r"a\\\", r"a\.b/c"] { + assert_eq!(Path::new(s), Err(Error::InvalidEscape), "accepted {s:?}"); + assert_eq!(Relative::new(s), Err(Error::InvalidEscape), "accepted relative {s:?}"); + } + + // An escape takes exactly the byte after it, so runs of backslashes pair up. + for s in [r"\\", r"a\/b", r"\/a", r"a\\/b", r"a\\\/b", r"\\\\"] { + assert_eq!(p(s).as_str(), s); + } + } + #[test] #[should_panic(expected = "empty segment")] fn test_from_static_refuses_empty_segments() { @@ -920,6 +951,12 @@ mod tests { // A long path's head is a valid path of its own. let long = p(&format!("{}/tail", "a".repeat(INLINE + 1))); assert_eq!(long.head(1).unwrap().as_str(), "a".repeat(INLINE + 1)); + + // No head ends in half an escape, but an escaped `\` before a separator is whole. + let escaped = p(r"a\/b/c\\/d"); + assert_eq!(escaped.head(1), None); + assert_eq!(escaped.head(2), Some(p(r"a\/b"))); + assert_eq!(escaped.head(3), Some(p(r"a\/b/c\\"))); } #[test] @@ -1000,22 +1037,19 @@ mod tests { // Every constructor accepts the limit and refuses one byte more. assert_eq!(Path::new(&longest).unwrap().len(), Path::MAX_BYTES); - assert_eq!(Path::new(&longer), Err(Invalid::TooLong)); - assert_eq!(Path::try_from(longer.clone()), Err(Invalid::TooLong)); - assert_eq!(Relative::new(&longer), Err(Invalid::TooLong)); + assert_eq!(Path::new(&longer), Err(Error::TooLong)); + assert_eq!(Path::try_from(longer.clone()), Err(Error::TooLong)); + assert_eq!(Relative::new(&longer), Err(Error::TooLong)); // So do joins and resolution, which can outgrow both sides. let half = Path::new(&"a".repeat(Path::MAX_BYTES / 2)).unwrap(); let rest = Path::new(&"b".repeat(Path::MAX_BYTES / 2 - 1)).unwrap(); assert_eq!(half.join(&rest).unwrap().len(), Path::MAX_BYTES); - assert_eq!( - half.join(&Path::new(&format!("{rest}b")).unwrap()), - Err(Invalid::TooLong) - ); + assert_eq!(half.join(&Path::new(&format!("{rest}b")).unwrap()), Err(Error::TooLong)); let rel = Relative::new(&format!("x/{rest}b")).unwrap(); assert_eq!( Path::new(&format!("{half}/y")).unwrap().resolve(&rel), - Err(Invalid::TooLong) + Err(Error::TooLong) ); assert_eq!(Path::new(&format!("{half}/y")).unwrap().try_resolve(&rel), None); @@ -1136,20 +1170,25 @@ mod tests { } #[test] - fn test_path_relative_normalize() { + fn test_path_relative_refuses_empty_segments() { assert_eq!(Relative::new("foo").unwrap().as_str(), "foo"); - assert_eq!(Relative::new("/foo/").unwrap().as_str(), "foo"); - assert_eq!(Relative::new("foo//bar").unwrap().as_str(), "foo/bar"); assert_eq!(Relative::new("../foo").unwrap().as_str(), "../foo"); assert_eq!(Relative::new("../../a/b").unwrap().as_str(), "../../a/b"); assert!(Relative::new("").unwrap().is_empty()); + + for s in ["/", "/foo", "foo/", "./", "foo//bar", "./foo/", "../"] { + assert_eq!(Relative::new(s), Err(Error::EmptySegment), "accepted {s:?}"); + } } #[test] fn test_path_relative_normalizes_dot_segments() { assert_eq!(Relative::new(".").unwrap().as_str(), "."); - assert_eq!(Relative::new("././").unwrap().as_str(), "."); + assert_eq!(Relative::new("./.").unwrap().as_str(), "."); assert_eq!(Relative::new("./foo").unwrap().as_str(), "foo"); + // Dropping a `.` cannot strand the `\` of an escaped `/` before it. + assert_eq!(Relative::new(r"a\/."), Err(Error::InvalidEscape)); + assert_eq!(Relative::new(r"a\\/.").unwrap().as_str(), r"a\\"); assert_eq!(Relative::new("foo/./bar").unwrap().as_str(), "foo/bar"); assert_eq!(Relative::new("./../foo").unwrap().as_str(), "../foo"); // TryFrom takes the same normalization. @@ -1228,6 +1267,16 @@ mod tests { assert_eq!(base.resolve(&Relative::new("./b").unwrap()).unwrap().as_str(), "a/b"); } + #[test] + fn test_resolve_refuses_a_stranded_escape() { + // Segments split at an escaped `/` too, so popping past one strands its `\`. + let base = p(r"a\/b/c"); + let up = Relative::new("..").unwrap(); + assert_eq!(base.resolve(&up), Err(Error::InvalidEscape)); + assert_eq!(base.try_resolve(&up), None); + assert_eq!(base.resolve(&Relative::new("d").unwrap()).unwrap().as_str(), r"a\/b/d"); + } + #[test] fn test_try_resolve_distinguishes_root_from_escape() { let base = Path::from_static("top"); diff --git a/rs/moq-relay/src/auth.rs b/rs/moq-relay/src/auth.rs index 8ca00c431db..58afe67e076 100644 --- a/rs/moq-relay/src/auth.rs +++ b/rs/moq-relay/src/auth.rs @@ -463,9 +463,9 @@ impl Token { /// The dialed path is a URL path, whose one leading `/` is syntax rather than a /// segment. Fails with [`Error::Request`] if the rest of it is not a valid path, /// and with [`Error::Forbidden`] if a root, mount, tier, or label the grant names - /// is not: one with an empty segment or longer than [`Path::MAX_BYTES`]. + /// is not: one [`Path::new`] refuses. pub fn new(path: &str, grant: &Grant) -> Result { - let invalid = |err: moq_net::path::Invalid| Error::Forbidden(err.to_string()); + let invalid = |err: moq_net::path::Error| Error::Forbidden(err.to_string()); let root = match &grant.root { Some(root) => Path::new(root).map_err(invalid)?, None => Path::new(path.strip_prefix('/').unwrap_or(path)) diff --git a/rs/moq-relay/src/internal.rs b/rs/moq-relay/src/internal.rs index cc470defdc5..3e6a42e918d 100644 --- a/rs/moq-relay/src/internal.rs +++ b/rs/moq-relay/src/internal.rs @@ -1237,7 +1237,7 @@ mod tests { #[test] fn label_values_are_escaped() { let stats = moq_net::stats::Registry::new(Default::default()); - let tier = "a\\b\"} 9\nmoq_relay_requests_total{tier=\"x"; + let tier = "a\\\\b\"} 9\nmoq_relay_requests_total{tier=\"x"; drop( stats .tier(moq_net::stats::Tier::new(moq_net::Path::from_static(tier))) @@ -1245,7 +1245,7 @@ mod tests { ); let body = render_metrics(&stats.snapshot(), &[], &[]); assert!( - body.contains(r#"moq_relay_requests_total{tier="a\\b\"} 9\nmoq_relay_requests_total{tier=\"x"} 1"#), + body.contains(r#"moq_relay_requests_total{tier="a\\\\b\"} 9\nmoq_relay_requests_total{tier=\"x"} 1"#), "{body}" ); assert!( diff --git a/rs/moq-relay/src/lib.rs b/rs/moq-relay/src/lib.rs index 2bdc0076ef2..fd896f264e6 100644 --- a/rs/moq-relay/src/lib.rs +++ b/rs/moq-relay/src/lib.rs @@ -55,7 +55,7 @@ pub const DEFAULT_DRAIN_TIMEOUT: std::time::Duration = std::time::Duration::from /// Resolve an optional stats tier label. An absent or empty label selects the /// default unprefixed tier. -fn configured_tier(label: Option) -> Result { +fn configured_tier(label: Option) -> Result { match label { Some(label) => Ok(moq_net::stats::Tier::new(moq_net::Path::new(&label)?)), None => Ok(Default::default()), diff --git a/rs/moq-room/MIGRATE.md b/rs/moq-room/MIGRATE.md index 9b47df9f5d5..aa6ebd7cc57 100644 --- a/rs/moq-room/MIGRATE.md +++ b/rs/moq-room/MIGRATE.md @@ -6,7 +6,7 @@ How to update your code for each breaking change, newest first. ### `moq_net::Path` is bounded -`broadcast_path` returns `Result`, failing when the identity and kind exceed `moq_net::Path::MAX_BYTES`. `claims` takes `impl TryInto>` and fails with `Error::Net` on a room or identity that long. +`broadcast_path` returns `Result`, failing when the identity and kind exceed `moq_net::Path::MAX_BYTES`. `claims` takes `impl TryInto>` and fails with `Error::Net` on a room or identity that long. ### `chat::PRIORITY` is signed (#6142) diff --git a/rs/moq-room/src/path.rs b/rs/moq-room/src/path.rs index d2b9033b8d6..109eb8358e9 100644 --- a/rs/moq-room/src/path.rs +++ b/rs/moq-room/src/path.rs @@ -4,7 +4,7 @@ //! `alice/camera`, `alice/camera.hang`, and `guest/uuid/screen` are all valid. //! A `.hang` suffix on the kind is optional and equivalent. -use moq_net::{Path, path::Invalid}; +use moq_net::{Path, path}; /// The two broadcasts each participant may publish. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -46,21 +46,21 @@ pub fn kind_from_segment(segment: &str) -> Option { /// Split a room-relative broadcast path into identity and kind. /// -/// Returns `None` when there is no identity, or the last segment is not a kind. +/// Returns `None` when there is no identity, the last segment is not a kind, or the +/// `/` before it is escaped. pub fn parse(path: impl Into) -> Option { let path: Path = path.into(); - let mut parts: Vec<&str> = path.parts().collect(); - let last = parts.pop()?; - if parts.is_empty() { + let count = path.parts().count(); + if count < 2 { return None; } - let kind = kind_from_segment(last)?; - let identity = Path::new(&parts.join("/")).expect("a prefix of a path fits"); + let kind = kind_from_segment(path.parts().last()?)?; + let identity = path.head(count - 1)?; Some(Parsed { identity, kind }) } /// The broadcast path a participant publishes for `kind`, failing if it is too long. -pub fn broadcast_path(identity: impl Into, kind: Kind) -> Result { +pub fn broadcast_path(identity: impl Into, kind: Kind) -> Result { let identity: Path = identity.into(); identity.join(&Path::new(&format!("{}.hang", kind.as_str()))?) } @@ -120,6 +120,8 @@ mod tests { assert_eq!(parse(Path::from_static("alice/chat")), None); assert_eq!(parse(Path::from_static("camera")), None); assert_eq!(parse(Path::empty()), None); + // A peer's escaped `/` is not the separator before a kind. + assert_eq!(parse(Path::from_static(r"alice\/camera")), None); } #[test] diff --git a/rs/moq-rtc/src/server/whep.rs b/rs/moq-rtc/src/server/whep.rs index 5acfe81b1cf..72602221077 100644 --- a/rs/moq-rtc/src/server/whep.rs +++ b/rs/moq-rtc/src/server/whep.rs @@ -222,7 +222,9 @@ async fn resolve(subscriber: &moq_net::origin::Consumer, path: moq_net::Path) -> #[cfg(feature = "server")] fn status_for(err: &Error) -> StatusCode { match err { - Error::InvalidSdp(_) | Error::Moq(moq_net::Error::EmptySegment) => StatusCode::BAD_REQUEST, + Error::InvalidSdp(_) | Error::Moq(moq_net::Error::EmptySegment | moq_net::Error::InvalidEscape) => { + StatusCode::BAD_REQUEST + } Error::UnsupportedCodec(_) => StatusCode::UNSUPPORTED_MEDIA_TYPE, Error::SessionNotFound => StatusCode::NOT_FOUND, Error::Moq(moq_net::Error::Unauthorized) => StatusCode::UNAUTHORIZED, diff --git a/rs/moq-rtc/src/server/whip.rs b/rs/moq-rtc/src/server/whip.rs index 608df548c60..23b4fcfc680 100644 --- a/rs/moq-rtc/src/server/whip.rs +++ b/rs/moq-rtc/src/server/whip.rs @@ -193,7 +193,9 @@ fn is_sdp(headers: &HeaderMap) -> bool { #[cfg(feature = "server")] fn status_for(err: &Error) -> StatusCode { match err { - Error::InvalidSdp(_) | Error::Moq(moq_net::Error::EmptySegment) => StatusCode::BAD_REQUEST, + Error::InvalidSdp(_) | Error::Moq(moq_net::Error::EmptySegment | moq_net::Error::InvalidEscape) => { + StatusCode::BAD_REQUEST + } Error::UnsupportedCodec(_) => StatusCode::UNSUPPORTED_MEDIA_TYPE, Error::SessionNotFound => StatusCode::NOT_FOUND, Error::Moq(moq_net::Error::Unauthorized) => StatusCode::UNAUTHORIZED, diff --git a/rs/moq-stats/src/produce.rs b/rs/moq-stats/src/produce.rs index 3417359f932..ab2855283f4 100644 --- a/rs/moq-stats/src/produce.rs +++ b/rs/moq-stats/src/produce.rs @@ -1280,7 +1280,8 @@ struct SideSlots { seen: u64, } -/// The leading `depth` segments of `path`, the group it publishes under. +/// The leading `depth` segments of `path`, the group it publishes under, or the whole path +/// when it has no such head: it is shallower, or its `depth`th `/` is escaped. fn group_key(path: &Path, depth: usize) -> Path { path.head(depth).unwrap_or_else(|| path.clone()) } @@ -1288,7 +1289,7 @@ fn group_key(path: &Path, depth: usize) -> Path { /// `/node/`, or `/node` without a node. The fixed `node` /// category leaves room for sibling categories (e.g. `/cluster` for /// relay-mesh stats) under the same prefix. -fn advertised_path(prefix: &Path, node: Option<&Path>) -> Result { +fn advertised_path(prefix: &Path, node: Option<&Path>) -> Result { let path = prefix.join(&Path::from_static("node"))?; match node { Some(node) => path.join(node),