From 5c4b8ad7a329a1939018b55ddee841fa511ffc6e Mon Sep 17 00:00:00 2001 From: strawberry Date: Sat, 13 Jan 2024 23:43:53 -0500 Subject: [PATCH] replace panics on unknown room versions with errors Signed-off-by: strawberry --- src/api/client_server/room.rs | 27 ++++++++++++++++++++++++--- src/service/admin/mod.rs | 10 +++++++++- src/service/rooms/timeline/mod.rs | 8 +++++++- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/api/client_server/room.rs b/src/api/client_server/room.rs index d2af99d0..d2924fa0 100644 --- a/src/api/client_server/room.rs +++ b/src/api/client_server/room.rs @@ -147,7 +147,13 @@ pub async fn create_room_route( ); } RoomVersionId::V11 => {} // V11 removed the "creator" key - _ => panic!("Unexpected room version {}", room_version), + _ => { + warn!("Unexpected or unsupported room version {}", room_version); + return Err(Error::BadRequest( + ErrorKind::BadJson, + "Unexpected or unsupported room version found", + )); + } } content.insert( @@ -172,7 +178,13 @@ pub async fn create_room_route( | RoomVersionId::V9 | RoomVersionId::V10 => RoomCreateEventContent::new_v1(sender_user.clone()), RoomVersionId::V11 => RoomCreateEventContent::new_v11(), - _ => panic!("Unexpected room version {}", room_version), + _ => { + warn!("Unexpected or unsupported room version {}", room_version); + return Err(Error::BadRequest( + ErrorKind::BadJson, + "Unexpected or unsupported room version found", + )); + } }; let mut content = serde_json::from_str::( to_raw_value(&content) @@ -673,7 +685,16 @@ pub async fn upgrade_room_route( // "creator" key no longer exists in V11 rooms create_event_content.remove("creator"); } - _ => panic!("Unexpected room version {}", body.new_version), + _ => { + warn!( + "Unexpected or unsupported room version {}", + body.new_version + ); + return Err(Error::BadRequest( + ErrorKind::BadJson, + "Unexpected or unsupported room version found", + )); + } } create_event_content.insert( diff --git a/src/service/admin/mod.rs b/src/service/admin/mod.rs index eb8c93b5..3385c902 100644 --- a/src/service/admin/mod.rs +++ b/src/service/admin/mod.rs @@ -10,6 +10,7 @@ use std::fmt::Write; use clap::{Parser, Subcommand}; use regex::Regex; use ruma::{ + api::client::error::ErrorKind, events::{ relation::InReplyTo, room::{ @@ -30,6 +31,7 @@ use ruma::{ }; use serde_json::value::to_raw_value; use tokio::sync::{mpsc, Mutex}; +use tracing::warn; use crate::{ api::client_server::{leave_all_rooms, AUTO_GEN_PASSWORD_LENGTH}, @@ -1420,7 +1422,13 @@ impl Service { | RoomVersionId::V9 | RoomVersionId::V10 => RoomCreateEventContent::new_v1(conduit_user.clone()), RoomVersionId::V11 => RoomCreateEventContent::new_v11(), - _ => panic!("Unexpected room version {}", room_version), + _ => { + warn!("Unexpected or unsupported room version {}", room_version); + return Err(Error::BadRequest( + ErrorKind::BadJson, + "Unexpected or unsupported room version found", + )); + } }; content.federate = true; diff --git a/src/service/rooms/timeline/mod.rs b/src/service/rooms/timeline/mod.rs index 964f5844..a66b8e6f 100644 --- a/src/service/rooms/timeline/mod.rs +++ b/src/service/rooms/timeline/mod.rs @@ -439,7 +439,13 @@ impl Service { self.redact_pdu(redact_id, pdu)?; } } - _ => panic!("Unexpected room version {}", room_version_id), + _ => { + warn!("Unexpected or unsupported room version {}", room_version_id); + return Err(Error::BadRequest( + ErrorKind::BadJson, + "Unexpected or unsupported room version found", + )); + } }; } TimelineEventType::SpaceChild => {