From 67c96f3626755a7ab1b7fde14ed636342019f981 Mon Sep 17 00:00:00 2001 From: Ginger Date: Thu, 5 Nov 2026 12:40:34 -0500 Subject: [PATCH] refactor: Add function to check user profile visibility --- src/api/client/profile.rs | 32 ++++++++++++++++------ src/service/users/mod.rs | 2 +- src/service/users/profile.rs | 51 ++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/api/client/profile.rs b/src/api/client/profile.rs index 4f2d6bedc..28399cf87 100644 --- a/src/api/client/profile.rs +++ b/src/api/client/profile.rs @@ -29,10 +29,18 @@ pub(crate) async fn get_profile_route( State(services): State, body: Ruma, ) -> Result { - if services.config.require_auth_for_profile_requests && body.identity.is_none() { - return Err!(Request(Unauthorized( - "This server requires authentication to access user profiles." - ))); + let querying_user = body + .identity + .as_ref() + .and_then(|identity| identity.sender_user()) + .into(); + + if !services + .users + .user_can_see_profile(&body.user_id, querying_user) + .await + { + return Err!(Request(Unauthorized("You cannot access this user's profile."))); } let Some(profile) = fetch_full_profile(&services, &body.user_id).await else { @@ -46,10 +54,18 @@ pub(crate) async fn get_profile_field_route( State(services): State, body: Ruma, ) -> Result { - if services.config.require_auth_for_profile_requests && body.identity.is_none() { - return Err!(Request(Unauthorized( - "This server requires authentication to access user profiles." - ))); + let querying_user = body + .identity + .as_ref() + .and_then(|identity| identity.sender_user()) + .into(); + + if !services + .users + .user_can_see_profile(&body.user_id, querying_user) + .await + { + return Err!(Request(Unauthorized("You cannot access this user's profile."))); } let value = fetch_profile_field(&services, &body.user_id, body.field.clone()).await?; diff --git a/src/service/users/mod.rs b/src/service/users/mod.rs index a09396a0e..3099cb625 100644 --- a/src/service/users/mod.rs +++ b/src/service/users/mod.rs @@ -12,7 +12,7 @@ pub use account::{AccessTokenStatus, AccountStatus}; use conduwuit::{Err, Error, Result, err, utils}; use database::Map; pub use device::{DeviceToken, PreviousTokenAction}; -pub use profile::ProfileFieldChange; +pub use profile::{ProfileFieldChange, ProfileQuerier}; use ruma::{UserId, api::error::ErrorKind, encryption::CrossSigningKey, serde::Raw}; use serde::{Deserialize, Serialize}; diff --git a/src/service/users/profile.rs b/src/service/users/profile.rs index cad67765c..2ce0da681 100644 --- a/src/service/users/profile.rs +++ b/src/service/users/profile.rs @@ -39,6 +39,30 @@ impl ProfileFieldChange { } } +/// The entity querying a user's profile. +#[derive(Clone, Copy, Default)] +pub enum ProfileQuerier<'id> { + /// The user owning the profile. This variant exists to improve the + /// ergonomics of code that looks up a user's own profile, so the user's + /// ID doesn't need to be passed twice. + Owner, + /// Another user, possibly but not necessarily the user owning the profile. + Other(&'id UserId), + /// An anonymous entity. + #[default] + Anonymous, +} + +impl<'id> From> for ProfileQuerier<'id> { + fn from(value: Option<&'id UserId>) -> Self { + if let Some(user_id) = value { + Self::Other(user_id) + } else { + Self::Anonymous + } + } +} + impl super::Service { pub async fn set_profile_field( &self, @@ -360,4 +384,31 @@ impl super::Service { .ready_for_each(|(key, _)| self.set_profile_key(user_id, &key, None)) .await; } + + /// Check if a user can access another user's profile. + // This function doesn't do much of anything right now, but future MSCs that + // add profile access controls should be implemented here. + pub fn user_can_see_profile( + &self, + target_user: &UserId, + querying_user: ProfileQuerier<'_>, + ) -> impl Future { + std::future::ready(match querying_user { + | ProfileQuerier::Anonymous => { + // For unauthenticated requests we check the relevant config + // option. + !self.services.config.require_auth_for_profile_requests + }, + | ProfileQuerier::Other(querying_user) if querying_user != target_user => { + // Currently users can't control their own profile visibility, + // so we always return true for authenticated + // requests from other users. + true + }, + | ProfileQuerier::Owner | ProfileQuerier::Other(_) => { + // Users can always query their own profile. + true + }, + }) + } }