diff --git a/crates/im-web/src/oidc/token.rs b/crates/im-web/src/oidc/token.rs index a219942..01f79f2 100644 --- a/crates/im-web/src/oidc/token.rs +++ b/crates/im-web/src/oidc/token.rs @@ -315,6 +315,19 @@ async fn grant_refresh(app: &AppState, client: &Client, pairs: &Pairs) -> Result if row.client_id != client.id { return Ok(invalid_grant()); } + // A refresh keeps the scope of its grant. A scope parameter is refused unless it stays inside that grant, + // so a client never believes it widened or changed its token. The token is not spent on a refusal. + if pairs.iter().any(|(name, _)| name == "scope") { + let within = one(pairs, "scope").is_some_and(|requested| { + requested + .split(' ') + .filter(|s| !s.is_empty()) + .all(|s| row.scope.split(' ').any(|g| g == s)) + }); + if !within { + return Ok(oauth_error(StatusCode::BAD_REQUEST, "invalid_scope")); + } + } if let Some(response) = throttled( app, &token_family_key(&client.id, &row.family_id),