From 4933d328247b2d2a291bc5b046e96f964f702955 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 28 Sep 2026 06:45:15 -0700 Subject: [PATCH] fix(auth): admit a SETUP token equal to the jwt query moq auth serve refused any session presenting both a SETUP token and a jwt query. A client offering a version without in-band auth copies its SETUP token into the URL too, so the same JWT arrives twice. Identical type-0 values are now verified once; different values, or a SETUP token of another type beside a jwt, are still refused with TwoTokens. Co-Authored-By: Claude Opus 5.5 --- doc/bin/relay/auth.md | 3 ++- quest/m1/auth/token-in-band.md | 8 +++----- rs/moq-auth/src/serve.rs | 37 +++++++++++++++++++++++++++------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/doc/bin/relay/auth.md b/doc/bin/relay/auth.md index 26dcc47f0c6..ec3dbe0f183 100644 --- a/doc/bin/relay/auth.md +++ b/doc/bin/relay/auth.md @@ -284,7 +284,8 @@ against `--key FILE` or `--key-dir DIR` (by `kid`, read per request so rotation needs no restart) and authorized at the dialed path, as in [Path matching](#path-matching). A malformed, expired, or unknown-key token is refused; it never falls through to the anonymous rules. So is a SETUP token of -any other `kind`, and a session presenting both a SETUP token and a `jwt` query. +any other `kind`. A SETUP token and a `jwt` query with the same value are one +JWT, verified once; different values are refused. A JWT presented with a certificate is refused, because neither can safely win: the certificate would override a JWT meant to narrow it, and the JWT would narrow or refuse a peer by accident. So a peer presents `cluster.token` or a diff --git a/quest/m1/auth/token-in-band.md b/quest/m1/auth/token-in-band.md index 51aa2562312..59bf43eb58f 100644 --- a/quest/m1/auth/token-in-band.md +++ b/quest/m1/auth/token-in-band.md @@ -45,11 +45,9 @@ AUTH can carry the full grant once the pattern-interest prerequisite lands. the first token also rides the AUTHORIZATION TOKEN setup option (`ietf::token::into_setup`, `USE_VALUE`, token type 0), which scopes at accept the way the URL does. While the URL also carries it, the - auth server sees the same credential twice. Decided by the maintainer (on - #4211): `moq auth serve` admits a SETUP token equal to the `?jwt=` value - and refuses only two different credentials. #4278 shipped refusing both - (`serve::Refusal::TwoTokens`, pinned by a test), so this quest changes that - and its test. Never send different values in the two places. + auth server sees the same credential twice: `moq auth serve` admits a SETUP + token equal to the `?jwt=` value and refuses two different ones + (`serve::Refusal::TwoTokens`). Never send different values in the two places. - The relay admits on the URL, then widens. An anonymous connection today is admitted with the public grant when one is configured and refused otherwise; with this quest a connection with no URL credential and no diff --git a/rs/moq-auth/src/serve.rs b/rs/moq-auth/src/serve.rs index 2a55c60ba1f..35bd8aeaa8b 100644 --- a/rs/moq-auth/src/serve.rs +++ b/rs/moq-auth/src/serve.rs @@ -59,8 +59,9 @@ pub struct Limits { /// each scoped to `anon/`, and refuses one dialed at `/other`. /// /// The JWT is the `jwt` query parameter or a moq-transport SETUP token of type 0 -/// ([`Token::OUT_OF_BAND`]), verified alike. A session presenting both is refused, -/// as is a SETUP token of any other type, and one presenting a JWT alongside a +/// ([`Token::OUT_OF_BAND`]), verified alike. A session presenting both is verified +/// once when they are the same JWT and refused when they differ, as is a SETUP token +/// of any other type, and one presenting a JWT alongside a /// certificate: neither can safely win, since a certificate would override a JWT /// meant to narrow it, and a JWT would narrow or refuse a peer by accident. /// @@ -112,7 +113,7 @@ pub enum Refusal { TokenLimit, #[error("too many live sessions from this address")] RemoteLimit, - #[error("both a SETUP token and a `jwt` query were presented; present one")] + #[error("a SETUP token and a different `jwt` query were presented; present one")] TwoTokens, #[error("SETUP token type {0:#x} is not supported; only type 0 (a JWT) is")] UnsupportedToken(u64), @@ -187,8 +188,12 @@ fn authorize(rules: &Permissions, path: &str) -> Result { } /// The JWT the request presents: its SETUP token, or else its `jwt` query parameter. +/// Both at once count as one only when they are the same JWT. fn jwt(request: &Request) -> Result, Refusal> { match (&request.token, query_jwt(request)) { + // A client that offers a version without in-band auth copies its SETUP token + // into the URL too, so the same JWT arrives twice. + (Some(token), Some(jwt)) if token.kind == Token::OUT_OF_BAND && token.value == jwt.as_bytes() => Ok(Some(jwt)), (Some(_), Some(_)) => Err(Refusal::TwoTokens), (Some(token), None) if token.kind == Token::OUT_OF_BAND => std::str::from_utf8(&token.value) .map(Some) @@ -643,9 +648,9 @@ mod tests { ); } - /// Two credentials would leave the server guessing which one the client meant. + /// The same JWT in the SETUP token and the query is one credential, evaluated once. #[tokio::test] - async fn a_setup_token_and_a_query_jwt_are_refused() { + async fn a_setup_token_equal_to_the_query_jwt_is_admitted() { let (dir, key) = key_dir(); let policy = Policy { keys: Some(Keys::Dir(dir.path().into())), @@ -653,8 +658,26 @@ mod tests { }; let jwt = sign(&key, "demo", &["**"], &[], None); let request = with_setup_token(with_token(request("/demo"), &jwt), Token::OUT_OF_BAND, &jwt); - let err = policy.decide(&request).await.unwrap_err(); - assert_eq!(err, Refusal::TwoTokens); + let grant = policy.decide(&request).await.unwrap(); + assert_eq!(grant.publish, patterns(&["**"])); + } + + /// Two different credentials would leave the server guessing which one the client meant. + #[tokio::test] + async fn a_setup_token_and_a_different_query_jwt_are_refused() { + let (dir, key) = key_dir(); + let policy = Policy { + keys: Some(Keys::Dir(dir.path().into())), + ..Default::default() + }; + let setup = sign(&key, "demo", &["**"], &[], None); + let query = sign(&key, "demo", &[], &["**"], None); + let different = with_setup_token(with_token(request("/demo"), &query), Token::OUT_OF_BAND, &setup); + assert_eq!(policy.decide(&different).await.unwrap_err(), Refusal::TwoTokens); + + // The same bytes under another token type are still a second credential. + let other_type = with_setup_token(with_token(request("/demo"), &setup), Token::CAT, &setup); + assert_eq!(policy.decide(&other_type).await.unwrap_err(), Refusal::TwoTokens); } #[tokio::test]