diff --git a/Cargo.lock b/Cargo.lock index 48376f542a8..7431424346e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5065,7 +5065,7 @@ dependencies = [ [[package]] name = "ruma" version = "0.10.1" -source = "git+https://github.com/matrix-org/ruma?rev=79289381444e886c454302e1e8f5b5f62f249a57#79289381444e886c454302e1e8f5b5f62f249a57" +source = "git+https://github.com/matrix-org/ruma?rev=e87f1d839e3821f0fb0ba20fb3164fedbc90a25c#e87f1d839e3821f0fb0ba20fb3164fedbc90a25c" dependencies = [ "assign", "js_int", @@ -5082,7 +5082,7 @@ dependencies = [ [[package]] name = "ruma-client-api" version = "0.18.0" -source = "git+https://github.com/matrix-org/ruma?rev=79289381444e886c454302e1e8f5b5f62f249a57#79289381444e886c454302e1e8f5b5f62f249a57" +source = "git+https://github.com/matrix-org/ruma?rev=e87f1d839e3821f0fb0ba20fb3164fedbc90a25c#e87f1d839e3821f0fb0ba20fb3164fedbc90a25c" dependencies = [ "as_variant", "assign", @@ -5105,7 +5105,7 @@ dependencies = [ [[package]] name = "ruma-common" version = "0.13.0" -source = "git+https://github.com/matrix-org/ruma?rev=79289381444e886c454302e1e8f5b5f62f249a57#79289381444e886c454302e1e8f5b5f62f249a57" +source = "git+https://github.com/matrix-org/ruma?rev=e87f1d839e3821f0fb0ba20fb3164fedbc90a25c#e87f1d839e3821f0fb0ba20fb3164fedbc90a25c" dependencies = [ "as_variant", "base64 0.22.1", @@ -5137,7 +5137,7 @@ dependencies = [ [[package]] name = "ruma-events" version = "0.28.1" -source = "git+https://github.com/matrix-org/ruma?rev=79289381444e886c454302e1e8f5b5f62f249a57#79289381444e886c454302e1e8f5b5f62f249a57" +source = "git+https://github.com/matrix-org/ruma?rev=e87f1d839e3821f0fb0ba20fb3164fedbc90a25c#e87f1d839e3821f0fb0ba20fb3164fedbc90a25c" dependencies = [ "as_variant", "indexmap 2.2.6", @@ -5162,7 +5162,7 @@ dependencies = [ [[package]] name = "ruma-federation-api" version = "0.9.0" -source = "git+https://github.com/matrix-org/ruma?rev=79289381444e886c454302e1e8f5b5f62f249a57#79289381444e886c454302e1e8f5b5f62f249a57" +source = "git+https://github.com/matrix-org/ruma?rev=e87f1d839e3821f0fb0ba20fb3164fedbc90a25c#e87f1d839e3821f0fb0ba20fb3164fedbc90a25c" dependencies = [ "http 1.1.0", "js_int", @@ -5176,7 +5176,7 @@ dependencies = [ [[package]] name = "ruma-html" version = "0.2.0" -source = "git+https://github.com/matrix-org/ruma?rev=79289381444e886c454302e1e8f5b5f62f249a57#79289381444e886c454302e1e8f5b5f62f249a57" +source = "git+https://github.com/matrix-org/ruma?rev=e87f1d839e3821f0fb0ba20fb3164fedbc90a25c#e87f1d839e3821f0fb0ba20fb3164fedbc90a25c" dependencies = [ "as_variant", "html5ever", @@ -5188,7 +5188,7 @@ dependencies = [ [[package]] name = "ruma-identifiers-validation" version = "0.9.5" -source = "git+https://github.com/matrix-org/ruma?rev=79289381444e886c454302e1e8f5b5f62f249a57#79289381444e886c454302e1e8f5b5f62f249a57" +source = "git+https://github.com/matrix-org/ruma?rev=e87f1d839e3821f0fb0ba20fb3164fedbc90a25c#e87f1d839e3821f0fb0ba20fb3164fedbc90a25c" dependencies = [ "js_int", "thiserror", @@ -5197,7 +5197,7 @@ dependencies = [ [[package]] name = "ruma-macros" version = "0.13.0" -source = "git+https://github.com/matrix-org/ruma?rev=79289381444e886c454302e1e8f5b5f62f249a57#79289381444e886c454302e1e8f5b5f62f249a57" +source = "git+https://github.com/matrix-org/ruma?rev=e87f1d839e3821f0fb0ba20fb3164fedbc90a25c#e87f1d839e3821f0fb0ba20fb3164fedbc90a25c" dependencies = [ "once_cell", "proc-macro-crate", @@ -5212,7 +5212,7 @@ dependencies = [ [[package]] name = "ruma-push-gateway-api" version = "0.9.0" -source = "git+https://github.com/matrix-org/ruma?rev=79289381444e886c454302e1e8f5b5f62f249a57#79289381444e886c454302e1e8f5b5f62f249a57" +source = "git+https://github.com/matrix-org/ruma?rev=e87f1d839e3821f0fb0ba20fb3164fedbc90a25c#e87f1d839e3821f0fb0ba20fb3164fedbc90a25c" dependencies = [ "js_int", "ruma-common", diff --git a/Cargo.toml b/Cargo.toml index cc3c89556eb..57da932622e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,7 +47,7 @@ once_cell = "1.16.0" pin-project-lite = "0.2.9" rand = "0.8.5" reqwest = { version = "0.12.4", default-features = false } -ruma = { git = "https://github.com/matrix-org/ruma", rev = "79289381444e886c454302e1e8f5b5f62f249a57", features = [ +ruma = { git = "https://github.com/matrix-org/ruma", rev = "e87f1d839e3821f0fb0ba20fb3164fedbc90a25c", features = [ "client-api-c", "compat-upload-signatures", "compat-user-id", @@ -61,7 +61,7 @@ ruma = { git = "https://github.com/matrix-org/ruma", rev = "79289381444e886c4543 "unstable-msc4075", "unstable-msc4140", ] } -ruma-common = { git = "https://github.com/matrix-org/ruma", rev = "79289381444e886c454302e1e8f5b5f62f249a57" } +ruma-common = { git = "https://github.com/matrix-org/ruma", rev = "e87f1d839e3821f0fb0ba20fb3164fedbc90a25c" } serde = "1.0.151" serde_html_form = "0.2.0" serde_json = "1.0.91" diff --git a/crates/matrix-sdk-base/src/rooms/mod.rs b/crates/matrix-sdk-base/src/rooms/mod.rs index 738d5110116..264e00052fb 100644 --- a/crates/matrix-sdk-base/src/rooms/mod.rs +++ b/crates/matrix-sdk-base/src/rooms/mod.rs @@ -40,7 +40,7 @@ use ruma::{ RedactedStateEventContent, StaticStateEventContent, SyncStateEvent, }, room::RoomType, - EventId, OwnedUserId, RoomVersionId, + EventId, OwnedUserId, RoomVersionId, UserId, }; use serde::{Deserialize, Serialize}; @@ -195,9 +195,12 @@ impl BaseRoomInfo { let mut o_ev = o_ev.clone(); o_ev.content.set_created_ts_if_none(o_ev.origin_server_ts); + let Some(owned_user_id) = get_user_id_for_state_key(m.state_key()) else { + return false; + }; + // add the new event. - self.rtc_member - .insert(m.state_key().clone(), SyncStateEvent::Original(o_ev).into()); + self.rtc_member.insert(owned_user_id, SyncStateEvent::Original(o_ev).into()); // Remove all events that don't contain any memberships anymore. self.rtc_member.retain(|_, ev| { @@ -319,6 +322,33 @@ impl BaseRoomInfo { } } +/// Extract a user ID from a state key that matches one of these formats: +/// - `` +/// - `_` +/// - `__` +fn get_user_id_for_state_key(state_key: &str) -> Option { + if let Ok(user_id) = UserId::parse(state_key) { + return Some(user_id); + } + + // Ignore leading underscore if present + // (used for avoiding auth rules on @-prefixed state keys) + let state_key = state_key.strip_prefix('_').unwrap_or(state_key); + if state_key.starts_with('@') { + if let Some(colon_idx) = state_key.find(':') { + let state_key_user_id = match state_key[colon_idx + 1..].find('_') { + None => state_key, + Some(suffix_idx) => &state_key[..colon_idx + 1 + suffix_idx], + }; + if let Ok(user_id) = UserId::parse(state_key_user_id) { + return Some(user_id); + } + } + } + + None +} + bitflags! { /// Notable tags, i.e. subset of tags that we are more interested by. /// @@ -536,9 +566,12 @@ impl RoomMemberships { mod tests { use std::ops::Not; - use ruma::events::tag::{TagInfo, TagName, Tags}; + use ruma::{ + events::tag::{TagInfo, TagName, Tags}, + user_id, + }; - use super::{BaseRoomInfo, RoomNotableTags}; + use super::{get_user_id_for_state_key, BaseRoomInfo, RoomNotableTags}; #[test] fn test_handle_notable_tags_favourite() { @@ -569,4 +602,32 @@ mod tests { base_room_info.handle_notable_tags(&tags); assert!(base_room_info.notable_tags.contains(RoomNotableTags::LOW_PRIORITY).not()); } + + #[test] + fn test_get_user_id_for_state_key() { + assert!(get_user_id_for_state_key("").is_none()); + assert!(get_user_id_for_state_key("abc").is_none()); + assert!(get_user_id_for_state_key("@nocolon").is_none()); + assert!(get_user_id_for_state_key("@noserverpart:").is_none()); + assert!(get_user_id_for_state_key("@noserverpart:_suffix").is_none()); + + let user_id = user_id!("@username:example.org"); + + assert_eq!(get_user_id_for_state_key(user_id.as_str()).as_deref(), Some(user_id)); + assert_eq!( + get_user_id_for_state_key(format!("{user_id}_valid_suffix").as_str()).as_deref(), + Some(user_id) + ); + assert!(get_user_id_for_state_key(format!("{user_id}:invalid_suffix").as_str()).is_none()); + + assert_eq!( + get_user_id_for_state_key(format!("_{user_id}").as_str()).as_deref(), + Some(user_id) + ); + assert_eq!( + get_user_id_for_state_key(format!("_{user_id}_valid_suffix").as_str()).as_deref(), + Some(user_id) + ); + assert!(get_user_id_for_state_key(format!("_{user_id}:invalid_suffix").as_str()).is_none()); + } } diff --git a/crates/matrix-sdk-base/src/rooms/normal.rs b/crates/matrix-sdk-base/src/rooms/normal.rs index 581d8dacae9..52fae8e89d8 100644 --- a/crates/matrix-sdk-base/src/rooms/normal.rs +++ b/crates/matrix-sdk-base/src/rooms/normal.rs @@ -2624,7 +2624,7 @@ mod tests { // we can simply use now here since this will be dropped when using a MinimalStateEvent // in the roomInfo origin_server_ts: timestamp(0), - state_key: user_id.to_owned(), + state_key: user_id.to_string(), unsigned: StateUnsigned::new(), })) }