From 49207a42f8b18a03250fc0caa64308ec589dfdca Mon Sep 17 00:00:00 2001 From: timedout Date: Sun, 15 Nov 2026 15:44:13 +0000 Subject: [PATCH] fix: Work around MSC4484 lockout when disabled --- src/api/router/auth.rs | 20 ++++++++++---------- src/service/oauth/grant.rs | 4 ++-- src/service/oauth/mod.rs | 6 ++---- src/web/pages/oauth/grant.rs | 7 ++----- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/api/router/auth.rs b/src/api/router/auth.rs index 6d666bebb..0569ebfbf 100644 --- a/src/api/router/auth.rs +++ b/src/api/router/auth.rs @@ -340,19 +340,19 @@ async fn check_access_token( .map(Box::new); // Make sure the user has the right scopes to use the route - let user_scopes = if let Some(session_info) = &session_info { + let mut user_scopes = if let Some(session_info) = &session_info { session_info.scopes() } else { - let mut scopes = BTreeSet::from_iter([OAuthClientScope::ApiFullAccess]); - - if services.config.experiments.msc4484.is_enabled() - && services.admin.user_is_admin(&sender_user).await - { - scopes.insert(OAuthClientScope::ServerAdministration); - } - - scopes + BTreeSet::from_iter([OAuthClientScope::ApiFullAccess]) }; + if (!services.config.experiments.msc4484.is_enabled() || session_info.is_none()) + && services.admin.user_is_admin(&sender_user).await + { + // This is required to make sure admin APIs still work when msc4484 + // is disabled. + // https://forgejo.ellis.link/continuwuation/continuwuity/pulls/2533#issuecomment-40042 + user_scopes.insert(OAuthClientScope::ServerAdministration); + } if !required_scopes .iter() diff --git a/src/service/oauth/grant.rs b/src/service/oauth/grant.rs index 806409213..1eccc8f98 100644 --- a/src/service/oauth/grant.rs +++ b/src/service/oauth/grant.rs @@ -107,7 +107,7 @@ pub struct RawScopes(String); impl RawScopes { #[allow(clippy::trivial_regex)] - pub fn to_scopes(&self, msc4484: bool) -> Result { + pub fn to_scopes(&self) -> Result { let client_api_token_regex = regex!(r"urn:matrix:(client|org.matrix.msc2967.client):api:\*"); let device_token_regex = @@ -121,7 +121,7 @@ impl RawScopes { let scope_was_new = { if client_api_token_regex.is_match(token) { scopes.scopes.insert(OAuthClientScope::ApiFullAccess) - } else if msc4484 && server_administration_regex.is_match(token) { + } else if server_administration_regex.is_match(token) { scopes.scopes.insert(OAuthClientScope::ServerAdministration) } else if let Some(captures) = device_token_regex.captures(token) { scopes diff --git a/src/service/oauth/mod.rs b/src/service/oauth/mod.rs index adf7111a9..e0d118b2a 100644 --- a/src/service/oauth/mod.rs +++ b/src/service/oauth/mod.rs @@ -286,9 +286,7 @@ impl Service { } } - let requested_scopes = query - .scope - .to_scopes(self.services.config.experiments.msc4484.is_enabled())?; + let requested_scopes = query.scope.to_scopes()?; let redirect_uri_query_separator = match query.response_mode { | ResponseMode::Fragment => '#', @@ -347,7 +345,7 @@ impl Service { let requested_scopes = query .scope - .to_scopes(self.services.config.experiments.msc4484.is_enabled()) + .to_scopes() .map_err(|err| OAuthError::new(ErrorCode::InvalidGrant, err))?; let device_code = Self::generate_token(); diff --git a/src/web/pages/oauth/grant.rs b/src/web/pages/oauth/grant.rs index c815cda6e..1cb268102 100644 --- a/src/web/pages/oauth/grant.rs +++ b/src/web/pages/oauth/grant.rs @@ -4,7 +4,6 @@ use axum::{ response::Redirect, routing::on, }; -use conduwuit_core::config::experiments::ToggledExperiment; use conduwuit_service::oauth::{ SessionInfo, client_metadata::ClientMetadata, @@ -105,10 +104,8 @@ async fn route_authorization_code( return Err(WebError::BadRequest("Invalid client ID".to_owned())); }; - let RequestedScopes { device_id, mut scopes } = query - .scope - .to_scopes(services.config.experiments.msc4484.is_enabled()) - .map_err(WebError::BadRequest)?; + let RequestedScopes { device_id, mut scopes } = + query.scope.to_scopes().map_err(WebError::BadRequest)?; let existing_session_info = if let Some(device_id) = &device_id { services