From f59cb32180b651c8a247e89320a47b22b5b12c55 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 22:44:04 -0700 Subject: [PATCH 01/24] chore(quest): claim quest/m1/ietf-token-auth.md Quest-Claim: b3f46324f9c8e42f From 9ca5acb46a49eb066a49a82992e51dfa5bb96564 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 23:24:24 -0700 Subject: [PATCH 02/24] feat(auth)!: checkpoint shared request-token grants and attachment leases Co-Authored-By: GPT-6.1 Sol --- doc/bin/relay/auth.md | 31 +- doc/concept/standard.md | 2 +- drafts/draft-lcurley-moq-auth.md | 11 + js/auth/api/index.api.md | 42 ++ js/auth/src/contract.test.ts | 17 + js/auth/src/contract.ts | 8 + quest/m1/ietf-token-auth.md | 18 + rs/moq-auth/src/client.rs | 140 +++--- rs/moq-auth/src/lib.rs | 2 +- rs/moq-auth/src/request.rs | 6 + rs/moq-auth/src/serve.rs | 67 ++- rs/moq-net/benches/auth.rs | 105 +++- rs/moq-net/src/auth.rs | 450 ++++++++++++++---- rs/moq-net/src/fuzz.rs | 1 + rs/moq-net/src/ietf/fetch.rs | 155 +++--- rs/moq-net/src/ietf/publish.rs | 10 +- rs/moq-net/src/ietf/publisher.rs | 248 +++++++++- rs/moq-net/src/ietf/publisher/tracks.rs | 68 ++- rs/moq-net/src/ietf/request_stream.rs | 8 +- rs/moq-net/src/ietf/session.rs | 3 + rs/moq-net/src/ietf/subscribe_namespace.rs | 11 +- rs/moq-net/src/ietf/subscriber.rs | 32 +- rs/moq-net/src/ietf/subscriber/tracks.rs | 65 ++- rs/moq-net/src/ietf/track.rs | 7 + rs/moq-net/src/test_interop.rs | 1 + rs/moq-relay/src/auth.rs | 2 +- .../tests/integration/auth_lifetime.rs | 89 +++- .../tests/integration/session_revalidate.rs | 2 +- 28 files changed, 1298 insertions(+), 303 deletions(-) diff --git a/doc/bin/relay/auth.md b/doc/bin/relay/auth.md index a968cf0d1ed..231f185d271 100644 --- a/doc/bin/relay/auth.md +++ b/doc/bin/relay/auth.md @@ -20,7 +20,7 @@ path patterns under a root, and the session can only see that part of the tree. ## The contract -The relay POSTs one JSON body per session event (`connect`, `revalidate`, +The relay POSTs one JSON body per session event (`connect`, `attach`, `revalidate`, `end`) to `--auth-url` and reads a grant back. The request carries everything the relay knows: a session id, its own `node` name, the transport, addresses, SNI and ALPN, the path and query exactly as dialed, a moq-transport SETUP @@ -77,7 +77,11 @@ reaches `expires` closes as `Expired` rather than `Unauthorized`, which also stops the reconnect loop: refresh the token, then reconnect with the new URL. **End.** Every close reports `end` with the reason, the duration, and the -bytes sent and received. A shutdown waits for these reports before exiting. +bytes sent and received. An event with `auth` names one attached credential; +its `end` releases only that credential. Without `auth`, `end` closes the +connection, after every attachment has reported its own end. A shutdown waits +for these reports before exiting. Auth servers must accept `attach` and handle `auth` before enabling +in-band credentials. An older server refuses `attach` before a credential is admitted. **The link.** `https://` presents the relay's `connect.tls` client certificate when one is configured, so a remote server can tell which relay is asking; @@ -174,17 +178,20 @@ The grant reports its real expiry so the client can renew in time. The relay verifies a presented JWT through the same admission path, replacing `jwt` in the original query and preserving the connection's identity. The auth -server sees another `connect` for the same session id. Attached grants revalidate -independently but never report `end`; only the connection reports its end, even -if its first credential expired earlier, after its attached grants stop. Each -attached JWT counts against its own `--limit-token` cap until the connection -ends. The initial credential remains responsible for the connection's stats tier. +server sees `attach` for the same session id, with a fresh `auth` id +that also names its revalidations and end. Attached grants revalidate and end +independently. Each JWT counts against its own `--limit-token` cap while any +credential using it remains on that connection. The initial credential remains +responsible for the connection's stats tier. Certificate sessions cannot renew a certificate in band, and public rules -cannot verify a presented token; both refuse it as unsupported. A moq-transport -`AUTHORIZATION TOKEN` on a `SUBSCRIBE` or `PUBLISH_NAMESPACE` is verified the -same way on any draft, but its grant authorizes only that request and ends with -it; it never joins the union. A `PUBLISH_NAMESPACE` grant must cover the whole -namespace: `room/**`, not just `room`. A session verifies at most +cannot verify a presented token; both refuse it as unsupported. On moq-transport, +each distinct `AUTHORIZATION TOKEN` is verified once while any open request on +that session names it. Its grant admits only requests naming that token, +independently of the session's scope. Discovery reveals only paths it grants; +a `PUBLISH_NAMESPACE` grant must cover the whole namespace: `room/**`, not just +`room`. The last naming request ending releases the lease at the auth server; +a later request verifies the token again. Expiry or revocation ends every +request naming it. A joining `FETCH` still follows its subscription. A session verifies at most 128 tokens at once, counting checks still running past the admission timeout. Later tokens wait their turn rather than being refused. Each HTTP request is admitted on its own, so an diff --git a/doc/concept/standard.md b/doc/concept/standard.md index 670612e71b6..38121aa21ae 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -32,7 +32,7 @@ subset drops. What a peer actually observes against this implementation: - **Compression.** Rust peers exchange compressed tracks when both offer the [Flate extension](/draft/moq-flate). Other peers receive one shared inflated copy. A publisher that seals the compression declaration makes that fallback unavailable: an incompatible peer is refused, preserving the sealed metadata. JavaScript does not offer the IETF extension. - **Timing.** A track whose `SUBSCRIBE_OK` declares no `TIMESCALE` is untimed. A Rust reader that never subscribed takes the units from `TRACK_STATUS`. - **Strict SETUP and parameters.** Repeated unknown `SETUP` options, GREASE included, are accepted if well-formed; a repeated known option is rejected. A `GROUP_ORDER` other than Ascending (1) or Descending (2) closes the session. An unknown message parameter, or one defined only for another message, closes the session. Other malformed control input closes the session with the draft's code too, such as a server `SETUP` carrying `PATH` or `AUTHORITY`, a namespace field ending in `/` (it would map to a path with an empty segment), a request cut short by FIN, or a second `GOAWAY` on one stream; a first `GOAWAY` on a request stream is legal. -- **One `SETUP` credential**, forwarded to the [auth server](/bin/relay/auth#the-contract) unverified. A second `SETUP` token closes the session. We advertise no token cache, so an alias reference in `SETUP` closes it with `PROTOCOL_VIOLATION`, one on a request with `UNKNOWN_AUTH_TOKEN_ALIAS`, and a registration on a request with `AUTH_TOKEN_CACHE_OVERFLOW`. Where the Rust side authorizes the peer or verifies tokens, a token on a `SUBSCRIBE` or `PUBLISH_NAMESPACE` is verified like an in-band session token and authorizes that one request alone, even where that authorization already covers it; it never widens the session. It is refused with `NOT_SUPPORTED` when nothing verifies it, when it is empty, or when its Token Type is anything but 0 (out of band). A side that does neither, JavaScript included, ignores the token. Rust refuses a request carrying more than one distinct token with `NOT_SUPPORTED`, since it verifies one; JavaScript, which verifies none, ignores them all. A token-authorized `SUBSCRIBE` still stays within the grant the peer gave us. A token-authorized `PUBLISH_NAMESPACE` needs a grant covering everything beneath the namespace, and the tracks we subscribe under it rely on that announce instead of our limit, ending with it. An update renews the token, and a refused renewal ends only that request. Rust and JavaScript send the token a route names (`Route::auth`) on its `PUBLISH_NAMESPACE`, and swapping it renews the request with `REQUEST_UPDATE`; the inline `NAMESPACE` that answers a solicited `SUBSCRIBE_NAMESPACE` carries no token. A token added to a reconnecting connection's auth handle rides every session it makes, so the announcement keeps it across a reconnect; one added to a single session's handle ends with that session. A token on any other request is decoded and grants nothing, and a request repeating one is refused with `MALFORMED_AUTH_TOKEN`. The negotiated [MoQ Auth extension](/concept/moq-lite#authorization) accepts additional session tokens in band. +- **One `SETUP` credential**, forwarded to the [auth server](/bin/relay/auth#the-contract) unverified. A second `SETUP` token closes the session. We advertise no token cache, so an alias reference in `SETUP` closes it with `PROTOCOL_VIOLATION`, one on a request with `UNKNOWN_AUTH_TOKEN_ALIAS`, and a registration on a request with `AUTH_TOKEN_CACHE_OVERFLOW`. Where the Rust side authorizes the peer or verifies tokens, each distinct request token is verified once while any open request on that session names it, and its grant authorizes only requests naming it, even where that authorization already covers it; it never widens the session. It is refused with `NOT_SUPPORTED` when nothing verifies it, when it is empty, or when its Token Type is anything but 0 (out of band). A side that does neither, JavaScript included, ignores the token. Rust refuses a request carrying more than one distinct token with `NOT_SUPPORTED`, since it verifies one; JavaScript, which verifies none, ignores them all. A token-authorized `SUBSCRIBE` still stays within the grant the peer gave us. A token-authorized `PUBLISH_NAMESPACE` needs a grant covering everything beneath the namespace, and the tracks we subscribe under it rely on that announce instead of our limit, ending with it. An update on a token-authorized request moves it to the token it carries; the old token ends with its last naming request. Expiry or revocation ends every request naming that token. The relay reports the lease end to its auth server, and a later request naming the token verifies it again. A refused renewal ends only that request. Rust and JavaScript send the token a route names (`Route::auth`) on its `PUBLISH_NAMESPACE`, and swapping it renews the request with `REQUEST_UPDATE`; the inline `NAMESPACE` that answers a solicited `SUBSCRIBE_NAMESPACE` carries no token. A token added to a reconnecting connection's auth handle rides every session it makes, so the announcement keeps it across a reconnect; one added to a single session's handle ends with that session. Rust honors a token on any supported request; discovery reveals only paths it grants, and a joining `FETCH` keeps following its subscription. A request repeating a token is refused with `MALFORMED_AUTH_TOKEN`. The negotiated [MoQ Auth extension](/concept/moq-lite#authorization) accepts additional session tokens in band. - **Pause delivery.** `FORWARD=0` on a `SUBSCRIBE` or its update drops the subscription, closing its data streams and holding nothing upstream, while the request and its slot stay. `FORWARD=1` subscribes again as a new `SUBSCRIBE` with the same filter would, so nothing carries across the pause: its `REQUEST_OK` names the live edge it joined at. A paused update may change only priority and max age. - **Refused, not fatal.** A legal request this stack does not serve is rejected on its own and the session stays up: the fetch forms above get `NOT_SUPPORTED`. Range Filters get `INVALID_FILTER` on draft 22 because the advertised filter limit is zero. On draft 22, namespaces whose first tuple field is exactly `.` or `.session` get `DOES_NOT_EXIST` before application lookup; other dot prefixes still reach the application. Subscription updates accept priority, negotiated subscriber max age, and delivery timeouts. Rust also carries range changes, keeps bounded subscriptions updatable, renews request tokens, and judges `NEW_GROUP_REQUEST` as a join (see dynamic groups below). On draft 22, widening fills cached history on a separate fetch stream, completing earlier groups and handing the newest head to its live subgroup tail; fills follow the requested group order. Other supported-but-unimplemented preferences end that subscription; a `TRACK_PROPERTY_FILTER` there closes the session with `PROTOCOL_VIOLATION`, since it belongs only to `SUBSCRIBE_TRACKS`. A namespace update may change its prefix; suffixes after the acknowledgement are relative to that new prefix. - **Objects and datagrams.** Only subgroup 0 is accepted: a stream for any other subgroup, whether its header gives the ID or takes it from the first Object ID, is stopped and the session stays up. Rust relays preserve Object IDs, gaps, object properties, FIRST\_OBJECT, and END\_OF\_GROUP status objects between moq-transport peers for subgroup 0, including across a moq-lite-07 hop between two relays that negotiate the [ietf](/draft/moq-ietf) extension. Datagrams retain their IDs, properties, priority, and status. Plain Rust readers and moq-lite peers without the extension see consecutive frames and only normal datagram payloads at Object ID 0; other datagram objects and statuses are dropped on those surfaces. JavaScript supports only normal datagrams at Object ID 0. A malformed datagram closes the session; a relayed one the downstream draft cannot carry is dropped. A relayed Timestamp leaves in the units the downstream response declares, and is dropped where it declares none, as for a plain frame; an immutable one that would change or vanish is refused. On a timed track the frame holds each object's time, so a mutable Timestamp that only repeats the immutable one is not restated; a status object has no frame, so it keeps its Timestamp among its properties. Only property 0x10 is read as a Timestamp. Property 0x06 stays opaque and keeps its value regardless of timestamp units; it is dropped between drafts 18 and 22, or refused when immutable, because draft 22 assigns it to SUBGROUP\_DELIVERY\_TIMEOUT. A status object carrying properties is refused. A `FETCH` has no status objects, so its response ends at the last frame. A group cannot resume across route changes by frame ordinal once Object IDs skip ahead of the ordinals on either route; recovery refuses it instead of splicing different objects. diff --git a/drafts/draft-lcurley-moq-auth.md b/drafts/draft-lcurley-moq-auth.md index 8acb49e65f8..9643747f428 100644 --- a/drafts/draft-lcurley-moq-auth.md +++ b/drafts/draft-lcurley-moq-auth.md @@ -105,6 +105,15 @@ When the union shrinks, the presenter SHOULD withdraw its advertisements and can A presenter that would advertise a namespace outside the union, once the tokens it presented at setup are answered, SHOULD close the session with UNAUTHORIZED instead of sending PUBLISH_NAMESPACE, since a subscription for it will never come. A publication that loses coverage because the union shrank is withdrawn, which is no reason to close the session. +## Request Tokens + +An acceptor that verifies {{moqt}} AUTHORIZATION TOKEN request parameters treats each distinct token on a session as one implicit Auth request while any open request names it. +Its grant MUST NOT join the session scope: a naming request uses that grant alone, and a request without the token gains nothing from it. +The acceptor verifies the token when its first naming request opens and ends its grant when the last naming request ends; a later request verifies it again. +Expiry or revocation ends every naming request. +A REQUEST_UPDATE carrying another token moves its request to that token's grant. +A joining FETCH retains its subscription's authority. + ## Patterns {#patterns} A grant names Path Patterns of {{moq-lite}}, with the same grammar, canonical form, and encoding as its AUTH_OK. @@ -240,6 +249,8 @@ This document requests three registrations in the "MOQT Message Types" registry ## Since draft-lcurley-moq-auth-00 (in progress) +- Request tokens share verification and lifetime per distinct token, isolated from session scope. + - AUTH_ERROR can carry GOING_AWAY, refusing an AUTH past the acceptor's GOAWAY without refusing the token. - AUTH_OK carries moq-lite Path Patterns instead of Track Namespace prefixes, and a grant is refused only when too large for one AUTH_OK. - The AUTH Setup Option moves from 0x40B60 to 0x40B70. diff --git a/js/auth/api/index.api.md b/js/auth/api/index.api.md index d8321776b27..8e14309ddc0 100644 --- a/js/auth/api/index.api.md +++ b/js/auth/api/index.api.md @@ -615,6 +615,7 @@ export type Request = infer; export const RequestSchema: ZodMiniDiscriminatedUnion<[ZodMiniObject<{ alpn: ZodMiniOptional>; + auth: ZodMiniOptional>; event: ZodMiniLiteral<"connect">; id: ZodMiniString; implementation: ZodMiniOptional>; @@ -653,6 +654,46 @@ export const RequestSchema: ZodMiniDiscriminatedUnion<[ZodMiniObject<{ }>>>; }, $strip>, ZodMiniObject<{ alpn: ZodMiniOptional>; + auth: ZodMiniString; + event: ZodMiniLiteral<"attach">; + id: ZodMiniString; + implementation: ZodMiniOptional>; + local: ZodMiniOptional>; + node: ZodMiniString; + path: ZodMiniString; + query: ZodMiniOptional>; + remote: ZodMiniOptional>; + role: ZodMiniOptional>; + server_name: ZodMiniOptional>; + tls: ZodMiniOptional; + fingerprint: ZodMiniString; + issuer: ZodMiniString; + name: ZodMiniString; + }, $strip>>; + token: ZodMiniOptional; + }, $strip>>; + transport: ZodMiniPipe, ZodMiniCatch>>; +}, $strip>, ZodMiniObject<{ + alpn: ZodMiniOptional>; + auth: ZodMiniOptional>; event: ZodMiniLiteral<"revalidate">; id: ZodMiniString; implementation: ZodMiniOptional>; @@ -691,6 +732,7 @@ export const RequestSchema: ZodMiniDiscriminatedUnion<[ZodMiniObject<{ }>>>; }, $strip>, ZodMiniObject<{ alpn: ZodMiniOptional>; + auth: ZodMiniOptional>; bytes: ZodMiniObject<{ received: ZodMiniInt; sent: ZodMiniInt; diff --git a/js/auth/src/contract.test.ts b/js/auth/src/contract.test.ts index 5f96d97d8c7..96bc8825d29 100644 --- a/js/auth/src/contract.test.ts +++ b/js/auth/src/contract.test.ts @@ -54,6 +54,23 @@ test("an end request carries its facts beside the rest", () => { expect(invalid.reason).toBe("invalid"); }); +test("an attachment has its own identity through its end", () => { + const base = { id: "connection", node: "relay", transport: "quic", path: "/room" }; + expect(() => RequestSchema.parse({ ...base, event: "attach" })).toThrow(); + const attach = RequestSchema.parse({ ...base, event: "attach", auth: "credential" }); + const end = RequestSchema.parse({ + ...base, + event: "end", + auth: "credential", + reason: "dropped", + duration: 1, + bytes: { sent: 0, received: 0 }, + }); + expect(attach.auth).toBe("credential"); + expect(end.auth).toBe(attach.auth); + expect(end.id).toBe(attach.id); +}); + test("a SETUP token parses as the Rust vector", () => { // What `moq_auth::Request` serializes a CAT of bytes 00 fb ff to. const request = RequestSchema.parse( diff --git a/js/auth/src/contract.ts b/js/auth/src/contract.ts index 93d4693566d..790b9c00d3c 100644 --- a/js/auth/src/contract.ts +++ b/js/auth/src/contract.ts @@ -98,6 +98,8 @@ export const BytesSchema = z.object({ export type Bytes = z.infer; const BaseRequestSchema = z.object({ + /** The attached credential this event concerns; absent for the connection itself. */ + auth: z.optional(z.string()), /** Random 128-bit hex, unique per session; the key every event for it shares. */ id: z.string(), /** The operator's name for the relay asking. */ @@ -142,6 +144,12 @@ export const RequestSchema = z.discriminatedUnion("event", [ /** A session was accepted and asks to be admitted. */ event: z.literal("connect"), }), + z.extend(BaseRequestSchema, { + /** Another credential asks to join the connection. */ + event: z.literal("attach"), + /** The credential's own lifecycle identity. */ + auth: z.string(), + }), z.extend(BaseRequestSchema, { /** The grant asked to be re-checked on its cadence. */ event: z.literal("revalidate"), diff --git a/quest/m1/ietf-token-auth.md b/quest/m1/ietf-token-auth.md index fecde8f1545..2d49012dd69 100644 --- a/quest/m1/ietf-token-auth.md +++ b/quest/m1/ietf-token-auth.md @@ -30,6 +30,24 @@ Decision prompts: - Request types: ✅ any. This takes the incoming half of [Token on FETCH](/quest/m3/auth-token-fetch.md). +Implementation checkpoint: [moq-dev/moq#7211](https://github.com/moq-dev/moq/pull/7211) +contains shared per-session token records, independent verdict readers, request +gates for supported incoming request types, and an HTTP attachment lifecycle. +It remains a draft. Before completing this quest: + +- Finish introducing a token on an initially unnamed SUBSCRIBE or + PUBLISH_NAMESPACE, and a SUBSCRIBE update combining a token with pausing. + The settled update rule covers these too. Their current refusal paths still + need a shared authority binding so the stream guard and route admission move + together. +- Review the HTTP contract shape: the implementation sends `attach` with a + random `auth` id and carries that id on revalidation and `end`. This lets an + older auth server refuse the new attachment before admission. Recommendation: + retain this explicit lifecycle rather than overloading a connection's end. +- Run Rust validation, cross-language interop, draft checks, and the token + benchmark sweep. Host capacity has held the Rust test command in `quest gate`; + JavaScript auth tests, type checks, build, and API report generation passed. + What changes from #5148: - One verification and one `requests()` auth per distinct T per session, diff --git a/rs/moq-auth/src/client.rs b/rs/moq-auth/src/client.rs index df347bd2406..5c11776e0a4 100644 --- a/rs/moq-auth/src/client.rs +++ b/rs/moq-auth/src/client.rs @@ -17,7 +17,7 @@ const TIMEOUT: Duration = Duration::from_secs(10); /// with thousands of patterns still fits. const MAX_REPLY: usize = 1 << 20; -/// The HTTP side of the contract: one JSON POST per event to an auth server. +/// The HTTP side of the contract: one JSON POST per credential event to an auth server. /// /// `connect` admits a session and hands back the [`lease::Consumer`] it holds; a task /// behind it re-POSTs `revalidate` on the grant's cadence, applies each reply, revokes @@ -86,11 +86,14 @@ impl Client { /// request for the session ([`attach`](Self::attach), revalidate, end) is bounded /// on its own, since nothing else bounds it. pub async fn connect(&self, request: Request) -> crate::Result { + if request.auth.is_some() { + return Err(Error::Refused(Default::default())); + } let context = Context::new(request.clone(), self.identity.clone()); admit(self.clone(), request, context).await } - /// Verify another credential for this connection without owning its end event. + /// Verify another credential for this connection and report its own end when it closes. pub async fn attach(&self, connection: &lease::Consumer, request: Request) -> crate::Result { let context = connection.context().ok_or(Error::Refused(Default::default()))?; let context = context.attach(&request, &self.identity)?; @@ -103,6 +106,7 @@ impl Client { pub(crate) struct Context { connection: Arc, owner: bool, + attachment: Option<(String, kio::Shared)>, } struct Connection { @@ -123,15 +127,12 @@ struct Lifecycle { /// connection id: its Connect, and each Revalidate until its driver stops. struct Admission(Arc); -/// Held by the owner's driver; dropping it, posted or not, releases -/// [`Context::reported`] so a task aborted mid-post cannot park a waiter forever. +/// Dropping a credential's driver releases [`Context::reported`] even if its POST failed. struct Reporting(Context); impl Drop for Reporting { fn drop(&mut self) { - if self.0.owner { - self.0.connection.lifecycle.lock().reported = true; - } + self.0.lifecycle().lock().reported = true; } } @@ -159,6 +160,7 @@ impl Context { lifecycle: Default::default(), }), owner: true, + attachment: None, } } @@ -178,6 +180,10 @@ impl Context { Ok(Self { connection: self.connection.clone(), owner: false, + attachment: Some(( + Request::new(&request.node, request.transport, &request.path).id, + Default::default(), + )), }) } @@ -193,25 +199,26 @@ impl Context { Ok(Some(Admission(self.connection.clone()))) } + fn lifecycle(&self) -> &kio::Shared { + match &self.attachment { + Some((_, lifecycle)) => lifecycle, + None => &self.connection.lifecycle, + } + } + pub(crate) fn close(&self, reason: Reason, bytes: Bytes) { if self.owner { - self.connection - .lifecycle + self.lifecycle() .lock() .ended .get_or_insert((reason, bytes, Instant::now())); } } - /// Resolve once the connection's `end` was posted, or its driver stopped without - /// posting it. At once for an attached credential, which never posts one. + /// Resolve after this credential's end was reported, or its driver stopped. pub(crate) async fn reported(&self) { - if !self.owner { - return; - } drop( - self.connection - .lifecycle + self.lifecycle() .wait(|lifecycle| match lifecycle.reported { true => std::task::Poll::Ready(()), false => std::task::Poll::Pending, @@ -238,23 +245,18 @@ impl Context { .await } - async fn end(&self) -> Option<(Reason, Bytes, Instant)> { - if !self.owner { - return None; - } - Some( - kio::wait(|waiter| { - let lifecycle = std::task::ready!(self.connection.lifecycle.poll(waiter, |ended| { - if ended.ended.is_some() && ended.pending == 0 { - std::task::Poll::Ready(()) - } else { - std::task::Poll::Pending - } - })); - std::task::Poll::Ready(lifecycle.ended.clone().expect("waited for connection end")) - }) - .await, - ) + async fn end(&self) -> (Reason, Bytes, Instant) { + kio::wait(|waiter| { + let lifecycle = std::task::ready!(self.lifecycle().poll(waiter, |ended| { + if ended.ended.is_some() && ended.pending == 0 { + std::task::Poll::Ready(()) + } else { + std::task::Poll::Pending + } + })); + std::task::Poll::Ready(lifecycle.ended.clone().expect("waited for credential end")) + }) + .await } } @@ -262,7 +264,7 @@ impl Context { /// answer in tests, so the lease's timers run on Tokio's paused clock with no /// socket for them to outrun. trait Post: Clone + Send + Sync + 'static { - /// Send a `connect` or `revalidate`: the grant as answered, not yet validated, + /// Send a `connect`, `attach`, or `revalidate`: the grant as answered, not yet validated, /// a refusal, or an outage. fn post(&self, request: &Request) -> impl Future> + Send; @@ -327,7 +329,12 @@ async fn reply(mut response: reqwest::Response) -> crate::Result> { /// The connection's own `connect` runs under the caller's deadline; an attached /// credential arrives after the handshake, so its request is bounded here. async fn admit(server: S, mut request: Request, context: Context) -> crate::Result { - request.event = Event::Connect; + request.auth = context.attachment.as_ref().map(|(id, _)| id.clone()); + request.event = if request.auth.is_some() { + Event::Attach + } else { + Event::Connect + }; let admission = context.admitting()?; let grant = match context.owner { @@ -402,9 +409,7 @@ struct Driver { impl Driver { async fn run(mut self) { self.drive().await; - let Some((reason, bytes, ended)) = self.context.end().await else { - return; - }; + let (reason, bytes, ended) = self.context.end().await; let mut request = self.request.clone(); request.event = Event::End { @@ -472,6 +477,15 @@ impl Driver { }, } }; + // An attachment reports its lease's verdict, even if its consumer closes later + // with another reason. The connection owner reports the connection's own close. + if !self.context.owner { + self.context + .lifecycle() + .lock() + .ended + .get_or_insert((ended.0.clone(), ended.1, Instant::now())); + } // Land an in-flight Revalidate before releasing the connection's End, so // End is the last request for this id however the lease ended. if let Some(request) = inflight { @@ -536,7 +550,7 @@ mod tests { /// Wait for the background `end` POST and return it. async fn end(&self) -> Request { - let is_end = |r: &Request| matches!(r.event, Event::End { .. }); + let is_end = |r: &Request| matches!(r.event, Event::End { .. }) && r.auth.is_none(); self.until(|log| log.iter().any(is_end)).await; self.0.read().iter().find(|r| is_end(r)).cloned().unwrap() } @@ -757,9 +771,15 @@ mod tests { let mut request = request(); request.query = Some("jwt=replacement".into()); let attached = script.attach(&owner, request).await.unwrap(); + let reported = attached.reported(); drop(attached); - tokio::task::yield_now().await; - assert_eq!(log.events(), [Event::Connect, Event::Connect]); + reported.await; + let events = log.0.read(); + assert!(matches!(events.last().unwrap().event, Event::End { .. })); + let attachment = events[1].auth.as_ref().expect("attachment identity"); + assert_eq!(events[2].auth.as_ref(), Some(attachment)); + assert_eq!(events[2].id, events[0].id); + drop(events); owner.close("done", Bytes { sent: 7, received: 11 }); match log.end().await.event { Event::End { reason, bytes, .. } => { @@ -787,7 +807,7 @@ mod tests { assert!( matches!( log.events().as_slice(), - [Event::Connect, Event::Connect, Event::End { .. }] + [Event::Connect, Event::Attach, Event::End { .. }, Event::End { .. }] ), "an attached lease outlived its connection's End" ); @@ -876,14 +896,20 @@ mod tests { tokio::task::yield_now().await; assert_eq!( log.events(), - [Event::Connect, Event::Connect], + [Event::Connect, Event::Attach], "end overtook a withdrawn attachment's in-flight revalidate" ); server.release.notify_one(); log.end().await; assert!(matches!( log.events().as_slice(), - [Event::Connect, Event::Connect, Event::Revalidate, Event::End { .. }] + [ + Event::Connect, + Event::Attach, + Event::Revalidate, + Event::End { .. }, + Event::End { .. } + ] )); } @@ -904,7 +930,7 @@ mod tests { request.query = Some("jwt=replacement".into()); let attached = script.attach(&owner, request).await.unwrap(); assert_eq!(owner.closed().await, Reason::Expired); - assert_eq!(log.events(), [Event::Connect, Event::Connect]); + assert_eq!(log.events(), [Event::Connect, Event::Attach]); assert!(matches!(attached.poll_closed(&kio::Waiter::noop()), Poll::Pending)); owner.close(Reason::Shutdown, Bytes::default()); assert!(matches!( @@ -964,7 +990,7 @@ mod tests { )); assert!(matches!( log.events().as_slice(), - [Event::Connect, Event::Connect, Event::End { .. }] + [Event::Connect, Event::Attach, Event::End { .. }] )); } @@ -1122,7 +1148,9 @@ mod tests { tokio::time::pause(); let server = Script::new(Log::default(), |request| { Some(match request.event { - Event::Connect => Ok(grant(Some(Duration::from_secs(3600)), Some(Duration::from_secs(1)))), + Event::Connect | Event::Attach => { + Ok(grant(Some(Duration::from_secs(3600)), Some(Duration::from_secs(1)))) + } _ => Err(crate::Refusal { reason: crate::refusal::Reason::Expired, ..Default::default() @@ -1216,7 +1244,9 @@ mod tests { for refuse in refusals { let script = Script::new(Log::default(), move |request| { Some(match request.event { - Event::Connect => Ok(grant(Some(Duration::from_secs(3600)), Some(Duration::from_secs(1)))), + Event::Connect | Event::Attach => { + Ok(grant(Some(Duration::from_secs(3600)), Some(Duration::from_secs(1)))) + } _ => refuse(), }) }); @@ -1233,7 +1263,7 @@ mod tests { async fn an_invalid_grant_on_recheck_revokes() { let script = Script::new(Log::default(), |request| { Some(Ok(match request.event { - Event::Connect => grant(Some(Duration::from_secs(3600)), Some(Duration::from_secs(1))), + Event::Connect | Event::Attach => grant(Some(Duration::from_secs(3600)), Some(Duration::from_secs(1))), _ => grant(None, Some(Duration::from_secs(1))), })) }); @@ -1286,7 +1316,7 @@ mod tests { let log = Log::default(); let script = Script::new(log.clone(), move |request| { Some(match request.event { - Event::Connect => { + Event::Connect | Event::Attach => { let mut grant = grant(None, Some(Duration::from_secs(1))); grant.expires = Some(expires); Ok(grant) @@ -1334,7 +1364,9 @@ mod tests { async fn expiry_fires_while_a_recheck_is_stalled() { tokio::time::pause(); let script = Script::new(Log::default(), |request| match request.event { - Event::Connect => Some(Ok(grant(Some(Duration::from_secs(3)), Some(Duration::from_secs(1))))), + Event::Connect | Event::Attach => { + Some(Ok(grant(Some(Duration::from_secs(3)), Some(Duration::from_secs(1))))) + } _ => None, }); let consumer = script.connect().await.unwrap(); @@ -1349,7 +1381,9 @@ mod tests { async fn a_close_is_reported_while_a_recheck_is_stalled() { let log = Log::default(); let script = Script::new(log.clone(), |request| match request.event { - Event::Connect => Some(Ok(grant(Some(Duration::from_secs(3600)), Some(Duration::from_secs(1))))), + Event::Connect | Event::Attach => { + Some(Ok(grant(Some(Duration::from_secs(3600)), Some(Duration::from_secs(1))))) + } _ => None, }); @@ -1422,7 +1456,7 @@ mod tests { async fn a_nudge_during_inflight_posts_once_more_when_the_reply_lands() { let log = Log::default(); let server = server(log.clone(), |request| match request.event { - Event::Connect => ResponseTemplate::new(200) + Event::Connect | Event::Attach => ResponseTemplate::new(200) .set_body_json(grant(Some(Duration::from_secs(3600)), Some(Duration::from_secs(3600)))), Event::Revalidate => ResponseTemplate::new(200) .set_body_json(grant(Some(Duration::from_secs(3600)), Some(Duration::from_secs(3600)))) diff --git a/rs/moq-auth/src/lib.rs b/rs/moq-auth/src/lib.rs index b238ed38c39..a66d5877438 100644 --- a/rs/moq-auth/src/lib.rs +++ b/rs/moq-auth/src/lib.rs @@ -4,7 +4,7 @@ //! every piece of the answer: //! //! - [`Request`], [`Grant`], and [`Refusal`]: the JSON a relay POSTs to an auth server on -//! [`Event::Connect`], [`Event::Revalidate`], and [`Event::End`], and what comes back. +//! [`Event::Connect`], [`Event::Attach`], [`Event::Revalidate`], and [`Event::End`], and what comes back. //! - [`lease::Producer`] / [`lease::Consumer`]: the handle a session holds for its //! grant, so whoever runs the accept loop decides how, in process or over HTTP. //! - [`Client`]: the HTTP implementation that drives a lease against `--auth-url`. diff --git a/rs/moq-auth/src/request.rs b/rs/moq-auth/src/request.rs index afcd17e941d..5b639f7b8a9 100644 --- a/rs/moq-auth/src/request.rs +++ b/rs/moq-auth/src/request.rs @@ -21,6 +21,9 @@ pub struct Request { /// Random 128-bit hex, unique per session; the key every event for it shares. pub id: String, + /// The attached credential this event concerns; absent for the connection itself. + pub auth: Option, + /// Which lifecycle event this is, with what an `end` carries. #[serde(flatten)] pub event: Event, @@ -77,6 +80,7 @@ impl Request { let id: String = bytes.iter().map(|b| format!("{b:02x}")).collect(); Self { id, + auth: None, event: Event::Connect, node: node.into(), transport, @@ -131,6 +135,8 @@ pub enum Event { /// What it moved. bytes: Bytes, }, + /// Another credential asks to join the connection, identified by `auth`. + Attach, } /// How a session reached the relay, including QUIC, relay HTTP, and gateways. diff --git a/rs/moq-auth/src/serve.rs b/rs/moq-auth/src/serve.rs index 9c7a090554d..3294be59847 100644 --- a/rs/moq-auth/src/serve.rs +++ b/rs/moq-auth/src/serve.rs @@ -365,7 +365,7 @@ fn remote(request: &Request) -> Option { /// One live session's share of the limits. struct Slot { /// Every JWT presented for this session: its connect, plus each one attached in band. - tokens: Vec, + tokens: HashMap, Option>, remote: Option, seen: Instant, } @@ -386,19 +386,24 @@ impl Sessions { /// Admit a `connect`, refusing over the cap. A known id is the same session /// attaching another credential: it holds no new remote slot, but a JWT new to - /// it counts against that JWT's cap until the session ends. + /// it counts against that JWT's cap until its last credential ends. fn connect(&mut self, request: &Request, limits: Limits, root: Option) -> Result<(), Denial> { // Decided before counting, so the credential is already known to be one JWT. let token = jwt(request).ok().flatten().as_deref().map(hash); let known = self.slots.get(&request.id); - let new_token = token.filter(|token| !known.is_some_and(|slot| slot.tokens.contains(token))); + let new_token = token + .filter(|token| !known.is_some_and(|slot| slot.tokens.values().any(|value| value.as_ref() == Some(token)))); if let (Some(cap), Some(token)) = (limits.token, new_token) - && self.slots.values().filter(|slot| slot.tokens.contains(&token)).count() >= cap + && self + .slots + .values() + .filter(|slot| slot.tokens.values().any(|value| *value == Some(token))) + .count() >= cap { return Err(Denial::TokenLimit { root }); } if let Some(slot) = self.slots.get_mut(&request.id) { - slot.tokens.extend(new_token); + slot.tokens.insert(request.auth.clone(), token); slot.seen = Instant::now(); return Ok(()); } @@ -411,7 +416,7 @@ impl Sessions { self.slots.insert( request.id.clone(), Slot { - tokens: token.into_iter().collect(), + tokens: [(request.auth.clone(), token)].into(), remote, seen: Instant::now(), }, @@ -425,20 +430,25 @@ impl Sessions { fn revalidate(&mut self, request: &Request) { let token = jwt(request).ok().flatten().as_deref().map(hash); let slot = self.slots.entry(request.id.clone()).or_insert_with(|| Slot { - tokens: Vec::new(), + tokens: HashMap::new(), remote: remote(request), seen: Instant::now(), }); - if let Some(token) = token - && !slot.tokens.contains(&token) - { - slot.tokens.push(token); - } + slot.tokens.insert(request.auth.clone(), token); slot.seen = Instant::now(); } - fn end(&mut self, id: &str) { - self.slots.remove(id); + fn end(&mut self, request: &Request) { + match &request.auth { + Some(_) => { + if let Some(slot) = self.slots.get_mut(&request.id) { + slot.tokens.remove(&request.auth); + } + } + None => { + self.slots.remove(&request.id); + } + } } } @@ -494,8 +504,11 @@ impl Server { /// Answer one event: the grant, or why the session is refused. pub async fn answer(&self, request: &Request) -> Result, Denial> { + if matches!(request.event, Event::Attach) && request.auth.is_none() { + return Err(Denial::MalformedToken("an attachment needs an auth id".into())); + } match request.event { - Event::Connect => { + Event::Connect | Event::Attach => { let (grant, root) = self.policy.decide_root(request).await?; if let Some(cadence) = self.cadence() { let mut sessions = self.sessions.lock().unwrap(); @@ -514,7 +527,7 @@ impl Server { Ok(Some(grant)) } Event::End { .. } => { - self.sessions.lock().unwrap().end(&request.id); + self.sessions.lock().unwrap().end(request); Ok(None) } } @@ -1344,7 +1357,8 @@ mod tests { let attach = |session: &Request| { let mut attach = with_token(session.clone(), &b); - attach.id = session.id.clone(); + attach.auth = Some("attachment".into()); + attach.event = Event::Attach; attach }; server.answer(&attach(&first)).await.unwrap(); @@ -1357,6 +1371,25 @@ mod tests { } ); assert_eq!(server.sessions.lock().unwrap().slots.len(), 2); + let mut duplicate = attach(&first); + duplicate.auth = Some("duplicate".into()); + server.answer(&duplicate).await.unwrap(); + let end = |mut request: Request| { + request.event = Event::End { + reason: crate::lease::Reason::Dropped, + duration: Duration::ZERO, + bytes: Default::default(), + }; + request + }; + server.answer(&end(attach(&first))).await.unwrap(); + assert!(matches!( + server.answer(&attach(&second)).await, + Err(Denial::TokenLimit { .. }) + )); + server.answer(&end(duplicate)).await.unwrap(); + server.answer(&attach(&second)).await.unwrap(); + assert_eq!(server.sessions.lock().unwrap().slots.len(), 2); } #[tokio::test] diff --git a/rs/moq-net/benches/auth.rs b/rs/moq-net/benches/auth.rs index 586bd3ced01..a265fed07fc 100644 --- a/rs/moq-net/benches/auth.rs +++ b/rs/moq-net/benches/auth.rs @@ -116,7 +116,7 @@ fn admission(c: &mut Criterion) { } /// A client publishing `origin` to its own server, whose session and token requests follow. -async fn publisher(origin: &origin::Producer) -> (Session, Session, auth::Requests) { +async fn publisher(origin: &origin::Producer) -> (Session, Session, auth::Requests, origin::Producer) { let version: Version = "moq-transport-22".parse().unwrap(); let mut mock = moq_net::transport::memory::Config::default(); mock.protocol = Some(version.alpn().to_string()); @@ -143,7 +143,7 @@ async fn publisher(origin: &origin::Producer) -> (Session, Session, auth::Reques let mut request = moq_net::Server::new() .with_versions(version.into()) .with_extensions(server_extensions) - .with_subscriber(subscribe) + .with_subscriber(subscribe.clone()) .accept_request(harness::now(), server) .await .unwrap(); @@ -153,7 +153,7 @@ async fn publisher(origin: &origin::Producer) -> (Session, Session, auth::Reques (session, requests) }; let (client, (server, requests)) = futures::join!(client, server); - (client, server, requests) + (client, server, requests, subscribe) } fn renewal(c: &mut Criterion) { @@ -181,17 +181,25 @@ fn renewal(c: &mut Criterion) { let (origin, driver) = origin::Producer::new(config); harness::spawn(driver); let mut publishers = runtime.block_on(join_all((0..sessions).map(|_| publisher(&origin)))); - let (session, _, requests) = &mut publishers[0]; + let (session, _, requests, _) = &mut publishers[0]; let handle = session.auth(); - let auths = runtime - .block_on(async { [handle.add("a").await.unwrap().0, handle.add("b").await.unwrap().0] }); + let auths = runtime.block_on(async { + let mut auths = Vec::new(); + for route in 0..routes { + auths.push([ + handle.add(format!("a/{route}")).await.unwrap().0, + handle.add(format!("b/{route}")).await.unwrap().0, + ]); + } + auths + }); let broadcasts: Vec<_> = (0..routes) .map(|route| origin.create_broadcast(format!("room/{route}").as_str()).unwrap()) .collect(); - let mut swap = |auth: &auth::Credential, touched: usize| { - for broadcast in broadcasts.iter().take(touched) { + let mut swap = |next: usize, touched: usize| { + for (index, broadcast) in broadcasts.iter().take(touched).enumerate() { broadcast - .announce(origin::Route::default().with_auth(auth.clone())) + .announce(origin::Route::default().with_auth(auths[index][next].clone())) .unwrap(); } runtime.block_on(async { @@ -204,10 +212,10 @@ fn renewal(c: &mut Criterion) { }; // Each round's grants replace the last round's, which drop once their // renewals are accepted. - let mut held = swap(&auths[0], routes); + let mut held = swap(0, routes); let mut next = 1; b.iter(|| { - let replacements = swap(&auths[next], touched); + let replacements = swap(next, touched); drop(held.splice(..touched, replacements)); next ^= 1; }); @@ -310,5 +318,78 @@ fn waiting(c: &mut Criterion) { } } -criterion_group!(benches, admission, renewal, waiting); +/// Request-token lookup and shared withdrawal, sweeping distinct tokens and naming requests. +fn shared_requests(c: &mut Criterion) { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + let mut group = c.benchmark_group("auth/request_tokens"); + group.sample_size(10); + group.warm_up_time(Duration::from_millis(100)); + group.measurement_time(Duration::from_millis(300)); + for tokens in [1, 8, 32] { + for requests in [1, 8, 32] { + group.throughput(Throughput::Elements((tokens * requests) as u64)); + group.bench_with_input( + BenchmarkId::new(tokens.to_string(), requests), + &(tokens, requests), + |b, &(tokens, requests)| { + let _guard = runtime.enter(); + let mut config = origin::Config::new(Hop::new(2).unwrap()); + config.update_hold = Duration::ZERO; + let (origin, driver) = origin::Producer::new(config); + harness::spawn(driver); + let (client, _server, mut pending, receive) = runtime.block_on(publisher(&origin)); + let mut announced = receive.consume().announced(); + let auths = runtime.block_on(async { + let mut auths = Vec::new(); + for token in 0..tokens { + auths.push(client.auth().add(format!("token/{token}")).await.unwrap().0); + } + auths + }); + let mut generation = 0; + b.iter(|| { + runtime.block_on(async { + let prefix = format!("round/{generation}"); + generation += 1; + let mut broadcasts = Vec::new(); + for (token, auth) in auths.iter().enumerate() { + for request in 0..requests { + broadcasts.push( + origin + .publish( + format!("{prefix}/{token}/{request}").as_str(), + origin::Route::default().with_auth(auth.clone()), + ) + .unwrap(), + ); + } + } + let mut issued = Vec::new(); + for _ in 0..tokens { + issued.push(pending.next().await.unwrap().accept(auth::Grant::all())); + } + let mut seen = std::collections::HashSet::new(); + while seen.len() < tokens * requests { + if let moq_net::announce::Event::Start(update) + | moq_net::announce::Event::Restart(update) = announced.next().await.unwrap() + && update.prefix.as_str().starts_with(&prefix) + { + seen.insert(update.prefix); + } + } + drop(broadcasts); + join_all(issued.iter().map(auth::Issued::closed)).await; + }) + }); + }, + ); + } + } + group.finish(); +} + +criterion_group!(benches, admission, renewal, waiting, shared_requests); criterion_main!(benches); diff --git a/rs/moq-net/src/auth.rs b/rs/moq-net/src/auth.rs index 353f54f13a4..2891c42ebab 100644 --- a/rs/moq-net/src/auth.rs +++ b/rs/moq-net/src/auth.rs @@ -23,7 +23,8 @@ //! //! Every token the peer presents is one kind of [`Request`], answered on the same queue, //! wherever it rode: its own AUTH stream, or a moq-transport `AUTHORIZATION TOKEN` on a -//! SUBSCRIBE or PUBLISH_NAMESPACE. The session enforces each grant it issues. A request +//! request. Requests naming the same token on one session share its verification and lifetime. +//! The session enforces each grant it issues. A request //! that carried a token is held to that token's grant alone and ends with it; any other //! request is held to the session's scope, which a token carried by a request never joins. //! @@ -212,6 +213,8 @@ struct Attached { /// Whether a request can carry a token itself (moq-transport), so an auth needs no /// AUTH stream to be named. requests: bool, + /// Request tokens remain indexed only while at least one naming request holds them. + request_tokens: BTreeMap>, /// Set once the session ends. closed: Option, /// The session's GOAWAY: once either side sends one, the session is draining. @@ -296,6 +299,7 @@ impl Attached { let mut attached = Self { supported, requests, + request_tokens: BTreeMap::new(), closed: None, goaway: None, slots: BTreeMap::new(), @@ -692,32 +696,6 @@ impl Handle { } } - /// Ask the acceptor to verify a token that rode on one of the peer's requests. - /// - /// The token reaches it through [`requests`](Self::requests) like any other. The grant it - /// answers covers only this request, never joins the session's scope, and ends when the - /// returned [`RequestVerdict`] is dropped. Without an application acceptor, the verdict is - /// [`Error::Unsupported`]. So it is for a token type other than - /// [`Token::OUT_OF_BAND`](crate::setup::Token::OUT_OF_BAND), which an AUTH token cannot name, - /// and for an empty token, which on AUTH means the connection's own credential and so would - /// reach the acceptor as that credential. `hold` is the request's slot, kept while the - /// application verifies the token. - pub(crate) fn verify_request(&self, token: crate::setup::Token, hold: Option) -> RequestVerdict { - let Some(queue) = self.acceptor() else { - return RequestVerdict { issue: None }; - }; - if token.kind != crate::setup::Token::OUT_OF_BAND || token.value.is_empty() { - return RequestVerdict { issue: None }; - } - let issue = kio::Shared::new(Issue { - hold, - ..Default::default() - }); - // A closed or full queue refuses the token. - queue.push(Request::new(token.value.into(), issue.clone())); - RequestVerdict { issue: Some(issue) } - } - /// Decide who answers the peer's tokens: the app if it took the requests, /// otherwise the session itself (`None`). Idempotent. fn acceptor(&self) -> Option { @@ -926,10 +904,45 @@ impl Link { }) } - /// Ask the acceptor to verify a token that rode on one of the peer's requests; see - /// [`Handle::verify_request`]. + /// Verify a request token once per session while any naming request remains open. pub(crate) fn verify_request(&self, token: crate::setup::Token, hold: Option) -> RequestVerdict { - self.handle.verify_request(token, hold) + let unsupported = || RequestVerdict { + issue: None, + _lifetime: None, + seen: Default::default(), + }; + if token.kind != crate::setup::Token::OUT_OF_BAND || token.value.is_empty() { + return unsupported(); + } + let Some(queue) = self.acceptor() else { + return unsupported(); + }; + let token = Bytes::from(token.value); + let (lifetime, fresh) = self.with_state(|state, id| { + let link = attached(&mut state.links, id); + if let Some(lifetime) = link.request_tokens.get(&token).and_then(std::sync::Weak::upgrade) { + return (lifetime, false); + } + let lifetime = std::sync::Arc::new(RequestLifetime { + issue: kio::Shared::new(Issue { + hold, + ..Default::default() + }), + handle: self.clone(), + token: token.clone(), + }); + link.request_tokens + .insert(token.clone(), std::sync::Arc::downgrade(&lifetime)); + (lifetime, true) + }); + if fresh { + queue.push(Request::new(token, lifetime.issue.clone())); + } + RequestVerdict { + issue: Some(lifetime.issue.clone()), + _lifetime: Some(lifetime), + seen: Default::default(), + } } /// Decide who answers the peer's tokens: the app if it took the requests, @@ -1308,6 +1321,7 @@ impl Link { /// End the session: fail every pending token here, leaving the handle's tokens for the /// next session. A handle the session created for itself ends with it. pub(crate) fn close(&self, err: Error) { + let mut lifetimes = Vec::new(); let queue = self.with_state(|state, link| { if state.links[&link].closed.is_some() { return None; @@ -1317,6 +1331,7 @@ impl Link { let link = attached(&mut state.links, link); link.closed = Some(err.clone()); link.opening.clear(); + lifetimes.extend(link.request_tokens.values().filter_map(std::sync::Weak::upgrade)); for slot in link.slots.values_mut() { slot.grant = None; slot.answered.get_or_insert(Err(err.clone())); @@ -1324,10 +1339,13 @@ impl Link { } link.recompute(); match state.bound { - true => state.abort(err), + true => state.abort(err.clone()), false => None, } }); + for lifetime in lifetimes { + lifetime.issue.lock().peer.get_or_insert(err.clone()); + } // Refusing the queued tokens takes the lock again, to drop them from the scope. if let Some(queue) = queue { queue.close(); @@ -1637,6 +1655,7 @@ impl Pending { } /// What the acceptor writes on one peer token's stream. +#[derive(Clone)] pub(crate) enum Reply { Grant(Grant), Refuse { code: SessionError, reason: String }, @@ -1647,6 +1666,9 @@ pub(crate) enum Reply { pub(crate) struct Issue { /// Replies for the driver to write, in order. pub outbox: VecDeque, + /// Request-token readers each observe the latest verdict independently. + latest: Option, + revision: u64, /// The app is done with the stream: the driver closes it once the outbox drains. pub done: bool, /// Why the stream ended on the peer's side, once it has. @@ -1670,7 +1692,12 @@ impl Issue { _ => handle.issue(*id, None), } } - self.outbox.push_back(reply); + self.revision += 1; + if self.scoped.is_some() { + self.outbox.push_back(reply); + } else { + self.latest = Some(reply); + } } /// The application is done with the token: its grant leaves the scope. @@ -1735,7 +1762,7 @@ impl Drop for Serving { /// /// It arrived on its own AUTH stream, or on one of the peer's moq-transport requests. Either /// way the grant it earns is enforced by the session: an AUTH token's joins the session's -/// scope, while one that rode on a request admits only that request, which ends with it. +/// scope, while one that rode on requests admits only those naming it, which end with it. /// /// Dropping it unanswered refuses the token with [`SessionError::Unauthorized`]. pub struct Request { @@ -1769,7 +1796,7 @@ impl Request { } /// Grant the token. The grant holds until the returned [`Issued`] is revoked - /// or dropped, or the peer withdraws the token or ends the request it rode on. + /// or dropped, or the peer withdraws the token or ends its last naming request. /// /// Revoking an AUTH token's grant once `expires` passes is the acceptor's job; a grant /// on a request lapses at `expires` by itself. @@ -1814,6 +1841,10 @@ pub(crate) enum RequestKind { Subscribe, /// A PUBLISH_NAMESPACE: the peer announces a namespace it will publish under. PublishNamespace, + /// Discover only broadcasts the token allows beneath the requested prefix. + Discover, + /// A PUBLISH offers a track to the peer. + Publish, } impl RequestKind { @@ -1824,6 +1855,8 @@ impl RequestKind { pub(crate) fn covers(self, grant: &Grant, path: &str) -> bool { match self { Self::Subscribe => grant.subscribe.matches(path), + Self::Discover => !grant.subscribe.is_empty(), + Self::Publish => grant.publish.matches(path), Self::PublishNamespace => { crate::Pattern::subtree(path).is_ok_and(|subtree| grant.publish.contains(&subtree)) } @@ -1837,6 +1870,35 @@ impl RequestKind { /// is over, so the grant lives exactly as long as the request and never outlives it. pub(crate) struct RequestVerdict { issue: Option>, + // Each request owns one reference, independent of its reader's progress. + _lifetime: Option>, + seen: crate::sync::Mutex, +} + +struct RequestLifetime { + issue: kio::Shared, + handle: Link, + token: Bytes, +} + +impl Drop for RequestLifetime { + fn drop(&mut self) { + self.handle.with_state(|state, id| { + let link = attached(&mut state.links, id); + if link + .request_tokens + .get(&self.token) + .is_some_and(|token| token.strong_count() == 0) + { + link.request_tokens.remove(&self.token); + } + }); + let mut issue = self.issue.lock(); + issue.peer.get_or_insert(Error::Cancel); + if !issue.taken { + issue.hold = None; + } + } } /// Why a request token did not authorize its request: the error whose code the peer is @@ -1879,62 +1941,42 @@ impl RequestVerdict { let Some(issue) = &self.issue else { return Poll::Ready(Err(Error::Unsupported.into())); }; - let mut guard = ready_or!( - issue.poll(waiter, |issue| match issue.outbox.is_empty() && !issue.done { - true => Poll::Pending, - false => Poll::Ready(()), - }) - ); - let mut grant = None; - while let Some(reply) = guard.outbox.pop_front() { - match reply { - Reply::Grant(next) => grant = Some(next), - Reply::Refuse { code, reason } => return Poll::Ready(Err(Refusal::new(Error::Session(code), &reason))), + let guard = ready_or!(issue.poll(waiter, |issue| { + if issue.latest.is_none() && !issue.done { + Poll::Pending + } else { + Poll::Ready(()) } - } - Poll::Ready(match (grant, guard.done) { - (Some(grant), false) => Ok(grant), - (Some(_), true) => Err(Refusal::new( + })); + *self.seen.lock().unwrap() = guard.revision; + Poll::Ready(match (&guard.latest, guard.done) { + (Some(Reply::Refuse { code, reason }), _) => Err(Refusal::new(Error::Session(code.clone()), reason)), + (Some(Reply::Grant(grant)), false) => Ok(grant.clone()), + _ => Err(Refusal::new( Error::Session(SessionError::Unauthorized), "grant dropped", )), - // Done with nothing written: the acceptor dropped the request unanswered. - (None, _) => Err(Error::Session(SessionError::Unauthorized).into()), }) } - /// After the first grant, poll for the acceptor's next action on this token: a - /// replacement grant (an update, such as a lowered expiry), a refusal (revoke), or the - /// enforced grant being dropped (`None`). `Pending` while the grant still stands. + /// Each naming request independently observes replacements and revocation. fn poll_reply(&self, waiter: &kio::Waiter) -> Poll> { let Some(issue) = &self.issue else { - return Poll::Ready(Some(Reply::Refuse { - code: SessionError::Unauthorized, - reason: "no verifier".to_string(), - })); + return Poll::Ready(None); }; - let mut guard = ready_or!( - issue.poll(waiter, |issue| match issue.outbox.is_empty() && !issue.done { - true => Poll::Pending, - false => Poll::Ready(()), - }) - ); - Poll::Ready(guard.outbox.pop_front()) - } -} - -impl Drop for RequestVerdict { - fn drop(&mut self) { - if let Some(issue) = &self.issue { - let mut issue = issue.lock(); - // The request is over: settle the acceptor's `closed`, and make a late answer land - // nowhere. A token still queued gives its slot back now; one the application took - // keeps it until the application answers. - issue.peer.get_or_insert(Error::Cancel); - if !issue.taken { - issue.hold = None; + let mut seen = self.seen.lock().unwrap(); + let guard = ready_or!(issue.poll(waiter, |issue| { + if issue.revision == *seen && !issue.done { + Poll::Pending + } else { + Poll::Ready(()) } + })); + if guard.revision != *seen { + *seen = guard.revision; + return Poll::Ready(guard.latest.clone()); } + Poll::Ready(None) } } @@ -1961,6 +2003,7 @@ struct Held { /// The token of the renewal being verified, until the serve loop applies its verdict. renewing: Option>, clock: crate::time::Deadlines, + epoch: u64, } impl RequestGrant { @@ -1990,6 +2033,7 @@ impl RequestGrant { hold, renewing: None, clock: clock.clone(), + epoch: 1, })))) } @@ -1997,6 +2041,28 @@ impl RequestGrant { self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) } + /// Watch this grant without owning a naming request or its verification credit. + fn observe(&self) -> Self { + let held = self.held(); + let mut deadline = crate::time::Deadline::new(&held.clock); + deadline.set(held.grant.expires); + Self(std::sync::Arc::new(crate::sync::Mutex::new(Held { + verdict: RequestVerdict { + issue: held.verdict.issue.clone(), + _lifetime: None, + seen: crate::sync::Mutex::new(*held.verdict.seen.lock().unwrap()), + }, + grant: held.grant.clone(), + deadline, + path: held.path.clone(), + kind: held.kind, + hold: None, + renewing: None, + clock: held.clock.clone(), + epoch: held.epoch, + }))) + } + /// The grant in force right now. Observed by the lifecycle tests. #[cfg(test)] pub(crate) fn grant(&self) -> Grant { @@ -2036,6 +2102,7 @@ impl RequestGrant { held.verdict = verdict; held.deadline.set(grant.expires); held.grant = grant; + held.epoch += 1; Ok(()) } @@ -2047,6 +2114,9 @@ impl RequestGrant { pub(crate) fn poll_ended(&self, waiter: &kio::Waiter) -> Poll { let mut held = self.held(); loop { + if let Some(err) = held.verdict.issue.as_ref().and_then(|issue| issue.read().peer.clone()) { + return Poll::Ready(err.into()); + } // The acceptor granted a renewal the serve loop has yet to apply: it takes over rather // than the request ending between the two, whether the old grant lapsed or dropped. let handover = held.renewing.as_ref().is_some_and(granted); @@ -2070,6 +2140,7 @@ impl RequestGrant { } held.deadline.set(grant.expires); held.grant = grant; + held.epoch += 1; // Re-poll: the new deadline may already have lapsed, or another reply may // be waiting. continue; @@ -2127,7 +2198,7 @@ fn live(clock: &crate::time::Deadlines, kind: RequestKind, path: &str, grant: &G /// Whether the acceptor granted the token behind `issue` and still holds the grant. fn granted(issue: &kio::Shared) -> bool { let issue = issue.read(); - !issue.done && issue.outbox.iter().any(|reply| matches!(reply, Reply::Grant(_))) + !issue.done && matches!(issue.latest, Some(Reply::Grant(_))) } /// A grant issued to one of the peer's tokens, which the session enforces. Dropping it @@ -2201,6 +2272,8 @@ pub(crate) struct Gate { direction: Direction, epoch: u64, part: Part, + token: Option, + discovery: bool, /// Whether the scope allowed the path at the last permit. within_limit: bool, admitted: bool, @@ -2238,6 +2311,8 @@ impl Gate { direction, epoch: 0, part: Part::Both, + token: None, + discovery: false, within_limit, admitted: false, waiting: None, @@ -2250,6 +2325,68 @@ impl Gate { self } + /// Filter a discovery request by the token rather than requiring its whole prefix. + pub(crate) fn discovery(mut self) -> Self { + self.discovery = true; + self + } + + /// Admit the verifier's answer under the request's path and role. + pub(crate) fn admit( + &mut self, + clock: &crate::time::Deadlines, + answer: (RequestVerdict, std::result::Result), + ) -> std::result::Result<(), Refusal> { + let (verdict, answer) = answer; + if let Some(token) = &self.token { + return token.renew(verdict, answer); + } + let kind = match (self.discovery, self.direction) { + (true, _) => RequestKind::Discover, + (false, Direction::Publish) => RequestKind::Subscribe, + (false, Direction::Subscribe) => RequestKind::Publish, + }; + let token = RequestGrant::new(clock, verdict, answer, self.path.clone(), kind, None)?; + self.bind(token); + Ok(()) + } + + /// Replace the session's authority with this request's token alone. + pub(crate) fn bind(&mut self, token: RequestGrant) { + self.token = Some(token); + self.part = Part::Both; + self.epoch = 0; + } + + /// Hold this request's current token across its serving future, including renewals. + pub(crate) fn token_guard(&self) -> Option { + self.token.clone() + } + + /// Observe the token without keeping its naming request alive. + pub(crate) fn token_observer(&self) -> Option { + self.token.as_ref().map(RequestGrant::observe) + } + + /// Start another request under this session gate, without inheriting a request token. + pub(crate) fn unnamed(&self) -> Self { + let mut gate = Self::new(self.handle.clone(), self.path.clone(), self.direction); + gate.part = match &self.part { + Part::Both => Part::Both, + Part::Granted => Part::Granted, + Part::Announced(live) => Part::Announced(live.clone()), + }; + gate + } + + /// The token ended, independently of whether its discovery prefix matches a grant. + pub(crate) fn poll_token_ended(&self, waiter: &kio::Waiter) -> Poll { + match &self.token { + Some(token) => token.poll_ended(waiter), + None => Poll::Pending, + } + } + /// A gate for a request under a namespace the peer announced, while `live` stays open: /// the announce stands in for the scope, as its own token may have authorized it. pub(crate) fn announced(handle: Link, path: crate::Path, direction: Direction, live: kio::Consumer<()>) -> Self { @@ -2264,7 +2401,16 @@ impl Gate { let path = self.path.as_str(); self.handle.within_grant(self.direction, path) && match &self.part { - Part::Both => self.within_limit, + Part::Both => match &self.token { + Some(token) => { + let token = token.held(); + match self.direction { + Direction::Publish => token.grant.subscribe.matches(path), + Direction::Subscribe => token.grant.publish.matches(path), + } + } + None => self.within_limit, + }, Part::Granted => true, Part::Announced(live) => self.within_limit || !live.is_closed(), } @@ -2282,7 +2428,28 @@ impl Gate { /// What the session allows in this gate's direction, once it may have changed since `epoch`. pub(crate) fn poll_permit(&self, epoch: &mut u64, waiter: &kio::Waiter) -> Poll { - self.handle.poll_permit(self.direction, epoch, waiter) + let Some(token) = &self.token else { + return self.handle.poll_permit(self.direction, epoch, waiter); + }; + let ended = token.poll_ended(waiter).is_ready(); + let token = token.held(); + let (mut permit, mut version) = self.handle.read(|link, _| (link.permit(self.direction), link.epoch)); + // Register the session wakeup even when only the token changed this time. + let _ = self.handle.poll_permit(self.direction, &mut version, waiter); + permit.limit = Some(if ended { + Patterns::new() + } else { + match self.direction { + Direction::Publish => token.grant.subscribe.clone(), + Direction::Subscribe => token.grant.publish.clone(), + } + }); + version += token.epoch; + if version == *epoch { + return Poll::Pending; + } + *epoch = version; + Poll::Ready(permit) } /// Preserve setup ordering before the first discovery snapshot. @@ -2333,7 +2500,12 @@ impl Gate { /// `None` (no answer yet, or a version without AUTH) and a session never limited /// allow everything. pub(crate) fn poll_denied(&mut self, waiter: &kio::Waiter) -> Poll<()> { - while let Poll::Ready(permit) = self.handle.poll_permit(self.direction, &mut self.epoch, waiter) { + if self.poll_token_ended(waiter).is_ready() { + return Poll::Ready(()); + } + let mut epoch = self.epoch; + while let Poll::Ready(permit) = self.poll_permit(&mut epoch, waiter) { + self.epoch = epoch; let path = self.path.as_str(); self.within_limit = permit.within_limit(path); let allowed = match self.part { @@ -3059,6 +3231,7 @@ mod tests { #[cfg(all(test, feature = "async"))] mod request_token_tests { use super::*; + use futures::FutureExt; fn token(value: &[u8], kind: u64) -> crate::setup::Token { crate::setup::Token { @@ -3087,6 +3260,117 @@ mod request_token_tests { assert!(grant.publish.matches("room/alice")); } + #[moq_clock::test] + async fn naming_requests_share_verification_and_withdraw_together() { + let handle = Link::new(true); + let mut requests = handle.handle().requests(); + let first = handle.verify_request(token(b"shared", 0), None); + let second = handle.verify_request(token(b"shared", 0), None); + let issued = requests.next().await.unwrap().accept(Grant::all()); + assert!(requests.poll_next(&kio::Waiter::noop()).is_pending()); + first.grant().await.unwrap(); + second.grant().await.unwrap(); + drop(first); + assert!(issued.closed().now_or_never().is_none()); + drop(second); + assert!(matches!(issued.closed().await, Error::Cancel)); + let later = handle.verify_request(token(b"shared", 0), None); + let _issued = requests.next().await.unwrap().accept(Grant::default()); + assert_eq!(later.grant().await.unwrap(), Grant::default()); + } + + #[moq_clock::test] + async fn every_naming_request_observes_updates_revocation_and_expiry() { + for revoke in [false, true] { + let runtime = crate::time::sim(); + let handle = Link::new(true); + let mut requests = handle.handle().requests(); + let first = handle.verify_request(token(b"shared", 0), None); + let second = handle.verify_request(token(b"shared", 0), None); + let issued = requests.next().await.unwrap().accept(grant_in(&runtime, 60)); + let mut grants = Vec::new(); + for verdict in [first, second] { + let answer = verdict.grant().await; + grants.push( + RequestGrant::new( + &runtime, + verdict, + answer, + subscribe_path(), + RequestKind::Subscribe, + None, + ) + .unwrap(), + ); + } + issued.update(grant_in(&runtime, 30)); + for grant in &grants { + assert!(grant.poll_ended(&kio::Waiter::noop()).is_pending()); + assert_eq!(grant.grant(), grant_in(&runtime, 30)); + } + if revoke { + issued.revoke(SessionError::Unauthorized, "revoked"); + } else { + moq_clock::sleep(Duration::from_secs(30)).await; + } + for grant in grants { + let Poll::Ready(refusal) = grant.poll_ended(&kio::Waiter::noop()) else { + panic!("token stayed live"); + }; + if revoke { + assert_eq!(refusal.reason, "revoked"); + } else { + assert!(matches!(refusal.error, Error::Expired)); + } + } + } + } + + #[moq_clock::test] + async fn discovery_uses_its_token_alone_and_observers_do_not_hold_its_lease() { + let runtime = crate::time::sim(); + let handle = Link::new(true); + handle.handle().authorize(&Grant::all()); + let mut requests = handle.handle().requests(); + let mut gate = Gate::new(handle.clone(), crate::Path::default(), Direction::Publish).discovery(); + let verdict = gate.verify(token(b"room", 0), None); + let issued = requests.next().await.unwrap().accept(subscribe_only("room")); + let answer = verdict.grant().await; + gate.admit(&runtime, (verdict, answer)).unwrap(); + let observer = gate.token_observer().unwrap(); + let mut epoch = 0; + let Poll::Ready(permit) = gate.poll_permit(&mut epoch, &kio::Waiter::noop()) else { + panic!("no initial permit"); + }; + assert!(permit.matches("room/alice")); + assert!(!permit.matches("elsewhere")); + issued.update(subscribe_only("room/bob")); + let Poll::Ready(permit) = gate.poll_permit(&mut epoch, &kio::Waiter::noop()) else { + panic!("update missed"); + }; + assert!(permit.matches("room/bob")); + assert!(!permit.matches("room/alice")); + drop(gate); + assert!(matches!(issued.closed().await, Error::Cancel)); + assert!(observer.poll_ended(&kio::Waiter::noop()).is_ready()); + } + + #[moq_clock::test] + async fn request_tokens_are_scoped_to_one_session_on_a_shared_handle() { + let handle = Handle::new(); + let first = Link::attach(&handle, false, true).unwrap(); + let mut requests = handle.requests(); + let verdict = first.verify_request(token(b"token", 0), None); + let issued = requests.next().await.unwrap().accept(Grant::all()); + verdict.grant().await.unwrap(); + first.close(Error::Cancel); + let second = Link::attach(&handle, false, true).unwrap(); + let next = second.verify_request(token(b"token", 0), None); + let _next = requests.next().await.unwrap().accept(Grant::default()); + assert_eq!(next.grant().await.unwrap(), Grant::default()); + assert!(matches!(issued.closed().await, Error::Cancel)); + } + /// An AUTH token has no type, so a request token of any type but out-of-band cannot reach /// the acceptor as one: it is unsupported, never handed over as if it were a plain token. #[moq_clock::test] @@ -3177,6 +3461,7 @@ mod request_token_tests { issued.revoke(SessionError::Unauthorized, "revoked"); let err = verdict.grant().await.expect_err("revoked before admission"); assert_eq!(err.reason, "revoked"); + drop(verdict); let verdict = handle.verify_request(token(b"jwt", 0), None); drop(requests.next().await.unwrap().accept(Grant::all())); @@ -3185,6 +3470,7 @@ mod request_token_tests { matches!(err.error, Error::Session(SessionError::Unauthorized)), "{err:?}" ); + drop(verdict); let verdict = handle.verify_request(token(b"jwt", 0), None); let issued = requests.next().await.unwrap().accept(Grant::all()); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 86018d5e628..6708b99d564 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -1329,6 +1329,7 @@ pub fn seeds() -> Vec { ]; let Some(encoded) = fetch_types.into_iter().find_map(|fetch_type| { let fetch = ietf::Fetch { + authorization_token: None, request_id: ietf::RequestId(0), subscriber_priority: 128, group_order: ietf::GroupOrder::Ascending, diff --git a/rs/moq-net/src/ietf/fetch.rs b/rs/moq-net/src/ietf/fetch.rs index 136dd2df9f1..d850108431e 100644 --- a/rs/moq-net/src/ietf/fetch.rs +++ b/rs/moq-net/src/ietf/fetch.rs @@ -120,6 +120,8 @@ impl<'a> Decode<'a, Version> for FetchType<'a> { #[derive(Debug, Clone, PartialEq, Eq)] pub struct Fetch<'a> { + /// The credential authorizing this request alone. + pub authorization_token: Option, pub request_id: RequestId, pub subscriber_priority: u8, pub group_order: GroupOrder, @@ -181,6 +183,7 @@ impl Fetch<'_> { }; Fetch { request_id: self.request_id, + authorization_token: self.authorization_token, subscriber_priority: self.subscriber_priority, group_order: self.group_order, fetch_type, @@ -207,6 +210,7 @@ impl<'a> Message<'a> for Fetch<'a> { self.fetch_type.encode(w, version)?; let epoch = self.epoch.clone(); encode_params!(w, version, + 0x03 => self.authorization_token.clone(), 0x20 => self.subscriber_priority, 0x22 => group_order, epoch::EPOCH_PARAM => epoch, @@ -226,6 +230,7 @@ impl<'a> Message<'a> for Fetch<'a> { // INCLUDE_PROPERTIES defaults to 1, so only the opt-out is worth bytes. let include_properties = (!self.properties_wanted).then_some(false); encode_params!(w, version, + 0x03 => self.authorization_token.clone(), 0x20 => self.subscriber_priority, 0x21 => *filter, 0x22 => group_order, @@ -240,75 +245,84 @@ impl<'a> Message<'a> for Fetch<'a> { fn decode_msg(buf: &mut Decoder<'a>, version: Version) -> Result { let request_id = RequestId::decode(buf, version)?; - // The token decodes but grants nothing: the session's grant is what authorizes the request. - let (fetch_type, subscriber_priority, group_order, range_filters, fill_timeout, properties_wanted, epoch) = - match version { - Version::Draft18 => { - let fetch_type = FetchType::decode(buf, version)?; - decode_params!(buf, version, - 0x03 => authorization_token: Vec, - 0x0A => fill_timeout: Option, - 0x20 => subscriber_priority: Option, - 0x22 => group_order: Option, - epoch::EPOCH_PARAM => epoch: Option, - ); - super::token::single(authorization_token)?; - - ( - fetch_type, - subscriber_priority, - group_order, - false, - fill_timeout.is_some(), - true, - epoch, - ) - } - // Draft-22 names the track up front and moves the range into LOCATION_FILTER. - _ => { - let (namespace, track) = decode_track(buf)?; - let track = Cow::Borrowed(track); - decode_params!(buf, version, - 0x03 => authorization_token: Vec, - 0x0A => fill_timeout: Option, - 0x20 => subscriber_priority: Option, - 0x21 => filter: Option, - 0x22 => group_order: Option, - 0x25 => subgroup_filter: Vec, - 0x26 => object_id_filter: Vec, - 0x27 => priority_filter: Vec, - 0x28 => object_property_filter: Vec, - 0x35 => include_properties: Option, - epoch::EPOCH_PARAM => epoch: Option, - ); - super::token::single(authorization_token)?; - let range_filters = [ - subgroup_filter, - object_id_filter, - priority_filter, - object_property_filter, - ] - .iter() - .any(|filter| !filter.is_empty()); - - let fetch_type = FetchType::Filtered { - namespace, - track, - // An absent LOCATION_FILTER fetches the whole track. - filter: filter.unwrap_or(Filter::Unfiltered), - }; - ( - fetch_type, - subscriber_priority, - group_order, - range_filters, - fill_timeout.is_some(), - // Defaults to 1, so an absent parameter means the requester wants them. - include_properties.unwrap_or(true), - epoch, - ) - } - }; + let ( + fetch_type, + subscriber_priority, + group_order, + range_filters, + fill_timeout, + properties_wanted, + epoch, + authorization_token, + ) = match version { + Version::Draft18 => { + let fetch_type = FetchType::decode(buf, version)?; + decode_params!(buf, version, + 0x03 => authorization_token: Vec, + 0x0A => fill_timeout: Option, + 0x20 => subscriber_priority: Option, + 0x22 => group_order: Option, + epoch::EPOCH_PARAM => epoch: Option, + ); + let authorization_token = super::token::single(authorization_token)?; + + ( + fetch_type, + subscriber_priority, + group_order, + false, + fill_timeout.is_some(), + true, + epoch, + authorization_token, + ) + } + // Draft-22 names the track up front and moves the range into LOCATION_FILTER. + _ => { + let (namespace, track) = decode_track(buf)?; + let track = Cow::Borrowed(track); + decode_params!(buf, version, + 0x03 => authorization_token: Vec, + 0x0A => fill_timeout: Option, + 0x20 => subscriber_priority: Option, + 0x21 => filter: Option, + 0x22 => group_order: Option, + 0x25 => subgroup_filter: Vec, + 0x26 => object_id_filter: Vec, + 0x27 => priority_filter: Vec, + 0x28 => object_property_filter: Vec, + 0x35 => include_properties: Option, + epoch::EPOCH_PARAM => epoch: Option, + ); + let authorization_token = super::token::single(authorization_token)?; + let range_filters = [ + subgroup_filter, + object_id_filter, + priority_filter, + object_property_filter, + ] + .iter() + .any(|filter| !filter.is_empty()); + + let fetch_type = FetchType::Filtered { + namespace, + track, + // An absent LOCATION_FILTER fetches the whole track. + filter: filter.unwrap_or(Filter::Unfiltered), + }; + ( + fetch_type, + subscriber_priority, + group_order, + range_filters, + fill_timeout.is_some(), + // Defaults to 1, so an absent parameter means the requester wants them. + include_properties.unwrap_or(true), + epoch, + authorization_token, + ) + } + }; // A joining FETCH is served by its subscription's instance; naming one is malformed. if epoch.is_some() @@ -320,6 +334,7 @@ impl<'a> Message<'a> for Fetch<'a> { } Ok(Self { + authorization_token, request_id, subscriber_priority: subscriber_priority.unwrap_or(super::priority::DEFAULT_WIRE), // No preference: the publisher picks the order. @@ -615,6 +630,7 @@ mod tests { #[test] fn test_fetch_v18_round_trip() { let msg = Fetch { + authorization_token: None, request_id: RequestId(1), subscriber_priority: 128, group_order: GroupOrder::Descending, @@ -724,6 +740,7 @@ mod tests { #[test] fn test_fetch_v22_wire() { let fetch = Fetch { + authorization_token: None, request_id: RequestId(1), subscriber_priority: 128, group_order: GroupOrder::Descending, diff --git a/rs/moq-net/src/ietf/publish.rs b/rs/moq-net/src/ietf/publish.rs index c63e29efb13..4c714a172bc 100644 --- a/rs/moq-net/src/ietf/publish.rs +++ b/rs/moq-net/src/ietf/publish.rs @@ -202,6 +202,8 @@ impl<'a> Message<'a> for PublishDone<'a> { #[derive(Debug)] pub struct Publish<'a> { + /// The credential authorizing this request alone. + pub authorization_token: Option, pub request_id: RequestId, pub track_namespace: Path, pub track_name: Cow<'a, str>, @@ -224,6 +226,7 @@ impl Publish<'_> { pub fn into_owned(self) -> Publish<'static> { Publish { request_id: self.request_id, + authorization_token: self.authorization_token, track_namespace: self.track_namespace, track_name: Cow::Owned(self.track_name.into_owned()), track_alias: self.track_alias, @@ -251,6 +254,7 @@ impl<'a> Message<'a> for Publish<'a> { // property instead. let draft22 = Filter::is_draft22(version); encode_params!(w, version, + 0x03 => self.authorization_token.clone(), 0x09 => self.largest_location, 0x10 => self.forward, 0x20 => self.subscriber_priority.filter(|_| draft22), @@ -290,9 +294,10 @@ impl<'a> Message<'a> for Publish<'a> { ); let properties = Properties::decode(r, version)?; // Last, so a malformed field closes the session before a token refusal. - super::token::single(authorization_token)?; + let authorization_token = super::token::single(authorization_token)?; Ok(Self { + authorization_token, request_id, track_namespace, track_name, @@ -530,6 +535,7 @@ mod tests { #[test] fn test_publish_v18_round_trip() { let msg = Publish { + authorization_token: None, request_id: RequestId(1), track_namespace: Path::from_static("test/ns"), track_name: "video".into(), @@ -560,6 +566,7 @@ mod tests { fn publish_priority_property_bytes_on_every_draft() { for version in [Version::Draft18, Version::Draft22] { let mut msg = Publish { + authorization_token: None, request_id: RequestId(1), track_namespace: Path::from_static("ns"), track_name: "video".into(), @@ -592,6 +599,7 @@ mod tests { #[test] fn test_publish_v18_group_order_is_a_property() { let msg = Publish { + authorization_token: None, request_id: RequestId(1), track_namespace: Path::from_static("ns"), track_name: "video".into(), diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 62e6c8aaada..d22db673fcc 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -756,6 +756,7 @@ impl Served { /// What woke an announce-forwarding loop. enum NamespaceEvent { + Denied(crate::auth::Refusal), /// The session or stream ended, with the result to surface. Closed(Result<(), Error>), /// A requester changes the namespace prefix. @@ -1658,6 +1659,29 @@ impl Publisher { super::request_stream::poll_withdrawn(stream, self.version, waiter) } + /// Verify an incoming request token while keeping its stream credit until verification ends. + async fn admit_request( + &self, + stream: &mut Stream, + gate: &mut crate::auth::Gate, + token: Option, + ) -> std::result::Result { + let Some(token) = token.filter(|_| gate.token_decides()) else { + return Ok(true); + }; + let credit = kio::Producer::<()>::default(); + let hold: crate::auth::Hold = Arc::new(credit.consume()); + let verdict = gate.verify(token.0, Some(hold)); + let answer = self.until_withdrawn(stream, |waiter| verdict.poll_grant(waiter)).await; + let Some(answer) = answer else { + drop(verdict); + self.released(&credit).await; + return Ok(false); + }; + gate.admit(&self.runtime, (verdict, answer))?; + Ok(true) + } + /// Poll `step` of a request's setup until ready, or `None` once [`Self::poll_withdrawn`] is. async fn until_withdrawn( &self, @@ -2874,6 +2898,14 @@ impl Publisher { let FetchGate::Standalone(mut gate) = admission else { return Err(Error::Unauthorized); }; + match self + .admit_request(&mut stream, &mut gate, msg.authorization_token.clone()) + .await + { + Ok(true) => {} + Ok(false) => return Ok(()), + Err(refusal) => return self.reject_fetch(stream, &refusal.error, &refusal.reason).await, + } if !gate.allows() { return self.reject_fetch(stream, &Error::Unauthorized, "not granted").await; } @@ -3293,6 +3325,14 @@ impl Publisher { if let Some(error) = super::namespace::refusal(&msg.track_namespace, self.version) { return super::request_stream::reject(stream, error).await; } + match self + .admit_request(&mut stream, &mut gate, msg.authorization_token.clone()) + .await + { + Ok(true) => {} + Ok(false) => return Ok(()), + Err(refusal) => return self.reject_track_status(stream, &refusal.error).await, + } if !gate.allows() { return self.reject_track_status(stream, &Error::Unauthorized).await; } @@ -4079,11 +4119,31 @@ impl Publisher { self, mut stream: Stream, msg: ietf::SubscribeNamespace, - gate: crate::auth::Gate, + mut gate: crate::auth::Gate, ) -> Result<(), Error> { if let Some(error) = super::namespace::refusal(&msg.namespace, self.version) { return super::request_stream::reject(stream, error).await; } + gate = gate.discovery(); + match self + .admit_request(&mut stream, &mut gate, msg.authorization_token.clone()) + .await + { + Ok(true) => {} + Ok(false) => return Ok(()), + Err(refusal) => { + super::request_stream::write_error( + &mut stream.writer, + request::Kind::SubscribeNamespace, + &refusal.error, + &refusal.reason, + self.version, + ) + .await?; + let _ = stream.writer.close().await; + return Ok(()); + } + } let prefix = msg.namespace.clone(); tracing::debug!(prefix = %crate::path::Rooted(self.origin.root(), &prefix), "subscribe_namespace stream"); @@ -4142,19 +4202,24 @@ impl Publisher { false => (Vec::new(), None), }; - // Send OK response - stream - .writer - .encode_message(&ietf::RequestOk { largest: None, active }) - .await?; + // Send the acknowledgement only while the token is live, even behind stream credit. + crate::auth::until( + stream.writer.encode_message(&ietf::RequestOk { largest: None, active }), + |waiter| gate.poll_token_ended(waiter), + ) + .await + .map_err(|refusal| refusal.error)??; let target = Target::Inline(stream); + let token = gate.token_guard(); let mut ns = Namespaces::new(peer, target); ns.gate = Some(gate); ns.hidden = !declared.hidden || msg.hidden || self.origin.includes_hidden(); ns.epochs = declared.epoch; - self.run_namespaces(announced, prefix, ns, initial).await + crate::auth::RequestGrant::guard(token.as_ref(), self.run_namespaces(announced, prefix, ns, initial)) + .await + .map_err(|refusal| refusal.error)? } /// Our publish permit as it stands now, with the auth epoch it was read at. @@ -4261,6 +4326,11 @@ impl Publisher { if self.withdrawal.poll(waiter).is_ready() { return Poll::Ready(NamespaceEvent::Update(None)); } + if let Some(gate) = gate + && let Poll::Ready(refusal) = gate.poll_token_ended(waiter) + { + return Poll::Ready(NamespaceEvent::Denied(refusal)); + } // A grant change applies before the next update, so a namespace it no // longer covers is withdrawn rather than re-sent. let permit = match gate { @@ -4303,6 +4373,21 @@ impl Publisher { }; match event { + NamespaceEvent::Denied(refusal) => { + if let Target::Inline(mut stream) = ns.target { + super::request_stream::write_error( + &mut stream.writer, + request::Kind::SubscribeNamespace, + &refusal.error, + &refusal.reason, + self.version, + ) + .await?; + let _ = stream.writer.close().await; + } + return Ok(()); + } + // Only a solicited loop has a stream to close, and its request task closes the // session over a protocol violation. NamespaceEvent::Closed(res) => break res, @@ -4338,6 +4423,28 @@ impl Publisher { stream.writer.finish()?; break stream.writer.closed().await; } + if update.authorization_token.is_some() { + let stream = ns.target.stream().expect("update has a stream"); + let gate = ns.gate.as_mut().expect("update has a gate"); + match self.admit_request(stream, gate, update.authorization_token).await { + Ok(true) => { + ns.epoch = 0; + } + Ok(false) => break Ok(()), + Err(refusal) => { + super::request_stream::write_error( + &mut stream.writer, + request::Kind::SubscribeNamespace, + &refusal.error, + &refusal.reason, + self.version, + ) + .await?; + let _ = stream.writer.close().await; + break Ok(()); + } + } + } if let Some(next) = update.prefix.filter(|next| next != &prefix) { // DONE still names the old prefix. After OK every suffix is relative // to the new prefix, so replay the new cursor only after acknowledging. @@ -7166,6 +7273,7 @@ mod serve_tests { .unwrap(); let mut fetch_body = Vec::new(); ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -8346,6 +8454,7 @@ mod serve_tests { let mut running = std::pin::pin!(h.publisher.clone().run_subscribe_namespace_stream( stream, ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(0), namespace: crate::Path::empty(), hidden: false @@ -8386,6 +8495,7 @@ mod serve_tests { .await .unwrap(); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(0), namespace: crate::Path::from_static("room"), hidden: false, @@ -8430,6 +8540,7 @@ mod serve_tests { .await .unwrap(); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(0), namespace: crate::Path::from_static("old"), hidden: false, @@ -8482,6 +8593,7 @@ mod serve_tests { .await .unwrap(); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(0), namespace: crate::Path::from_static(""), hidden: false, @@ -8620,6 +8732,7 @@ mod serve_tests { let mut subscribe = subscribe(Filter::NextObject, None); subscribe.epoch = Some(epoch.clone()); let fetch = ietf::Fetch { + authorization_token: None, request_id: RequestId(REQUEST_ID), subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -8635,6 +8748,7 @@ mod serve_tests { epoch: Some(epoch.clone()), }; let status = ietf::TrackStatus { + authorization_token: None, request_id: RequestId(REQUEST_ID), track_namespace: crate::Path::from_static("room"), track_name: "video".into(), @@ -8773,6 +8887,7 @@ mod serve_tests { fn track_status(namespace: &str, wanted: bool, version: Version) -> Vec { let msg = ietf::TrackStatus { + authorization_token: None, request_id: RequestId(REQUEST_ID), track_namespace: crate::Path::new(namespace).unwrap(), track_name: "video".into(), @@ -8912,28 +9027,29 @@ mod serve_tests { } } - /// A request's USE_VALUE token decodes but grants nothing yet, so a TRACK_STATUS outside - /// the session grant is still refused. + /// A TRACK_STATUS uses its own token without admitting the next unnamed request. #[moq_clock::test] - async fn a_request_token_does_not_widen_the_grant() { - let nothing = crate::auth::Grant { - publish: Default::default(), - subscribe: Default::default(), - expires: None, - }; - for version in [Version::Draft22] { + async fn track_status_uses_only_its_named_token() { + for version in ALL_VERSIONS { let mut h = serve(version); let auth = crate::auth::Link::new(false); - auth.handle().authorize(¬hing); + let mut requests = auth.handle().requests(); + auth.handle().authorize(&crate::auth::Grant::default()); h.publisher = h.publisher.clone().with_auth(auth); - let mut body = track_status("room", true, version); - // Replace the empty parameter block with an AUTHORIZATION TOKEN: USE_VALUE, type 0. - assert_eq!(body.pop(), Some(0), "{version}"); + assert_eq!(body.pop(), Some(0)); body.extend([0x01, 0x03, 0x03, 0x03, 0x00, 0xAA]); - + let answer = answer_code(&h, body, version); + let (code, issued) = futures::join!(answer, async { + requests.next().await.unwrap().accept(crate::auth::Grant::all()) + }); + assert_eq!(code, None); + assert!(matches!(issued.closed().await, Error::Cancel)); let unauthorized = request::to_code(&Error::Unauthorized, request::Kind::TrackStatus, version); - assert_eq!(answer_code(&h, body, version).await, Some(unauthorized), "{version}"); + assert_eq!( + answer_code(&h, track_status("room", true, version), version).await, + Some(unauthorized) + ); } } @@ -8976,6 +9092,7 @@ mod serve_tests { let mut answer = std::pin::pin!(publisher.clone().run_track_status_stream( stream, ietf::TrackStatus { + authorization_token: None, request_id: RequestId(REQUEST_ID), track_namespace: crate::Path::from_static("room"), track_name: "video".into(), @@ -9120,6 +9237,7 @@ mod serve_tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: RequestId(REQUEST_ID), subscriber_priority: 128, group_order: GroupOrder::Descending, @@ -9331,6 +9449,7 @@ mod serve_tests { let mut body = Vec::new(); let mut w = Encoder::new(&mut body, version.into()); ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(REQUEST_ID), namespace: crate::Path::from_static(""), hidden: false, @@ -10094,6 +10213,7 @@ mod serve_tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -10417,6 +10537,7 @@ mod serve_tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -10649,6 +10770,7 @@ mod serve_tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order, @@ -10747,6 +10869,65 @@ mod serve_tests { } } + #[moq_clock::test] + async fn standalone_fetch_uses_only_its_named_token() { + for version in FETCH_DRAFTS { + let mut h = serve(version); + publish_pairs(&mut h, 1, None); + settle().await; + let auth = crate::auth::Link::new(false); + let mut requests = auth.handle().requests(); + auth.handle().authorize(&crate::auth::Grant::default()); + h.publisher = h.publisher.clone().with_auth(auth); + let msg = ietf::Fetch { + authorization_token: Some(request_token()), + request_id: FETCH_ID, + subscriber_priority: 128, + group_order: GroupOrder::Ascending, + fetch_type: fetch_range( + version, + "video", + Location { group: 0, object: 0 }, + Location { group: 0, object: 0 }, + ), + range_filters: false, + fill_timeout: false, + properties_wanted: true, + epoch: None, + }; + let mut body = Vec::new(); + msg.encode_msg(&mut Encoder::new(&mut body, version.into()), version) + .unwrap(); + let stream = Stream::open(&mut crate::driver::Transport::new(h.session.clone()), version) + .await + .unwrap(); + let mark = h.log.writes.lock().unwrap().len(); + let serving = h + .publisher + .clone() + .handle_stream(ietf::Fetch::ID, ietf::Body(body.into()), stream, false) + .unwrap(); + let ((), issued) = futures::join!(serving, async { + requests.next().await.unwrap().accept(crate::auth::Grant::all()) + }); + let wire = bytes::Bytes::from(h.log.writes.lock().unwrap()[mark..].to_vec()); + let (_, objects) = fetch_answer(wire, version); + assert_eq!(objects, pairs([0])); + assert!(matches!(issued.closed().await, Error::Cancel)); + let mut unnamed = msg; + unnamed.authorization_token = None; + let stream = Stream::open(&mut crate::driver::Transport::new(h.session.clone()), version) + .await + .unwrap(); + let mark = h.log.writes.lock().unwrap().len(); + h.publisher.clone().run_fetch_stream(stream, unnamed).await.unwrap(); + assert_eq!( + refusal_code(&h.log.writes.lock().unwrap()[mark..], version), + Some(request::to_code(&Error::Unauthorized, request::Kind::Fetch, version)) + ); + } + } + /// FETCH_OK carries the properties SUBSCRIBE_OK would, as far as each draft has room for /// them, and INCLUDE_PROPERTIES=0 empties the block. Either way the objects keep their /// Timestamps wherever the draft can declare their units. @@ -11672,6 +11853,7 @@ mod serve_tests { let mut fetch = std::pin::pin!(publisher.run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -11755,6 +11937,7 @@ mod serve_tests { let mut fetch = Box::pin(publisher.run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -11878,6 +12061,7 @@ mod serve_tests { let mut fetch = std::pin::pin!(publisher.clone().run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -12003,6 +12187,7 @@ mod serve_tests { let mut fetch = std::pin::pin!(publisher.clone().run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -12268,6 +12453,7 @@ mod serve_tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: RequestId(REQUEST_ID), subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -12291,6 +12477,7 @@ mod serve_tests { .run_track_status_stream( stream, ietf::TrackStatus { + authorization_token: None, request_id: RequestId(REQUEST_ID), track_namespace: crate::Path::from_static("room"), track_name: "video".into(), @@ -12492,6 +12679,7 @@ mod serve_tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -12542,6 +12730,7 @@ mod serve_tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -12865,7 +13054,10 @@ mod serve_tests { subscriber_priority: Some(128), forward: Some(forward), filter: None, - authorization_token: Some(request_token()), + authorization_token: Some(crate::ietf::token::RequestToken(crate::setup::Token { + kind: 0, + value: 0x40_u64.to_be_bytes().to_vec(), + })), fill: None, }; writer.varint(ietf::SubscribeUpdate::ID).await.unwrap(); @@ -13054,7 +13246,10 @@ mod serve_tests { subscriber_priority: Some(128), forward: Some(true), filter: None, - authorization_token: Some(request_token()), + authorization_token: Some(crate::ietf::token::RequestToken(crate::setup::Token { + kind: 0, + value: rid.to_be_bytes().to_vec(), + })), fill: None, }; writer.varint(ietf::SubscribeUpdate::ID).await.unwrap(); @@ -14571,6 +14766,7 @@ mod tests { .await .unwrap(); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(1), namespace: crate::Path::from_static(""), hidden: false, @@ -14642,6 +14838,7 @@ mod tests { .await .unwrap(); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(1), namespace: crate::Path::from_static(""), hidden: false, @@ -15106,6 +15303,7 @@ mod tests { .await .unwrap(); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(1), namespace: crate::Path::new(prefix).unwrap(), hidden, @@ -15236,6 +15434,7 @@ mod tests { .await .unwrap(); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(1), namespace: crate::Path::from_static(""), hidden: false, @@ -16687,6 +16886,7 @@ mod tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: RequestId(1), subscriber_priority: 128, group_order: GroupOrder::Descending, diff --git a/rs/moq-net/src/ietf/publisher/tracks.rs b/rs/moq-net/src/ietf/publisher/tracks.rs index 066a563f983..c0d69f345dd 100644 --- a/rs/moq-net/src/ietf/publisher/tracks.rs +++ b/rs/moq-net/src/ietf/publisher/tracks.rs @@ -147,6 +147,7 @@ type Skips = kio::Shared), ()>>; /// What a request's listing shares with the tasks listing each broadcast. #[derive(Clone)] struct Request { + token_grant: Option, origin: origin::Consumer, /// The request's prefix, which every PUBLISH namespace starts with. prefix: crate::Path, @@ -309,6 +310,27 @@ impl Publisher { let error = refuse(request::NOT_SUPPORTED, "SUBSCRIBE_TRACKS needs FORWARD=0"); return super::super::request_stream::reject(stream, error).await; } + let mut gate = + crate::auth::Gate::new(self.auth.clone(), msg.prefix.clone(), crate::auth::Direction::Publish).discovery(); + match self + .admit_request(&mut stream, &mut gate, msg.authorization_token.clone()) + .await + { + Ok(true) => {} + Ok(false) => return Ok(()), + Err(refusal) => { + super::super::request_stream::write_error( + &mut stream.writer, + request::Kind::SubscribeNamespace, + &refusal.error, + &refusal.reason, + self.version, + ) + .await?; + let _ = stream.writer.close().await; + return Ok(()); + } + } let prefix = msg.prefix.clone(); let Some(established) = Established::new(&self.listings, &prefix) else { let error = refuse(request::PREFIX_OVERLAP, "overlaps another SUBSCRIBE_TRACKS"); @@ -337,17 +359,20 @@ impl Publisher { .discovery(!declared.hidden || self.origin.includes_hidden()); let mut announced = origin.announced(); - stream - .writer - .encode_message(&ietf::RequestOk { + crate::auth::until( + stream.writer.encode_message(&ietf::RequestOk { active: None, largest: None, - }) - .await?; + }), + |waiter| gate.poll_token_ended(waiter), + ) + .await + .map_err(|refusal| refusal.error)??; // Dropped when the request is cancelled, which tells every broadcast to stop. let live = kio::Producer::new(()); let request = Request { + token_grant: gate.token_observer(), origin, prefix: prefix.clone(), forward: msg.forward, @@ -393,6 +418,9 @@ impl Publisher { { return Poll::Ready(Event::Cancelled(res)); } + if let Poll::Ready(refusal) = gate.poll_token_ended(waiter) { + return Poll::Ready(Event::Cancelled(Err(refusal.error))); + } if let Err(err) = Self::poll_write_skips(&mut stream, &request.skips, waiter) { return Poll::Ready(Event::Cancelled(Err(err))); } @@ -438,6 +466,7 @@ impl Publisher { // already sent still ends when its track does. drop(established); drop(live); + drop(gate); request.skips.lock().clear(); // A refused update left its REQUEST_ERROR buffered, so a cancel flushes before the FIN. let mut closing = match cancelled { @@ -627,7 +656,11 @@ impl Publisher { // The broadcast's listing, held only while our publish permit covers it: a track // it does not cover is not revealed at all, and one it comes to cover is listed // from scratch. - let (permit, mut auth_epoch) = self.permit_now(None); + let mut gate = crate::auth::Gate::new(self.auth.clone(), namespace.clone(), crate::auth::Direction::Publish); + if let Some(token) = request.token_grant.clone() { + gate.bind(token); + } + let (permit, mut auth_epoch) = self.permit_now(Some(&gate)); let mut tracks = None; if permit.matches(namespace.as_str()) { match broadcast.tracks() { @@ -705,10 +738,7 @@ impl Publisher { Route::Withdrawn => Event::Withdrawn, })); } - if let Poll::Ready(permit) = - self.auth - .poll_permit(crate::auth::Direction::Publish, &mut auth_epoch, waiter) - { + if let Poll::Ready(permit) = gate.poll_permit(&mut auth_epoch, waiter) { return Poll::Ready(Some(Event::Regrant(permit))); } if entries.staged.is_some() @@ -880,6 +910,7 @@ impl Publisher { tracing::debug!(id = %request_id, broadcast = %absolute, track = %name, "publish started"); let publish = ietf::Publish { + authorization_token: None, request_id, track_namespace: namespace.clone(), track_name: name.as_ref().into(), @@ -1057,18 +1088,18 @@ impl Publisher { Wait::Failed(err) => break Served::Track(Err(err)), }; // Refused like an update to any subscription: it fails, ending this one. + match self + .admit_request(&mut stream, &mut gate, update.authorization_token.clone()) + .await + { + Ok(true) => {} + Ok(false) => return Ok(()), + Err(refusal) => break Served::Track(Err(refusal.error)), + } let refusal = match update.refused { Some(Error::Decode(err)) => Some((super::super::token::reason(&err), Error::Decode(err))), Some(Error::GoingAway) => Some(("going away", Error::GoingAway)), Some(err) => Some(("REQUEST_UPDATE parameters not supported", err)), - None if update.authorization_token.is_some() - && self.auth.token_decides(crate::auth::Direction::Publish) => - { - Some(( - "AUTHORIZATION TOKEN on a request admitted without one", - Error::Unsupported, - )) - } None if update.new_group.is_some() && !dynamic => Some(( "NEW_GROUP_REQUEST on a track without DYNAMIC_GROUPS", Error::Unsupported, @@ -1877,6 +1908,7 @@ mod tests { let live = kio::Producer::new(()); let route = kio::Producer::new(Route::Replaced); let request = Request { + token_grant: None, origin: h._origin.consume(), prefix: crate::Path::from_static(""), forward: true, diff --git a/rs/moq-net/src/ietf/request_stream.rs b/rs/moq-net/src/ietf/request_stream.rs index f154559aaad..189beeb7c2a 100644 --- a/rs/moq-net/src/ietf/request_stream.rs +++ b/rs/moq-net/src/ietf/request_stream.rs @@ -332,6 +332,7 @@ impl<'a> Decode<'a, Version> for Update { /// FORWARD. #[derive(Debug)] pub(super) struct NamespaceUpdate { + pub(super) authorization_token: Option, pub(super) request_id: super::RequestId, pub(super) prefix: Option, pub(super) refused: Option, @@ -368,10 +369,15 @@ impl<'a, const TRACKS: bool> Decode<'a, Version> for NamespaceUpdate { if !data.is_empty() { return Err(DecodeError::InvalidValue); } + let (authorization_token, refused) = match super::token::single(token) { + Ok(token) => (token, None), + Err(err) => (None, Some(err.into())), + }; Ok(Self { request_id, prefix: prefix.map(|p| p.0), - refused: super::token::single(token).err().map(Into::into), + authorization_token, + refused, }) }; let update = decode().map_err(DecodeError::complete)?; diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index 28a7f6f5c14..a369d749e70 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -1445,6 +1445,7 @@ mod tests { auth::Auth::ID } else { ietf::TrackStatus { + authorization_token: None, request_id: RequestId(id), track_namespace: crate::Path::from_static("test"), track_name: "video".into(), @@ -1893,6 +1894,7 @@ mod tests { version, ); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id, namespace: crate::Path::new(namespace).unwrap(), hidden: false, @@ -3558,6 +3560,7 @@ mod tests { fn subscribe_namespace_then(version: Version, request_id: u64, after: &[u8]) -> Vec { let mut body = Vec::new(); ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(request_id), namespace: crate::Path::from_static(""), hidden: false, diff --git a/rs/moq-net/src/ietf/subscribe_namespace.rs b/rs/moq-net/src/ietf/subscribe_namespace.rs index 5ceb1f74fec..f66702a97f0 100644 --- a/rs/moq-net/src/ietf/subscribe_namespace.rs +++ b/rs/moq-net/src/ietf/subscribe_namespace.rs @@ -12,6 +12,8 @@ use super::Version; /// SUBSCRIBE_NAMESPACE message (type 0x50). #[derive(Clone, Debug)] pub struct SubscribeNamespace { + /// The credential authorizing this request alone. + pub authorization_token: Option, pub request_id: RequestId, pub namespace: Path, /// MoQ Hidden: also advertise hidden namespaces. Only sent to a peer that declared @@ -39,23 +41,23 @@ impl<'a> Message<'a> for SubscribeNamespace { fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { self.request_id.encode(w, version)?; encode_namespace(w, &self.namespace)?; - encode_params!(w, version, HIDDEN_PARAM => hidden_param(self.hidden)); + encode_params!(w, version, HIDDEN_PARAM => hidden_param(self.hidden), 0x03 => self.authorization_token.clone()); Ok(()) } fn decode_msg(r: &mut Decoder<'a>, version: Version) -> Result { let request_id = RequestId::decode(r, version)?; let namespace = decode_namespace(r)?; - // The token decodes but grants nothing: the session's grant is what authorizes the request. decode_params!(r, version, 0x03 => authorization_token: Vec, HIDDEN_PARAM => hidden: Option, ); let hidden = hidden_from_param(hidden)?; // Last, so a malformed field closes the session before a token refusal. - super::token::single(authorization_token)?; + let authorization_token = super::token::single(authorization_token)?; Ok(Self { + authorization_token, request_id, namespace, hidden, @@ -374,6 +376,7 @@ mod tests { fn body_has_no_subscribe_options() { // Request ID (0x00), empty namespace field-count (0x00), Number of Parameters (0x00). let msg = SubscribeNamespace { + authorization_token: None, request_id: RequestId(0), namespace: Path::default(), hidden: false, @@ -384,6 +387,7 @@ mod tests { #[test] fn round_trips() { let msg = SubscribeNamespace { + authorization_token: None, request_id: RequestId(4), namespace: Path::from_static("example/meeting"), hidden: false, @@ -401,6 +405,7 @@ mod tests { fn hidden_round_trips() { for hidden in [false, true] { let msg = SubscribeNamespace { + authorization_token: None, request_id: RequestId(4), namespace: Path::from_static("example"), hidden, diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index ed6fa325a39..1af121d4e08 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -1418,6 +1418,7 @@ impl Subscriber { let stream = &mut request.stream; let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id, namespace: prefix.clone(), hidden, @@ -2926,6 +2927,9 @@ impl Subscriber { group_fetches: &mut TaskSet, gate: &mut crate::auth::Gate, ) -> Option { + let mut request_gate = gate.unnamed(); + let gate = &mut request_gate; + let mut token_grant = None; let validity = self.validity.start(); let mut resolved = ResolvedRanges::default(); let subscription = target @@ -2986,6 +2990,7 @@ impl Subscriber { Some(upstream) => { self.adopted(&key, upstream.request_id); let tracks::Upstream { + token_grant: token, stream, messages, request_id, @@ -2995,6 +3000,10 @@ impl Subscriber { // This subscription forwards into the copy itself from here. copy: _, } = upstream; + if let Some(token) = &token { + gate.bind(token.clone()); + } + token_grant = token; held = Some(tracks::Resumed { alias, properties, @@ -3238,6 +3247,7 @@ impl Subscriber { && self.state.lock().subscribes.contains_key(&request_id) { let upstream = tracks::Upstream { + token_grant: token_grant.clone(), stream, messages, request_id, @@ -3773,6 +3783,7 @@ impl Subscriber { // A listed track's subscription pauses rather than ends, held again as its entry. track.set_idle(); let upstream = tracks::Upstream { + token_grant: token_grant.clone(), stream, messages, request_id, @@ -4091,6 +4102,7 @@ impl Subscriber { stream .writer .encode_message(&ietf::Fetch { + authorization_token: None, request_id: fetch_id, subscriber_priority: super::priority::to_wire( track.subscription().map(|s| s.priority).unwrap_or(0), @@ -4260,6 +4272,7 @@ impl Subscriber { stream .writer .encode_message(&ietf::TrackStatus { + authorization_token: None, request_id, track_namespace: broadcast_path.clone(), track_name: track_name.into(), @@ -5385,6 +5398,7 @@ impl Subscriber { stream .writer .encode_message(&ietf::Fetch { + authorization_token: None, request_id: fetch_id, subscriber_priority: super::priority::to_wire(fill.priority()), group_order: GroupOrder::Ascending, @@ -9894,6 +9908,7 @@ mod tests { .await .unwrap(); let msg = ietf::Publish { + authorization_token: None, request_id: RequestId(1), track_namespace: crate::Path::from_static("room/host"), track_name: "video".into(), @@ -10066,7 +10081,10 @@ mod tests { request_id: RequestId(3), hops: None, cost: None, - authorization_token: Some(announce_token()), + authorization_token: Some(crate::ietf::token::RequestToken(crate::setup::Token { + kind: 0, + value: 3_u64.to_be_bytes().to_vec(), + })), }; writer.varint(ietf::PublishNamespaceUpdate::ID).await.unwrap(); writer.encode(&msg).await.unwrap(); @@ -10085,7 +10103,10 @@ mod tests { request_id: RequestId(3), hops: Some(hop_path(&[7, 9])), cost: Some(0), - authorization_token: Some(announce_token()), + authorization_token: Some(crate::ietf::token::RequestToken(crate::setup::Token { + kind: 0, + value: 3_u64.to_be_bytes().to_vec(), + })), }; writer.varint(ietf::PublishNamespaceUpdate::ID).await.unwrap(); writer.encode(&msg).await.unwrap(); @@ -10878,7 +10899,10 @@ mod tests { request_id: RequestId(rid), hops, cost, - authorization_token: Some(announce_token()), + authorization_token: Some(crate::ietf::token::RequestToken(crate::setup::Token { + kind: 0, + value: rid.to_be_bytes().to_vec(), + })), }; writer.varint(ietf::PublishNamespaceUpdate::ID).await.unwrap(); writer.encode(&msg).await.unwrap(); @@ -12575,6 +12599,7 @@ mod stitch_tests { } }; ietf::Fetch { + authorization_token: None, request_id: RequestId(3), subscriber_priority: 128, group_order: ietf::GroupOrder::Ascending, @@ -14432,6 +14457,7 @@ mod stitch_tests { }; let mut body = Vec::new(); ietf::Fetch { + authorization_token: None, request_id: RequestId(3), subscriber_priority: 128, group_order: ietf::GroupOrder::Ascending, diff --git a/rs/moq-net/src/ietf/subscriber/tracks.rs b/rs/moq-net/src/ietf/subscriber/tracks.rs index d27a291ba33..af94ad4da3a 100644 --- a/rs/moq-net/src/ietf/subscriber/tracks.rs +++ b/rs/moq-net/src/ietf/subscriber/tracks.rs @@ -42,6 +42,7 @@ pub(super) fn listing_end(done: &ietf::PublishDone, version: Version) -> Result< /// A paused upstream subscription and the request stream that controls it. pub(super) struct Upstream { + pub(super) token_grant: Option, pub(super) stream: Stream, pub(super) messages: Messages, pub(super) request_id: RequestId, @@ -710,6 +711,7 @@ impl Subscriber { return Ok(None); } Ok(Some(Upstream { + token_grant: None, stream, messages, request_id, @@ -747,6 +749,54 @@ impl Subscriber { mut stream: Stream, msg: ietf::Publish<'static>, ) -> Result<(), Error> { + let token_grant = if let Some(token) = msg + .authorization_token + .clone() + .filter(|_| self.auth.token_decides(crate::auth::Direction::Subscribe)) + { + let credit = kio::Producer::<()>::default(); + let hold: crate::auth::Hold = std::sync::Arc::new(credit.consume()); + let verdict = self.auth.verify_request(token.0, Some(hold)); + let session = self.session.clone(); + let verified = kio::wait(|waiter| { + if session.poll_closed(waiter).is_ready() + || request_stream::poll_withdrawn(&mut stream, self.version, waiter).is_ready() + { + return Poll::Ready(None); + } + verdict.poll_grant(waiter).map(Some) + }) + .await; + let Some(verified) = verified else { + drop(verdict); + kio::wait(|waiter| { + if session.poll_closed(waiter).is_ready() || credit.poll_unused(waiter).is_ready() { + Poll::Ready(()) + } else { + Poll::Pending + } + }) + .await; + return Ok(()); + }; + match crate::auth::RequestGrant::new( + &self.runtime, + verdict, + verified, + msg.track_namespace.clone(), + crate::auth::RequestKind::Publish, + None, + ) { + Ok(grant) => Some(grant), + Err(refusal) => { + return self + .write_publish_error(&mut stream, &refusal.error, &refusal.reason) + .await; + } + } + } else { + None + }; let key: TrackKey = (msg.track_namespace.clone(), msg.track_name.as_ref().into()); let info = accepted_info(&msg.properties, self.version); let flate = self.peer_setup.get().await.flate; @@ -842,7 +892,7 @@ impl Subscriber { ..TrackState::pending(key.1.to_string(), key.0.clone(), kio::Producer::new(Fill::Done)) }, ); - let ok = async { + let ok = crate::auth::RequestGrant::guard(token_grant.as_ref(), async { self.bind_alias(request_id, msg.track_alias)?; // PUBLISH_OK is a REQUEST_OK on draft-22. FORWARD=0 holds it paused on draft-18; // on draft-22 the PUBLISH our FORWARD=0 asked for already is. @@ -861,8 +911,10 @@ impl Subscriber { }, )?; kio::wait(|waiter| stream.writer.poll_flush(waiter)).await - } - .await; + }) + .await + .map_err(|refusal| refusal.error) + .and_then(|result| result); if let Err(err) = ok { tracing::debug!(broadcast = %key.0, track = %key.1, %err, "failed to hold publish"); self.remove_subscribe(request_id); @@ -875,6 +927,7 @@ impl Subscriber { } tracing::debug!(broadcast = %crate::path::Rooted(self.origin.root(), &key.0), track = %key.1, id = %request_id, "holding publish"); let upstream = Upstream { + token_grant, stream, messages: Messages::new(self.session.clone(), self.control.client()), request_id, @@ -911,6 +964,11 @@ impl Subscriber { false => Step::Wanted, }); } + if let Some(grant) = &upstream.token_grant + && let Poll::Ready(refusal) = grant.poll_ended(waiter) + { + return Poll::Ready(Step::Message(Err(refusal.error))); + } upstream .messages .poll_next(&mut upstream.stream.reader, waiter) @@ -1437,6 +1495,7 @@ mod tests { /// A paused PUBLISH of `name` in `namespace`, aliased by its request ID. fn offered<'a>(namespace: &'a str, name: &'a str, request_id: u64) -> ietf::Publish<'a> { ietf::Publish { + authorization_token: None, request_id: RequestId(request_id), track_namespace: Path::new(namespace).unwrap(), track_name: name.into(), diff --git a/rs/moq-net/src/ietf/track.rs b/rs/moq-net/src/ietf/track.rs index 093c6617f7d..5928bc5aa22 100644 --- a/rs/moq-net/src/ietf/track.rs +++ b/rs/moq-net/src/ietf/track.rs @@ -19,6 +19,8 @@ use super::Version; /// REQUEST_OK. #[derive(Clone, Debug)] pub struct TrackStatus<'a> { + /// The credential authorizing this request alone. + pub authorization_token: Option, pub request_id: RequestId, pub track_namespace: Path, pub track_name: Cow<'a, str>, @@ -34,6 +36,7 @@ impl TrackStatus<'_> { pub fn into_owned(self) -> TrackStatus<'static> { TrackStatus { request_id: self.request_id, + authorization_token: self.authorization_token, track_namespace: self.track_namespace, track_name: Cow::Owned(self.track_name.into_owned()), properties_wanted: self.properties_wanted, @@ -55,6 +58,7 @@ impl<'a> Message<'a> for TrackStatus<'a> { let epoch = self.epoch.clone(); encode_params!(w, version, 0x35 => include_properties, + 0x03 => self.authorization_token.clone(), super::epoch::EPOCH_PARAM => epoch, ); Ok(()) @@ -67,6 +71,7 @@ impl<'a> Message<'a> for TrackStatus<'a> { let subscribe = Subscribe::decode_msg(r, version)?; Ok(Self { request_id: subscribe.request_id, + authorization_token: subscribe.authorization_token, track_namespace: subscribe.track_namespace, track_name: subscribe.track_name, properties_wanted: subscribe.properties_wanted, @@ -162,6 +167,7 @@ mod tests { #[test] fn test_track_status_v18_round_trip() { let msg = TrackStatus { + authorization_token: None, request_id: RequestId(1), track_namespace: Path::from_static("test/ns"), track_name: "video".into(), @@ -186,6 +192,7 @@ mod tests { for version in ALL { for wanted in [true, false] { let msg = TrackStatus { + authorization_token: None, request_id: RequestId(1), track_namespace: Path::from_static("ns"), track_name: "video".into(), diff --git a/rs/moq-net/src/test_interop.rs b/rs/moq-net/src/test_interop.rs index 68c639c2862..16914f9371e 100644 --- a/rs/moq-net/src/test_interop.rs +++ b/rs/moq-net/src/test_interop.rs @@ -601,6 +601,7 @@ fn ietf_track_status_interop() { let mut w = Encoder::new(&mut buf, version.into()); w.varint(::ID).unwrap(); ietf::TrackStatus { + authorization_token: None, request_id: ietf::RequestId(id), track_namespace: crate::Path::new(namespace).unwrap(), track_name: "video".into(), diff --git a/rs/moq-relay/src/auth.rs b/rs/moq-relay/src/auth.rs index 3e978f447df..c77a30e375b 100644 --- a/rs/moq-relay/src/auth.rs +++ b/rs/moq-relay/src/auth.rs @@ -29,7 +29,7 @@ const ATTACH_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); #[serde(default)] #[non_exhaustive] pub struct Config { - /// The auth server asked once per session event: `connect`, `revalidate`, and + /// The auth server asked once per session event: `connect`, `attach`, `revalidate`, and /// `end`, each one JSON POST carrying everything the relay knows. `https://` /// presents the `--connect-tls-*` identity, `unix://` speaks HTTP over a socket, /// and `http://` is accepted for a loopback host only. diff --git a/rs/moq-relay/tests/integration/auth_lifetime.rs b/rs/moq-relay/tests/integration/auth_lifetime.rs index 24a1e590264..abab2e146df 100644 --- a/rs/moq-relay/tests/integration/auth_lifetime.rs +++ b/rs/moq-relay/tests/integration/auth_lifetime.rs @@ -102,7 +102,7 @@ impl Script { async fn handle(State(script): State