diff --git a/js/net/MIGRATE.md b/js/net/MIGRATE.md index 696b79e8349..2a0baea40e3 100644 --- a/js/net/MIGRATE.md +++ b/js/net/MIGRATE.md @@ -4,6 +4,12 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Published names refuse navigation segments + +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. + ### A listing says whether a removed track finished (#6399) `Broadcast.TracksEvent` gains `{ kind: "finished", name }`: the track left the listing because it finished. `{ kind: "removed", name }` now means it left unfinished. Handle both where you matched `"removed"`. diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index 35ef3a24553..843c75eb597 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -565,8 +565,13 @@ test("reserved namespace requests get DOES_NOT_EXIST while other dot namespaces for (const path of [".", "./child", ".session", ".session/extension", ".application", ".sessionish"]) { const pair = createMockTransportPair(ALPNS[version]); const { pub, origin } = publisher(pair.server, { session: new NativeSession(pair.server, version, true) }); - const broadcast = publish(origin, Path.from(path)); - const track = broadcast.createTrack("video", { timescale: Timescale.MILLI }); + // Navigation segments cannot be published. A covering application + // route still proves these reserved requests are refused before lookup. + const navigation = path.split("/")[0] === "."; + const claim = navigation ? origin.dynamic(Path.empty()) : undefined; + const requested = claim?.requested().next(); + const broadcast = navigation ? undefined : publish(origin, Path.from(path)); + const track = broadcast?.createTrack("video", { timescale: Timescale.MILLI }); const request = requestStream(version); try { await pub.runTrackStatusRequest(trackStatus(path), request.stream); @@ -580,9 +585,11 @@ test("reserved namespace requests get DOES_NOT_EXIST while other dot namespaces } } finally { request.cancel(); - track.close(); - broadcast.close(); + track?.close(); + broadcast?.close(); + claim?.close(); origin.close(); + if (requested) expect((await requested).done).toBe(true); } } } diff --git a/js/net/src/origin.test.ts b/js/net/src/origin.test.ts index e266ffa6733..cc09a7c40f8 100644 --- a/js/net/src/origin.test.ts +++ b/js/net/src/origin.test.ts @@ -2700,3 +2700,22 @@ test("public announcements saturate static costs and refuse invalid cost shapes" broadcast.close(); origin.close(); }); + +test("published names refuse navigation segments", () => { + const origin = new Producer(); + try { + for (const name of ["room/./cam", "room/../cam", ".", ".."]) { + const path = Path.from(name); + expect(() => origin.createBroadcast(path)).toThrow(); + expect(() => origin.dynamic(path)).toThrow(); + expect(() => wireOf(origin).receive(path, Route.default)).toThrow(); + } + const scoped = origin.scope(Path.from("room/.."), new Path.Patterns([Path.Pattern.all()])); + expect(() => scoped.createBroadcast(Path.from("cam"))).toThrow(); + for (const path of ["room/.pro/cam", "room/a..b", "room/cam"]) { + origin.createBroadcast(Path.from(path)).close(); + } + } finally { + origin.close(); + } +}); diff --git a/js/net/src/origin.ts b/js/net/src/origin.ts index 8606a3a4c4e..33154e15a2b 100644 --- a/js/net/src/origin.ts +++ b/js/net/src/origin.ts @@ -511,6 +511,13 @@ class VersionedSignal extends Signal { } } +// Navigation belongs to relative references, resolved before publication. +function publishPath(path: Path.Valid): void { + if (Path.parts(path).some((segment) => segment === "." || segment === "..")) { + throw new Error("broadcast path contains a navigation segment"); + } +} + /** Reactive backing state shared by origin producers and consumers. */ class OriginState { // Both tables decouple the application producing into the origin from the @@ -1232,6 +1239,7 @@ export class Producer implements Table { */ createBroadcast(path: Path.Valid): broadcast.Producer { path = this.#scope.path(path); + publishPath(path); const created = this.#state.created; if (!created) throw new Error("origin is closed"); @@ -1343,6 +1351,7 @@ export class Producer implements Table { #insertRoute(prefix: Path.Valid, route: Route, originated: boolean, publishes: boolean): Dynamic { prefix = this.#scope.prefix(prefix); + publishPath(prefix); const server = new ServeState(this.#scope.root, prefix, originated); server.onChange = (path) => this.#state.refresh(path); const entry: RouteEntry = { diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index fb52968088d..d3185409596 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 +### Published names refuse navigation segments + +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. +Rust reports `Error::NavigationSegment`; the existing protocol error codes carry the refusal to a peer. +Path normalization, relative references, and subscription paths retain their semantics. + ### A listing says whether a removed track finished (#6399) `broadcast::TracksEvent` gains `Finished { name }`: the track left the listing because its producer declared its end. `Removed { name }` now means it left unfinished (dropped or aborted without an end, or its route withdrawn). Handle both where you matched `Removed`. diff --git a/rs/moq-net/src/error.rs b/rs/moq-net/src/error.rs index 0f4f1e052dc..cf300d9e851 100644 --- a/rs/moq-net/src/error.rs +++ b/rs/moq-net/src/error.rs @@ -365,6 +365,10 @@ pub enum Error { #[error("invalid path: {0}")] InvalidPath(#[from] crate::InvalidPattern), + /// A published broadcast or route contains a literal `.` or `..` segment. + #[error("broadcast path contains a navigation segment")] + NavigationSegment, + /// A duplicate ID was used // The broadcast/track is a duplicate #[error("duplicate")] @@ -638,7 +642,8 @@ impl From<&Error> for SessionError { | Error::Encode(_) | Error::WrongSize | Error::BoundsExceeded(_) - | Error::InvalidPath(_) => Self::ProtocolViolation, + | Error::InvalidPath(_) + | Error::NavigationSegment => 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 @@ -700,9 +705,11 @@ impl From<&Error> for StreamError { Error::Timeout => Self::DeliveryTimeout, Error::GoingAway => Self::GoingAway, // Our own parse failure is, from the peer's side, a malformed track. - Error::Decode(_) | Error::BoundsExceeded(_) | Error::InvalidPath(_) | Error::MalformedTrack => { - Self::MalformedTrack - } + Error::Decode(_) + | Error::BoundsExceeded(_) + | Error::InvalidPath(_) + | Error::NavigationSegment + | Error::MalformedTrack => Self::MalformedTrack, Error::App(app) => Self::App(*app), // See the SessionError impl: an unregistered code carries no registry, so // re-sending the number could mistranslate it. diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 03c3cf2f3d6..1375e7ccdb1 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -1342,6 +1342,15 @@ impl RouteEntry { } } +// Published names are literal coordinates. Navigation belongs to Relative, +// resolved before publication, so a name remains stable when carried by a URL. +fn publish_path(path: &Path) -> Result<(), Error> { + if path.parts().any(|segment| matches!(segment, "." | "..")) { + return Err(Error::NavigationSegment); + } + Ok(()) +} + /// What `scope` lets a route at `claim` serve: see [`RouteEntry::reach`]. fn reach(scope: &Patterns, claim: &Pattern) -> Vec { scope @@ -2165,7 +2174,8 @@ impl Producer { /// [`mount`](Self::mount), /// [`Error::BoundsExceeded`] if the full rooted path exceeds /// [`Path::MAX_PARTS`], [`Error::InvalidPath`] if it holds a segment no - /// pattern can spell (`*` or `**`), or [`Error::Closed`] once the origin's + /// pattern can spell (`*` or `**`), [`Error::NavigationSegment`] for a literal `.` or `..`, + /// or [`Error::Closed`] once the origin's /// [`Driver`] has been dropped. pub fn create_broadcast(&self, path: impl AsPath) -> Result { let full = self.root.join(path.as_path()).to_owned(); @@ -2176,6 +2186,7 @@ impl Producer { /// A new broadcast at `full` with its announcer withheld, so it cannot announce /// until the announcer is handed back. fn create_withheld(&self, full: PathOwned) -> Result<(broadcast::Producer, Announcer), Error> { + publish_path(&full)?; if !self.scope.permits(&full) || !self.scope.publishes(&full) { return Err(Error::Unauthorized); } @@ -2537,6 +2548,7 @@ impl Announcing { /// The requested prefix as-is, refused when its subtree does not overlap the scope. fn new(producer: &Producer, prefix: impl AsPath) -> Result { let requested = producer.root.join(prefix.as_path()).to_owned(); + publish_path(&requested)?; if requested.parts().count() > Path::MAX_PARTS { return Err(BoundsExceeded.into()); } @@ -13098,6 +13110,22 @@ mod tests { let _pending = consumer.request_broadcast("room/a", None); } + #[moq_clock::test] + async fn published_names_refuse_navigation_segments() { + let producer = origin(1).produce(); + for path in ["room/./cam", "room/../cam", ".", ".."] { + assert!(producer.create_broadcast(path).is_err(), "created {path}"); + assert!(producer.announce(path, Route::default()).is_err(), "announced {path}"); + } + let scoped = producer + .scope("room/..", &Patterns::from_iter([Pattern::all()])) + .unwrap(); + assert!(scoped.create_broadcast("cam").is_err()); + for path in ["room/.pro/cam", "room/a..b", "room/cam"] { + producer.create_broadcast(path).unwrap(); + } + } + #[moq_clock::test] async fn create_broadcast_refuses_a_path_no_pattern_can_spell() { let producer = origin(1).produce();