diff --git a/doc/bin/relay/auth.md b/doc/bin/relay/auth.md index 26dcc47f0c6..8bfe4deff93 100644 --- a/doc/bin/relay/auth.md +++ b/doc/bin/relay/auth.md @@ -46,7 +46,10 @@ 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, 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 [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..f1fc3ff0218 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 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 8643499979a..bfb20e19b58 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 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. 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,17 @@ 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 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 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); } let mounts = self @@ -4612,7 +4622,31 @@ 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 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. + #[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