From b375170fba006434095ec4c943deb0cfad8bfd3d Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Mon, 30 Nov 2026 17:14:23 +0000 Subject: [PATCH] fix: SEC25 Reviewed-By: timedout --- changelog.d/SEC25.bugfix.md | 1 + src/api/server/invite.rs | 76 +++++++++++++------ src/service/rooms/event_handler/pdu_checks.rs | 2 +- src/service/rooms/state_cache/update.rs | 26 ++++--- src/service/rooms/timeline/append.rs | 39 +++++++++- 5 files changed, 107 insertions(+), 37 deletions(-) create mode 100644 changelog.d/SEC25.bugfix.md diff --git a/changelog.d/SEC25.bugfix.md b/changelog.d/SEC25.bugfix.md new file mode 100644 index 000000000..20738cb5f --- /dev/null +++ b/changelog.d/SEC25.bugfix.md @@ -0,0 +1 @@ +SEC25 fix. See at a later date for more info. Contributed by @jade. \ No newline at end of file diff --git a/src/api/server/invite.rs b/src/api/server/invite.rs index aaadd3055..de7eeb9a7 100644 --- a/src/api/server/invite.rs +++ b/src/api/server/invite.rs @@ -147,6 +147,33 @@ async fn create_invite( .server_in_room(services.globals.server_name(), &body.room_id) .await; if resident { + // Make sure the invite is for the room we are in, then authorise it + // against our current view of the room state. + let create_event = services + .rooms + .state_accessor + .room_state_get(&body.room_id, &StateEventType::RoomCreate, "") + .await?; + if create_event.event_id != create_event_id + || services.rooms.state.get_room_version(&body.room_id).await? != body.room_version + { + return Err!(Request(InvalidParam( + "Invite does not match the room's create event or version." + ))); + } + + let invite_pdu = PduEvent::from_id_val(&body.event_id, signed_event.clone())?; + if !services + .rooms + .event_handler + .current_state_check_6(&invite_pdu, &room_version_rules, &create_event) + .await? + { + return Err!(Request(Forbidden( + "Invite is not authorised by the current room state." + ))); + } + services .rooms .event_handler @@ -174,10 +201,31 @@ async fn create_invite( .expect("must be able to create PDU object"); invite_state.push(RawStrippedState::Pdu(serde_json::value::to_raw_value(&signed_event)?)); - // If we are active in the room, the remote server will notify us about the - // join/invite through /send. If we are not in the room, we need to manually - // record the invited state for client /sync through update_membership(), - // and send the invite PDU to the relevant appservices. + // We always mark the room as invited, because we don't act on invites through /send + services + .rooms + .state_cache + .mark_as_invited( + &recipient_user, + &body.room_id, + &sender_user, + invite_state, + body.via.clone(), + ) + .await?; + + services + .rooms + .state_cache + .update_joined_count(&body.room_id) + .await; + + services.sync.wake(&recipient_user).await; + + // If we are active in the room, the remote server will send us the invite + // through /send, which notifies pushers and appservices now that the invite + // has been recorded. If we are not in the room, we need to manually send + // the invite PDU to the relevant appservices. if !resident { // We will start by recording the room's create event as an outlier. // This will allow us to recognise it later in case the sender revokes @@ -190,26 +238,6 @@ async fn create_invite( .outlier .add_pdu_outlier(&create_event_id, create_event_json); - services - .rooms - .state_cache - .mark_as_invited( - &recipient_user, - &body.room_id, - &sender_user, - invite_state, - body.via.clone(), - ) - .await?; - - services - .rooms - .state_cache - .update_joined_count(&body.room_id) - .await; - - services.sync.wake(&recipient_user).await; - let matching_appservices: Vec<_> = services .appservice .read() diff --git a/src/service/rooms/event_handler/pdu_checks.rs b/src/service/rooms/event_handler/pdu_checks.rs index af0338a97..dd5745d7d 100644 --- a/src/service/rooms/event_handler/pdu_checks.rs +++ b/src/service/rooms/event_handler/pdu_checks.rs @@ -249,7 +249,7 @@ impl super::Service { /// /// Returns a boolean indicating whether the event is authorised, or an /// error if the auth check fails. - pub(super) async fn current_state_check_6( + pub async fn current_state_check_6( &self, incoming_pdu: &PduEvent, room_version_rules: &RoomVersionRules, diff --git a/src/service/rooms/state_cache/update.rs b/src/service/rooms/state_cache/update.rs index be51f6403..b7d19d757 100644 --- a/src/service/rooms/state_cache/update.rs +++ b/src/service/rooms/state_cache/update.rs @@ -109,17 +109,23 @@ impl super::Service { self.mark_as_joined(user_id, room_id); }, | MembershipState::Invite => { - let invite_state = if is_local { - self.services - .state - .summary_stripped(pdu, room_id, user_id, false) - .await - } else { - vec![] - }; + // Remote invites for local users are not processed via the + // /send endpoint or state + // resolution, so they cannot bypass invite anti-spam or + // signature checks. + if !is_local || self.services.globals.user_is_local(pdu.sender()) { + let invite_state = if is_local { + self.services + .state + .summary_stripped(pdu, room_id, user_id, false) + .await + } else { + vec![] + }; - self.mark_as_invited(user_id, room_id, pdu.sender(), invite_state, None) - .await?; + self.mark_as_invited(user_id, room_id, pdu.sender(), invite_state, None) + .await?; + } }, | MembershipState::Knock => { let knocked_state = if is_local { diff --git a/src/service/rooms/timeline/append.rs b/src/service/rooms/timeline/append.rs index 887dca516..68e34b842 100644 --- a/src/service/rooms/timeline/append.rs +++ b/src/service/rooms/timeline/append.rs @@ -415,7 +415,9 @@ impl super::Service { let mut notifies = Vec::with_capacity(push_targets.len().saturating_add(1)); let mut highlights = Vec::with_capacity(push_targets.len().saturating_add(1)); - if *pdu.kind() == TimelineEventType::RoomMember { + if *pdu.kind() == TimelineEventType::RoomMember + && !self.is_untrusted_remote_invite(pdu, room_id).await + { if let Some(state_key) = pdu.state_key() { match UserId::parse(state_key) { | Ok(target_user_id) => { @@ -701,6 +703,37 @@ impl super::Service { } } + /// Returns true if `pdu` is an invite for a local user from a remote + /// sender that was not accepted through the federation /invite endpoint, + /// and so has not passed the invite anti-spam checks. + async fn is_untrusted_remote_invite(&self, pdu: &PduEvent, room_id: &RoomId) -> bool { + if *pdu.kind() != TimelineEventType::RoomMember + || self.services.globals.user_is_local(pdu.sender()) + || !pdu + .get_content::() + .is_ok_and(|c| c.membership == MembershipState::Invite) + { + return false; + } + + let Some(target) = pdu.state_key().and_then(|sk| UserId::parse(sk).ok()) else { + return false; + }; + + if !self.services.globals.user_is_local(&target) { + return false; + } + + // /invite records the sender when it accepts an invite. + self.services + .state_cache + .invite_sender(&target, room_id) + .await + .ok() + .as_deref() + != Some(pdu.sender()) + } + /// Determines if an appservice is interested in a particular event. async fn is_appservice_interested( &self, @@ -717,7 +750,9 @@ impl super::Service { return true; } - let target = if *pdu.kind() == TimelineEventType::RoomMember { + let target = if *pdu.kind() == TimelineEventType::RoomMember + && !self.is_untrusted_remote_invite(pdu, room_id).await + { pdu.state_key().and_then(|sk| UserId::parse(sk).ok()) } else { None