fix: SEC53

Reviewed-By: Ginger <ginger@gingershaped.computer>
This commit is contained in:
timedout committed 2026-11-30 17:59:26 +00:00
1 parent b375170fba
commit 43da51ca0d
1 file changed
+26 -54
+26 -54
View File
@@ -13,7 +13,7 @@ use ruma::{
canonical_alias::RoomCanonicalAliasEventContent,
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
join_rules::{JoinRule, RoomJoinRulesEventContent},
member::{MembershipState, RoomMemberEventContent},
member::RoomMemberEventContent,
server_acl::RoomServerAclEventContent,
},
sticky::StickyDurationMs,
@@ -36,8 +36,8 @@ impl super::Service {
sticky_duration_ms: Option<StickyDurationMs>,
timestamp: Option<MilliSecondsSinceUnixEpoch>,
) -> Result<OwnedEventId> {
let mut content: Raw<AnyStateEventContent> = content.clone();
self.assert_allowed_to_send_state_event(room_id, event_type, state_key, &mut content)
let content: Raw<AnyStateEventContent> = content.clone();
self.assert_allowed_to_send_state_event(room_id, event_type, state_key, &content)
.await?;
let content = serde_json::from_str(content.json().get())
@@ -67,7 +67,7 @@ impl super::Service {
room_id: &RoomId,
event_type: &StateEventType,
state_key: &str,
json: &mut Raw<AnyStateEventContent>,
json: &Raw<AnyStateEventContent>,
) -> Result {
match event_type {
| StateEventType::RoomCreate => {
@@ -95,8 +95,7 @@ impl super::Service {
self.assert_allowed_to_send_room_canonical_alias_event(room_id, json)
.await?,
| StateEventType::RoomMember =>
self.assert_allowed_to_send_room_member_event(room_id, state_key, json)
.await?,
Self::assert_allowed_to_send_room_member_event(state_key, json)?,
| _ => (),
}
@@ -257,64 +256,37 @@ impl super::Service {
Ok(())
}
async fn assert_allowed_to_send_room_member_event(
&self,
room_id: &RoomId,
fn assert_allowed_to_send_room_member_event(
state_key: &str,
json: &mut Raw<AnyStateEventContent>,
json: &Raw<AnyStateEventContent>,
) -> Result {
let mut membership_content = json
if UserId::parse(state_key).is_err() {
return Err!(Request(BadJson(
"Membership event has invalid or non-existent state key"
)));
}
let membership_content = json
.deserialize_as_unchecked::<RoomMemberEventContent>()
.map_err(|e| {
err!(Request(BadJson(debug_warn!("Room member event is invalid: {e}"))))
})?;
let Ok(state_key) = UserId::parse(state_key) else {
return Err!(Request(BadJson(
"Membership event has invalid or non-existent state key"
)));
};
let Some(authorising_user) = membership_content.join_authorized_via_users_server else {
return Ok(());
};
// join_authorized_via_users_server must be thrown away, if user is
// already a member of the room.
if self
.services
.state_cache
.is_joined(&state_key, room_id)
.await
// join_authorized_via_users_server and third_party_invite cannot be
// sent via /state, otherwise auth checks could be tricked into
// authorising illegal events.
if membership_content
.join_authorized_via_users_server
.is_some()
{
membership_content.join_authorized_via_users_server = None;
*json = Raw::<AnyStateEventContent>::from_json_string(serde_json::to_string(
&membership_content,
)?)?;
return Ok(());
}
if membership_content.membership != MembershipState::Join {
return Err!(Request(BadJson(
"join_authorised_via_users_server is only for member joins"
return Err!(Request(Forbidden(
"join_authorized_via_users_server cannot be included in m.room.member events \
sent via /state"
)));
}
if !self.services.globals.user_is_local(&authorising_user) {
return Err!(Request(InvalidParam(
"Authorising user {authorising_user} does not belong to this homeserver"
)));
}
if !self
.services
.state_cache
.is_joined(&authorising_user, room_id)
.await
{
return Err!(Request(InvalidParam(
"Authorising user {authorising_user} is not in the room, they cannot authorise \
the join."
if membership_content.third_party_invite.is_some() {
return Err!(Request(Forbidden(
"third_party_invite cannot be included in m.room.member events sent via /state"
)));
}