fix(appservices): don't forward events relating to remote users, and forward events relating to remote aliases

This commit is contained in:
Matthias Ahouansou 2024-04-27 20:41:28 +01:00
parent bd5d9a7560
commit df0ad2d07c
No known key found for this signature in database
2 changed files with 37 additions and 16 deletions

View file

@ -87,11 +87,13 @@ pub struct RegistrationInfo {
} }
impl RegistrationInfo { impl RegistrationInfo {
/// Checks if a given user ID matches either the users namespace or the localpart specified in the appservice registration
pub fn is_user_match(&self, user_id: &UserId) -> bool { pub fn is_user_match(&self, user_id: &UserId) -> bool {
self.users.is_match(user_id.as_str()) self.users.is_match(user_id.as_str())
|| self.registration.sender_localpart == user_id.localpart() || self.registration.sender_localpart == user_id.localpart()
} }
/// Checks if a given user ID exclusively matches either the users namespace or the localpart specified in the appservice registration
pub fn is_exclusive_user_match(&self, user_id: &UserId) -> bool { pub fn is_exclusive_user_match(&self, user_id: &UserId) -> bool {
self.users.is_exclusive_match(user_id.as_str()) self.users.is_exclusive_match(user_id.as_str())
|| self.registration.sender_localpart == user_id.localpart() || self.registration.sender_localpart == user_id.localpart()

View file

@ -14,7 +14,8 @@ use ruma::{
events::{ events::{
push_rules::PushRulesEvent, push_rules::PushRulesEvent,
room::{ room::{
create::RoomCreateEventContent, encrypted::Relation, member::MembershipState, canonical_alias::RoomCanonicalAliasEventContent, create::RoomCreateEventContent,
encrypted::Relation, member::MembershipState,
power_levels::RoomPowerLevelsEventContent, redaction::RoomRedactionEventContent, power_levels::RoomPowerLevelsEventContent, redaction::RoomRedactionEventContent,
}, },
GlobalAccountDataEventType, StateEventType, TimelineEventType, GlobalAccountDataEventType, StateEventType, TimelineEventType,
@ -32,10 +33,7 @@ use tracing::{error, info, warn};
use crate::{ use crate::{
api::server_server, api::server_server,
service::{ service::pdu::{EventHash, PduBuilder},
appservice::NamespaceRegex,
pdu::{EventHash, PduBuilder},
},
services, utils, Error, PduEvent, Result, services, utils, Error, PduEvent, Result,
}; };
@ -594,26 +592,47 @@ impl Service {
} }
} }
let matching_users = |users: &NamespaceRegex| { let matching_users = || {
appservice.users.is_match(pdu.sender.as_str()) services().globals.server_name() == pdu.sender.server_name()
&& appservice.is_user_match(&pdu.sender)
|| pdu.kind == TimelineEventType::RoomMember || pdu.kind == TimelineEventType::RoomMember
&& pdu && pdu.state_key.as_ref().map_or(false, |state_key| {
.state_key UserId::parse(state_key).map_or(false, |user_id| {
.as_ref() services().globals.server_name() == user_id.server_name()
.map_or(false, |state_key| users.is_match(state_key)) && appservice.is_user_match(&user_id)
})
})
}; };
let matching_aliases = |aliases: &NamespaceRegex| {
let matching_aliases = || {
services() services()
.rooms .rooms
.alias .alias
.local_aliases_for_room(&pdu.room_id) .local_aliases_for_room(&pdu.room_id)
.filter_map(|r| r.ok()) .filter_map(Result::ok)
.any(|room_alias| aliases.is_match(room_alias.as_str())) .any(|room_alias| appservice.aliases.is_match(room_alias.as_str()))
|| if let Ok(Some(pdu)) = services().rooms.state_accessor.room_state_get(
&pdu.room_id,
&StateEventType::RoomCanonicalAlias,
"",
) {
serde_json::from_str::<RoomCanonicalAliasEventContent>(pdu.content.get())
.map_or(false, |content| {
content.alias.map_or(false, |alias| {
appservice.aliases.is_match(alias.as_str())
}) || content
.alt_aliases
.iter()
.any(|alias| appservice.aliases.is_match(alias.as_str()))
})
} else {
false
}
}; };
if matching_aliases(&appservice.aliases) if matching_aliases()
|| appservice.rooms.is_match(pdu.room_id.as_str()) || appservice.rooms.is_match(pdu.room_id.as_str())
|| matching_users(&appservice.users) || matching_users()
{ {
services() services()
.sending .sending