Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/bin/relay/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion doc/concept/moq-lite.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
50 changes: 42 additions & 8 deletions rs/moq-net/src/model/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Producer, Error> {
let at = self.root.join(at).to_owned();
let target = self.root.join(target).to_owned();
Expand All @@ -1570,18 +1572,26 @@ 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
.covers(&Patterns::from(Pattern::subtree(target.as_str())?))
{
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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions rs/moq-relay/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading