diff --git a/doc/bin/relay/auth.md b/doc/bin/relay/auth.md index e9f1601afb5..78303eba4b7 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 @@ -79,7 +79,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; @@ -180,17 +184,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 2de07142f37..ac1775442c1 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -33,7 +33,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` or `PUBLISH` declares no `TIMESCALE` is untimed. A `SUBSCRIBE` or `FETCH` with `INCLUDE_PROPERTIES=0` gets no `TIMESCALE` in its `SUBSCRIBE_OK` or `FETCH_OK`, so its objects go out unstamped. A Rust reader that never subscribed takes the units from `TRACK_STATUS`, and still asks for the properties whenever it subscribes to or fetches a timed track. - **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. A request is admitted by the session or by a token for good, so an update naming a token on one the session admitted is refused with `NOT_SUPPORTED`. A token-less `PUBLISH_NAMESPACE` our limit does not cover is refused `UNAUTHORIZED`, and one the limit stops covering ends; the publisher re-announces once it widens. 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: `SUBSCRIBE_TRACKS`, multi-group `FETCH`, and joining requests reaching before the joined group get `NOT_SUPPORTED`. Range Filters get `INVALID_FILTER` from draft 20 because the advertised filter limit is zero. From draft 20, 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). From draft 20, a wider or historical fill fails on its separate fetch stream while its subscription stays open; a largest-group fill hands its head to the live subgroup tail. 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 draft 18 and later drafts, or refused when immutable, because draft 20 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..2bb3dfbcbd8 100644 --- a/drafts/draft-lcurley-moq-auth.md +++ b/drafts/draft-lcurley-moq-auth.md @@ -105,6 +105,17 @@ 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 is admitted either by the session or by a token, and that never changes. +A REQUEST_UPDATE carrying a token replaces the token authority of a request a token admitted, including when the update pauses forwarding; the acceptor MUST verify the token before acknowledging the update. +On a request the session admitted, the acceptor MUST refuse an update carrying a token. +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 +251,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; updates replace a token-admitted request's token, even while pausing or resuming, and a token on a session-admitted request's update is refused. + - 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 08babb27f8d..86967f8d5f0 100644 --- a/js/auth/api/index.api.md +++ b/js/auth/api/index.api.md @@ -674,6 +674,7 @@ export type Request = infer; export const RequestSchema: ZodMiniDiscriminatedUnion<[ZodMiniObject<{ alpn: ZodMiniOptional>; + auth: ZodMiniOptional>; event: ZodMiniLiteral<"connect">; id: ZodMiniString; implementation: ZodMiniOptional>; @@ -712,6 +713,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>; @@ -750,6 +791,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 dbe867039eb..d37ad0391e4 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 375427d0d0b..52fc67a08ee 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/README.md b/quest/m1/README.md index 2bb26b66113..a61de4b94cb 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -161,7 +161,6 @@ per-frame CPU growth since #5926. - [A smaller, safer API than release](/quest/m1/api-audit/README.md) - every surface added since release has a consumer or is gone, overlapping operations are collapsed, names mirror across Rust, JS, and the bindings, and every break says how to migrate - [moq-crypto replaces moq-sign and moq-e2ee](/quest/m1/moq-crypto.md) - one Rust crate and one JS package hold signing and end-to-end encryption, sharing moq-net's timestamp check - [moq-transport 18, 20, and 22 only](/quest/m1/ietf-drop/README.md) - drafts 14-17, 19, and 21 are deleted from Rust, JS, tests, interop lanes, and docs, removing the legacy control-stream adapter and its request limit -- [A moq-transport request token is one auth per distinct token](/quest/m1/ietf-token-auth.md) - requests naming the same token share one verification and grant, on any request type, ending with the last such request - [A lapsed request token resets with EXPIRED_AUTH_TOKEN](/quest/m1/auth-expiry-reset.md) - group streams of a lapsed grant reset with the expiry code, so a client knows to renew - [moq-quic's API improves on quinn's](/quest/m1/quic-api/README.md) - moq-quic and both QUIC drivers ship one mirrored API: `send::`/`recv::` namespaces, one error per direction, consuming terminal operations, plain configs, no quinn baggage - [On-demand JavaScript FETCH](/quest/m1/js-fetch-on-demand.md) - browser publishers answer IETF FETCH for evicted or uncached groups through the JS ranges request surface diff --git a/quest/m1/auth-expiry-reset.md b/quest/m1/auth-expiry-reset.md index 98935af95b7..eb1d9d9364a 100644 --- a/quest/m1/auth-expiry-reset.md +++ b/quest/m1/auth-expiry-reset.md @@ -29,7 +29,3 @@ code and a revoked one doesn't, and note it in `doc/bin/relay/auth.md`. Public API: none. Wire: moq-transport group resets on token lapse change code. - -## Related - -- [moq-transport request tokens](/quest/m1/ietf-token-auth.md) - #7646's points 2 to 4 land there diff --git a/quest/m1/ietf-token-auth.md b/quest/m1/ietf-token-auth.md deleted file mode 100644 index fecde8f1545..00000000000 --- a/quest/m1/ietf-token-auth.md +++ /dev/null @@ -1,68 +0,0 @@ -# [M] A moq-transport request token is one auth per distinct token - -## Goal - -On a moq-transport session, every request carrying the same AUTHORIZATION -TOKEN T shares one auth: T is verified once, when the first open request -names it, and its grant admits exactly the requests that name T. A request -without T never gets T's grant, and a request naming T is held to T's grant -alone. Any request type may carry a token. T's auth ends, and the relay ends -its lease at the auth server, when the last open request naming it ends; a -later request naming T verifies it again. - -## Plan - -Decided with the maintainer on 2026-10-28 (`/quest-plan`, "what's not worth -supporting in IETF"). The maintainer's idea: requests with the same -AUTHORIZATION TOKEN implicitly use an AUTH of that token. Keep the -feature, since [moq-dev/moq#5148](https://github.com/moq-dev/moq/pull/5148) -added it for an external user's peer, whose authority comes per request. - -Decision prompts: - -- Cut request tokens: ✅ keep, as an implicit AUTH per distinct token; cut. -- Reach: "widens the session" was picked, then corrected by the maintainer: - ✅ the same token is required on every request, so a token on a SUBSCRIBE - grants nothing to other requests. -- Grant: ✅ T's grant alone; scope plus T. -- Lifetime: ✅ until the last naming request ends, bounded by the request - limit; until expiry or session end. -- Request types: ✅ any. This takes the incoming half of - [Token on FETCH](/quest/m3/auth-token-fetch.md). - -What changes from #5148: - -- One verification and one `requests()` auth per distinct T per session, - instead of one per request. Two requests naming the same bytes share a - lease. -- TRACK_STATUS, FETCH, SUBSCRIBE_NAMESPACE, and the rest are admitted by a - token they carry, not only SUBSCRIBE and PUBLISH_NAMESPACE. - `auth::RequestKind` covers them through the request gate from - [moq-dev/moq#5782](https://github.com/moq-dev/moq/pull/5782). A joining - FETCH keeps following its subscription. -- Several distinct tokens on one request: check whether - `NOT_SUPPORTED` for more than one is still needed, or whether each - becomes its own auth. Pick the simpler one and record it in the PR. -- A REQUEST_UPDATE carrying a new token moves the request to that token's - auth. The old auth ends if no other request names it. -- The relay tells the auth server when T's auth ends, through a call on - `moq_auth::Client` or an `end` for the attachment. Name it while - implementing. This folds in the deleted m3 "Relay request tokens" quest. -- Keep the rule that the tracks we subscribe under a token-authorized - PUBLISH_NAMESPACE are admitted on that announce. - -Tests on mocked time: two requests naming T verify once and share one grant. -A request without T is refused where only T covers it. T's auth ends with -its last request and verifies again on the next. Expiry and revocation end -every request naming T. A FETCH and a TRACK_STATUS are admitted by their own -token. Through the relay, the auth server sees T's lease end. Update the -token bullet of `doc/concept/standard.md` and `doc/bin/relay/auth.md`. - -Public API: possibly additive on `moq_auth::Client` for the lease end. Wire: -a token on any request is honored, and repeating it on a request no longer -re-verifies. - -## Related - -- [Request tokens beyond Rust](/quest/m3/request-token-surfaces.md) - outgoing and non-Rust surfaces, still waiting on a consumer -- [Token on FETCH](/quest/m3/auth-token-fetch.md) - our outgoing TRACK_STATUS and FETCH carrying a token diff --git a/quest/m3/auth-token-fetch.md b/quest/m3/auth-token-fetch.md index 1005c642055..1a77e9fe06f 100644 --- a/quest/m3/auth-token-fetch.md +++ b/quest/m3/auth-token-fetch.md @@ -10,10 +10,9 @@ them. ## Plan A [moq-dev/moq#5148](https://github.com/moq-dev/moq/pull/5148) follow-up. -The incoming half, a token admitting TRACK_STATUS and FETCH, moved into -[One auth per distinct token](/quest/m1/ietf-token-auth.md) on 2026-10-28, -since that quest honors a token on any request type. This quest keeps only -the outgoing half. +The incoming half, a token admitting TRACK_STATUS and FETCH, landed with +[moq-dev/moq#7211](https://github.com/moq-dev/moq/pull/7211), which honors a +token on any request type. This quest keeps only the outgoing half. Decided 2026-10-20 (maintainer): stays in m3 with the other request-token surfaces until a consumer asks. Outgoing tokens wait for `add()` with @@ -25,7 +24,3 @@ interop for the wire change. Public API: none. Wire: the existing AUTHORIZATION TOKEN parameter (0x03), now sent on TRACK_STATUS and FETCH. - -## Required - -- [One auth per distinct token](/quest/m1/ietf-token-auth.md) - the incoming rules these requests meet at a peer of ours diff --git a/rs/moq-auth/src/client.rs b/rs/moq-auth/src/client.rs index 3e68ad29e4c..8941b0ef962 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 { @@ -531,7 +545,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() } @@ -752,9 +766,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, .. } => { @@ -782,7 +802,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" ); @@ -871,14 +891,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 { .. } + ] )); } @@ -899,7 +925,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!( @@ -959,7 +985,7 @@ mod tests { )); assert!(matches!( log.events().as_slice(), - [Event::Connect, Event::Connect, Event::End { .. }] + [Event::Connect, Event::Attach, Event::End { .. }] )); } @@ -1117,7 +1143,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() @@ -1211,7 +1239,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(), }) }); @@ -1228,7 +1258,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))), })) }); @@ -1281,7 +1311,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) @@ -1329,7 +1359,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(); @@ -1344,7 +1376,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, }); @@ -1417,7 +1451,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 bb48a08b43d..80b53998eca 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 1fec61ce6bf..1e2ed252437 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, @@ -99,6 +102,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, @@ -159,6 +163,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 bde4808d75e..a8b7b67ed73 100644 --- a/rs/moq-auth/src/serve.rs +++ b/rs/moq-auth/src/serve.rs @@ -373,7 +373,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, } @@ -394,19 +394,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(()); } @@ -419,7 +424,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(), }, @@ -433,20 +438,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); + } + } } } @@ -511,8 +521,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(); @@ -531,7 +544,7 @@ impl Server { Ok(Some(grant)) } Event::End { .. } => { - self.sessions.lock().unwrap().end(&request.id); + self.sessions.lock().unwrap().end(request); Ok(None) } } @@ -1420,7 +1433,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(); @@ -1433,6 +1447,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 657bcafed9c..bcdf18e8916 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; }); @@ -311,5 +319,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 ee6eeff6d5a..0b712710ca2 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. //! @@ -692,32 +693,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 { @@ -747,6 +722,9 @@ pub(crate) struct Link { struct Detach { state: kio::Shared, id: u64, + /// Request tokens, indexed only while at least one naming request holds them. Kept out of + /// the handle's state, whose every lock wakes everything waiting on the session's permits. + request_tokens: std::sync::Mutex>>, } impl Drop for Detach { @@ -823,6 +801,7 @@ impl Link { attached: std::sync::Arc::new(Detach { state: handle.state.clone(), id, + request_tokens: Default::default(), }), }) } @@ -926,10 +905,61 @@ impl Link { }) } - /// Ask the acceptor to verify a token that rode on one of the peer's requests; see - /// [`Handle::verify_request`]. - pub(crate) fn verify_request(&self, token: crate::setup::Token, hold: Option) -> RequestVerdict { - self.handle.verify_request(token, hold) + /// Verify a request token once per session while any naming request remains open. + /// + /// The acceptor sees the `kind` and `path` of the request that names it first. + pub(crate) fn verify_request( + &self, + token: crate::setup::Token, + hold: Option, + kind: RequestKind, + path: &crate::Path, + ) -> RequestVerdict { + 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); + // A lifetime this upgrades is dropped only once the index is unlocked, since the last + // one out removes its entry. + let mut stale = None; + let (lifetime, fresh) = { + let mut tokens = self.attached.request_tokens.lock().unwrap(); + match tokens.get(&token).and_then(std::sync::Weak::upgrade) { + // A verdict the acceptor already finished with is not shared: the token verifies + // again. + Some(lifetime) if !lifetime.issue.read().done => (lifetime, false), + existing => { + stale = existing; + let lifetime = std::sync::Arc::new(RequestLifetime { + issue: kio::Shared::new(Issue { + hold, + ..Default::default() + }), + handle: self.clone(), + token: token.clone(), + }); + tokens.insert(token.clone(), std::sync::Arc::downgrade(&lifetime)); + (lifetime, true) + } + } + }; + drop(stale); + if fresh { + queue.push(Request::named(token, lifetime.issue.clone(), kind, path.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,7 +1338,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 queue = self.with_state(|state, link| { + let closing = self.with_state(|state, link| { if state.links[&link].closed.is_some() { return None; } @@ -1323,11 +1353,22 @@ impl Link { slot.ended.get_or_insert(err.clone()); } link.recompute(); - match state.bound { - true => state.abort(err), + Some(match state.bound { + true => state.abort(err.clone()), false => None, - } + }) }); + let Some(queue) = closing else { + return; + }; + // Collected first: a lifetime dropped here removes its own entry. + let lifetimes: Vec<_> = (self.attached.request_tokens.lock().unwrap().values()) + .filter_map(std::sync::Weak::upgrade) + .collect(); + for lifetime in &lifetimes { + lifetime.issue.lock().peer.get_or_insert(err.clone()); + } + drop(lifetimes); // Refusing the queued tokens takes the lock again, to drop them from the scope. if let Some(queue) = queue { queue.close(); @@ -1637,6 +1678,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 +1689,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 +1715,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,12 +1785,14 @@ 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 { token: Bytes, issue: Option>, + /// The moq-transport request the token rode on, and its path; `None` for an AUTH token. + named: Option<(RequestKind, crate::Path)>, } impl Request { @@ -1748,6 +1800,16 @@ impl Request { Self { token, issue: Some(issue), + named: None, + } + } + + /// A token that rode on a moq-transport request of `kind` at `path`. + fn named(token: Bytes, issue: kio::Shared, kind: RequestKind, path: crate::Path) -> Self { + Self { + token, + issue: Some(issue), + named: Some((kind, path)), } } @@ -1761,6 +1823,23 @@ impl Request { &self.token } + /// The kind of moq-transport request the token rode on, or `None` for a token presented + /// on the AUTH stream, whose grant joins the session's scope. + /// + /// Requests that repeat a token share its verification, so this is the request that + /// named it first; each later one is still checked against the grant it earns. + pub fn kind(&self) -> Option { + self.named.as_ref().map(|(kind, _)| *kind) + } + + /// The path the request named, in the terms the grant's patterns match: the track's + /// namespace for a [`RequestKind::Subscribe`] or [`RequestKind::Publish`], the announced + /// namespace for a [`RequestKind::PublishNamespace`], and the prefix for a + /// [`RequestKind::Discover`]. `None` exactly when [`Self::kind`] is. + pub fn path(&self) -> Option<&crate::Path> { + self.named.as_ref().map(|(_, path)| path) + } + /// Wait for the peer to withdraw this unanswered token, end the request it rode on, or /// close the session. #[cfg(feature = "async")] @@ -1769,7 +1848,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. @@ -1807,13 +1886,22 @@ fn refuse(issue: &kio::Shared, code: SessionError, reason: String) { } } -/// Which request a token rode on. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) enum RequestKind { - /// A SUBSCRIBE: the peer reads the named track. +/// Which kind of moq-transport request a token rode on, as [`Request::kind`] reports it, and +/// so which part of the [`Grant`] must cover it. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum RequestKind { + /// The peer reads a track: a SUBSCRIBE, a FETCH, or a TRACK_STATUS. Covered by the + /// grant's `subscribe` patterns. Subscribe, - /// A PUBLISH_NAMESPACE: the peer announces a namespace it will publish under. + /// A PUBLISH_NAMESPACE: the peer announces a namespace it will publish under. Its + /// `publish` patterns must cover the whole namespace. PublishNamespace, + /// A SUBSCRIBE_NAMESPACE: the peer discovers only the broadcasts the token allows + /// beneath the requested prefix. + Discover, + /// A PUBLISH: the peer offers a track. Covered by the grant's `publish` patterns. + Publish, } impl RequestKind { @@ -1824,6 +1912,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 +1927,30 @@ 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: std::sync::Mutex, +} + +struct RequestLifetime { + issue: kio::Shared, + handle: Link, + token: Bytes, +} + +impl Drop for RequestLifetime { + fn drop(&mut self) { + let mut tokens = self.handle.attached.request_tokens.lock().unwrap(); + if tokens.get(&self.token).is_some_and(|token| token.strong_count() == 0) { + tokens.remove(&self.token); + } + drop(tokens); + 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 +1993,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 +2055,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, /// Why the request ended, once it has. A channel, so every clone waiting on the grant /// wakes when [`RequestGrant::end`] latches it. ended: kio::Producer>, @@ -1970,6 +2065,9 @@ impl Held { /// See [`RequestGrant::poll_ended`]. fn poll_end(&mut self, waiter: &kio::Waiter) -> Poll { loop { + if let Some(err) = self.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. // Watched, so a request that abandons the renewal, such as one cancelled while a @@ -1982,35 +2080,39 @@ impl Held { }) .is_pending() }); - if self.deadline.poll(waiter).is_ready() { - if handover { - return Poll::Pending; - } - return Poll::Ready(Refusal::new(Error::Expired, "request token expired")); - } - match self.verdict.poll_reply(waiter) { - Poll::Ready(None) if handover => return Poll::Pending, - Poll::Ready(None) => { - return Poll::Ready(Refusal::new( - Error::Session(SessionError::Unauthorized), - "grant dropped", - )); - } + // A grant the acceptor sent is read before the deadline, so an extension that arrived + // before the old expiry keeps the request even when it is polled only after. + let ended = match self.verdict.poll_reply(waiter) { Poll::Ready(Some(Reply::Grant(grant))) => { if !self.kind.covers(&grant, self.path.as_str()) { return Poll::Ready(Refusal::new(Error::Unauthorized, "request token no longer granted")); } self.deadline.set(grant.expires); self.grant = grant; + self.epoch += 1; // Re-poll: the new deadline may already have lapsed, or another reply may // be waiting. continue; } - Poll::Ready(Some(Reply::Refuse { code, reason })) => { - return Poll::Ready(Refusal::new(Error::Session(code), &reason)); + Poll::Ready(None) if handover => None, + Poll::Ready(None) => Some(Refusal::new( + Error::Session(SessionError::Unauthorized), + "grant dropped", + )), + Poll::Ready(Some(Reply::Refuse { code, reason })) => Some(Refusal::new(Error::Session(code), &reason)), + Poll::Pending => None, + }; + // Any other end comes after an expiry that already passed, which it reports. + if self.deadline.poll(waiter).is_ready() { + if handover { + return Poll::Pending; } - Poll::Pending => return Poll::Pending, + return Poll::Ready(Refusal::new(Error::Expired, "request token expired")); } + return match ended { + Some(refusal) => Poll::Ready(refusal), + None => Poll::Pending, + }; } } } @@ -2042,6 +2144,7 @@ impl RequestGrant { hold, renewing: None, clock: clock.clone(), + epoch: 1, ended: kio::Producer::new(None), })))) } @@ -2057,14 +2160,27 @@ impl RequestGrant { } /// The request's slot, for re-attaching what it authorized without taking another. + #[cfg(test)] pub(crate) fn hold(&self) -> Option { self.held().hold.clone() } /// Start verifying a renewal's token, holding the request's slot while it runs. pub(crate) fn verify_renewal(&self, handle: &Link, token: crate::setup::Token) -> RequestVerdict { + let hold = self.held().hold.clone(); + self.verify_renewal_holding(handle, token, hold) + } + + /// [`Self::verify_renewal`], holding `hold` instead while it runs. Until the serve loop + /// applies a granted renewal, it takes over rather than the request ending between the two. + pub(crate) fn verify_renewal_holding( + &self, + handle: &Link, + token: crate::setup::Token, + hold: Option, + ) -> RequestVerdict { let mut held = self.held(); - let verdict = handle.verify_request(token, held.hold.clone()); + let verdict = handle.verify_request(token, hold, held.kind, &held.path); held.renewing = verdict.issue.clone(); verdict } @@ -2089,6 +2205,7 @@ impl RequestGrant { held.verdict = verdict; held.deadline.set(grant.expires); held.grant = grant; + held.epoch += 1; Ok(()) } @@ -2179,7 +2296,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, for a /// request still waiting to apply it. fn granted(issue: &Issue) -> bool { - issue.peer.is_none() && !issue.done && issue.outbox.iter().any(|reply| matches!(reply, Reply::Grant(_))) + issue.peer.is_none() && !issue.done && matches!(issue.latest, Some(Reply::Grant(_))) } /// A grant issued to one of the peer's tokens, which the session enforces. Dropping it @@ -2253,6 +2370,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, @@ -2290,6 +2409,8 @@ impl Gate { direction, epoch: 0, part: Part::Both, + token: None, + discovery: false, within_limit, admitted: false, waiting: None, @@ -2302,6 +2423,69 @@ 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 token = RequestGrant::new(clock, verdict, answer, self.path.clone(), self.kind(), None)?; + self.bind(token); + Ok(()) + } + + /// The kind of request this gate admits, for its token's grant to cover. A gate on what + /// the peer publishes to us verifies only a PUBLISH_NAMESPACE's token; a PUBLISH's goes + /// straight to [`Link::verify_request`]. + fn kind(&self) -> RequestKind { + match (self.discovery, self.direction) { + (true, _) => RequestKind::Discover, + (false, Direction::Publish) => RequestKind::Subscribe, + (false, Direction::Subscribe) => RequestKind::PublishNamespace, + } + } + + /// 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() + } + + /// 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 { @@ -2316,7 +2500,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(), } @@ -2327,14 +2520,47 @@ impl Gate { self.handle.token_decides(self.direction) } - /// Verify an incoming request's token against the local acceptor. + /// Verify an incoming request's token against the local acceptor, holding `hold` while it + /// runs. Once a token bound the gate, another is verified as that grant's renewal, so a + /// granted replacement takes over rather than the old grant ending before it is admitted. pub(crate) fn verify(&self, token: crate::setup::Token, hold: Option) -> RequestVerdict { - self.handle.verify_request(token, hold) + match &self.token { + Some(grant) => grant.verify_renewal_holding(&self.handle, token, hold), + None => self.handle.verify_request(token, hold, self.kind(), &self.path), + } } /// 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(); + // Read the session's permit with the epoch it stands at, under one lock, then poll again + // to register the wakeup even when only the token changed this time. A session change + // between the two is read too, so no epoch is counted without its permit. + let mut version = u64::MAX; + let mut permit = None; + while let Poll::Ready(next) = self.handle.poll_permit(self.direction, &mut version, waiter) { + permit = Some(next); + } + let mut permit = permit.expect("a session epoch is never u64::MAX"); + permit.limit = Some(if ended { + Patterns::new() + } else { + match self.direction { + Direction::Publish => token.grant.subscribe.clone(), + Direction::Subscribe => token.grant.publish.clone(), + } + }); + // The token ending empties the limit, so it counts as a change too. + version += token.epoch + u64::from(ended); + if version == *epoch { + return Poll::Pending; + } + *epoch = version; + Poll::Ready(permit) } /// Preserve setup ordering before the first discovery snapshot. @@ -2385,7 +2611,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 { @@ -2644,7 +2875,12 @@ mod tests { kind: crate::setup::Token::OUT_OF_BAND, value: b"jwt".to_vec(), }; - let _verdict = handle.verify_request(token, Some(slot.clone())); + let _verdict = handle.verify_request( + token, + Some(slot.clone()), + RequestKind::Subscribe, + &crate::Path::from_static("room"), + ); assert_eq!( std::sync::Arc::strong_count(&slot), 2, @@ -3111,6 +3347,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 { @@ -3129,7 +3366,7 @@ mod request_token_tests { async fn a_request_token_reaches_the_acceptor() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let request = requests.next().await.expect("a request"); assert_eq!(request.token().as_ref(), b"jwt"); @@ -3139,13 +3376,269 @@ mod request_token_tests { assert!(grant.publish.matches("room/alice")); } + #[kio::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, RequestKind::Subscribe, &subscribe_path()); + let second = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + 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, RequestKind::Subscribe, &subscribe_path()); + let _issued = requests.next().await.unwrap().accept(Grant::default()); + assert_eq!(later.grant().await.unwrap(), Grant::default()); + } + + /// A request naming a token whose grant the acceptor already dropped verifies it again, + /// rather than sharing the finished verdict with a naming request still on its way out. + #[kio::test] + async fn a_token_the_acceptor_dropped_verifies_again() { + let handle = Link::new(true); + let mut requests = handle.handle().requests(); + let first = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + let issued = requests.next().await.unwrap().accept(Grant::all()); + first.grant().await.unwrap(); + drop(issued); + let later = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + let _issued = requests.next().await.unwrap().accept(Grant::default()); + assert_eq!(later.grant().await.unwrap(), Grant::default()); + drop(first); + let again = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + assert!( + requests.poll_next(&kio::Waiter::noop()).is_pending(), + "the live verdict is shared" + ); + assert_eq!(again.grant().await.unwrap(), Grant::default()); + } + + #[kio::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, RequestKind::Subscribe, &subscribe_path()); + let second = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + 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 { + kio::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)); + } + } + } + } + + #[kio::test] + async fn discovery_uses_its_token_alone() { + 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 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)); + } + + /// A gate bound to a token reports the token ending as a permit that allows nothing, so a + /// caller reading permits alone stops too. + #[kio::test] + async fn a_bound_token_ending_is_a_new_permit() { + 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 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")); + issued.revoke(SessionError::Unauthorized, "revoked"); + let Poll::Ready(permit) = gate.poll_permit(&mut epoch, &kio::Waiter::noop()) else { + panic!("the token's end went unreported"); + }; + assert!(!permit.matches("room/alice")); + assert!(gate.poll_permit(&mut epoch, &kio::Waiter::noop()).is_pending()); + } + + /// The acceptor sees the kind and path of the request that first named a token, which + /// later requests repeating it share, and neither for a token from the AUTH stream. + #[kio::test] + async fn a_request_reports_the_kind_and_path_that_named_it() { + let handle = Link::new(true); + let mut requests = handle.handle().requests(); + let room = crate::Path::from_static("room"); + let _first = handle.verify_request(token(b"jwt", 0), None, RequestKind::PublishNamespace, &room); + let _repeat = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let request = requests.next().await.unwrap(); + assert_eq!(request.kind(), Some(RequestKind::PublishNamespace)); + assert_eq!(request.path(), Some(&room)); + assert!( + requests.next().now_or_never().is_none(), + "a repeated token verified again" + ); + + let auth = Request::new(Bytes::from_static(b"auth"), Default::default()); + assert_eq!(auth.kind(), None); + assert_eq!(auth.path(), None); + + // Each gate names the request it admits. + for (gate, kind) in [ + ( + Gate::new(handle.clone(), room.clone(), Direction::Publish), + RequestKind::Subscribe, + ), + ( + Gate::new(handle.clone(), room.clone(), Direction::Publish).discovery(), + RequestKind::Discover, + ), + ( + Gate::new(handle.clone(), room.clone(), Direction::Subscribe), + RequestKind::PublishNamespace, + ), + ] { + let _verdict = gate.verify(token(format!("{kind:?}").as_bytes(), 0), None); + let request = requests.next().await.unwrap(); + assert_eq!(request.kind(), Some(kind)); + assert_eq!(request.path(), Some(&room)); + } + } + + /// A gate its token bound verifies another token as that grant's renewal: one the acceptor + /// granted takes over, rather than the old grant lapsing before the gate admits it. + #[kio::test] + async fn a_bound_gate_hands_over_to_a_granted_replacement() { + 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"jwt", 0), None); + let _issued = requests.next().await.unwrap().accept(grant_in(&runtime, 60)); + let answer = verdict.grant().await; + gate.admit(&runtime, (verdict, answer)).unwrap(); + + let renewal = gate.verify(token(b"jwt2", 0), None); + let _issued2 = requests.next().await.unwrap().accept(grant_in(&runtime, 600)); + kio::clock::sleep(Duration::from_secs(61)).await; + let pending = kio::wait(|waiter| Poll::Ready(gate.poll_token_ended(waiter).is_pending())).await; + assert!(pending, "the old grant lapsed ahead of its granted replacement"); + + let answer = renewal.grant().await; + gate.admit(&runtime, (renewal, answer)).unwrap(); + let pending = kio::wait(|waiter| Poll::Ready(gate.poll_token_ended(waiter).is_pending())).await; + assert!(pending, "the replacement holds the gate"); + } + + /// Naming and releasing a request token touches nothing the session's permits watch, so + /// it wakes none of the requests waiting on them: a session with many announces would + /// otherwise poll every one for each token. + #[kio::test] + async fn request_tokens_do_not_wake_permit_waiters() { + struct Woken(std::sync::atomic::AtomicBool); + impl std::task::Wake for Woken { + fn wake(self: std::sync::Arc) { + self.0.store(true, std::sync::atomic::Ordering::SeqCst); + } + } + let handle = Link::new(true); + let mut requests = handle.handle().requests(); + let first = handle.verify_request(token(b"warm", 0), None, RequestKind::Subscribe, &subscribe_path()); + let _first = requests.next().await.unwrap().accept(Grant::all()); + first.grant().await.unwrap(); + + let woken = std::sync::Arc::new(Woken(Default::default())); + let waiter = kio::Waiter::new(std::task::Waker::from(woken.clone())); + let mut epoch = 0; + while handle.poll_permit(Direction::Publish, &mut epoch, &waiter).is_ready() {} + let second = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + let third = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + drop((second, third)); + assert!( + !woken.0.load(std::sync::atomic::Ordering::SeqCst), + "a request token woke the permit waiters" + ); + } + + #[kio::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, RequestKind::Subscribe, &subscribe_path()); + 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, RequestKind::Subscribe, &subscribe_path()); + 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. #[kio::test] async fn a_typed_request_token_is_unsupported() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"cat", 1), None); + let verdict = handle.verify_request(token(b"cat", 1), None, RequestKind::Subscribe, &subscribe_path()); let err = verdict.grant().await.expect_err("a typed token"); assert!(matches!(err.error, Error::Unsupported), "{err:?}"); assert!( @@ -3160,7 +3653,12 @@ mod request_token_tests { fn an_empty_request_token_is_unsupported() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"", crate::setup::Token::OUT_OF_BAND), None); + let verdict = handle.verify_request( + token(b"", crate::setup::Token::OUT_OF_BAND), + None, + RequestKind::Subscribe, + &subscribe_path(), + ); assert!(matches!( verdict.poll_grant(&kio::Waiter::noop()), Poll::Ready(Err(Refusal { @@ -3183,7 +3681,7 @@ mod request_token_tests { let handle = Link::new(false); let mut requests = handle.handle().requests(); handle.unsupported(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let request = requests.next().await.expect("a request reaches the acceptor"); let _issued = request.accept(Grant::all()); assert!( @@ -3197,7 +3695,7 @@ mod request_token_tests { #[kio::test] async fn no_consumer_refuses_a_request_token_as_unsupported() { let handle = Link::new(true); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let err = verdict.grant().await.expect_err("no verifier"); assert!(matches!(err.error, Error::Unsupported), "{err:?}"); } @@ -3208,7 +3706,7 @@ mod request_token_tests { async fn a_refused_request_token_is_unauthorized() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); requests.next().await.unwrap().reject(SessionError::Unauthorized, "no"); let err = verdict.grant().await.expect_err("refused"); assert!( @@ -3224,21 +3722,23 @@ mod request_token_tests { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let issued = requests.next().await.unwrap().accept(Grant::all()); 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); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); drop(requests.next().await.unwrap().accept(Grant::all())); let err = verdict.grant().await.expect_err("dropped before admission"); assert!( matches!(err.error, Error::Session(SessionError::Unauthorized)), "{err:?}" ); + drop(verdict); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let issued = requests.next().await.unwrap().accept(Grant::all()); issued.update(Grant::default()); let grant = verdict.grant().await.expect("still granted"); @@ -3254,7 +3754,7 @@ mod request_token_tests { async fn dropping_the_verdict_ends_the_grant() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let issued = requests.next().await.unwrap().accept(Grant::all()); verdict.grant().await.expect("granted"); drop(verdict); @@ -3268,7 +3768,7 @@ mod request_token_tests { async fn a_request_token_never_joins_the_union() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let _issued = requests.next().await.unwrap().accept(Grant::all()); verdict.grant().await.expect("granted"); // The union only reflects tokens presented on this side (none here), never a @@ -3299,7 +3799,7 @@ mod request_token_tests { assert!(!handle.allows(Direction::Publish, "room/bob")); // A token on a request admits that request, and nothing else. - let verdict = handle.verify_request(token(b"request", 0), None); + let verdict = handle.verify_request(token(b"request", 0), None, RequestKind::Subscribe, &subscribe_path()); let _request = requests.next().await.unwrap().accept(subscribe_only("room/bob")); verdict.grant().await.expect("granted"); assert!( @@ -3387,7 +3887,7 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let grant = grant_in(&runtime, 60); let expires = grant.expires; let _issued = requests.next().await.unwrap().accept(grant); @@ -3417,7 +3917,7 @@ mod request_token_tests { ..grant_in(&runtime, 0) }; - let verdict = handle.verify_request(token(b"old", 0), None); + let verdict = handle.verify_request(token(b"old", 0), None, RequestKind::Subscribe, &subscribe_path()); let _issued = requests.next().await.unwrap().accept(expired()); let answered = verdict.grant().await; let refused = RequestGrant::new( @@ -3433,7 +3933,7 @@ mod request_token_tests { "an expired grant admits nothing" ); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let _issued = requests.next().await.unwrap().accept(grant_in(&runtime, 60)); let answered = verdict.grant().await; let request_grant = RequestGrant::new( @@ -3445,7 +3945,7 @@ mod request_token_tests { None, ) .unwrap(); - let renewal = handle.verify_request(token(b"old2", 0), None); + let renewal = handle.verify_request(token(b"old2", 0), None, RequestKind::Subscribe, &subscribe_path()); let _issued = requests.next().await.unwrap().accept(expired()); let renewed = renewal.grant().await; assert!( @@ -3461,7 +3961,7 @@ mod request_token_tests { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let first = grant_in(&runtime, 60); let first_expires = first.expires; let _issued = requests.next().await.unwrap().accept(first); @@ -3477,7 +3977,7 @@ mod request_token_tests { .unwrap(); // A REQUEST_UPDATE carrying a fresh token the acceptor accepts with a later expiry. - let renewal = handle.verify_request(token(b"jwt2", 0), None); + let renewal = handle.verify_request(token(b"jwt2", 0), None, RequestKind::Subscribe, &subscribe_path()); let second = grant_in(&runtime, 600); let second_expires = second.expires; let _issued2 = requests.next().await.unwrap().accept(second); @@ -3498,7 +3998,7 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); // Held for the request's life, so only the deadline (not a drop) ends it. let _issued = requests.next().await.unwrap().accept(grant_in(&runtime, 60)); let answered = verdict.grant().await.unwrap(); @@ -3519,12 +4019,50 @@ mod request_token_tests { /// A renewal the acceptor granted before the old deadline lapsed keeps the request, even /// when the serve loop applies it only after the deadline. + /// An extension the acceptor sent before the old expiry keeps the request, even when the + /// request reads it only after that expiry passed. + #[kio::test] + async fn an_extension_read_after_the_old_expiry_keeps_the_request() { + let runtime = crate::time::sim(); + let handle = Link::new(true); + let mut requests = handle.handle().requests(); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let issued = requests.next().await.unwrap().accept(grant_in(&runtime, 60)); + let answered = verdict.grant().await.unwrap(); + let request_grant = RequestGrant::new( + &runtime, + verdict, + Ok(answered), + subscribe_path(), + RequestKind::Subscribe, + None, + ) + .unwrap(); + let pending = kio::wait(|waiter| Poll::Ready(request_grant.poll_ended(waiter).is_pending())).await; + assert!(pending, "the first grant stands"); + + // Extended before the old expiry, and read only after it. + let extended = grant_in(&runtime, 600); + let expires = extended.expires; + issued.update(extended); + kio::clock::sleep(Duration::from_secs(61)).await; + + let pending = kio::wait(|waiter| Poll::Ready(request_grant.poll_ended(waiter).is_pending())).await; + assert!(pending, "the old expiry ended a request its acceptor had extended"); + assert_eq!(request_grant.grant().expires, expires); + + // The extension's own expiry still ends it. + kio::clock::sleep(Duration::from_secs(600)).await; + let ended = kio::wait(|waiter| Poll::Ready(request_grant.poll_ended(waiter))).await; + assert!(matches!(ended, Poll::Ready(refusal) if matches!(refusal.error, Error::Expired))); + } + #[kio::test] async fn a_granted_renewal_outlives_the_old_deadline() { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let _issued = requests.next().await.unwrap().accept(grant_in(&runtime, 60)); let answered = verdict.grant().await.unwrap(); let request_grant = RequestGrant::new( @@ -3562,7 +4100,7 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let first = grant_in(&runtime, 60); let first_expires = first.expires; let _issued = requests.next().await.unwrap().accept(first); @@ -3578,7 +4116,7 @@ mod request_token_tests { .unwrap(); // The renewal token is refused, so it renews nothing and the old grant is untouched. - let renewal = handle.verify_request(token(b"bad", 0), None); + let renewal = handle.verify_request(token(b"bad", 0), None, RequestKind::Subscribe, &subscribe_path()); requests .next() .await @@ -3608,7 +4146,7 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); let issued = requests.next().await.unwrap().accept(Grant::all()); let answered = verdict.grant().await.unwrap(); let request_grant = RequestGrant::new( @@ -3691,7 +4229,7 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None); + let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); // A grant that never expires, so only the revoke can end the request. let issued = requests.next().await.unwrap().accept(Grant::all()); let answered = verdict.grant().await.unwrap(); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 41a27af2c79..3255184b462 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -1391,6 +1391,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 015e09598f1..b8d8080d85c 100644 --- a/rs/moq-net/src/ietf/fetch.rs +++ b/rs/moq-net/src/ietf/fetch.rs @@ -119,6 +119,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, @@ -177,6 +179,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, @@ -201,6 +204,7 @@ impl<'a> Message<'a> for Fetch<'a> { Version::Draft18 => { self.fetch_type.encode(w, version)?; encode_params!(w, version, + 0x03 => self.authorization_token.clone(), 0x20 => self.subscriber_priority, 0x22 => group_order, ); @@ -219,6 +223,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, @@ -232,73 +237,82 @@ 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) = - 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, - ); - super::token::single(authorization_token)?; - - ( - fetch_type, - subscriber_priority, - group_order, - false, - fill_timeout.is_some(), - true, - ) - } - // 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, - ); - 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), - ) - } - }; + let ( + fetch_type, + subscriber_priority, + group_order, + range_filters, + fill_timeout, + properties_wanted, + 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, + ); + let authorization_token = super::token::single(authorization_token)?; + + ( + fetch_type, + subscriber_priority, + group_order, + false, + fill_timeout.is_some(), + true, + 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, + ); + 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), + authorization_token, + ) + } + }; Ok(Self { + authorization_token, request_id, subscriber_priority: subscriber_priority.unwrap_or(super::priority::DEFAULT_WIRE), // No preference: the publisher picks the order. @@ -593,6 +607,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, @@ -701,6 +716,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 a12748baf82..e1e0ad6dc63 100644 --- a/rs/moq-net/src/ietf/publish.rs +++ b/rs/moq-net/src/ietf/publish.rs @@ -209,6 +209,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>, @@ -231,6 +233,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, @@ -258,6 +261,7 @@ impl<'a> Message<'a> for Publish<'a> { // property instead. let draft20 = Filter::is_draft20(version); encode_params!(w, version, + 0x03 => self.authorization_token.clone(), 0x09 => self.largest_location, 0x10 => self.forward, 0x20 => self.subscriber_priority.filter(|_| draft20), @@ -297,9 +301,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, @@ -545,6 +550,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(), @@ -575,6 +581,7 @@ mod tests { fn publish_priority_property_bytes_on_every_draft() { for version in [Version::Draft18, Version::Draft20, Version::Draft22] { let mut msg = Publish { + authorization_token: None, request_id: RequestId(1), track_namespace: Path::from_static("ns"), track_name: "video".into(), @@ -607,6 +614,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 f42ce014e1e..6c5d1e90041 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -649,6 +649,12 @@ struct Namespaces { permit: crate::auth::Permit, /// The dispatcher-owned gate for a solicited namespace request. gate: Option, + /// The update whose request token the acceptor is still verifying. It is answered once + /// the verdict lands, while the loop keeps advertising and withdrawing meanwhile. + verifying: Option, + /// An abandoned verification's credit, kept until the acceptor lets go of it, so a drain + /// the request's guard cancels is finished by the next. + abandoned: Option>, /// The auth epoch last applied to `permit`. epoch: u64, /// Whether this discovery cursor includes hidden namespaces. @@ -667,6 +673,8 @@ impl Namespaces { woken: Default::default(), permit: Default::default(), gate: None, + verifying: None, + abandoned: None, epoch: 0, hidden: false, } @@ -685,6 +693,15 @@ impl Namespaces { } } +/// A namespace update waiting on the acceptor's verdict for its request token. +struct Verifying { + verdict: crate::auth::RequestVerdict, + /// Held by the verification, so the request keeps its stream credit until it ends. + credit: kio::Producer<()>, + /// The prefix the update moves to once admitted. + prefix: Option, +} + /// How a subscription's serve loop ended, short of the peer cancelling it. #[derive(Debug)] enum Served { @@ -715,6 +732,17 @@ enum UpdateRead { Failed(Error), } +/// Why an update naming an AUTHORIZATION TOKEN is refused on a request the session admitted. +const TOKEN_ON_SESSION_REQUEST: &str = "AUTHORIZATION TOKEN on a request admitted without one"; + +/// A subscription's update state, which survives its pauses. +struct SubscriptionUpdates { + /// The grant the subscription's own request token earned, if one admitted it, which its + /// renewals replace in place. A request admitted without one never names one later. + grant: Option, + renewal: request_update::Renewal, +} + /// How an admitted subscription came out of serving. enum Subscribed { /// It never started, refused with this reason and error before SUBSCRIBE_OK. @@ -755,10 +783,13 @@ 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. Request(super::request_stream::NamespaceUpdate), + /// The acceptor answered the token of the update being verified. + Verified(std::result::Result), /// An origin-level route (un)announce: whether it is active and whether it /// restarts, `None` once the announce stream ends. Update(Option<(crate::announce::Announce, bool, bool)>), @@ -1401,6 +1432,10 @@ impl Publisher { } None => (gate, None), }; + let mut updates = SubscriptionUpdates { + grant: request_grant.clone(), + renewal: Default::default(), + }; let _slot = slot; // Everything the subscription does once admitted, its setup included, runs under its @@ -1529,7 +1564,7 @@ impl Publisher { &mut request_finished, &mut goaways, fill, - request_grant.clone(), + &mut updates, ) .await } @@ -1554,7 +1589,7 @@ impl Publisher { self.write_request_ok(&mut stream.writer, None).await?; } match self - .wait_resume(&mut stream, &mut request_finished, &mut goaways, &mut msg) + .wait_resume(&mut stream, &mut request_finished, &mut goaways, &mut msg, &mut updates) .await { Ok(()) => {} @@ -1600,7 +1635,7 @@ impl Publisher { &mut request_finished, &mut goaways, async {}, - request_grant.clone(), + &mut updates, ) .await; } @@ -1637,6 +1672,8 @@ impl Publisher { }) .await }; + // No update follows the subscription's end, so its pending renewals no longer matter. + drop(updates); // A track that ended cleanly keeps what a joining FETCH needs (see [`Retain`]); any // other end drops the join, refusing a joining FETCH that has not answered yet. let retain = match (&subscribed, _join) { @@ -1703,6 +1740,32 @@ impl Publisher { super::request_stream::poll_withdrawn(stream, fin_cancels, 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, + fin_cancels: bool, + ) -> 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, fin_cancels, |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, @@ -1857,7 +1920,7 @@ impl Publisher { finished: &mut bool, goaways: &mut super::request_stream::Messages, fill: impl std::future::Future, - request_grant: Option, + updates: &mut SubscriptionUpdates, ) -> Option { let session = self.session.clone(); let mut fill = std::pin::pin!(fill); @@ -1876,7 +1939,8 @@ impl Publisher { // further update, token or not, to handle once the verdict resolves: answers // carry no Request ID, so they must go out in the order the updates arrived. The stash is // a FIFO bounded by MAX_REQUEST_UPDATES (counting the one being verified). - let mut renewal = request_update::Renewal::::default(); + let SubscriptionUpdates { grant, renewal } = updates; + let mut pausing = false; // What one turn of the loop resolved to. enum Turn { @@ -1973,10 +2037,13 @@ impl Publisher { Turn::Cancelled => return None, Turn::Renewal(request_update::Verdict { verdict, grant: res }) => { // Only a token-authorized request starts a renewal. - let rg = request_grant.as_ref().expect("a renewal without a request grant"); + let rg = grant.as_ref().expect("a renewal without a request grant"); // On accept the old grant is dropped (ending the old token) and the deadline // re-armed at the new expiry. let Err(refusal) = rg.renew(verdict, res) else { + if pausing { + return Some(Served::Paused(true)); + } if let Err(err) = self.write_request_ok(&mut stream.writer, renewal_largest.take()).await { return Some(Served::Track(Err(err))); } @@ -2009,8 +2076,9 @@ impl Publisher { waiting = Some(update); } Turn::Update(update) => { + pausing = update.forward == Some(false); match self - .apply_subscription_update(stream, serve, &request_grant, &mut renewal, update) + .apply_subscription_update(stream, serve, grant, renewal, update) .await { Err(served) => return Some(served), @@ -2142,31 +2210,64 @@ impl Publisher { /// Hold a paused SUBSCRIBE, answering its updates, until one resumes it, or return how the /// request ended (`None` when cancelled). /// - /// A paused subscription holds nothing upstream, so only its priority and max age change - /// while it waits; anything else fails the update, ending the request. + /// A paused subscription holds nothing upstream. Its priority, max age and authority can + /// change while it waits; serving parameters are applied on resume. async fn wait_resume( &self, stream: &mut Stream, finished: &mut bool, goaways: &mut super::request_stream::Messages, msg: &mut ietf::Subscribe<'_>, + updates: &mut SubscriptionUpdates, ) -> Result<(), Option> { let session = self.session.clone(); + let SubscriptionUpdates { grant, renewal } = updates; + let mut resuming = false; + enum Turn { + Read(UpdateRead), + Renewal(request_update::Verdict), + } // Each update is answered, so a peer streaming them must not hold the session's // other tasks. let mut budget = kio::coop::Budget::new(32); loop { kio::wait(|waiter| budget.poll_yield(waiter)).await; - let read = kio::wait(|waiter| { + let mut queued = renewal.next(); + let turn = kio::wait(|waiter| { if session.poll_closed(waiter).is_ready() { - return Poll::Ready(UpdateRead::Cancelled); + return Poll::Ready(Turn::Read(UpdateRead::Cancelled)); } if stream.writer.poll_closed(waiter).is_ready() { - return Poll::Ready(UpdateRead::Cancelled); + return Poll::Ready(Turn::Read(UpdateRead::Cancelled)); } - self.poll_update(stream, finished, goaways, waiter) + if let Poll::Ready(verdict) = renewal.poll(waiter) { + return Poll::Ready(Turn::Renewal(verdict)); + } + if let Some(update) = queued.take() { + return Poll::Ready(Turn::Read(UpdateRead::Update(update))); + } + self.poll_update(stream, finished, goaways, waiter).map(Turn::Read) }) .await; + let read = match turn { + Turn::Read(read) => read, + Turn::Renewal(request_update::Verdict { verdict, grant: res }) => { + let rg = grant.as_ref().expect("a renewal without a request grant"); + if let Err(refusal) = rg.renew(verdict, res) { + // As while serving: the grant ends, and its REQUEST_ERROR goes out once + // the guard is gone. + rg.end(refusal.clone()); + return Err(Some(Served::RenewalRefused(refusal))); + } + if resuming { + return Ok(()); + } + self.write_request_ok(&mut stream.writer, None) + .await + .map_err(|err| Some(Served::Track(Err(err))))?; + continue; + } + }; let update = match read { UpdateRead::Update(update) => update, UpdateRead::Cancelled => return Err(None), @@ -2181,25 +2282,34 @@ impl Publisher { } UpdateRead::Failed(err) => return Err(Some(Served::Track(Err(err)))), }; + if renewal.pending() { + if let Err(overflow) = renewal.queue(update, self.version) { + let err = match overflow { + request_update::Overflow::Session => request_update::too_many(&mut self.session.clone()), + request_update::Overflow::Request => Error::ProtocolViolation, + }; + return Err(Some(Served::Track(Err(err)))); + } + continue; + } + let paused = "REQUEST_UPDATE parameters not supported while paused"; let refused = match update.refused { - Some(err) => Some(err), - None if update.filter.is_some() - || update.fill.is_some() - || update.new_group.is_some() - || update.authorization_token.is_some() => + Some(err) => Some((err, paused)), + None if update.filter.is_some() || update.fill.is_some() || update.new_group.is_some() => { + Some((Error::Unsupported, paused)) + } + None if update.authorization_token.is_some() + && grant.is_none() + && self.auth.token_decides(crate::auth::Direction::Publish) => { - Some(Error::Unsupported) + Some((Error::Unsupported, TOKEN_ON_SESSION_REQUEST)) } None => None, }; - if let Some(err) = refused { - self.write_subscribe_error( - &mut stream.writer, - &err, - "REQUEST_UPDATE parameters not supported while paused", - ) - .await - .map_err(|err| Some(Served::Track(Err(err))))?; + if let Some((err, reason)) = refused { + self.write_subscribe_error(&mut stream.writer, &err, reason) + .await + .map_err(|err| Some(Served::Track(Err(err))))?; return Err(Some(Served::UpdateFailed(Error::Unsupported))); } if let Some(priority) = update.priority { @@ -2208,7 +2318,17 @@ impl Publisher { if let Some(max_age) = update.max_age { msg.max_age = Some(max_age); } - if update.forward == Some(true) { + resuming = update.forward == Some(true); + if let (Some(token), Some(rg)) = ( + update + .authorization_token + .filter(|_| self.auth.token_decides(crate::auth::Direction::Publish)), + grant.as_ref(), + ) { + renewal.verify(rg.verify_renewal(&self.auth, token.0)); + continue; + } + if resuming { return Ok(()); } self.write_request_ok(&mut stream.writer, None) @@ -2257,10 +2377,8 @@ impl Publisher { } /// Apply one REQUEST_UPDATE on an in-flight subscription, with no verdict pending: refuse - /// unsupported parameters, apply a priority, then start verifying a renewal's token, or - /// answer at once. A token-less update, or any update on a union-authorized request (which - /// holds no grant to renew), owes one answer so the peer's MAX_REQUEST_UPDATES credit is - /// restored. + /// unsupported parameters, apply the serving parameters, then verify its token or answer + /// at once. Each answer restores the peer's MAX_REQUEST_UPDATES credit. /// /// An error is how the request ends. async fn apply_subscription_update( @@ -2274,7 +2392,8 @@ impl Publisher { let failed = |err| Served::Track(Err(err)); // A token renews a token-authorized request. Where tokens decide, one arriving on a // request admitted without one is refused rather than taken unread, so every token this - // side admits is verified. + // side admits is verified: a request is admitted by the session or by a token, and that + // never changes. let refused = match update.refused.clone() { Some(Error::Decode(err)) => Some((super::token::reason(&err), Error::Decode(err))), Some(Error::GoingAway) => Some(("going away", Error::GoingAway)), @@ -2288,10 +2407,7 @@ impl Publisher { && request_grant.is_none() && self.auth.token_decides(crate::auth::Direction::Publish) => { - Some(( - "AUTHORIZATION TOKEN on a request admitted without one", - Error::Unsupported, - )) + Some((TOKEN_ON_SESSION_REQUEST, Error::Unsupported)) } // The draft forbids it on a track that did not advertise DYNAMIC_GROUPS. None if update.new_group.is_some() && !serve.dynamic => Some(( @@ -2302,11 +2418,6 @@ impl Publisher { None if update.new_group.is_some() && update.forward == Some(false) => { Some(("NEW_GROUP_REQUEST while pausing", Error::Unsupported)) } - // The pause is answered before a renewal could be verified, which would acknowledge - // a token nobody checked. - None if update.authorization_token.is_some() && update.forward == Some(false) => { - Some(("AUTHORIZATION TOKEN while pausing", Error::Unsupported)) - } None => None, }; if let Some((reason, err)) = refused { @@ -2335,14 +2446,19 @@ impl Publisher { if let Some(value) = update.new_group { serve.request_new_group(value); } + if let (Some(token), Some(grant)) = ( + update + .authorization_token + .filter(|_| self.auth.token_decides(crate::auth::Direction::Publish)), + request_grant, + ) { + renewal.verify(grant.verify_renewal(&self.auth, token.0)); + return Ok(fill); + } // The caller stops forwarding, then answers. if update.forward == Some(false) { return Err(Served::Paused(true)); } - if let (Some(token), Some(grant)) = (update.authorization_token, request_grant) { - renewal.verify(grant.verify_renewal(&self.auth, token.0)); - return Ok(fill); - } self.write_request_ok(&mut stream.writer, largest) .await .map_err(failed)?; @@ -2902,6 +3018,19 @@ 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(), + super::request_stream::fin_cancels(self.version), + ) + .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; } @@ -3323,6 +3452,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(), false) + .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; } @@ -4026,11 +4163,36 @@ 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(), + super::request_stream::fin_cancels(self.version), + ) + .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"); @@ -4089,11 +4251,13 @@ 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); @@ -4144,12 +4308,32 @@ impl Publisher { /// /// Shared by both announce paths: they differ in where the advertisements go /// ([`Target`]) and where the origin is rooted (`prefix`, empty when nothing asked - /// for a subset). + /// for a subset). The token that admitted the request, if one did, guards the loop. async fn run_namespaces( + &self, + announced: crate::announce::Consumer, + prefix: crate::Path, + mut ns: Namespaces, + initial: Vec, + ) -> Result<(), Error> { + let token = ns.gate.as_ref().and_then(|gate| gate.token_guard()); + let forward = self.forward_namespaces(announced, prefix, &mut ns, initial); + let res = crate::auth::RequestGrant::guard(token.as_ref(), forward) + .await + .unwrap_or_else(|refusal| Err(refusal.error)); + // Every exit, a failed write's or an ended token's included, keeps the request's + // stream, and so its credit, until the acceptor lets go of a token it is still + // verifying. + self.abandon_verification(&mut ns).await; + res + } + + /// [`Self::run_namespaces`]'s loop, leaving a token still being verified in `ns`. + async fn forward_namespaces( &self, mut announced: crate::announce::Consumer, mut prefix: crate::Path, - mut ns: Namespaces, + ns: &mut Namespaces, initial: Vec, ) -> Result<(), Error> { let mut finished = false; @@ -4166,7 +4350,7 @@ impl Publisher { } (ns.permit, ns.epoch) = self.permit_now(ns.gate.as_ref()); for update in initial { - self.apply_update(&mut ns, &prefix, update, true).await?; + self.apply_update(ns, &prefix, update, true).await?; } // When to re-offer whatever the peer should hold and doesn't, and how long to wait @@ -4182,6 +4366,7 @@ impl Publisher { // its side first. A peer streaming REQUEST_UPDATEs must not hold the session's other // tasks. let mut budget = kio::coop::Budget::new(32); + let mut session = self.session.clone(); let res = loop { kio::wait(|waiter| budget.poll_yield(waiter)).await; match ns.watched.values().any(|watch| watch.deferred) { @@ -4194,7 +4379,7 @@ impl Publisher { } } retry.set(retry_at); - self.prune_updates(&mut ns, &prefix); + self.prune_updates(ns, &prefix); let event = { let Namespaces { @@ -4205,12 +4390,18 @@ impl Publisher { updates, epoch, gate, + verifying, .. - } = &mut ns; + } = &mut *ns; kio::wait(|waiter| { 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 { @@ -4220,8 +4411,33 @@ impl Publisher { if let Poll::Ready(permit) = permit { return Poll::Ready(NamespaceEvent::Regrant(permit)); } - if let Poll::Ready(event) = target.poll_event(&mut finished, &mut goaways, self.version, waiter) { - return Poll::Ready(event); + match verifying { + // Nothing more is read until the token's verdict, so the updates behind it + // wait in the stream, under its flow control, and are answered in order. + Some(verifying) => { + if let Some(stream) = target.stream() + && self + .poll_withdrawn( + &mut session, + stream, + super::request_stream::fin_cancels(self.version), + waiter, + ) + .is_ready() + { + return Poll::Ready(NamespaceEvent::Closed(Ok(()))); + } + if let Poll::Ready(answer) = verifying.verdict.poll_grant(waiter) { + return Poll::Ready(NamespaceEvent::Verified(answer)); + } + } + None => { + if let Poll::Ready(event) = + target.poll_event(&mut finished, &mut goaways, self.version, waiter) + { + return Poll::Ready(event); + } + } } // Advertisements and updates flush and read on their own streams, so none // holds up the withdrawals behind it. @@ -4258,6 +4474,10 @@ impl Publisher { }; match event { + // Ended as the request's guard would have, had it seen the token end first: the + // stream is established, so no REQUEST_ERROR may follow its REQUEST_OK. + NamespaceEvent::Denied(refusal) => return Err(refusal.error), + // 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, @@ -4293,40 +4513,63 @@ impl Publisher { stream.writer.finish()?; break stream.writer.closed().await; } - 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. - let held: Vec<_> = ns - .watched - .iter() - .filter(|(_, watch)| watch.sent.wanted()) - .map(|(suffix, _)| suffix.clone()) - .collect(); - for suffix in held { - self.withdraw_namespace(&mut ns.target, &mut ns.requests, suffix) - .await?; + let gate = ns.gate.as_ref().expect("update has a gate"); + match update.authorization_token.filter(|_| gate.token_decides()) { + // A request the session admitted is never moved to a token's authority. + Some(_) if gate.token_guard().is_none() => { + let stream = ns.target.stream().expect("update has a stream"); + super::request_stream::write_error( + &mut stream.writer, + request::Kind::SubscribeNamespace, + &Error::Unsupported, + TOKEN_ON_SESSION_REQUEST, + self.version, + ) + .await?; + stream.writer.finish()?; + break stream.writer.closed().await; + } + // A renewal, answered once the acceptor decides; the loop runs on meanwhile. + Some(token) => { + let credit = kio::Producer::<()>::default(); + let hold: crate::auth::Hold = Arc::new(credit.consume()); + ns.verifying = Some(Verifying { + verdict: gate.verify(token.0, Some(hold)), + credit, + prefix: update.prefix, + }); } - ns.watched.clear(); - prefix = next; - let scope = crate::Pattern::subtree(prefix.as_str()) - .map(|p| p.rebase(prefix.as_str())) - .unwrap_or_default(); - let origin = self - .origin - .scope(&prefix, &scope) - .unwrap_or_else(|_| self.origin.empty()); - announced = self.advertising(origin, &ns.peer).discovery(ns.hidden).announced(); + None => { + if self.rescope(ns, &mut prefix, &mut announced, update.prefix).await? { + retry_at = None; + retry_delay = RETRY_BASE; + } + } + } + } + NamespaceEvent::Verified(answer) => { + let Verifying { + verdict, prefix: next, .. + } = ns.verifying.take().expect("a token is verifying"); + let gate = ns.gate.as_mut().expect("update has a gate"); + if let Err(refusal) = gate.admit(&self.runtime, (verdict, answer)) { + let stream = ns.target.stream().expect("update has a stream"); + super::request_stream::write_error( + &mut stream.writer, + request::Kind::SubscribeNamespace, + &refusal.error, + &refusal.reason, + self.version, + ) + .await?; + stream.writer.finish()?; + break stream.writer.closed().await; + } + ns.epoch = 0; + if self.rescope(ns, &mut prefix, &mut announced, next).await? { retry_at = None; retry_delay = RETRY_BASE; } - let stream = ns.target.stream().expect("update has a stream"); - stream - .writer - .encode_message(&ietf::RequestOk { - largest: None, - active: None, - }) - .await?; } NamespaceEvent::Retry => { retry_at = None; @@ -4344,7 +4587,7 @@ impl Publisher { for suffix in deferred { let path = prefix.rejoin(&suffix); - self.sync_namespace(&mut ns, &suffix, &path).await?; + self.sync_namespace(ns, &suffix, &path).await?; } } NamespaceEvent::Regrant(permit) => { @@ -4352,15 +4595,16 @@ impl Publisher { let suffixes: Vec = ns.watched.keys().cloned().collect(); for suffix in suffixes { let path = prefix.rejoin(&suffix); - self.sync_namespace(&mut ns, &suffix, &path).await?; + self.sync_namespace(ns, &suffix, &path).await?; } } - NamespaceEvent::Renew(request_id) => self.send_update(&mut ns, &prefix, request_id).await?, - NamespaceEvent::Answered(suffix, answer) => self.answered(&mut ns, &prefix, suffix, answer).await?, + NamespaceEvent::Renew(request_id) => self.send_update(ns, &prefix, request_id).await?, + NamespaceEvent::Answered(suffix, answer) => self.answered(ns, &prefix, suffix, answer).await?, NamespaceEvent::Update(None) => { // The origin is gone: withdraw everything, then finish the // stream and wait for delivery. self.withdraw_requests(&mut ns.target, &mut ns.requests).await; + self.abandon_verification(ns).await; let Some(stream) = ns.target.stream() else { return Ok(()); }; @@ -4371,9 +4615,9 @@ impl Publisher { // moq-transport has no restart: the peer sees the namespace end and start // again, and resubscribes. if restart { - self.apply_update(&mut ns, &prefix, update.clone(), false).await?; + self.apply_update(ns, &prefix, update.clone(), false).await?; } - self.apply_update(&mut ns, &prefix, update, active).await?; + self.apply_update(ns, &prefix, update, active).await?; } } }; @@ -4384,6 +4628,67 @@ impl Publisher { res } + /// Drop an unanswered verification, keeping the request's stream credit until the + /// acceptor lets go of it. Cancelled, it leaves the credit in `ns` for the next call. + async fn abandon_verification(&self, ns: &mut Namespaces) { + if let Some(Verifying { verdict, credit, .. }) = ns.verifying.take() { + drop(verdict); + ns.abandoned = Some(credit); + } + if let Some(credit) = &ns.abandoned { + self.released(credit).await; + ns.abandoned = None; + } + } + + /// Answer an admitted namespace update: move to the prefix it names, if that is new, + /// then acknowledge it. Returns whether the prefix moved. + async fn rescope( + &self, + ns: &mut Namespaces, + prefix: &mut crate::Path, + announced: &mut crate::announce::Consumer, + next: Option, + ) -> Result { + let moved = match next.filter(|next| next != prefix) { + Some(next) => { + // DONE still names the old prefix. After OK every suffix is relative to the + // new prefix, so replay the new cursor only after acknowledging. + let held: Vec<_> = ns + .watched + .iter() + .filter(|(_, watch)| watch.sent.wanted()) + .map(|(suffix, _)| suffix.clone()) + .collect(); + for suffix in held { + self.withdraw_namespace(&mut ns.target, &mut ns.requests, suffix) + .await?; + } + ns.watched.clear(); + *prefix = next; + let scope = crate::Pattern::subtree(prefix.as_str()) + .map(|p| p.rebase(prefix.as_str())) + .unwrap_or_default(); + let origin = self + .origin + .scope(&*prefix, &scope) + .unwrap_or_else(|_| self.origin.empty()); + *announced = self.advertising(origin, &ns.peer).discovery(ns.hidden).announced(); + true + } + None => false, + }; + let stream = ns.target.stream().expect("update has a stream"); + stream + .writer + .encode_message(&ietf::RequestOk { + largest: None, + active: None, + }) + .await?; + Ok(moved) + } + /// Reconcile what the peer holds with one route (un)announce. async fn apply_update( &self, @@ -7005,9 +7310,17 @@ mod serve_tests { ServeRange::default(), None, ); + let mut updates = subscription_updates(); let served = h .publisher - .run_subscription(&mut stream, &mut serving, &mut false, &mut goaways, async {}, None) + .run_subscription( + &mut stream, + &mut serving, + &mut false, + &mut goaways, + async {}, + &mut updates, + ) .await; assert!(matches!(served, Some(Served::UpdateFailed(_))), "{served:?}"); assert!(h.log.closes().is_empty()); @@ -7041,13 +7354,14 @@ mod serve_tests { let mut goaways = super::super::request_stream::Messages::new(crate::driver::Transport::new(h.session.clone()), false); { + let mut updates = subscription_updates(); let run = h.publisher.run_subscription( &mut stream, &mut serving, &mut finished, &mut goaways, async {}, - None, + &mut updates, ); let mut run = std::pin::pin!(run); assert!(futures::poll!(run.as_mut()).is_pending()); @@ -7164,13 +7478,14 @@ mod serve_tests { None, ); let mut finished = false; + let mut updates = subscription_updates(); let mut run = std::pin::pin!(h.publisher.run_subscription( &mut stream, &mut serving, &mut finished, &mut goaways, async {}, - None, + &mut updates, )); assert_eq!( futures::poll!(run.as_mut()).is_ready(), @@ -7209,9 +7524,15 @@ mod serve_tests { None, ); let mut finished = false; - let run = - h.publisher - .run_subscription(&mut stream, &mut serving, &mut finished, &mut goaways, async {}, None); + let mut updates = subscription_updates(); + let run = h.publisher.run_subscription( + &mut stream, + &mut serving, + &mut finished, + &mut goaways, + async {}, + &mut updates, + ); assert!(futures::poll!(std::pin::pin!(run)).is_ready(), "{version}"); } } @@ -7228,6 +7549,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, @@ -7306,13 +7628,14 @@ mod serve_tests { None, ); let mut finished = false; + let mut updates = subscription_updates(); let mut run = std::pin::pin!(h.publisher.run_subscription( &mut stream, &mut serving, &mut finished, &mut goaways, async {}, - None, + &mut updates, )); assert!(futures::poll!(run.as_mut()).is_pending()); assert_eq!(h.track.subscription().unwrap().priority, 117); @@ -7347,13 +7670,14 @@ mod serve_tests { ) .ranked(queue); let mut finished = false; + let mut updates = subscription_updates(); let mut run = std::pin::pin!(h.publisher.run_subscription( &mut stream, &mut serving, &mut finished, &mut goaways, async {}, - None, + &mut updates, )); // An unfinished group keeps its stream open. @@ -7706,9 +8030,17 @@ mod serve_tests { ); h.track.clone().abort(Error::Unsupported).unwrap(); let mut finished = false; + let mut updates = subscription_updates(); let served = h .publisher - .run_subscription(&mut stream, &mut serving, &mut finished, &mut goaways, async {}, None) + .run_subscription( + &mut stream, + &mut serving, + &mut finished, + &mut goaways, + async {}, + &mut updates, + ) .await .expect("the track's error ends the subscription"); assert!(matches!(served, Served::Track(Err(Error::Unsupported))), "{served:?}"); @@ -8108,13 +8440,14 @@ mod serve_tests { crate::driver::Transport::new(h.session.clone()), h.publisher.control.client(), ); + let mut updates = subscription_updates(); let mut run = std::pin::pin!(h.publisher.run_subscription( &mut stream, &mut serving, &mut finished, &mut goaways, async {}, - None + &mut updates )); assert!(futures::poll!(run.as_mut()).is_pending(), "{version}: update ended it"); assert_eq!(h.track.demand().wanted(), Some(wanted), "{version} {value}"); @@ -8147,9 +8480,17 @@ mod serve_tests { None, ); let mut finished = false; + let mut updates = subscription_updates(); let served = h .publisher - .run_subscription(&mut stream, &mut serving, &mut finished, &mut goaways, async {}, None) + .run_subscription( + &mut stream, + &mut serving, + &mut finished, + &mut goaways, + async {}, + &mut updates, + ) .await; assert!( matches!(served, Some(Served::UpdateFailed(Error::Unsupported))), @@ -8210,9 +8551,17 @@ mod serve_tests { None, ); let mut finished = false; + let mut updates = subscription_updates(); let served = h .publisher - .run_subscription(&mut stream, &mut serving, &mut finished, &mut goaways, async {}, None) + .run_subscription( + &mut stream, + &mut serving, + &mut finished, + &mut goaways, + async {}, + &mut updates, + ) .await; assert!( matches!(served, Some(Served::Track(Err(_)) | Served::UpdateFailed(_))), @@ -8310,13 +8659,14 @@ mod serve_tests { None, ); let mut finished = false; + let mut updates = subscription_updates(); let mut run = std::pin::pin!(h.publisher.run_subscription( &mut stream, &mut serving, &mut finished, &mut goaways, async {}, - None + &mut updates )); let mut served = None; for _ in 0..50 { @@ -8360,9 +8710,17 @@ mod serve_tests { ServeRange::default(), None, ); + let mut updates = subscription_updates(); let result = h .publisher - .run_subscription(&mut stream, &mut serving, &mut false, &mut goaways, async {}, None) + .run_subscription( + &mut stream, + &mut serving, + &mut false, + &mut goaways, + async {}, + &mut updates, + ) .await; assert!( matches!(result, Some(Served::Track(Err(Error::Decode(_))))), @@ -8399,9 +8757,17 @@ mod serve_tests { None, ); h.track.clone().abort(Error::Decode(DecodeError::InvalidValue)).unwrap(); + let mut updates = subscription_updates(); let result = h .publisher - .run_subscription(&mut stream, &mut serving, &mut false, &mut goaways, async {}, None) + .run_subscription( + &mut stream, + &mut serving, + &mut false, + &mut goaways, + async {}, + &mut updates, + ) .await; assert!( matches!(result, Some(Served::Track(Err(Error::Decode(_))))), @@ -8423,6 +8789,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 @@ -8463,6 +8830,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, @@ -8478,75 +8846,425 @@ mod serve_tests { "{version}: ended before the ack" ); assert!( - session - .log - .trail() - .iter() - .any(|(_, sent)| matches!(sent, crate::lite::test_transport::Sent::Finish)), - "{version}: no FIN" + session + .log + .trail() + .iter() + .any(|(_, sent)| matches!(sent, crate::lite::test_transport::Sent::Finish)), + "{version}: no FIN" + ); + assert!(session.log.resets().is_empty(), "{version}: the refusal was reset"); + assert!(h.log.closes().is_empty(), "{version}: the session closed"); + } + + #[kio::test] + async fn namespace_updates_move_the_prefix_after_acknowledgement() { + for version in [Version::Draft18, Version::Draft20, Version::Draft22] { + let h = serve(version); + let _old = h + ._origin + .announce("old/first", crate::origin::Route::default()) + .unwrap(); + let _new = h + ._origin + .announce("new/second", crate::origin::Route::default()) + .unwrap(); + settle().await; + let session = ScriptedSession::new(Vec::new()); + let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), version) + .await + .unwrap(); + let msg = ietf::SubscribeNamespace { + authorization_token: None, + request_id: RequestId(0), + namespace: crate::Path::from_static("old"), + hidden: false, + }; + let mut running = std::pin::pin!(h.publisher.clone().run_subscribe_namespace_stream(stream, msg)); + assert!(futures::poll!(running.as_mut()).is_pending()); + let first = session.log.writes.lock().unwrap().clone(); + let mut wire = Decoder::new(&first, version.into()); + assert_eq!(wire.varint().unwrap(), ietf::RequestOk::ID); + ietf::RequestOk::decode(&mut wire, version).unwrap(); + assert_eq!(wire.varint().unwrap(), ietf::Namespace::ID); + assert_eq!( + ietf::Namespace::decode(&mut wire, version).unwrap().suffix.as_str(), + "first" + ); + assert!(wire.is_empty()); + // Request ID 2; TRACK_NAMESPACE_PREFIX is the one-field tuple "new". + let update = [0x02, 0, 8, 2, 1, 0x34, 1, 3, b'n', b'e', b'w']; + // Partial messages do not consume the update or change the current prefix. + session.push(&update[..6]); + assert!(futures::poll!(running.as_mut()).is_pending()); + assert_eq!(*session.log.writes.lock().unwrap(), first); + session.push(&update[6..]); + assert!(futures::poll!(running.as_mut()).is_pending()); + let answer = session.log.writes.lock().unwrap().clone(); + let mut wire = Decoder::new(&answer[first.len()..], version.into()); + assert_eq!(wire.varint().unwrap(), ietf::NamespaceDone::ID, "{version}"); + assert_eq!( + ietf::NamespaceDone::decode(&mut wire, version).unwrap().suffix.as_str(), + "first" + ); + assert_eq!(wire.varint().unwrap(), ietf::RequestOk::ID, "{version}"); + ietf::RequestOk::decode(&mut wire, version).unwrap(); + assert_eq!(wire.varint().unwrap(), ietf::Namespace::ID, "{version}"); + assert_eq!( + ietf::Namespace::decode(&mut wire, version).unwrap().suffix.as_str(), + "second" + ); + assert!(wire.is_empty()); + } + } + + /// A SUBSCRIBE_NAMESPACE on `version` for every namespace, served by a publisher whose + /// application verifies request tokens, with the requester's stream and the acceptor. + async fn token_namespaces(version: Version) -> TokenNamespaces { + token_namespaces_expiring(version, None).await + } + + /// A SUBSCRIBE_NAMESPACE its own request token admitted, granted everything, lapsing in + /// `expires` (or never), so an update's token is a renewal. Holds the token's grant. + async fn token_namespaces_expiring(version: Version, expires: Option) -> TokenNamespaces { + let auth = crate::auth::Link::new(false); + let mut requests = auth.handle().requests(); + let h = serve_with_auth(version, auth, Vec::new()); + settle().await; + let session = ScriptedSession::new(Vec::new()); + let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), version) + .await + .unwrap(); + let msg = ietf::SubscribeNamespace { + authorization_token: Some(request_token()), + request_id: RequestId(0), + namespace: crate::Path::empty(), + hidden: false, + }; + let mut running: Running = Box::pin(h.publisher.clone().run_subscribe_namespace_stream(stream, msg)); + let request = taken_token(&mut running, &mut requests).await; + let issued = request.accept(crate::auth::Grant { + expires: expires.map(|after| h.publisher.runtime.now() + after), + ..crate::auth::Grant::all() + }); + assert!(futures::poll!(running.as_mut()).is_pending()); + assert_eq!( + written_since(&session, 0, version), + [ + (ietf::RequestOk::ID, String::new()), + (ietf::Namespace::ID, "room".to_string()), + ], + "{version}" + ); + (h, requests, session, running, issued) + } + + /// What [`token_namespaces`] returns: the serving harness, the acceptor's queue, the + /// request's stream, the running request, and the grant that admitted it. + type TokenNamespaces = ( + Serve, + crate::auth::Requests, + ScriptedSession, + Running, + crate::auth::Issued, + ); + + type Running = std::pin::Pin>>>; + + /// Poll `running` until the acceptor holds a token, which stays unanswered. + async fn taken_token(running: &mut Running, requests: &mut crate::auth::Requests) -> crate::auth::Request { + for _ in 0..10 { + assert!(futures::poll!(running.as_mut()).is_pending(), "verification is pending"); + if let Poll::Ready(Some(request)) = requests.poll_next(&kio::Waiter::noop()) { + return request; + } + settle().await; + } + panic!("the acceptor never saw the token"); + } + + /// The messages written to `session` since `mark`, by type and, for a NAMESPACE or + /// NAMESPACE_DONE, suffix. + fn written_since(session: &ScriptedSession, mark: usize, version: Version) -> Vec<(u64, String)> { + let wire = session.log.writes.lock().unwrap().clone(); + let mut wire = Decoder::new(&wire[mark..], version.into()); + let mut messages = Vec::new(); + while !wire.is_empty() { + let id = wire.varint().unwrap(); + let suffix = match id { + ietf::RequestOk::ID => { + ietf::RequestOk::decode(&mut wire, version).unwrap(); + String::new() + } + ietf::Namespace::ID => ietf::Namespace::decode(&mut wire, version).unwrap().suffix.to_string(), + ietf::NamespaceDone::ID => ietf::NamespaceDone::decode(&mut wire, version) + .unwrap() + .suffix + .to_string(), + other => panic!("{version}: unexpected message {other:#x}"), + }; + messages.push((id, suffix)); + } + messages + } + + /// An update whose token the acceptor is still verifying holds up neither announcements + /// nor withdrawals on its stream; it is answered once the acceptor decides. + #[kio::test] + async fn a_pending_update_token_does_not_hold_up_announcements() { + for version in [Version::Draft18, Version::Draft20, Version::Draft22] { + let (h, mut requests, session, mut running, _issued) = token_namespaces(version).await; + let mark = session.log.writes.lock().unwrap().len(); + session.push(&update_with_tokens(&[TOKEN])); + let request = taken_token(&mut running, &mut requests).await; + + let other = h._origin.announce("other", crate::origin::Route::default()).unwrap(); + settle().await; + assert!(futures::poll!(running.as_mut()).is_pending()); + drop(other); + settle().await; + assert!(futures::poll!(running.as_mut()).is_pending()); + assert_eq!( + written_since(&session, mark, version), + [ + (ietf::Namespace::ID, "other".to_string()), + (ietf::NamespaceDone::ID, "other".to_string()), + ], + "{version}: advertised while the token is pending, and not answered yet" + ); + + let _issued = request.accept(crate::auth::Grant::all()); + assert!(futures::poll!(running.as_mut()).is_pending()); + assert_eq!( + written_since(&session, mark, version)[2..], + [(ietf::RequestOk::ID, String::new())], + "{version}: answered once verified" + ); + } + } + + /// Updates behind one whose token is being verified wait their turn, so their answers + /// keep the order they were asked in. + #[kio::test] + async fn updates_behind_a_pending_token_are_answered_in_order() { + for version in [Version::Draft18, Version::Draft20, Version::Draft22] { + let (_h, mut requests, session, mut running, _issued) = token_namespaces(version).await; + let mark = session.log.writes.lock().unwrap().len(); + session.push(&update_with_tokens(&[TOKEN])); + let request = taken_token(&mut running, &mut requests).await; + // Request ID 4; TRACK_NAMESPACE_PREFIX is the one-field tuple "room". + session.push(&[0x02, 0, 9, 4, 1, 0x34, 1, 4, b'r', b'o', b'o', b'm']); + for _ in 0..10 { + assert!(futures::poll!(running.as_mut()).is_pending()); + settle().await; + } + assert!( + written_since(&session, mark, version).is_empty(), + "{version}: the later update was answered ahead of the pending one" + ); + + let _issued = request.accept(crate::auth::Grant::all()); + assert!(futures::poll!(running.as_mut()).is_pending()); + assert_eq!( + written_since(&session, mark, version), + [ + (ietf::RequestOk::ID, String::new()), + (ietf::NamespaceDone::ID, "room".to_string()), + (ietf::RequestOk::ID, String::new()), + (ietf::Namespace::ID, String::new()), + ], + "{version}: the token's answer, then the move to the new prefix" + ); + } + } + + /// A narrowed grant withdraws what it no longer covers while a renewal's token is still + /// being verified; the renewal, once admitted, covers it again. + #[kio::test] + async fn narrowing_withdraws_while_an_update_token_is_pending() { + for version in [Version::Draft18, Version::Draft20, Version::Draft22] { + let (_h, mut requests, session, mut running, issued) = token_namespaces(version).await; + let mark = session.log.writes.lock().unwrap().len(); + session.push(&update_with_tokens(&[TOKEN])); + let request = taken_token(&mut running, &mut requests).await; + + issued.update(crate::auth::Grant { + subscribe: crate::Pattern::subtree("other").unwrap().into(), + ..crate::auth::Grant::default() + }); + assert!(futures::poll!(running.as_mut()).is_pending()); + assert_eq!( + written_since(&session, mark, version), + [(ietf::NamespaceDone::ID, "room".to_string())], + "{version}: withdrawn while the token is pending" + ); + + let _issued = request.accept(crate::auth::Grant::all()); + assert!(futures::poll!(running.as_mut()).is_pending()); + assert_eq!( + written_since(&session, mark, version)[1..], + [ + (ietf::RequestOk::ID, String::new()), + (ietf::Namespace::ID, "room".to_string()), + ], + "{version}: the renewal covers what the narrowed grant no longer does" + ); + } + } + + /// A token an update names on a SUBSCRIBE_NAMESPACE the session admitted is refused and + /// never reaches the acceptor: a request is admitted by the session or by a token, and + /// that never changes. + #[kio::test] + async fn a_token_on_a_session_namespace_update_is_refused() { + for version in [Version::Draft18, Version::Draft20, Version::Draft22] { + let auth = crate::auth::Link::new(false); + let mut requests = auth.handle().requests(); + let h = serve_with_auth(version, auth, Vec::new()); + settle().await; + let session = ScriptedSession::new(Vec::new()); + let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), version) + .await + .unwrap(); + let msg = ietf::SubscribeNamespace { + authorization_token: None, + request_id: RequestId(0), + namespace: crate::Path::empty(), + hidden: false, + }; + let mut running: Running = Box::pin(h.publisher.clone().run_subscribe_namespace_stream(stream, msg)); + assert!(futures::poll!(running.as_mut()).is_pending()); + session.push(&update_with_tokens(&[TOKEN])); + for _ in 0..10 { + if futures::poll!(running.as_mut()).is_ready() { + break; + } + settle().await; + } + let log = session.log.writes.lock().unwrap().clone(); + assert_eq!( + log.windows(TOKEN_ON_SESSION_REQUEST.len()) + .filter(|window| *window == TOKEN_ON_SESSION_REQUEST.as_bytes()) + .count(), + 1, + "{version}: the update was not refused" + ); + assert!( + requests.poll_next(&kio::Waiter::noop()).is_pending(), + "{version}: the token reached the acceptor" + ); + } + } + + /// A requester that withdraws while an update's token is being verified keeps the + /// request's stream, and so its credit, until the application answers. + #[kio::test] + async fn a_withdrawn_namespace_update_keeps_its_stream_until_the_application_answers() { + let version = Version::Draft22; + let (_h, mut requests, session, mut running, _issued) = token_namespaces(version).await; + session.push(&update_with_tokens(&[TOKEN])); + let request = taken_token(&mut running, &mut requests).await; + + session.close(crate::lite::test_transport::Close::Reset); + for _ in 0..10 { + assert!( + futures::poll!(running.as_mut()).is_pending(), + "the withdrawn request let go of its stream before the application answered" + ); + settle().await; + } + drop(request.accept(crate::auth::Grant::all())); + assert!( + futures::poll!(running.as_mut()).is_ready(), + "answered, the request ends" + ); + } + + /// A write that fails while an update's token is being verified ends the request, but + /// it keeps the request's stream, and so its credit, until the application answers. + #[kio::test] + async fn a_failed_write_keeps_a_verifying_update_stream_until_the_application_answers() { + let version = Version::Draft22; + let (h, mut requests, session, mut running, _issued) = token_namespaces(version).await; + session.push(&update_with_tokens(&[TOKEN])); + let request = taken_token(&mut running, &mut requests).await; + + session.log.fail_writes(); + let _other = h._origin.announce("other", crate::origin::Route::default()).unwrap(); + for _ in 0..10 { + assert!( + futures::poll!(running.as_mut()).is_pending(), + "the failed request let go of its stream before the application answered" + ); + settle().await; + } + drop(request.accept(crate::auth::Grant::all())); + let res = futures::poll!(running.as_mut()); + assert!( + matches!(res, Poll::Ready(Err(_))), + "answered, the failed write ends the request: {res:?}" + ); + } + + /// The request's token ending while a renewal is being verified ends the request, but it + /// keeps the request's stream, and so its credit, until the application answers. + #[kio::test] + async fn an_ended_token_keeps_a_verifying_update_stream_until_the_application_answers() { + let version = Version::Draft22; + let (_h, mut requests, session, mut running, _issued) = + token_namespaces_expiring(version, Some(Duration::from_secs(1))).await; + // A renewal naming another token. + session.push(&update_with_tokens(&[TOKEN])); + let request = taken_token(&mut running, &mut requests).await; + + kio::clock::sim::advance(Duration::from_secs(2)).await; + for _ in 0..10 { + assert!( + futures::poll!(running.as_mut()).is_pending(), + "the ended request let go of its stream before the application answered" + ); + settle().await; + } + drop(request); + let res = futures::poll!(running.as_mut()); + assert!( + matches!(res, Poll::Ready(Err(_))), + "answered, the ended token ends the request: {res:?}" ); - assert!(session.log.resets().is_empty(), "{version}: the refusal was reset"); - assert!(h.log.closes().is_empty(), "{version}: the session closed"); } + /// The request's token ending while a withdrawn publisher waits for a renewal's + /// verification to let go still keeps the request's stream until the application answers. #[kio::test] - async fn namespace_updates_move_the_prefix_after_acknowledgement() { - for version in [Version::Draft18, Version::Draft20, Version::Draft22] { - let h = serve(version); - let _old = h - ._origin - .announce("old/first", crate::origin::Route::default()) - .unwrap(); - let _new = h - ._origin - .announce("new/second", crate::origin::Route::default()) - .unwrap(); - settle().await; - let session = ScriptedSession::new(Vec::new()); - let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), version) - .await - .unwrap(); - let msg = ietf::SubscribeNamespace { - request_id: RequestId(0), - namespace: crate::Path::from_static("old"), - hidden: false, - }; - let mut running = std::pin::pin!(h.publisher.clone().run_subscribe_namespace_stream(stream, msg)); - assert!(futures::poll!(running.as_mut()).is_pending()); - let first = session.log.writes.lock().unwrap().clone(); - let mut wire = Decoder::new(&first, version.into()); - assert_eq!(wire.varint().unwrap(), ietf::RequestOk::ID); - ietf::RequestOk::decode(&mut wire, version).unwrap(); - assert_eq!(wire.varint().unwrap(), ietf::Namespace::ID); - assert_eq!( - ietf::Namespace::decode(&mut wire, version).unwrap().suffix.as_str(), - "first" - ); - assert!(wire.is_empty()); - // Request ID 2; TRACK_NAMESPACE_PREFIX is the one-field tuple "new". - let update = [0x02, 0, 8, 2, 1, 0x34, 1, 3, b'n', b'e', b'w']; - // Partial messages do not consume the update or change the current prefix. - session.push(&update[..6]); - assert!(futures::poll!(running.as_mut()).is_pending()); - assert_eq!(*session.log.writes.lock().unwrap(), first); - session.push(&update[6..]); - assert!(futures::poll!(running.as_mut()).is_pending()); - let answer = session.log.writes.lock().unwrap().clone(); - let mut wire = Decoder::new(&answer[first.len()..], version.into()); - assert_eq!(wire.varint().unwrap(), ietf::NamespaceDone::ID, "{version}"); - assert_eq!( - ietf::NamespaceDone::decode(&mut wire, version).unwrap().suffix.as_str(), - "first" + async fn an_ended_token_does_not_cut_short_a_withdrawal_verification_drain() { + let version = Version::Draft22; + let (h, mut requests, session, mut running, _issued) = + token_namespaces_expiring(version, Some(Duration::from_secs(1))).await; + // A renewal naming another token. + session.push(&update_with_tokens(&[TOKEN])); + let request = taken_token(&mut running, &mut requests).await; + + h.publisher.withdrawal.begin(); + for _ in 0..10 { + assert!( + futures::poll!(running.as_mut()).is_pending(), + "draining the verification" ); - assert_eq!(wire.varint().unwrap(), ietf::RequestOk::ID, "{version}"); - ietf::RequestOk::decode(&mut wire, version).unwrap(); - assert_eq!(wire.varint().unwrap(), ietf::Namespace::ID, "{version}"); - assert_eq!( - ietf::Namespace::decode(&mut wire, version).unwrap().suffix.as_str(), - "second" + settle().await; + } + kio::clock::sim::advance(Duration::from_secs(2)).await; + for _ in 0..10 { + assert!( + futures::poll!(running.as_mut()).is_pending(), + "the ended request let go of its stream before the application answered" ); - assert!(wire.is_empty()); + settle().await; } + drop(request); + assert!( + futures::poll!(running.as_mut()).is_ready(), + "answered, the request ends" + ); } #[kio::test] @@ -8559,6 +9277,7 @@ mod serve_tests { .await .unwrap(); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(0), namespace: crate::Path::from_static(""), hidden: false, @@ -8694,6 +9413,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(), @@ -8868,28 +9588,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. #[kio::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) + ); } } @@ -8932,6 +9653,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(), @@ -9074,6 +9796,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, @@ -9268,6 +9991,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, @@ -9958,6 +10682,7 @@ mod serve_tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -10368,6 +11093,7 @@ mod serve_tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -10937,6 +11663,7 @@ mod serve_tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order, @@ -11034,6 +11761,64 @@ mod serve_tests { } } + #[kio::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, + }; + 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) + .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. The objects carry Timestamps only /// when that block declares their units. @@ -11833,6 +12618,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, @@ -11943,6 +12729,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, @@ -12089,6 +12876,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, @@ -12231,6 +13019,7 @@ mod serve_tests { let fetch = kio::clock::sim::spawn(h.publisher.clone().run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -12539,6 +13328,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, @@ -12561,6 +13351,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(), @@ -12766,6 +13557,7 @@ mod serve_tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -12811,6 +13603,7 @@ mod serve_tests { .run_fetch_stream( stream, ietf::Fetch { + authorization_token: None, request_id: FETCH_ID, subscriber_priority: 128, group_order: GroupOrder::Ascending, @@ -13042,6 +13835,13 @@ mod serve_tests { assert!(serve(None).await.flags.has_end, "the whole group"); } + fn subscription_updates() -> SubscriptionUpdates { + SubscriptionUpdates { + grant: None, + renewal: Default::default(), + } + } + /// Like [`serve`], but with an app auth acceptor wired in and the subscribe stream's /// reader scripted with `first_script` (a REQUEST_UPDATE, for the renewal tests). fn serve_with_auth(version: Version, auth: crate::auth::Link, first_script: Vec) -> Serve { @@ -13133,7 +13933,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(); @@ -13265,45 +14068,74 @@ mod serve_tests { } } - /// A pause is answered before a renewal riding it could be verified, so an update carrying - /// both fails instead of acknowledging a token nobody checked. + /// Pausing with a token waits for verification, drops demand, and keeps the new grant. #[kio::test] - async fn a_pause_carrying_a_token_fails_the_update() { - for version in [Version::Draft18] { - let (auth, mut requests, _cred) = auth_covering_other(); - let h = serve_with_auth(version, auth, subscribe_update_with_token(version, false).await); - let rt = h.publisher.runtime.clone(); - let stream = Stream::open(&mut crate::driver::Transport::new(h.session.clone()), version) - .await - .unwrap(); - let acceptor = async move { - let first = requests.next().await.unwrap(); - let _held = first.accept(grant_all_expiring(&rt, Some(60))); - std::future::pending::<()>().await - }; - let mut acceptor = std::pin::pin!(acceptor); - let mut serving = std::pin::pin!(h.publisher.clone().run_subscribe_stream(stream, token_subscribe())); - let mut ended = false; - for _ in 0..200 { - let _ = futures::poll!(acceptor.as_mut()); - if futures::poll!(serving.as_mut()).is_ready() { - ended = true; - break; + async fn a_pause_carrying_a_token_changes_authority() { + for version in [Version::Draft18, Version::Draft22] { + for queue_resume in [false, true] { + let (auth, mut requests, _cred) = auth_covering_other(); + let mut script = subscribe_update_with_token(version, false).await; + if queue_resume { + script.extend(subscribe_update_token_rid(version, 0x42).await); } - settle().await; + let h = serve_with_auth(version, auth, script); + let rt = h.publisher.runtime.clone(); + let stream = Stream::open(&mut crate::driver::Transport::new(h.session.clone()), version) + .await + .unwrap(); + let mut serving = std::pin::pin!(h.publisher.clone().run_subscribe_stream(stream, token_subscribe())); + assert!(futures::poll!(serving.as_mut()).is_pending()); + let first = requests.next().await.unwrap().accept(grant_all_expiring(&rt, Some(60))); + for _ in 0..50 { + assert!(futures::poll!(serving.as_mut()).is_pending()); + settle().await; + } + let next = match requests.poll_next(&kio::Waiter::noop()) { + Poll::Ready(Some(request)) => request, + _ => panic!("token update reached verifier"), + }; + assert!(Wire::read(&h.log, version).sent(ietf::RequestOk::ID).is_none()); + let mut issued = next.accept(grant_all_expiring(&rt, None)); + for _ in 0..50 { + assert!(futures::poll!(serving.as_mut()).is_pending()); + settle().await; + } + assert!(Wire::read(&h.log, version).sent(ietf::RequestOk::ID).is_some()); + assert!(h.track.subscription().is_none(), "pause released demand"); + assert!( + futures::FutureExt::now_or_never(first.closed()).is_some(), + "old token ended" + ); + if queue_resume { + let request = match requests.poll_next(&kio::Waiter::noop()) { + Poll::Ready(Some(request)) => request, + _ => panic!("queued resume token never reached verifier"), + }; + let resumed = request.accept(grant_all_expiring(&rt, None)); + for _ in 0..50 { + assert!(futures::poll!(serving.as_mut()).is_pending()); + settle().await; + } + assert!(h.track.subscription().is_some(), "queued resume survived pause"); + assert!(futures::FutureExt::now_or_never(issued.closed()).is_some()); + issued = resumed; + } + kio::clock::sim::advance(Duration::from_secs(120)).await; + assert!( + futures::poll!(serving.as_mut()).is_pending(), + "new grant outlives old expiry" + ); + issued.revoke(crate::SessionError::Unauthorized, "revoked while paused"); + let mut ended = false; + for _ in 0..50 { + if futures::poll!(serving.as_mut()).is_ready() { + ended = true; + break; + } + settle().await; + } + assert!(ended, "paused guard follows new token"); } - assert!(ended, "{version:?}: the failed update ends the subscription"); - let wire = Wire::read(&h.log, version); - assert!( - wire.sent(ietf::RequestError::ID).is_some(), - "{version:?}: the update is refused" - ); - assert!( - wire.sent(ietf::RequestOk::ID).is_none(), - "{version:?}: nothing acknowledged it" - ); - let (status, _) = wire.publish_done.expect("PUBLISH_DONE was sent"); - assert_eq!(status, ietf::PublishDoneStatus::UpdateFailed.code(), "{version:?}"); } } @@ -13322,7 +14154,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(); @@ -14240,59 +15075,52 @@ mod serve_tests { "narrowing the limit leaves the token-verified subscription" ); } - - /// Where tokens decide, a token arriving on an update for a subscription admitted without - /// one is refused NOT_SUPPORTED rather than taken unread, failing only that subscription. + /// A token an update names on a subscription the session admitted is refused, live or + /// paused, and never reaches the acceptor: a request is admitted by the session or by a + /// token, and that never changes. The subscription ends, REQUEST_ERROR then PUBLISH_DONE. #[kio::test] - async fn a_token_on_a_subscription_admitted_without_one_is_refused() { - for version in [Version::Draft22] { - let auth = crate::auth::Link::new(false); - auth.handle().authorize(&crate::auth::Grant::all()); - let h = serve_with_auth(version, auth, Vec::new()); - let session = ScriptedSession::per_stream(vec![update_with_tokens(&[TOKEN])]); - let mut stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), version) - .await - .unwrap(); - let mut goaways = super::super::request_stream::Messages::new( - crate::driver::Transport::new(h.session.clone()), - h.publisher.control.client(), - ); - let mut serving = TrackServe::new( - crate::driver::Transport::new(h.session.clone()), - h.track.subscribe(None), - RequestId(0), - version, - ServeRange::default(), - None, - ); - let mut finished = false; - let mut run = std::pin::pin!(h.publisher.run_subscription( - &mut stream, - &mut serving, - &mut finished, - &mut goaways, - async {}, - None - )); - let mut served = None; - for _ in 0..50 { - if let std::task::Poll::Ready(done) = futures::poll!(run.as_mut()) { - served = Some(done); - break; + async fn a_token_on_a_session_subscription_update_is_refused() { + for version in [Version::Draft18, Version::Draft22] { + for initially_forwarding in [false, true] { + for forwarding in [false, true] { + let case = format!("{version} initial={initially_forwarding} forward={forwarding}"); + let auth = crate::auth::Link::new(false); + auth.handle().authorize(&crate::auth::Grant::all()); + let mut requests = auth.handle().requests(); + let h = serve_with_auth(version, auth, subscribe_update_with_token(version, forwarding).await); + let stream = Stream::open(&mut crate::driver::Transport::new(h.session.clone()), version) + .await + .unwrap(); + let mut msg = subscribe(Filter::NextObject, None); + msg.forward = initially_forwarding; + let mut run = std::pin::pin!(h.publisher.clone().run_subscribe_stream(stream, msg)); + let done = || { + !h.log.trail().is_empty() && Wire::read(&h.log, version).sent(ietf::PublishDone::ID).is_some() + }; + for _ in 0..200 { + if done() || futures::poll!(run.as_mut()).is_ready() { + break; + } + settle().await; + } + assert!(done(), "{case}: PUBLISH_DONE never went out"); + let wire = Wire::read(&h.log, version); + assert!( + wire.sent(ietf::RequestError::ID).is_some(), + "{case}: the update was not refused" + ); + assert_eq!( + occurrences(&h.log, TOKEN_ON_SESSION_REQUEST.as_bytes()), + 1, + "{case}: refused for another reason" + ); + assert!( + requests.poll_next(&kio::Waiter::noop()).is_pending(), + "{case}: the token reached the acceptor" + ); + assert!(h.log.closes().is_empty(), "{case}: the session closed"); } - settle().await; } - assert!( - matches!(served, Some(Some(Served::UpdateFailed(Error::Unsupported)))), - "{version}: {served:?}" - ); - assert!(h.log.closes().is_empty(), "{version}: the session closed"); - - let wire = session.log.writes.lock().unwrap().clone(); - let mut buf = Decoder::new(&wire, version.into()); - assert_eq!(buf.varint().unwrap(), ietf::RequestError::ID, "{version}"); - let error = ietf::RequestError::decode(&mut buf, version).unwrap(); - assert_eq!(error.error_code, 0x3, "{version}"); } } @@ -14840,6 +15668,7 @@ mod tests { .await .unwrap(); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(1), namespace: crate::Path::from_static(""), hidden: false, @@ -14911,6 +15740,7 @@ mod tests { .await .unwrap(); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(1), namespace: crate::Path::from_static(""), hidden: false, @@ -15292,6 +16122,7 @@ mod tests { .await .unwrap(); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(1), namespace: crate::Path::new(prefix).unwrap(), hidden, @@ -15422,6 +16253,7 @@ mod tests { .await .unwrap(); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id: RequestId(1), namespace: crate::Path::from_static(""), hidden: false, @@ -17000,6 +17832,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/request_stream.rs b/rs/moq-net/src/ietf/request_stream.rs index ac39ce7094a..ec1aa463483 100644 --- a/rs/moq-net/src/ietf/request_stream.rs +++ b/rs/moq-net/src/ietf/request_stream.rs @@ -387,6 +387,7 @@ impl<'a> Decode<'a, Version> for Update { /// The namespace-only parameters of a REQUEST_UPDATE. #[derive(Debug)] pub(crate) struct NamespaceUpdate { + pub(super) authorization_token: Option, pub(super) request_id: super::RequestId, pub(super) prefix: Option, pub(super) refused: Option, @@ -421,10 +422,15 @@ impl<'a> 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 ebb3e128260..fba6ed0d5e0 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -1480,6 +1480,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(), @@ -1927,6 +1928,7 @@ mod tests { version, ); let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id, namespace: crate::Path::new(namespace).unwrap(), hidden: false, @@ -3687,6 +3689,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 fcb2964d782..8ab6249cf9f 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, 0x03 => self.authorization_token.clone(), HIDDEN_PARAM => hidden_param(self.hidden)); 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, @@ -285,6 +287,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, @@ -295,6 +298,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, @@ -312,6 +316,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 e3b5e247c24..edaa84e6d7c 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -910,6 +910,23 @@ enum Admit { Token(Option), } +/// What admitted a PUBLISH_NAMESPACE: its own token's grant, if one did, and the announce +/// slot it took, which a re-attach reuses. +struct Admission { + grant: Option, + slot: Option, +} + +impl Admission { + /// How [`Subscriber::start_announce`] attaches it. + fn admit(&self) -> Admit { + match self.grant { + Some(_) => Admit::Token(self.slot.clone()), + None => Admit::Session(self.slot.clone()), + } + } +} + /// How a live advertisement ends, written once its route is released. #[derive(Debug)] enum Withdraw { @@ -1441,6 +1458,7 @@ impl Subscriber { let stream = &mut request.stream; let msg = ietf::SubscribeNamespace { + authorization_token: None, request_id, namespace: prefix.clone(), hidden, @@ -1866,7 +1884,8 @@ impl Subscriber { }; // Our authorization of the peer (the limit) admits an announce without a token, and - // holds back one it does not cover until it does. An AUTHORIZATION TOKEN on the + // refuses one it does not cover: the peer learns at once rather than publishing into + // nothing, and re-announces once its limit widens. An AUTHORIZATION TOKEN on the // announce goes to the application to verify for this announce alone (MoQ // request-token), even where the limit covers it, and a verified one is exempt from the // limit while it lasts. @@ -1874,15 +1893,21 @@ impl Subscriber { Some(token) if gate.token_decides() => Some(token.0.clone()), _ => None, }; + if token.is_none() && !self.auth.within_limit(crate::auth::Direction::Subscribe, path.as_str()) { + tracing::debug!(%path, "refusing publish_namespace outside the limit"); + let reason = crate::auth::unauthorized_reason(&path); + self.write_error(&mut stream, &Error::Unauthorized, &reason).await?; + let _ = stream.writer.close().await; + return Ok(()); + } + let existing = self.state.lock().broadcasts.get(&path).map(|entry| entry._slot.clone()); + let slot: Option = Some(match existing { + Some(slot) => slot, + None => std::sync::Arc::new(self.acquire_announce()?), + }); let (admit, token_grant) = match token { - None => (Admit::Session(None), None), + None => (Admit::Session(slot.clone()), None), Some(token) => { - // Counted against the cap while the application verifies, unless the - // namespace already holds a slot. - let slot: Option = match self.state.lock().broadcasts.contains_key(&path) { - true => None, - false => Some(std::sync::Arc::new(self.acquire_announce()?)), - }; let verdict = gate.verify(token, slot.clone()); // The session closing or the peer withdrawing the announce withdraws the token. let session = self.session.clone(); @@ -1913,7 +1938,7 @@ impl Subscriber { slot.clone(), ); match admitted { - Ok(grant) => (Admit::Token(slot), Some(grant)), + Ok(grant) => (Admit::Token(slot.clone()), Some(grant)), Err(refusal) => { self.write_error(&mut stream, &refusal.error, &refusal.reason).await?; let _ = stream.writer.close().await; @@ -1924,35 +1949,62 @@ impl Subscriber { }; let token = token_grant.is_some(); - // Everything the advertisement does once admitted runs under its grant: one that ends - // at any await, the first answer included, drops it right there. `attached` says the - // route is up, and `acked` that the OK went out, which decides how the peer is told. + // Everything the advertisement does once admitted runs under what admitted it: its + // token's grant, or the session's limit, which never holds an announce back. Either + // ending at any await, the first answer included, drops it right there, and a + // publisher whose limit widens again re-announces. `attached` says the route is up, and + // `acked` that the OK went out, which decides how the peer is told. let mut attached = None; let mut acked = false; - let lived = crate::auth::RequestGrant::guard(token_grant.as_ref(), async { - match self.start_announce(path.clone(), advert, admit) { - Ok(id) => attached = Some(id), - Err(err) => return Ok(Withdraw::Reject(err.into())), - } - // Buffered whole before its first await, so however this is dropped from here, the - // advertisement can still end on the wire. - acked = true; - self.write_ok(&mut stream).await?; - - // An endpoint updates an advertisement with REQUEST_UPDATE on the stream that - // already carries it, so keep reading until the stream closes. - self.run_publish_namespace_updates( - &mut stream, - &path, - Held { - cluster: msg.params.cluster, - }, - peer, - &mut attached, - token_grant.clone(), - ) - .await - }) + let auth = self.auth.clone(); + let mut epoch = 0; + let mut limited = |waiter: &kio::Waiter| { + let mut excluded = false; + while let Poll::Ready(permit) = auth.poll_permit(crate::auth::Direction::Subscribe, &mut epoch, waiter) { + excluded = !permit.within_limit(path.as_str()); + } + match excluded { + true => Poll::Ready(crate::auth::Refusal { + error: Error::Unauthorized, + reason: crate::auth::unauthorized_reason(&path), + }), + false => Poll::Pending, + } + }; + let mut ended = |waiter: &kio::Waiter| match &token_grant { + Some(grant) => grant.poll_ended(waiter), + None => limited(waiter), + }; + let lived = crate::auth::until( + async { + match self.start_announce(path.clone(), advert, admit) { + Ok(id) => attached = Some(id), + Err(err) => return Ok(Withdraw::Reject(err.into())), + } + // Buffered whole before its first await, so however this is dropped from here, the + // advertisement can still end on the wire. + acked = true; + self.write_ok(&mut stream).await?; + + // An endpoint updates an advertisement with REQUEST_UPDATE on the stream that + // already carries it, so keep reading until the stream closes. + self.run_publish_namespace_updates( + &mut stream, + &path, + Held { + cluster: msg.params.cluster, + }, + peer, + &mut attached, + Admission { + grant: token_grant.clone(), + slot: slot.clone(), + }, + ) + .await + }, + &mut ended, + ) .await; // The route goes before anything waits on the peer, so a peer that stops reading @@ -1963,7 +2015,7 @@ impl Subscriber { let withdraw = match lived { Ok(withdraw) => withdraw?, Err(refusal) => { - tracing::info!(%path, err = %refusal.error, reason = %refusal.reason, "request token grant ended"); + tracing::info!(%path, err = %refusal.error, reason = %refusal.reason, "announce no longer authorized"); if !acked { self.write_error(&mut stream, &refusal.error, &refusal.reason).await?; let _ = stream.writer.close().await; @@ -2004,8 +2056,9 @@ impl Subscriber { held: Held, peer: cluster::Peer, attached: &mut Option, - token_grant: Option, + admission: Admission, ) -> Result { + let token_grant = &admission.grant; let Held { cluster: mut held } = held; // A REQUEST_UPDATE carrying a fresh token refreshes the announce's request grant (MoQ // request-token); its verify is raced against the stream in the loop's single wait @@ -2227,11 +2280,7 @@ impl Subscriber { // Re-attach: a clean path replaced the reflected one we detached from. None => { // The grant still holds the slot it was admitted with, so reuse it. - let admit = match &token_grant { - Some(grant) => Admit::Token(grant.hold()), - None => Admit::Session(None), - }; - self.start_announce(path.clone(), advert, admit) + self.start_announce(path.clone(), advert, admission.admit()) .map(|id| *attached = Some(id)) } } @@ -2252,23 +2301,20 @@ impl Subscriber { // awaited here: it becomes the pending renewal raced against the deadline above. // The routing above is already applied, so the renewal's answer (written when its // verdict resolves) is this update's single response, cluster parameters included. - if let Some(token) = msg.authorization_token { - match &token_grant { - Some(grant) => { - renewal.verify(grant.verify_renewal(&self.auth, token.0)); - continue; - } - // Where tokens decide, a token on an announce admitted without one is refused - // rather than taken unread, so every token this side admits is verified. - None if self.auth.token_decides(crate::auth::Direction::Subscribe) => { - let refusal = crate::auth::Refusal { - error: Error::Unsupported, - reason: "AUTHORIZATION TOKEN on an announce admitted without one".to_string(), - }; - return Ok(Withdraw::Cancel(refusal)); - } - None => {} - } + if let Some(token) = msg + .authorization_token + .filter(|_| self.auth.token_decides(crate::auth::Direction::Subscribe)) + { + let Some(grant) = &token_grant else { + // An announce is admitted by the session or by a token, and that never + // changes, so a token here is refused rather than taken unread. + return Ok(Withdraw::Cancel(crate::auth::Refusal { + error: Error::Unsupported, + reason: "AUTHORIZATION TOKEN on an announce admitted without one".to_string(), + })); + }; + renewal.verify(grant.verify_renewal(&self.auth, token.0)); + continue; } // No token rides this update: acknowledge it now. @@ -2488,8 +2534,10 @@ impl Subscriber { Some(slot) => slot, None => std::sync::Arc::new(self.acquire_announce()?), }; - // Outside the session's limit: held, not refused, so a wider limit can - // attach it while the peer still advertises it. + // Outside the session's limit: held, not refused, so a wider limit can attach + // it while the peer still advertises it. Only a NAMESPACE entry gets here + // outside it: nothing can refuse one, and the peer never repeats it. A + // PUBLISH_NAMESPACE the limit does not cover is refused before it attaches. if !token && !self.auth.within_limit(crate::auth::Direction::Subscribe, path.as_str()) { tracing::debug!(route = %crate::path::Rooted(self.origin.root(), &path), "withholding announce outside the limit"); entry.insert(BroadcastState { @@ -2941,6 +2989,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 _working = self.session.work().start(); let validity = self.validity.start(); let mut resolved = ResolvedRanges::default(); @@ -3033,6 +3084,7 @@ impl Subscriber { Some(upstream) => { self.adopted(&key, upstream.request_id); let tracks::Upstream { + token_grant: token, stream, messages, request_id, @@ -3042,6 +3094,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, @@ -3287,6 +3343,7 @@ impl Subscriber { && self.state.lock().subscribes.contains_key(&request_id) { let upstream = tracks::Upstream { + token_grant: token_grant.clone(), stream, messages, request_id, @@ -3926,6 +3983,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, @@ -4230,6 +4288,7 @@ impl Subscriber { stream.writer.buffer_message( ietf::Fetch::ID, &ietf::Fetch { + authorization_token: None, request_id: fetch_id, subscriber_priority: super::priority::to_wire( track.subscription().map(|s| s.priority).unwrap_or(0), @@ -4438,6 +4497,7 @@ impl Subscriber { let written = stream.writer.buffer_message( ietf::TrackStatus::ID, &ietf::TrackStatus { + authorization_token: None, request_id, track_namespace: broadcast_path.clone(), track_name: track_name.into(), @@ -5627,6 +5687,7 @@ impl Subscriber { let res = stream.writer.buffer_message( ietf::Fetch::ID, &ietf::Fetch { + authorization_token: None, request_id: fetch_id, subscriber_priority: super::priority::to_wire(fill.priority()), group_order: GroupOrder::Ascending, @@ -9870,7 +9931,10 @@ mod tests { }, peer, &mut attached, - None, + Admission { + grant: None, + slot: None, + }, )); for _ in 0..100 { @@ -9919,7 +9983,10 @@ mod tests { }, peer, &mut attached, - None, + Admission { + grant: None, + slot: None, + }, )); // Both updates apply, then the loop parks on the exhausted script. The @@ -9978,7 +10045,12 @@ mod tests { kind: crate::setup::Token::OUT_OF_BAND, value: vec![0x00, 0xff], }; - let verdict = auth.verify_request(token, Some(hold.clone())); + let verdict = auth.verify_request( + token, + Some(hold.clone()), + crate::auth::RequestKind::PublishNamespace, + &path, + ); let _issued = requests.next().await.unwrap().accept(crate::auth::Grant::all()); let answered = verdict.grant().await; let grant = crate::auth::RequestGrant::new( @@ -10008,7 +10080,10 @@ mod tests { }, peer, &mut attached, - Some(grant), + Admission { + grant: Some(grant.clone()), + slot: grant.hold(), + }, )); for _ in 0..20 { assert!( @@ -10080,7 +10155,10 @@ mod tests { }, peer, &mut attached, - None, + Admission { + grant: None, + slot: None, + }, )); for _ in 0..20 { assert!(futures::poll!(run.as_mut()).is_pending(), "the stream stays open"); @@ -10131,7 +10209,10 @@ mod tests { }, peer, &mut attached, - None, + Admission { + grant: None, + slot: None, + }, )); for _ in 0..20 { if let std::task::Poll::Ready(res) = futures::poll!(run.as_mut()) { @@ -10184,7 +10265,10 @@ mod tests { }, peer, &mut attached, - None, + Admission { + grant: None, + slot: None, + }, )); for _ in 0..20 { assert!( @@ -10251,7 +10335,10 @@ mod tests { }, peer, &mut attached, - None, + Admission { + grant: None, + slot: None, + }, )); let mut result = None; for _ in 0..20 { @@ -10329,6 +10416,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(), @@ -10501,7 +10589,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(); @@ -10520,7 +10611,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(); @@ -11196,47 +11290,135 @@ mod tests { } } - /// Where tokens decide, a token arriving on an update for an announce the limit admitted - /// without one is refused rather than taken unread: it ends only that announce, unverified. + /// Poll `run` until it ends, failing if it never does. + async fn run_to_end(mut run: std::pin::Pin<&mut F>) -> F::Output { + for _ in 0..100 { + if let Poll::Ready(res) = futures::poll!(run.as_mut()) { + return res; + } + settle().await; + } + panic!("the announce never ended"); + } + + /// A PUBLISH_NAMESPACE for `path`, presenting no token. + fn tokenless_announce(path: &'static str) -> ietf::PublishNamespace { + ietf::PublishNamespace { + request_id: RequestId(1), + track_namespace: crate::Path::from_static(path), + params: Default::default(), + authorization_token: None, + } + } + + /// A token an update names on an announce the session admitted is refused and never + /// reaches the acceptor: an announce is admitted by the session or by a token, and that + /// never changes. The refusal withdraws the announce. #[kio::test] - async fn a_token_on_an_announce_admitted_without_one_is_refused() { - for version in [Version::Draft18] { + async fn a_token_on_a_session_announce_update_is_refused() { + for version in [Version::Draft18, Version::Draft22] { let (mut subscriber, mut requests, _cred, consumer, session) = auth_announce_harness(version, publish_namespace_update_with_token(version).await); let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), version) .await .unwrap(); - let tokenless = ietf::PublishNamespace { - request_id: RequestId(1), - track_namespace: crate::Path::from_static("other/alice"), - params: Default::default(), - authorization_token: None, - }; - let mut run = std::pin::pin!(subscriber.run_publish_namespace_stream( + let run = std::pin::pin!(subscriber.run_publish_namespace_stream( stream, - tokenless, + tokenless_announce("other/alice"), cluster::Peer::default(), - None, + None )); - - let mut result = None; - for _ in 0..500 { - if let std::task::Poll::Ready(res) = futures::poll!(run.as_mut()) { - result = Some(res); - break; - } - settle().await; - } + run_to_end(run).await.unwrap(); + assert_eq!( + replies(&session.log, ietf::RequestOk::ID), + 1, + "{version}: never admitted" + ); + assert_eq!( + occurrences(&session.log, b"AUTHORIZATION TOKEN on an announce admitted without one"), + 1, + "{version}: the update was not refused" + ); assert!( - matches!(result, Some(Ok(()))), - "{version}: the update's token ends the announce, got {result:?}" + requests.poll_next(&kio::Waiter::noop()).is_pending(), + "{version}: the token reached the acceptor" ); assert!(routed_now(&consumer, "other/alice").is_none(), "{version}"); + assert!(session.log.closes().is_empty(), "{version}"); + } + } + + /// A tokenless PUBLISH_NAMESPACE the limit does not cover is refused UNAUTHORIZED rather + /// than acknowledged and held, so the publisher learns it at once. + #[kio::test] + async fn a_tokenless_announce_outside_the_limit_is_refused() { + for version in [Version::Draft18, Version::Draft22] { + let (mut subscriber, _requests, _cred, consumer, session) = auth_announce_harness(version, Vec::new()); + let probe = subscriber.clone(); + let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), version) + .await + .unwrap(); + let run = std::pin::pin!(subscriber.run_publish_namespace_stream( + stream, + tokenless_announce("room/alice"), + cluster::Peer::default(), + None + )); + run_to_end(run).await.unwrap(); + assert_eq!(replies(&session.log, ietf::RequestOk::ID), 0, "{version}: acknowledged"); + assert_eq!( + replies(&session.log, ietf::RequestError::ID), + 1, + "{version}: not refused" + ); + assert_eq!(occurrences(&session.log, b"unauthorized: room/alice"), 1, "{version}"); + assert!(routed_now(&consumer, "room/alice").is_none(), "{version}"); + assert!(probe.state.lock().broadcasts.is_empty(), "{version}: held"); + assert!(session.log.closes().is_empty(), "{version}"); + } + } + + /// A session-authorized announce ends once the limit stops covering it, rather than being + /// held for a wider limit to bring back: the publisher re-announces. + #[kio::test] + async fn a_narrowed_limit_ends_a_session_announce() { + for version in [Version::Draft18, Version::Draft22] { + let (mut subscriber, _requests, _cred, consumer, session) = auth_announce_harness(version, Vec::new()); + let auth = subscriber.auth.clone(); + let probe = subscriber.clone(); + let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), version) + .await + .unwrap(); + let mut run = std::pin::pin!(subscriber.run_publish_namespace_stream( + stream, + tokenless_announce("other/alice"), + cluster::Peer::default(), + None + )); + for _ in 0..20 { + assert!(futures::poll!(run.as_mut()).is_pending()); + settle().await; + } + assert!(routed_now(&consumer, "other/alice").is_some(), "{version}: admitted"); + + auth.handle().authorize(&crate::auth::Grant::default()); + let res = run_to_end(run).await; + assert!(matches!(res, Err(Error::Unauthorized)), "{version}: {res:?}"); assert!( - requests.poll_next(&kio::Waiter::noop()).is_pending(), - "{version}: the token reached the verifier" + routed_now(&consumer, "other/alice").is_none(), + "{version}: still routed" ); - assert!(session.log.closes().is_empty(), "{version}: the session closed"); + + // Nothing was held for the limit to bring back. + auth.handle().authorize(&crate::auth::Grant { + publish: crate::Pattern::subtree("other").unwrap().into(), + subscribe: crate::Patterns::new(), + expires: None, + }); + settle().await; + assert!(probe.state.lock().broadcasts.is_empty(), "{version}: held"); + assert!(routed_now(&consumer, "other/alice").is_none(), "{version}: came back"); + assert!(session.log.closes().is_empty(), "{version}"); } } @@ -11313,7 +11495,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(); @@ -13033,6 +13218,7 @@ mod stitch_tests { } }; 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 c12b89b13d4..73e5630e5ae 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, @@ -718,6 +719,7 @@ impl Subscriber { return Ok(None); } Ok(Some(Upstream { + token_grant: None, stream, messages, request_id, @@ -755,6 +757,61 @@ 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), + crate::auth::RequestKind::Publish, + &msg.track_namespace, + ); + let session = self.session.clone(); + let verified = kio::wait(|waiter| { + if session.poll_closed(waiter).is_ready() + || request_stream::poll_withdrawn(&mut stream, request_stream::fin_cancels(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_wanted_changed(waiter, true).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) => { + let (kind, err) = (request::Kind::Publish, &refusal.error); + request_stream::buffer_error(&mut stream.writer, kind, err, &refusal.reason, self.version)?; + // Dropping an unfinished writer resets it, which could discard the refusal. + return request_stream::deliver(&mut stream.writer).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; @@ -849,7 +906,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. @@ -868,8 +925,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); @@ -882,6 +941,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, @@ -926,6 +986,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) @@ -1500,6 +1565,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 d962a6f5884..2217c6aef3d 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>, @@ -32,6 +34,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, @@ -50,6 +53,7 @@ impl<'a> Message<'a> for TrackStatus<'a> { // parameter as unknown, which is a protocol violation. let include_properties = (!self.properties_wanted && Filter::is_draft20(version)).then_some(false); encode_params!(w, version, + 0x03 => self.authorization_token.clone(), 0x35 => include_properties, ); Ok(()) @@ -62,6 +66,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, @@ -156,6 +161,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(), @@ -179,6 +185,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/lite/test_transport.rs b/rs/moq-net/src/lite/test_transport.rs index 5071888d88e..483bad4b34d 100644 --- a/rs/moq-net/src/lite/test_transport.rs +++ b/rs/moq-net/src/lite/test_transport.rs @@ -11,7 +11,7 @@ use std::{ sync::{ Arc, Mutex, - atomic::{AtomicUsize, Ordering}, + atomic::{AtomicBool, AtomicUsize, Ordering}, }, task::Poll, }; @@ -78,6 +78,8 @@ pub struct Log { groups: Arc>>, joins: Arc>>, orders: Arc>>, + /// Set by [`Self::fail_writes`]. + writes_fail: Arc, } /// One thing a send stream did, as recorded in [`Log::trail`]. @@ -108,6 +110,12 @@ impl Log { }) } + /// Fail every later write on every send stream, as a peer that stopped reading + /// them (STOP_SENDING) would. + pub fn fail_writes(&self) { + self.writes_fail.store(true, Ordering::Relaxed); + } + /// What every send stream did, in call order, tagged with the stream's id (its /// creation order). Unlike [`Self::writes`] and [`Self::resets`], this keeps the /// order across streams, so a test can check one stream's message against @@ -302,6 +310,12 @@ impl poll::SendStream for SinkSend { type Group = SinkGroup; fn poll_write(&mut self, waiter: &kio::Waiter, buf: &[u8]) -> Poll> { + if self.log.writes_fail.load(Ordering::Relaxed) { + return Poll::Ready(Err(SinkError { + stream: Some(0), + ..Default::default() + })); + } if self.prefix == 0 && let Some(gate) = &self.gate { diff --git a/rs/moq-net/src/server.rs b/rs/moq-net/src/server.rs index 32d5037f220..9c7dfe52320 100644 --- a/rs/moq-net/src/server.rs +++ b/rs/moq-net/src/server.rs @@ -1162,8 +1162,8 @@ mod tests { /// An acceptor taken with `Handshake::requests()` BEFORE `ok()` must govern the /// session the driver then runs, through the REAL accept path (`accept_request` -> `ok`). - /// The session's own `auth()` (the handle the driver polls) routes a request token to that - /// pre-ok consumer instead of falling to the `Unsupported` default, and granting admits. + /// Its auth link routes a request token to that pre-ok consumer after `ok()`, instead of + /// falling to the `Unsupported` default, and granting admits. /// A QUIC server has no other point to install the verifier. #[kio::test] async fn a_pre_ok_verifier_governs_the_session() { @@ -1180,17 +1180,18 @@ mod tests { // Install the verifier before ok(), as a QUIC server must. let mut requests = request.requests(); - let (net_session, _driver) = request.ok().await.unwrap_or_else(|e| panic!("ok at {version:?}: {e}")); + let auth = request.auth.clone(); + let (_net_session, _driver) = request.ok().await.unwrap_or_else(|e| panic!("ok at {version:?}: {e}")); - // The session's own handle is the one the driver polls. Verifying a request token on - // it must reach the pre-ok consumer; if the handle were not shared it would fall to - // the Unsupported default and the consumer would never see this request. - let verdict = net_session.auth().verify_request( + // The same link passed to the driver must still reach the pre-ok consumer. + let verdict = auth.verify_request( crate::setup::Token { kind: crate::setup::Token::OUT_OF_BAND, value: b"jwt".to_vec(), }, None, + crate::auth::RequestKind::Subscribe, + &crate::Path::from_static("room"), ); let received = requests.next().await.expect("the pre-ok consumer receives the request"); assert_eq!(received.token().as_ref(), b"jwt", "{version:?}"); diff --git a/rs/moq-net/src/test_interop.rs b/rs/moq-net/src/test_interop.rs index 7e7964ff2dd..4d9082c5612 100644 --- a/rs/moq-net/src/test_interop.rs +++ b/rs/moq-net/src/test_interop.rs @@ -635,6 +635,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 032b37c1b32..a62e44c3d9f 100644 --- a/rs/moq-relay/src/auth.rs +++ b/rs/moq-relay/src/auth.rs @@ -32,7 +32,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 06ad9a296d1..fbbbad0c383 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