Fix invalid room ID check & prevent room IDs being prefixed with !

This commit is contained in:
nexy7574 2025-04-20 15:41:19 +01:00
parent d3022b4112
commit 41581c9ae8
No known key found for this signature in database
GPG key ID: 0FA334385D0B689F

View file

@ -107,7 +107,6 @@ pub(crate) async fn create_room_route(
return Err!(Request(Forbidden("Publishing rooms to the room directory is not allowed"))); return Err!(Request(Forbidden("Publishing rooms to the room directory is not allowed")));
} }
let _short_id = services let _short_id = services
.rooms .rooms
.short .short
@ -615,17 +614,26 @@ fn custom_room_id_check(services: &Services, custom_room_id: &str) -> Result<Own
"Custom room ID contains an unexpected `:` which is not allowed.", "Custom room ID contains an unexpected `:` which is not allowed.",
)); ));
} }
} else if custom_room_id.starts_with('!'){
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Room ID is prefixed with !, but is not fully qualified. You likely did not want this."));
} else { } else {
room_id = format!("!{custom_room_id}:{server_name}"); room_id = format!("!{custom_room_id}:{server_name}");
} }
OwnedRoomId::parse(room_id) OwnedRoomId::parse(room_id)
.map_err(Into::into) .map_err(Into::into)
.and_then(
|full_room_id| {
if full_room_id.server_name().expect("failed to extract server name from room ID") != server_name {
Err(Error::BadRequest(ErrorKind::InvalidParam, "Custom room ID must be on this server."))
} else {
Ok(full_room_id)
}
}
)
.inspect(|full_room_id| { .inspect(|full_room_id| {
debug_info!(?full_room_id, "Full custom room ID"); debug_info!(?full_room_id, "Full custom room ID");
if full_room_id.server_name().expect("failed to extract server name from room ID") != server_name {
error!("Custom room ID does not match server name");
}
}) })
.inspect_err(|e| warn!(?e, ?custom_room_id, "Failed to create room with custom room ID",)) .inspect_err(|e| warn!(?e, ?custom_room_id, "Failed to create room with custom room ID",))
} }