From 96ed01968a341c3b4de6a02bafe86e73832992b2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 28 Sep 2026 06:57:54 -0700 Subject: [PATCH 1/2] fix(net): refuse chained and wildcard origin mounts in any order origin::Producer::mount compared a new mount only against existing mount points, so a -> z then z -> secret chained while the reverse was refused, making a grant's acceptance depend on its key order. The overlap check is now symmetric, and a mount point holding a wildcard is InvalidPath. Co-Authored-By: Claude Opus 5.5 --- doc/bin/relay/auth.md | 4 +++- doc/concept/moq-lite.md | 3 ++- rs/moq-net/src/model/origin.rs | 41 +++++++++++++++++++++++++++------- rs/moq-relay/src/cluster.rs | 19 ++++++++++++++++ 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/doc/bin/relay/auth.md b/doc/bin/relay/auth.md index 26dcc47f0c6..fa819c8f01c 100644 --- a/doc/bin/relay/auth.md +++ b/doc/bin/relay/auth.md @@ -46,7 +46,9 @@ dialed path, which is how a slug aliases to a canonical id), `mounts` (optional object; each key, a path relative to the root, reads from the absolute path it maps to: `{".svc": ".svc/pid"}` resolves `.svc/foo` at `.svc/pid/foo` and presents its announcements under `.svc`, the patterns still -authorize `.svc/foo`, and nothing may be published beneath a key), `expires` +authorize `.svc/foo`, and nothing may be published beneath a key; a key +that holds a wildcard or overlaps another key or any value refuses the grant), +`expires` (optional unix seconds; the session closes then), `revalidate` (optional seconds until the relay asks again), `tier` (optional label handed to [stats](/bin/relay/config#stats)), and `peer` (optional; `true` marks another diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index 6f2985e87c3..a58dec8194d 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -167,7 +167,8 @@ relative to `root`. Nested scopes intersect with their parent. In Rust, `origin.mount(at, target)` reads the subtree at `at` from `target` instead: a request for `at/rest` joins the one front at `target/rest`, announcements under `target` present under `at`, the handle's patterns still authorize `at/rest`, -and nothing is published beneath `at`. A session receiving +and nothing is published beneath `at`. Mounts never chain: a mount point that +overlaps another mount's point or target is refused. A session receiving into that scoped origin asks for the literal heads of its allowed patterns, coalescing duplicate or nested heads. An unscoped origin still asks for the empty prefix, covering every namespace. These subscriptions include hidden routes; diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 8643499979a..cce0887d633 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -1558,9 +1558,11 @@ impl Producer { /// /// Returns [`Error::Unauthorized`] unless this producer reaches all of /// `target`, so a mount never widens a scope, and [`Error::Duplicate`] when - /// `at` or `target` overlaps a mount this producer already has: mounts never - /// chain, so a target is always read as the origin holds it. - /// [`Error::BoundsExceeded`] if either rooted path exceeds [`Path::MAX_PARTS`]. + /// `at` or `target` overlaps a mount point this producer already has, or `at` + /// overlaps an existing target: mounts never chain, in whatever order they are added, + /// so a target is always read as the origin holds it. Mounts may share a target. + /// [`Error::BoundsExceeded`] if either rooted path exceeds [`Path::MAX_PARTS`], + /// and [`Error::InvalidPath`] if `at` holds a segment no pattern can spell. pub fn mount(&self, at: impl AsPath, target: impl AsPath) -> Result { let at = self.root.join(at).to_owned(); let target = self.root.join(target).to_owned(); @@ -1570,6 +1572,8 @@ impl Producer { { return Err(BoundsExceeded.into()); } + // A mount point no pattern can spell could never be announced or authorized. + Pattern::literal(at.as_str())?; if !self .scope .allowed @@ -1577,11 +1581,15 @@ impl Producer { { return Err(Error::Unauthorized); } - if self.scope.mounts.iter().any(|mount| { - [&at, &target] - .into_iter() - .any(|path| mount.at.has_prefix(path) || path.has_prefix(&mount.at)) - }) { + // Symmetric, so the order mounts are added never changes which sets are accepted: + // no mount point overlaps another mount's point or target. Targets may overlap. + let overlaps = |a: &Path, b: &Path| a.has_prefix(b) || b.has_prefix(a); + if self + .scope + .mounts + .iter() + .any(|mount| overlaps(&at, &mount.at) || overlaps(&target, &mount.at) || overlaps(&at, &mount.target)) + { return Err(Error::Duplicate); } let mounts = self @@ -4612,7 +4620,24 @@ mod tests { // A target through a mount would read the subtree the mount shadows. assert!(matches!(mounted.mount("p2", "p1/.svc/x"), Err(Error::Duplicate))); assert!(matches!(mounted.mount("p2", "p1"), Err(Error::Duplicate))); + // A mount point on a target would chain through it, in either order. + assert!(matches!(mounted.mount(".svc/p1/x", ".other"), Err(Error::Duplicate))); + assert!(matches!(mounted.mount(".svc", ".other"), Err(Error::Duplicate))); mounted.mount("p1/.other", ".other/p1").unwrap(); + // Mount points may share a target. + mounted.mount("p2/.svc", ".svc/p1").unwrap(); + } + + /// A mount point no pattern can spell could never be announced or authorized. + #[test] + fn mount_refuses_a_wildcard_mount_point() { + let (producer, _driver) = Producer::new(Config::new(origin(1))); + for at in ["*", "p1/*", "p1/**", "p1/a*"] { + assert!( + matches!(producer.mount(at, ".svc/p1"), Err(Error::InvalidPath(_))), + "{at}" + ); + } } /// Egress through a mount counts under the path the reader named, so it diff --git a/rs/moq-relay/src/cluster.rs b/rs/moq-relay/src/cluster.rs index cfb8304d2fb..545b76b3bf6 100644 --- a/rs/moq-relay/src/cluster.rs +++ b/rs/moq-relay/src/cluster.rs @@ -2280,6 +2280,25 @@ mod tests { assert!(cluster.publisher(&token).is_none()); } + /// A grant whose mounts chain, or whose mount point is a wildcard, admits + /// nothing. Mounts apply in key order, so the chain is written with the + /// mount point on the target sorting last. + #[tokio::test] + async fn session_handles_refuse_invalid_grant_mounts() { + let cluster = new_cluster(Config::default()).expect("cluster"); + let everything = || moq_net::Patterns::from(moq_net::Pattern::all()); + let invalid: [&[(&str, &str)]; 2] = [&[(".a", "pid/.z"), (".z", "secret")], &[("*", ".svc/pid")]]; + for mounts in invalid { + let mut grant = moq_auth::Grant::new(everything(), everything()); + for (at, target) in mounts { + grant.mounts.insert((*at).into(), (*target).into()); + } + let token = auth::Token::new("/pid", &grant); + assert!(cluster.subscriber(&token).is_none(), "{mounts:?}"); + assert!(cluster.publisher(&token).is_none(), "{mounts:?}"); + } + } + /// The publish task holds only a `Weak` to its producer, so it stops when the /// last `moq_stats::Producer` clone drops. Attaching one must therefore hand /// its lifetime to the cluster: an embedder clones handles off a `Relay` and From 72c49ddfb5c0259d3379531c4eaa7d49a5e855d6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 28 Sep 2026 10:08:42 -0700 Subject: [PATCH 2/2] fix(net): refuse a mount overlapping its own target Co-Authored-By: Claude Opus 5.5 --- doc/bin/relay/auth.md | 3 ++- doc/concept/moq-lite.md | 2 +- rs/moq-net/src/model/origin.rs | 23 ++++++++++++++++------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/doc/bin/relay/auth.md b/doc/bin/relay/auth.md index fa819c8f01c..8bfe4deff93 100644 --- a/doc/bin/relay/auth.md +++ b/doc/bin/relay/auth.md @@ -47,7 +47,8 @@ dialed path, which is how a slug aliases to a canonical id), `mounts` absolute path it maps to: `{".svc": ".svc/pid"}` resolves `.svc/foo` at `.svc/pid/foo` and presents its announcements under `.svc`, the patterns still authorize `.svc/foo`, and nothing may be published beneath a key; a key -that holds a wildcard or overlaps another key or any value refuses the grant), +that holds a wildcard or overlaps another key or any value, its own included, +refuses the grant), `expires` (optional unix seconds; the session closes then), `revalidate` (optional seconds until the relay asks again), `tier` (optional label handed to diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index a58dec8194d..f1fc3ff0218 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -168,7 +168,7 @@ relative to `root`. Nested scopes intersect with their parent. In Rust, request for `at/rest` joins the one front at `target/rest`, announcements under `target` present under `at`, the handle's patterns still authorize `at/rest`, and nothing is published beneath `at`. Mounts never chain: a mount point that -overlaps another mount's point or target is refused. A session receiving +overlaps another mount's point or any target, its own included, is refused. A session receiving into that scoped origin asks for the literal heads of its allowed patterns, coalescing duplicate or nested heads. An unscoped origin still asks for the empty prefix, covering every namespace. These subscriptions include hidden routes; diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index cce0887d633..bfb20e19b58 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -1559,7 +1559,7 @@ impl Producer { /// Returns [`Error::Unauthorized`] unless this producer reaches all of /// `target`, so a mount never widens a scope, and [`Error::Duplicate`] when /// `at` or `target` overlaps a mount point this producer already has, or `at` - /// overlaps an existing target: mounts never chain, in whatever order they are added, + /// overlaps an existing target or its own: mounts never chain, in whatever order they are added, /// so a target is always read as the origin holds it. Mounts may share a target. /// [`Error::BoundsExceeded`] if either rooted path exceeds [`Path::MAX_PARTS`], /// and [`Error::InvalidPath`] if `at` holds a segment no pattern can spell. @@ -1582,13 +1582,15 @@ impl Producer { return Err(Error::Unauthorized); } // Symmetric, so the order mounts are added never changes which sets are accepted: - // no mount point overlaps another mount's point or target. Targets may overlap. + // no mount point overlaps any mount's point or target, its own included. + // Targets may overlap. let overlaps = |a: &Path, b: &Path| a.has_prefix(b) || b.has_prefix(a); - if self - .scope - .mounts - .iter() - .any(|mount| overlaps(&at, &mount.at) || overlaps(&target, &mount.at) || overlaps(&at, &mount.target)) + if overlaps(&at, &target) + || self + .scope + .mounts + .iter() + .any(|mount| overlaps(&at, &mount.at) || overlaps(&target, &mount.at) || overlaps(&at, &mount.target)) { return Err(Error::Duplicate); } @@ -4626,6 +4628,13 @@ mod tests { mounted.mount("p1/.other", ".other/p1").unwrap(); // Mount points may share a target. mounted.mount("p2/.svc", ".svc/p1").unwrap(); + // A mount point overlapping its own target is refused like any other overlap. + for (at, target) in [("a", "a/b"), ("a/b", "a"), ("a", "a")] { + assert!( + matches!(producer.mount(at, target), Err(Error::Duplicate)), + "{at} -> {target}" + ); + } } /// A mount point no pattern can spell could never be announced or authorized.