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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/bin/relay/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions quest/m1/auth/token-in-band.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 30 additions & 7 deletions rs/moq-auth/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -187,8 +188,12 @@ fn authorize(rules: &Permissions, path: &str) -> Result<Permissions, Refusal> {
}

/// 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<Option<&str>, 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)
Expand Down Expand Up @@ -643,18 +648,36 @@ 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())),
..Default::default()
};
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]
Expand Down
Loading