mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-12-03 13:53:21 +00:00
fix: Use a SyncMutex and constant time string comparison
This commit is contained in:
2 files changed
+14
-15
No files matched your search
@@ -432,7 +432,7 @@ async fn check_access_token(
|
||||
sender_device,
|
||||
appservice_info: Box::new(appservice_info),
|
||||
})
|
||||
} else if let Some(session) = services.oauth.get_client_credentials_session(token).await {
|
||||
} else if let Some(session) = services.oauth.get_client_credentials_session(token) {
|
||||
if !required_scopes
|
||||
.iter()
|
||||
.any(|scope| session.scopes.contains(scope))
|
||||
|
||||
+13
-14
@@ -7,10 +7,10 @@ use std::{
|
||||
|
||||
use base64::Engine;
|
||||
use conduwuit::{
|
||||
Result, SyncMutex,
|
||||
Result, SyncMappedMutexGuard, SyncMutex, SyncMutexGuard,
|
||||
config::experiments::ToggledExperiment,
|
||||
error, info,
|
||||
utils::{self, MutexMap, hash::sha256},
|
||||
utils::{self, MutexMap, hash::sha256, string::constant_time_eq},
|
||||
warn,
|
||||
};
|
||||
use database::{Deserialized, Json, Map};
|
||||
@@ -54,7 +54,7 @@ pub struct Service {
|
||||
tickets: SyncMutex<HashMap<String, HashMap<OAuthTicket, SystemTime>>>,
|
||||
pending_auth_code_grants: SyncMutex<LruCache<String, PendingAuthCodeGrant>>,
|
||||
pending_device_code_grants: SyncMutex<LruCache<String, PendingDeviceCodeGrant>>,
|
||||
client_credential_sessions: tokio::sync::Mutex<HashMap<String, ClientCredentialsSession>>,
|
||||
client_credential_sessions: SyncMutex<HashMap<String, ClientCredentialsSession>>,
|
||||
}
|
||||
|
||||
struct Data {
|
||||
@@ -209,7 +209,7 @@ impl crate::Service for Service {
|
||||
tickets: SyncMutex::default(),
|
||||
pending_auth_code_grants: SyncMutex::new(LruCache::new(Self::MAX_PENDING_GRANTS)),
|
||||
pending_device_code_grants: SyncMutex::new(LruCache::new(Self::MAX_PENDING_GRANTS)),
|
||||
client_credential_sessions: tokio::sync::Mutex::default(),
|
||||
client_credential_sessions: SyncMutex::default(),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -597,8 +597,7 @@ impl Service {
|
||||
| TokenRequestType::RefreshToken { refresh_token } =>
|
||||
self.refresh_session(client_id, refresh_token).await,
|
||||
| TokenRequestType::ClientCredentials { client_secret, scope: scopes } =>
|
||||
self.create_client_credentials_token(&client_id, &client_secret, scopes)
|
||||
.await,
|
||||
self.create_client_credentials_token(&client_id, &client_secret, &scopes),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -884,11 +883,11 @@ impl Service {
|
||||
})
|
||||
}
|
||||
|
||||
async fn create_client_credentials_token(
|
||||
fn create_client_credentials_token(
|
||||
&self,
|
||||
client_id: &str,
|
||||
client_secret: &str,
|
||||
scopes: RawScopes,
|
||||
scopes: &RawScopes,
|
||||
) -> Result<TokenResponse, OAuthError> {
|
||||
let now = SystemTime::now();
|
||||
|
||||
@@ -907,7 +906,7 @@ impl Service {
|
||||
));
|
||||
}
|
||||
|
||||
if client_secret != client.client_secret {
|
||||
if !constant_time_eq(client_secret, &client.client_secret) {
|
||||
return Err(OAuthError::new_static(
|
||||
ErrorCode::InvalidClient,
|
||||
"Client secret does not match",
|
||||
@@ -921,7 +920,7 @@ impl Service {
|
||||
));
|
||||
}
|
||||
|
||||
let mut sessions = self.client_credential_sessions.lock().await;
|
||||
let mut sessions = self.client_credential_sessions.lock();
|
||||
|
||||
// Clear expired sessions while we're at it
|
||||
sessions.retain(|_, session| {
|
||||
@@ -947,13 +946,13 @@ impl Service {
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get_client_credentials_session(
|
||||
pub fn get_client_credentials_session(
|
||||
&self,
|
||||
token: &str,
|
||||
) -> Option<tokio::sync::MappedMutexGuard<'_, ClientCredentialsSession>> {
|
||||
let sessions = self.client_credential_sessions.lock().await;
|
||||
) -> Option<SyncMappedMutexGuard<'_, ClientCredentialsSession>> {
|
||||
let sessions = self.client_credential_sessions.lock();
|
||||
|
||||
tokio::sync::MutexGuard::try_map(sessions, |sessions| {
|
||||
SyncMutexGuard::try_map(sessions, |sessions| {
|
||||
sessions.get_mut(token).filter(|session| {
|
||||
SystemTime::now()
|
||||
.duration_since(session.created_at)
|
||||
|
||||
Reference in new issue
Block a user