diff --git a/rs/moq-hls/MIGRATE.md b/rs/moq-hls/MIGRATE.md index ec7bd3c9c8a..6b9ce1f99cd 100644 --- a/rs/moq-hls/MIGRATE.md +++ b/rs/moq-hls/MIGRATE.md @@ -4,6 +4,16 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Denied broadcast references report authorization failure + +A rendition whose referenced broadcast is outside the source origin's scope now +returns `Error::Mux(moq_mux::Error::Moq(moq_net::Error::Unauthorized))` from +playlist (ranged too), init and segment reads, and from the segment cursor, +instead of behaving like missing media. +`Broadcaster::ready` and `Rendition::playable` settle on that refusal. Embedders +should answer it with an uncacheable 403; the HTTP server does so automatically. +Allowed renditions keep serving, and missing or disconnected media remains 404. + ### A failed timeline fails the segment cursor `export::segments::Consumer::next` returns `Err(Error::TimelineFailed { .. })` once a failed timeline's covered segments drain, instead of `Ok(None)`. diff --git a/rs/moq-hls/src/export/mod.rs b/rs/moq-hls/src/export/mod.rs index 7aa0d99ead7..ab344ef9f2b 100644 --- a/rs/moq-hls/src/export/mod.rs +++ b/rs/moq-hls/src/export/mod.rs @@ -197,8 +197,8 @@ impl Broadcaster { self.renditions.subscribe() } - /// Resolve once at least one rendition has a playable media playlist. How long to wait is - /// the caller's policy: wrap this in a timeout (or select against it) as needed. + /// Resolve once at least one rendition has a playable media playlist or a settled failure. + /// How long to wait is the caller's policy: wrap this in a timeout as needed. pub async fn ready(&self) { self.renditions.ready().await; } diff --git a/rs/moq-hls/src/export/rendition.rs b/rs/moq-hls/src/export/rendition.rs index d4d1a797fbc..e4ac4912411 100644 --- a/rs/moq-hls/src/export/rendition.rs +++ b/rs/moq-hls/src/export/rendition.rs @@ -186,6 +186,15 @@ impl Media { handle.binding.clone() } + /// The settled authorization refusal of this rendition's referenced broadcast. + fn refusal(&self) -> Option { + let handle = self.handle.lock().expect("media lock poisoned"); + match handle.binding.poll_broadcast(&kio::Waiter::noop()) { + Poll::Ready(Err(err @ moq_mux::Error::Moq(moq_net::Error::Unauthorized))) => Some(err.into()), + _ => None, + } + } + fn poll_track( &self, binding: &moq_mux::Binding, @@ -550,9 +559,12 @@ impl Rendition { kio::wait(|waiter| self.poll_playable(waiter)).await; } - /// Poll until this rendition has a renderable media playlist, or a timeline failed. + /// Poll until this rendition is playable, its media is denied, or a timeline failed. pub(crate) fn poll_playable(&self, waiter: &kio::Waiter) -> Poll<()> { self.poll_media(waiter); + if self.media.refusal().is_some() { + return Poll::Ready(()); + } if self.spans.poll_failed(waiter).is_ready() { return Poll::Ready(()); } @@ -574,6 +586,9 @@ impl Rendition { /// or this rendition's own timeline failed, rather than ending a truncated playlist with /// `EXT-X-ENDLIST`. A server should answer that with an uncacheable error. /// + /// A denied referenced broadcast propagates the underlying authorization error; it is + /// not missing media. The HTTP server answers it with 403 and no-store. + /// /// The playlist names its init segment by a hash of the bytes, so it is also `None` until /// the init is known. An inline-parameter-set codec (no catalog `description`) only learns /// it from media: await [`init`](Self::init) first. @@ -625,6 +640,8 @@ impl Rendition { /// A failed timeline fails it with [`TimelineFailed`](crate::Error::TimelineFailed), as in /// [`media_playlist`](Self::media_playlist). pub async fn playlist(&self, range: Option, query: Option<&str>) -> Result> { + // A denied reference is refused before anything else, so no other error masks it. + self.authorize()?; // Refuse an unsupported range up front rather than wait for media it can never list. if range.is_some() { self.check_range()?; @@ -669,9 +686,12 @@ impl Rendition { Ok(self.snapshot_from_window(window, finished, generation)) } - /// Why every listing of `window` refuses: the reference timeline failed, or this rendition's - /// own timeline failed before covering every row it lists or will list. + /// Why listings refuse: the referenced media is denied, or a timeline failed before + /// covering every row it lists or will list. fn failure(&self, window: &segments::Window) -> Option { + if let Some(err) = self.media.refusal() { + return Some(err); + } if let Some(err) = &window.failed { return Some(err.clone()); } @@ -681,8 +701,18 @@ impl Rendition { (!failure.late || window.segments.iter().any(|row| self.is_failed(row))).then_some(failure.err) } - /// Fail if a timeline this rendition lists failed (see [`failure`](Self::failure)). + /// Return bound media or its authorization refusal, leaving timeline state alone. + pub(crate) fn authorize(&self) -> Result> { + let binding = self.media.sync(&self.live); + match self.media.refusal() { + Some(err) => Err(err), + None => Ok(binding), + } + } + + /// Refuse denied media or a failed timeline (see [`failure`](Self::failure)). pub(crate) fn check(&self) -> Result<()> { + self.authorize()?; if let Some(err) = self.live.failure() { return Err(err); } @@ -690,15 +720,14 @@ impl Rendition { if self.spans.failure().is_none() { return Ok(()); } - self.media.sync(&self.live); match self.failure(&self.live.window()) { Some(err) => Err(err), None => Ok(()), } } - /// Whether only this rendition's own timeline failed, so the broadcast-wide listings leave it - /// out while its siblings stay available. + /// Whether this rendition failed independently of the reference timeline, so listings + /// leave it out while its siblings stay available. pub(crate) fn failed_alone(&self) -> bool { self.live.failure().is_none() && self.check().is_err() } @@ -957,7 +986,7 @@ impl Rendition { allow(dead_code, reason = "only the `server` feature uses it") )] pub(crate) async fn segment_at(&self, tag: &str, time: u64) -> Result> { - self.media.sync(&self.live); + self.authorize()?; let Some(segment) = self.live.segment_number_at(tag, time, self.timescale()) else { return Ok(None); }; @@ -1016,8 +1045,17 @@ impl Rendition { async fn track( &self, binding: &moq_mux::Binding, - ) -> Option<(moq_net::broadcast::Consumer, moq_net::track::Consumer)> { - kio::wait(|waiter| self.media.poll_track(binding, waiter)).await + ) -> Result> { + let track = kio::wait(|waiter| self.media.poll_track(binding, waiter)).await; + // The bind can be refused while this read waits. Preserve that exact instance's + // authorization error; other failed or disconnected references stay missing. + if track.is_none() + && let Poll::Ready(Err(err @ moq_mux::Error::Moq(moq_net::Error::Unauthorized))) = + binding.poll_broadcast(&kio::Waiter::noop()) + { + return Err(err.into()); + } + Ok(track) } /// The media track's name in its broadcast, which can differ from the rendition's ID. @@ -1070,7 +1108,7 @@ impl Rendition { } async fn load_init(&self) -> Result>> { - let binding = self.media.sync(&self.live); + let binding = self.authorize()?; // A transmuxed segment can cache the init while another bootstrap holds `building`, // blocked on an unfinished group. if let Some(init) = self.run().init { @@ -1094,7 +1132,7 @@ impl Rendition { let Some(sequence) = latest.or_else(|| self.live.latest_keyframe_group(&reference)) else { return Ok(None); }; - let Some((broadcast, track)) = self.track(&binding).await else { + let Some((broadcast, track)) = self.track(&binding).await? else { return Ok(None); }; let Ok(mut group) = fetch(&track, hang::timeline::Position::group(sequence), None, &broadcast).await? @@ -1121,9 +1159,10 @@ impl Rendition { /// short groups) and encodes them as a single CMAF fragment. `None` when the segment isn't in /// the playlist window, is a gap or not yet resolved for this rendition, or its groups already /// left the relay cache. After End or Restart, only the bound instance's retained media - /// drains; a replacement's media is never read under these rows. + /// drains; a replacement's media is never read under these rows. A denied referenced + /// broadcast returns its authorization error even when the requested row is absent. pub async fn segment(&self, tag: &str, segment: u64) -> Result> { - self.media.sync(&self.live); + self.authorize()?; let Some(row) = self.live.row(tag, segment) else { return Ok(None); }; @@ -1132,7 +1171,7 @@ impl Rendition { /// Fetch and transmux `row`'s frames on this rendition; see [`segment`](Self::segment). pub(crate) async fn fetch(&self, row: &segments::Row) -> Result> { - let binding = self.media.sync(&self.live); + let binding = self.authorize()?; // Read the run before resolving the row (see `restart`), so an init decoded from its // groups is never cached for a run that started while they were being fetched. let run = self.run(); @@ -1155,7 +1194,7 @@ impl Rendition { return Ok(None); } - let Some((broadcast, track)) = self.track(&binding).await else { + let Some((broadcast, track)) = self.track(&binding).await? else { return Ok(None); }; @@ -1413,6 +1452,66 @@ mod tests { producer } + #[tokio::test] + async fn pending_reads_preserve_a_later_authorization_refusal() { + let origin = moq_tokio::origin::spawn(); + let broadcast = origin.create_broadcast("room/live").unwrap(); + let dynamic = origin.dynamic_route("room/secret", Default::default()).unwrap(); + let source = moq_mux::Source::new(origin.consume(), broadcast.consume()); + let mut config = VideoConfig::new(hang::catalog::VideoCodec::VP8); + config.broadcast = Some(moq_net::path::Relative::new("./secret").to_owned()); + let rendition = Arc::new(Rendition::video("video".into(), &config, &source, Archive::default(), None).unwrap()); + let request = dynamic.requested_broadcast().await.unwrap(); + let other = origin.dynamic_route("room/hidden", Default::default()).unwrap(); + config.broadcast = Some(moq_net::path::Relative::new("./hidden").to_owned()); + let resolving = Arc::new(Rendition::video("video".into(), &config, &source, Archive::default(), None).unwrap()); + let other_request = other.requested_broadcast().await.unwrap(); + let row = |reference: &str| segments::Row { + index: 0, + segment: 0, + period: moq_net::Timestamp::ZERO, + reference: Arc::new((Kind::Video, reference.into(), reference.into())), + frames: hang::timeline::Position::group(0)..hang::timeline::Position::group(1), + keyframe: true, + duration: Duration::from_secs(1), + pts: moq_net::Timestamp::ZERO, + end: Duration::from_secs(1), + discontinuity: 0, + }; + let own = row("video"); + let segment = rendition.fetch(&own); + tokio::pin!(segment); + // The cursor waits on a window with no rows, which the refusal neither fills nor ends. + let mut cursor = rendition.segments(); + let next = cursor.next(); + tokio::pin!(next); + // This cursor waits for its own timeline to resolve another rendition's row, which the + // refusal never does either. + resolving.live.push(row("other"), None); + let mut resolving_cursor = resolving.segments(); + let resolving_next = resolving_cursor.next(); + tokio::pin!(resolving_next); + tokio::select! { + biased; + _ = &mut segment => panic!("the reference request has not been answered"), + _ = &mut next => panic!("the reference request has not been answered"), + _ = &mut resolving_next => panic!("the reference request has not been answered"), + _ = std::future::ready(()) => {} + } + request.reject(moq_net::Error::Unauthorized); + other_request.reject(moq_net::Error::Unauthorized); + for read in [ + segment.await, + next.await.map(|_| None), + resolving_next.await.map(|_| None), + ] { + assert!(matches!( + read, + Err(crate::Error::Mux(moq_mux::Error::Moq(moq_net::Error::Unauthorized))) + )); + } + } + // End may land while capture's query installs a dynamically served track. The handle must // stay on that instance rather than repeat a lookup End refuses, or its retained media is lost. #[test] diff --git a/rs/moq-hls/src/export/segments.rs b/rs/moq-hls/src/export/segments.rs index cd6dd3ec744..451edd2cb22 100644 --- a/rs/moq-hls/src/export/segments.rs +++ b/rs/moq-hls/src/export/segments.rs @@ -529,7 +529,8 @@ impl Consumer { /// /// Once a timeline fails (the reference's, or this rendition's own), the cursor drains the /// segments it still covers, then fails every call with - /// [`TimelineFailed`](crate::Error::TimelineFailed), as [`Rendition::playlist`] does. + /// [`TimelineFailed`](crate::Error::TimelineFailed), as [`Rendition::playlist`] does. A + /// denied referenced broadcast fails every call with its authorization error. /// /// Waits for the next segment to resolve on this rendition, then FETCHes and transmuxes its /// frames. A segment whose groups already left the relay cache (or that is a gap for this @@ -542,7 +543,13 @@ impl Consumer { let Some((row, resets)) = kio::wait(|waiter| self.poll_next(waiter)).await? else { return Ok(None); }; - kio::wait(|waiter| self.rendition.poll_resolved(waiter, &row)).await; + kio::wait(|waiter| { + // The refusal settles the binding, not this rendition's timeline, so watch both. + self.rendition.poll_media(waiter); + self.rendition.authorize()?; + self.rendition.poll_resolved(waiter, &row).map(Ok::<_, crate::Error>) + }) + .await?; // This rendition's timeline failed before covering the row, so no later row resolves. if let Some(err) = self.rendition.own_failure() && self.rendition.is_failed(&row) @@ -565,6 +572,11 @@ impl Consumer { fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll>> { self.poll_feed(waiter); + // A refused binding never held media, so there is nothing to drain. `poll_feed` registered + // the wake for a refusal that arrives while the window waits for rows. + if let Err(err) = self.rendition.authorize() { + return Poll::Ready(Err(err)); + } let poll = self .state .poll(waiter, |state| match state.next_after(self.after.as_ref()) { diff --git a/rs/moq-hls/src/server/routes.rs b/rs/moq-hls/src/server/routes.rs index 65db4df98f0..ff2b085a988 100644 --- a/rs/moq-hls/src/server/routes.rs +++ b/rs/moq-hls/src/server/routes.rs @@ -423,7 +423,12 @@ fn range_not_satisfiable() -> Response { fn server_error(err: crate::Error) -> Response { tracing::warn!(%err, "hls request failed"); - (StatusCode::INTERNAL_SERVER_ERROR, [(header::CACHE_CONTROL, "no-store")]).into_response() + let status = match err { + crate::Error::Moq(moq_net::Error::Unauthorized) + | crate::Error::Mux(moq_mux::Error::Moq(moq_net::Error::Unauthorized)) => StatusCode::FORBIDDEN, + _ => StatusCode::INTERNAL_SERVER_ERROR, + }; + (status, [(header::CACHE_CONTROL, "no-store")]).into_response() } #[cfg(test)] @@ -1117,6 +1122,120 @@ mod tests { } } + #[tokio::test(start_paused = true)] + async fn denied_sibling_media_answers_403_without_poisoning_allowed_siblings() { + let origin = moq_tokio::origin::spawn(); + let mut broadcast = origin.create_broadcast("room/live").unwrap(); + broadcast.announce(Default::default()).unwrap(); + let source = origin.create_broadcast("room/source").unwrap(); + source.announce(Default::default()).unwrap(); + let secret = origin.create_broadcast("room/secret").unwrap(); + secret.announce(Default::default()).unwrap(); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); + let reserved = catalog.reserve(); + let mut config = video_config(); + config.broadcast = Some(moq_net::path::Relative::new("./source").to_owned()); + let mut allowed = reserved + .video( + source.create_track("video0", timed()).unwrap(), + moq_mux::catalog::hang::Container::Legacy(moq_mux::container::Kind::Video), + config, + ) + .unwrap(); + let mut config = video_config(); + config.broadcast = Some(moq_net::path::Relative::new("./secret").to_owned()); + let mut denied = reserved + .video( + secret.create_track("video1", timed()).unwrap(), + moq_mux::catalog::hang::Container::Legacy(moq_mux::container::Kind::Video), + config, + ) + .unwrap(); + drop(reserved); + write_three_gops(&mut allowed); + write_three_gops(&mut denied); + + let patterns = ["room/live", "room/source"] + .into_iter() + .map(|path| path.parse::().unwrap()) + .collect(); + let restricted = origin.consume().scope("", &patterns).unwrap(); + let server = Server::new(restricted, Default::default()); + let broadcaster = server.broadcaster("room/live").await.unwrap(); + broadcaster.ready().await; + let allowed_uri = "/room/live/video/video0/seg/656e3d1b.0.m4s"; + let denied_uri = "/room/live/video/video1/seg/656e3d1b.0.m4s"; + let app = server.router(); + assert_eq!(oneshot(app.clone(), allowed_uri).await.status(), StatusCode::OK); + for uri in [ + denied_uri, + "/room/live/video/video1/media.m3u8", + "/room/live/video/video1/media.m3u8?start=0&end=2", + "/room/live/video/video1/init.unknown.mp4", + ] { + let response = oneshot(app.clone(), uri).await; + assert_eq!(response.status(), StatusCode::FORBIDDEN, "{uri}"); + assert_eq!(response.headers()[header::CACHE_CONTROL], "no-store"); + assert!( + axum::body::to_bytes(response.into_body(), usize::MAX) + .await + .unwrap() + .is_empty() + ); + } + let master = oneshot(app.clone(), "/room/live/master.m3u8").await; + assert_eq!(master.status(), StatusCode::OK); + let master = axum::body::to_bytes(master.into_body(), usize::MAX).await.unwrap(); + let master = String::from_utf8(master.to_vec()).unwrap(); + assert!(master.contains("video/video0/")); + assert!(!master.contains("video/video1/")); + let rendition = broadcaster.rendition(Kind::Video, "video1").unwrap(); + assert!(matches!( + rendition.segment("656e3d1b", 0).await, + Err(crate::Error::Mux(moq_mux::Error::Moq(moq_net::Error::Unauthorized))) + )); + // Broader authority can consume the referenced bytes, but cannot warm the + // restricted server's export into using them. + let broad = moq_mux::Source::new(origin.consume(), broadcast.consume()); + let referenced = broad + .resolve(Some(&moq_net::path::Relative::new("./secret")), None) + .await + .unwrap(); + let mut group = referenced.track("video1").unwrap().fetch_group(0, None).await.unwrap(); + assert!(group.read_frame().await.unwrap().is_some()); + assert_eq!(oneshot(app.clone(), denied_uri).await.status(), StatusCode::FORBIDDEN); + assert_eq!(oneshot(app.clone(), allowed_uri).await.status(), StatusCode::OK); + assert_eq!( + oneshot(app, "/room/live/video/video0/seg/656e3d1b.9999.m4s") + .await + .status(), + StatusCode::NOT_FOUND + ); + // Readiness settles even when no rendition is authorized; it must not need + // an allowed sibling to wake the server into reporting the typed refusal. + let only_catalog = origin + .consume() + .scope( + "", + &moq_net::Patterns::from("room/live".parse::().unwrap()), + ) + .unwrap(); + let all_denied = Server::new(only_catalog, Default::default()); + all_denied.broadcaster("room/live").await.unwrap().ready().await; + assert_eq!( + oneshot(all_denied.router(), denied_uri).await.status(), + StatusCode::FORBIDDEN + ); + + // A timeline failure refuses listings, but its already-retained media + // still drains. Authorization checks must not broaden that failure. + let retained = broadcaster.rendition(Kind::Video, "video0").unwrap(); + retained.fail(crate::Error::Moq(moq_net::Error::SessionClosed)); + assert!(retained.media_playlist(None).is_err()); + assert!(retained.init().await.unwrap().is_some()); + assert!(retained.segment("656e3d1b", 0).await.unwrap().is_some()); + } + #[tokio::test] async fn a_disconnected_sibling_publisher_answers_404() { let pair = lite_pair().await;