Simplify identifier parsing code

merge-requests/228/head
Jonas Platte 3 years ago
parent 41fef1da64
commit bffddbd487
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67

@ -1,8 +1,4 @@
use std::{ use std::{collections::BTreeMap, convert::TryInto, sync::Arc};
collections::BTreeMap,
convert::{TryFrom, TryInto},
sync::Arc,
};
use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH}; use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH};
use crate::{database::DatabaseGuard, pdu::PduBuilder, utils, ConduitResult, Error, Ruma}; use crate::{database::DatabaseGuard, pdu::PduBuilder, utils, ConduitResult, Error, Ruma};
@ -396,9 +392,8 @@ pub async fn register_route(
)?; )?;
// 6. Events implied by name and topic // 6. Events implied by name and topic
let room_name = let room_name = RoomName::parse(format!("{} Admin Room", db.globals.server_name()))
Box::<RoomName>::try_from(format!("{} Admin Room", db.globals.server_name())) .expect("Room name is valid");
.expect("Room name is valid");
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomName, event_type: EventType::RoomName,

@ -64,7 +64,7 @@ pub async fn join_room_by_id_route(
.filter_map(|event| serde_json::from_str(event.json().get()).ok()) .filter_map(|event| serde_json::from_str(event.json().get()).ok())
.filter_map(|event: serde_json::Value| event.get("sender").cloned()) .filter_map(|event: serde_json::Value| event.get("sender").cloned())
.filter_map(|sender| sender.as_str().map(|s| s.to_owned())) .filter_map(|sender| sender.as_str().map(|s| s.to_owned()))
.filter_map(|sender| Box::<UserId>::try_from(sender).ok()) .filter_map(|sender| UserId::parse(sender).ok())
.map(|user| user.server_name().to_owned()) .map(|user| user.server_name().to_owned())
.collect(); .collect();
@ -92,16 +92,17 @@ pub async fn join_room_by_id_route(
/// - If the server does not know about the room: asks other servers over federation /// - If the server does not know about the room: asks other servers over federation
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
post("/_matrix/client/r0/join/<_>", data = "<body>") post("/_matrix/client/r0/join/<_>", data = "<req>")
)] )]
#[tracing::instrument(skip(db, body))] #[tracing::instrument(skip(db, req))]
pub async fn join_room_by_id_or_alias_route( pub async fn join_room_by_id_or_alias_route(
db: DatabaseGuard, db: DatabaseGuard,
body: Ruma<join_room_by_id_or_alias::Request<'_>>, req: Ruma<join_room_by_id_or_alias::Request<'_>>,
) -> ConduitResult<join_room_by_id_or_alias::Response> { ) -> ConduitResult<join_room_by_id_or_alias::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let body = req.body;
let sender_user = req.sender_user.as_ref().expect("user is authenticated");
let (servers, room_id) = match Box::<RoomId>::try_from(body.room_id_or_alias.clone()) { let (servers, room_id) = match Box::<RoomId>::try_from(body.room_id_or_alias) {
Ok(room_id) => { Ok(room_id) => {
let mut servers: HashSet<_> = db let mut servers: HashSet<_> = db
.rooms .rooms
@ -111,7 +112,7 @@ pub async fn join_room_by_id_or_alias_route(
.filter_map(|event| serde_json::from_str(event.json().get()).ok()) .filter_map(|event| serde_json::from_str(event.json().get()).ok())
.filter_map(|event: serde_json::Value| event.get("sender").cloned()) .filter_map(|event: serde_json::Value| event.get("sender").cloned())
.filter_map(|sender| sender.as_str().map(|s| s.to_owned())) .filter_map(|sender| sender.as_str().map(|s| s.to_owned()))
.filter_map(|sender| Box::<UserId>::try_from(sender).ok()) .filter_map(|sender| UserId::parse(sender).ok())
.map(|user| user.server_name().to_owned()) .map(|user| user.server_name().to_owned())
.collect(); .collect();
@ -127,7 +128,7 @@ pub async fn join_room_by_id_or_alias_route(
let join_room_response = join_room_by_id_helper( let join_room_response = join_room_by_id_helper(
&db, &db,
body.sender_user.as_deref(), req.sender_user.as_deref(),
&room_id, &room_id,
&servers, &servers,
body.third_party_signed.as_ref(), body.third_party_signed.as_ref(),
@ -619,12 +620,13 @@ async fn join_room_by_id_helper(
.expect("event is valid, we just created it"); .expect("event is valid, we just created it");
// Generate event id // Generate event id
let event_id = Box::<EventId>::try_from(&*format!( let event_id = format!(
"${}", "${}",
ruma::signatures::reference_hash(&join_event_stub, &room_version) ruma::signatures::reference_hash(&join_event_stub, &room_version)
.expect("ruma can calculate reference hashes") .expect("ruma can calculate reference hashes")
)) );
.expect("ruma's reference hashes are valid event ids"); let event_id = <&EventId>::try_from(event_id.as_str())
.expect("ruma's reference hashes are valid event ids");
// Add event_id back // Add event_id back
join_event_stub.insert( join_event_stub.insert(
@ -642,7 +644,7 @@ async fn join_room_by_id_helper(
remote_server, remote_server,
federation::membership::create_join_event::v2::Request { federation::membership::create_join_event::v2::Request {
room_id, room_id,
event_id: &event_id, event_id,
pdu: &PduEvent::convert_to_outgoing_federation_event(join_event.clone()), pdu: &PduEvent::convert_to_outgoing_federation_event(join_event.clone()),
}, },
) )
@ -650,7 +652,7 @@ async fn join_room_by_id_helper(
db.rooms.get_or_create_shortroomid(room_id, &db.globals)?; db.rooms.get_or_create_shortroomid(room_id, &db.globals)?;
let pdu = PduEvent::from_id_val(&event_id, join_event.clone()) let pdu = PduEvent::from_id_val(event_id, join_event.clone())
.map_err(|_| Error::BadServerResponse("Invalid join event PDU."))?; .map_err(|_| Error::BadServerResponse("Invalid join event PDU."))?;
let mut state = HashMap::new(); let mut state = HashMap::new();
@ -788,7 +790,7 @@ fn validate_and_add_event_id(
error!("Invalid PDU in server response: {:?}: {:?}", pdu, e); error!("Invalid PDU in server response: {:?}: {:?}", pdu, e);
Error::BadServerResponse("Invalid PDU in server response") Error::BadServerResponse("Invalid PDU in server response")
})?; })?;
let event_id = Box::<EventId>::try_from(&*format!( let event_id = EventId::parse(format!(
"${}", "${}",
ruma::signatures::reference_hash(&value, room_version) ruma::signatures::reference_hash(&value, room_version)
.expect("ruma can calculate reference hashes") .expect("ruma can calculate reference hashes")
@ -1011,12 +1013,13 @@ pub(crate) async fn invite_helper<'a>(
}; };
// Generate event id // Generate event id
let expected_event_id = Box::<EventId>::try_from(&*format!( let expected_event_id = format!(
"${}", "${}",
ruma::signatures::reference_hash(&pdu_json, &room_version_id) ruma::signatures::reference_hash(&pdu_json, &room_version_id)
.expect("ruma can calculate reference hashes") .expect("ruma can calculate reference hashes")
)) );
.expect("ruma's reference hashes are valid event ids"); let expected_event_id = <&EventId>::try_from(expected_event_id.as_str())
.expect("ruma's reference hashes are valid event ids");
let response = db let response = db
.sending .sending
@ -1025,7 +1028,7 @@ pub(crate) async fn invite_helper<'a>(
user_id.server_name(), user_id.server_name(),
create_invite::v2::Request { create_invite::v2::Request {
room_id, room_id,
event_id: &expected_event_id, event_id: expected_event_id,
room_version: &room_version_id, room_version: &room_version_id,
event: &PduEvent::convert_to_outgoing_federation_event(pdu_json.clone()), event: &PduEvent::convert_to_outgoing_federation_event(pdu_json.clone()),
invite_room_state: &invite_room_state, invite_room_state: &invite_room_state,

@ -5,13 +5,8 @@ use ruma::{
r0::message::{get_message_events, send_message_event}, r0::message::{get_message_events, send_message_event},
}, },
events::EventType, events::EventType,
EventId,
};
use std::{
collections::BTreeMap,
convert::{TryFrom, TryInto},
sync::Arc,
}; };
use std::{collections::BTreeMap, convert::TryInto, sync::Arc};
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
use rocket::{get, put}; use rocket::{get, put};
@ -67,11 +62,10 @@ pub async fn send_message_event_route(
)); ));
} }
let event_id = Box::<EventId>::try_from( let event_id = utils::string_from_bytes(&response)
utils::string_from_bytes(&response) .map_err(|_| Error::bad_database("Invalid txnid bytes in database."))?
.map_err(|_| Error::bad_database("Invalid txnid bytes in database."))?, .try_into()
) .map_err(|_| Error::bad_database("Invalid event id in txnid data."))?;
.map_err(|_| Error::bad_database("Invalid event id in txnid data."))?;
return Ok(send_message_event::Response { event_id }.into()); return Ok(send_message_event::Response { event_id }.into());
} }

@ -26,12 +26,7 @@ use ruma::{
RoomAliasId, RoomId, RoomVersionId, RoomAliasId, RoomId, RoomVersionId,
}; };
use serde_json::{json, value::to_raw_value}; use serde_json::{json, value::to_raw_value};
use std::{ use std::{cmp::max, collections::BTreeMap, convert::TryInto, sync::Arc};
cmp::max,
collections::BTreeMap,
convert::{TryFrom, TryInto},
sync::Arc,
};
use tracing::{info, warn}; use tracing::{info, warn};
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
@ -93,12 +88,11 @@ pub async fn create_room_route(
.as_ref() .as_ref()
.map_or(Ok(None), |localpart| { .map_or(Ok(None), |localpart| {
// TODO: Check for invalid characters and maximum length // TODO: Check for invalid characters and maximum length
let alias = Box::<RoomAliasId>::try_from(format!( let alias =
"#{}:{}", RoomAliasId::parse(format!("#{}:{}", localpart, db.globals.server_name()))
localpart, .map_err(|_| {
db.globals.server_name(), Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias.")
)) })?;
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?;
if db.rooms.id_from_alias(&alias)?.is_some() { if db.rooms.id_from_alias(&alias)?.is_some() {
Err(Error::BadRequest( Err(Error::BadRequest(

@ -10,7 +10,7 @@ use ruma::{
}; };
use std::{ use std::{
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet}, collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
convert::{TryFrom, TryInto}, convert::TryInto,
sync::Arc, sync::Arc,
time::Duration, time::Duration,
}; };
@ -298,10 +298,9 @@ async fn sync_helper(
})?; })?;
if let Some(state_key) = &pdu.state_key { if let Some(state_key) = &pdu.state_key {
let user_id = let user_id = UserId::parse(state_key.clone()).map_err(|_| {
Box::<UserId>::try_from(state_key.clone()).map_err(|_| { Error::bad_database("Invalid UserId in member PDU.")
Error::bad_database("Invalid UserId in member PDU.") })?;
})?;
// The membership was and still is invite or join // The membership was and still is invite or join
if matches!( if matches!(
@ -427,7 +426,7 @@ async fn sync_helper(
} }
if let Some(state_key) = &state_event.state_key { if let Some(state_key) = &state_event.state_key {
let user_id = Box::<UserId>::try_from(state_key.clone()) let user_id = UserId::parse(state_key.clone())
.map_err(|_| Error::bad_database("Invalid UserId in member PDU."))?; .map_err(|_| Error::bad_database("Invalid UserId in member PDU."))?;
if user_id == sender_user { if user_id == sender_user {

@ -476,11 +476,9 @@ impl Database {
if db.globals.database_version()? < 6 { if db.globals.database_version()? < 6 {
// Set room member count // Set room member count
for (roomid, _) in db.rooms.roomid_shortstatehash.iter() { for (roomid, _) in db.rooms.roomid_shortstatehash.iter() {
let room_id = let string = utils::string_from_bytes(&roomid).unwrap();
Box::<RoomId>::try_from(utils::string_from_bytes(&roomid).unwrap()) let room_id = <&RoomId>::try_from(string.as_str()).unwrap();
.unwrap(); db.rooms.update_joined_count(room_id, &db)?;
db.rooms.update_joined_count(&room_id, &db)?;
} }
db.globals.bump_database_version(6)?; db.globals.bump_database_version(6)?;
@ -587,10 +585,9 @@ impl Database {
.get(&seventid) .get(&seventid)
.unwrap() .unwrap()
.unwrap(); .unwrap();
let event_id = let string = utils::string_from_bytes(&event_id).unwrap();
Box::<EventId>::try_from(utils::string_from_bytes(&event_id).unwrap()) let event_id = <&EventId>::try_from(string.as_str()).unwrap();
.unwrap(); let pdu = db.rooms.get_pdu(event_id).unwrap().unwrap();
let pdu = db.rooms.get_pdu(&event_id).unwrap().unwrap();
if Some(&pdu.room_id) != current_room.as_ref() { if Some(&pdu.room_id) != current_room.as_ref() {
current_room = Some(pdu.room_id.clone()); current_room = Some(pdu.room_id.clone());

@ -1,10 +1,10 @@
use std::{convert::TryFrom, sync::Arc}; use std::{convert::TryInto, sync::Arc};
use crate::{pdu::PduBuilder, Database}; use crate::{pdu::PduBuilder, Database};
use rocket::futures::{channel::mpsc, stream::StreamExt}; use rocket::futures::{channel::mpsc, stream::StreamExt};
use ruma::{ use ruma::{
events::{room::message::RoomMessageEventContent, EventType}, events::{room::message::RoomMessageEventContent, EventType},
RoomAliasId, UserId, UserId,
}; };
use serde_json::value::to_raw_value; use serde_json::value::to_raw_value;
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard}; use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
@ -33,18 +33,16 @@ impl Admin {
let guard = db.read().await; let guard = db.read().await;
let conduit_user = let conduit_user = UserId::parse(format!("@conduit:{}", guard.globals.server_name()))
Box::<UserId>::try_from(format!("@conduit:{}", guard.globals.server_name())) .expect("@conduit:server_name is valid");
.expect("@conduit:server_name is valid");
let conduit_room = guard let conduit_room = guard
.rooms .rooms
.id_from_alias( .id_from_alias(
&Box::<RoomAliasId>::try_from(format!( format!("#admins:{}", guard.globals.server_name())
"#admins:{}", .as_str()
guard.globals.server_name() .try_into()
)) .expect("#admins:server_name is a valid room alias"),
.expect("#admins:server_name is a valid room alias"),
) )
.unwrap(); .unwrap();

@ -6,7 +6,7 @@ use ruma::{
}, },
RoomId, UserId, RoomId, UserId,
}; };
use std::{collections::BTreeMap, convert::TryFrom, sync::Arc}; use std::{collections::BTreeMap, sync::Arc};
use super::abstraction::Tree; use super::abstraction::Tree;
@ -231,7 +231,7 @@ impl KeyBackups {
Error::bad_database("backupkeyid_backup session_id is invalid.") Error::bad_database("backupkeyid_backup session_id is invalid.")
})?; })?;
let room_id = Box::<RoomId>::try_from( let room_id = RoomId::parse(
utils::string_from_bytes(parts.next().ok_or_else(|| { utils::string_from_bytes(parts.next().ok_or_else(|| {
Error::bad_database("backupkeyid_backup key is invalid.") Error::bad_database("backupkeyid_backup key is invalid.")
})?) })?)

@ -434,7 +434,7 @@ impl Rooms {
None => continue, None => continue,
}; };
let user_id = match Box::<UserId>::try_from(state_key) { let user_id = match UserId::parse(state_key) {
Ok(id) => id, Ok(id) => id,
Err(_) => continue, Err(_) => continue,
}; };
@ -871,12 +871,10 @@ impl Rooms {
.get(&shorteventid.to_be_bytes())? .get(&shorteventid.to_be_bytes())?
.ok_or_else(|| Error::bad_database("Shorteventid does not exist"))?; .ok_or_else(|| Error::bad_database("Shorteventid does not exist"))?;
let event_id = Arc::from( let event_id = EventId::parse_arc(utils::string_from_bytes(&bytes).map_err(|_| {
Box::<EventId>::try_from(utils::string_from_bytes(&bytes).map_err(|_| { Error::bad_database("EventID in shorteventid_eventid is invalid unicode.")
Error::bad_database("EventID in shorteventid_eventid is invalid unicode.") })?)
})?) .map_err(|_| Error::bad_database("EventId in shorteventid_eventid is invalid."))?;
.map_err(|_| Error::bad_database("EventId in shorteventid_eventid is invalid."))?,
);
self.shorteventid_cache self.shorteventid_cache
.lock() .lock()
@ -1169,7 +1167,7 @@ impl Rooms {
self.roomid_pduleaves self.roomid_pduleaves
.scan_prefix(prefix) .scan_prefix(prefix)
.map(|(_, bytes)| { .map(|(_, bytes)| {
Box::<EventId>::try_from(utils::string_from_bytes(&bytes).map_err(|_| { EventId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("EventID in roomid_pduleaves is invalid unicode.") Error::bad_database("EventID in roomid_pduleaves is invalid unicode.")
})?) })?)
.map_err(|_| Error::bad_database("EventId in roomid_pduleaves is invalid.")) .map_err(|_| Error::bad_database("EventId in roomid_pduleaves is invalid."))
@ -1420,7 +1418,7 @@ impl Rooms {
} }
// if the state_key fails // if the state_key fails
let target_user_id = Box::<UserId>::try_from(state_key.clone()) let target_user_id = UserId::parse(state_key.clone())
.expect("This state_key was previously validated"); .expect("This state_key was previously validated");
let content = serde_json::from_str::<ExtractMembership>(pdu.content.get()) let content = serde_json::from_str::<ExtractMembership>(pdu.content.get())
@ -1476,10 +1474,9 @@ impl Rooms {
if body.starts_with(&format!("@conduit:{}: ", db.globals.server_name())) if body.starts_with(&format!("@conduit:{}: ", db.globals.server_name()))
&& self && self
.id_from_alias( .id_from_alias(
&Box::<RoomAliasId>::try_from(format!( <&RoomAliasId>::try_from(
"#admins:{}", format!("#admins:{}", db.globals.server_name()).as_str(),
db.globals.server_name() )
))
.expect("#admins:server_name is a valid room alias"), .expect("#admins:server_name is a valid room alias"),
)? )?
.as_ref() .as_ref()
@ -1530,7 +1527,7 @@ impl Rooms {
} }
"get_auth_chain" => { "get_auth_chain" => {
if args.len() == 1 { if args.len() == 1 {
if let Ok(event_id) = Box::<EventId>::try_from(args[0]) { if let Ok(event_id) = EventId::parse_arc(args[0]) {
if let Some(event) = db.rooms.get_pdu_json(&event_id)? { if let Some(event) = db.rooms.get_pdu_json(&event_id)? {
let room_id_str = event let room_id_str = event
.get("room_id") .get("room_id")
@ -1541,12 +1538,12 @@ impl Rooms {
) )
})?; })?;
let room_id = Box::<RoomId>::try_from(room_id_str) let room_id = <&RoomId>::try_from(room_id_str)
.map_err(|_| Error::bad_database("Invalid room id field in event in database"))?; .map_err(|_| Error::bad_database("Invalid room id field in event in database"))?;
let start = Instant::now(); let start = Instant::now();
let count = server_server::get_auth_chain( let count = server_server::get_auth_chain(
&room_id, room_id,
vec![Arc::from(event_id)], vec![event_id],
db, db,
)? )?
.count(); .count();
@ -1569,7 +1566,7 @@ impl Rooms {
let string = body[1..body.len() - 1].join("\n"); let string = body[1..body.len() - 1].join("\n");
match serde_json::from_str(&string) { match serde_json::from_str(&string) {
Ok(value) => { Ok(value) => {
let event_id = Box::<EventId>::try_from(&*format!( let event_id = EventId::parse(format!(
"${}", "${}",
// Anything higher than version3 behaves the same // Anything higher than version3 behaves the same
ruma::signatures::reference_hash( ruma::signatures::reference_hash(
@ -1624,7 +1621,7 @@ impl Rooms {
} }
"get_pdu" => { "get_pdu" => {
if args.len() == 1 { if args.len() == 1 {
if let Ok(event_id) = Box::<EventId>::try_from(args[0]) { if let Ok(event_id) = EventId::parse(args[0]) {
let mut outlier = false; let mut outlier = false;
let mut pdu_json = let mut pdu_json =
db.rooms.get_non_outlier_pdu_json(&event_id)?; db.rooms.get_non_outlier_pdu_json(&event_id)?;
@ -2083,7 +2080,7 @@ impl Rooms {
.expect("event is valid, we just created it"); .expect("event is valid, we just created it");
// Generate event id // Generate event id
pdu.event_id = Box::<EventId>::try_from(&*format!( pdu.event_id = EventId::parse(format!(
"${}", "${}",
ruma::signatures::reference_hash(&pdu_json, &room_version_id) ruma::signatures::reference_hash(&pdu_json, &room_version_id)
.expect("ruma can calculate reference hashes") .expect("ruma can calculate reference hashes")
@ -2758,7 +2755,7 @@ impl Rooms {
.filter_map(|event| serde_json::from_str(event.json().get()).ok()) .filter_map(|event| serde_json::from_str(event.json().get()).ok())
.filter_map(|event: serde_json::Value| event.get("sender").cloned()) .filter_map(|event: serde_json::Value| event.get("sender").cloned())
.filter_map(|sender| sender.as_str().map(|s| s.to_owned())) .filter_map(|sender| sender.as_str().map(|s| s.to_owned()))
.filter_map(|sender| Box::<UserId>::try_from(sender).ok()) .filter_map(|sender| UserId::parse(sender).ok())
.map(|user| user.server_name().to_owned()) .map(|user| user.server_name().to_owned())
.collect(); .collect();
@ -2819,7 +2816,7 @@ impl Rooms {
.expect("event is valid, we just created it"); .expect("event is valid, we just created it");
// Generate event id // Generate event id
let event_id = Box::<EventId>::try_from(&*format!( let event_id = EventId::parse(format!(
"${}", "${}",
ruma::signatures::reference_hash(&leave_event_stub, &room_version_id) ruma::signatures::reference_hash(&leave_event_stub, &room_version_id)
.expect("ruma can calculate reference hashes") .expect("ruma can calculate reference hashes")
@ -2908,7 +2905,7 @@ impl Rooms {
self.alias_roomid self.alias_roomid
.get(alias.alias().as_bytes())? .get(alias.alias().as_bytes())?
.map(|bytes| { .map(|bytes| {
Box::<RoomId>::try_from(utils::string_from_bytes(&bytes).map_err(|_| { RoomId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Room ID in alias_roomid is invalid unicode.") Error::bad_database("Room ID in alias_roomid is invalid unicode.")
})?) })?)
.map_err(|_| Error::bad_database("Room ID in alias_roomid is invalid.")) .map_err(|_| Error::bad_database("Room ID in alias_roomid is invalid."))
@ -2951,7 +2948,7 @@ impl Rooms {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn public_rooms(&self) -> impl Iterator<Item = Result<Box<RoomId>>> + '_ { pub fn public_rooms(&self) -> impl Iterator<Item = Result<Box<RoomId>>> + '_ {
self.publicroomids.iter().map(|(bytes, _)| { self.publicroomids.iter().map(|(bytes, _)| {
Box::<RoomId>::try_from( RoomId::parse(
utils::string_from_bytes(&bytes).map_err(|_| { utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Room ID in publicroomids is invalid unicode.") Error::bad_database("Room ID in publicroomids is invalid unicode.")
})?, })?,
@ -3039,7 +3036,7 @@ impl Rooms {
Ok(utils::common_elements(iterators, Ord::cmp) Ok(utils::common_elements(iterators, Ord::cmp)
.expect("users is not empty") .expect("users is not empty")
.map(|bytes| { .map(|bytes| {
Box::<RoomId>::try_from(utils::string_from_bytes(&*bytes).map_err(|_| { RoomId::parse(utils::string_from_bytes(&*bytes).map_err(|_| {
Error::bad_database("Invalid RoomId bytes in userroomid_joined") Error::bad_database("Invalid RoomId bytes in userroomid_joined")
})?) })?)
.map_err(|_| Error::bad_database("Invalid RoomId in userroomid_joined.")) .map_err(|_| Error::bad_database("Invalid RoomId in userroomid_joined."))
@ -3056,7 +3053,7 @@ impl Rooms {
prefix.push(0xff); prefix.push(0xff);
self.roomserverids.scan_prefix(prefix).map(|(key, _)| { self.roomserverids.scan_prefix(prefix).map(|(key, _)| {
Box::<ServerName>::try_from( ServerName::parse(
utils::string_from_bytes( utils::string_from_bytes(
key.rsplit(|&b| b == 0xff) key.rsplit(|&b| b == 0xff)
.next() .next()
@ -3089,7 +3086,7 @@ impl Rooms {
prefix.push(0xff); prefix.push(0xff);
self.serverroomids.scan_prefix(prefix).map(|(key, _)| { self.serverroomids.scan_prefix(prefix).map(|(key, _)| {
Box::<RoomId>::try_from( RoomId::parse(
utils::string_from_bytes( utils::string_from_bytes(
key.rsplit(|&b| b == 0xff) key.rsplit(|&b| b == 0xff)
.next() .next()
@ -3111,7 +3108,7 @@ impl Rooms {
prefix.push(0xff); prefix.push(0xff);
self.roomuserid_joined.scan_prefix(prefix).map(|(key, _)| { self.roomuserid_joined.scan_prefix(prefix).map(|(key, _)| {
Box::<UserId>::try_from( UserId::parse(
utils::string_from_bytes( utils::string_from_bytes(
key.rsplit(|&b| b == 0xff) key.rsplit(|&b| b == 0xff)
.next() .next()
@ -3159,7 +3156,7 @@ impl Rooms {
self.roomuseroncejoinedids self.roomuseroncejoinedids
.scan_prefix(prefix) .scan_prefix(prefix)
.map(|(key, _)| { .map(|(key, _)| {
Box::<UserId>::try_from( UserId::parse(
utils::string_from_bytes( utils::string_from_bytes(
key.rsplit(|&b| b == 0xff) key.rsplit(|&b| b == 0xff)
.next() .next()
@ -3185,7 +3182,7 @@ impl Rooms {
self.roomuserid_invitecount self.roomuserid_invitecount
.scan_prefix(prefix) .scan_prefix(prefix)
.map(|(key, _)| { .map(|(key, _)| {
Box::<UserId>::try_from( UserId::parse(
utils::string_from_bytes( utils::string_from_bytes(
key.rsplit(|&b| b == 0xff) key.rsplit(|&b| b == 0xff)
.next() .next()
@ -3238,7 +3235,7 @@ impl Rooms {
self.userroomid_joined self.userroomid_joined
.scan_prefix(user_id.as_bytes().to_vec()) .scan_prefix(user_id.as_bytes().to_vec())
.map(|(key, _)| { .map(|(key, _)| {
Box::<RoomId>::try_from( RoomId::parse(
utils::string_from_bytes( utils::string_from_bytes(
key.rsplit(|&b| b == 0xff) key.rsplit(|&b| b == 0xff)
.next() .next()
@ -3264,7 +3261,7 @@ impl Rooms {
self.userroomid_invitestate self.userroomid_invitestate
.scan_prefix(prefix) .scan_prefix(prefix)
.map(|(key, state)| { .map(|(key, state)| {
let room_id = Box::<RoomId>::try_from( let room_id = RoomId::parse(
utils::string_from_bytes( utils::string_from_bytes(
key.rsplit(|&b| b == 0xff) key.rsplit(|&b| b == 0xff)
.next() .next()
@ -3337,7 +3334,7 @@ impl Rooms {
self.userroomid_leftstate self.userroomid_leftstate
.scan_prefix(prefix) .scan_prefix(prefix)
.map(|(key, state)| { .map(|(key, state)| {
let room_id = Box::<RoomId>::try_from( let room_id = RoomId::parse(
utils::string_from_bytes( utils::string_from_bytes(
key.rsplit(|&b| b == 0xff) key.rsplit(|&b| b == 0xff)
.next() .next()

@ -11,7 +11,7 @@ use ruma::{
}; };
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
convert::{TryFrom, TryInto}, convert::TryInto,
mem, mem,
sync::Arc, sync::Arc,
}; };
@ -97,7 +97,7 @@ impl RoomEdus {
let count = let count =
utils::u64_from_bytes(&k[prefix.len()..prefix.len() + mem::size_of::<u64>()]) utils::u64_from_bytes(&k[prefix.len()..prefix.len() + mem::size_of::<u64>()])
.map_err(|_| Error::bad_database("Invalid readreceiptid count in db."))?; .map_err(|_| Error::bad_database("Invalid readreceiptid count in db."))?;
let user_id = Box::<UserId>::try_from( let user_id = UserId::parse(
utils::string_from_bytes(&k[prefix.len() + mem::size_of::<u64>() + 1..]) utils::string_from_bytes(&k[prefix.len() + mem::size_of::<u64>() + 1..])
.map_err(|_| { .map_err(|_| {
Error::bad_database("Invalid readreceiptid userid bytes in db.") Error::bad_database("Invalid readreceiptid userid bytes in db.")
@ -310,17 +310,13 @@ impl RoomEdus {
let mut user_ids = HashSet::new(); let mut user_ids = HashSet::new();
for user_id in self for (_, user_id) in self.typingid_userid.scan_prefix(prefix) {
.typingid_userid let user_id = UserId::parse(utils::string_from_bytes(&user_id).map_err(|_| {
.scan_prefix(prefix) Error::bad_database("User ID in typingid_userid is invalid unicode.")
.map(|(_, user_id)| { })?)
Box::<UserId>::try_from(utils::string_from_bytes(&user_id).map_err(|_| { .map_err(|_| Error::bad_database("User ID in typingid_userid is invalid."))?;
Error::bad_database("User ID in typingid_userid is invalid unicode.")
})?) user_ids.insert(user_id);
.map_err(|_| Error::bad_database("User ID in typingid_userid is invalid."))
})
{
user_ids.insert(user_id?);
} }
Ok(SyncEphemeralRoomEvent { Ok(SyncEphemeralRoomEvent {
@ -518,7 +514,7 @@ impl RoomEdus {
.iter_from(&*first_possible_edu, false) .iter_from(&*first_possible_edu, false)
.take_while(|(key, _)| key.starts_with(&prefix)) .take_while(|(key, _)| key.starts_with(&prefix))
{ {
let user_id = Box::<UserId>::try_from( let user_id = UserId::parse(
utils::string_from_bytes( utils::string_from_bytes(
key.rsplit(|&b| b == 0xff) key.rsplit(|&b| b == 0xff)
.next() .next()

@ -1,6 +1,6 @@
use std::{ use std::{
collections::{BTreeMap, HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet},
convert::{TryFrom, TryInto}, convert::TryInto,
fmt::Debug, fmt::Debug,
sync::Arc, sync::Arc,
time::{Duration, Instant}, time::{Duration, Instant},
@ -583,19 +583,18 @@ impl Sending {
} }
} }
let userid = let userid = UserId::parse(utils::string_from_bytes(user).map_err(|_| {
Box::<UserId>::try_from(utils::string_from_bytes(user).map_err(|_| { (
( kind.clone(),
kind.clone(), Error::bad_database("Invalid push user string in db."),
Error::bad_database("Invalid push user string in db."), )
) })?)
})?) .map_err(|_| {
.map_err(|_| { (
( kind.clone(),
kind.clone(), Error::bad_database("Invalid push user id in db."),
Error::bad_database("Invalid push user id in db."), )
) })?;
})?;
let mut senderkey = user.clone(); let mut senderkey = user.clone();
senderkey.push(0xff); senderkey.push(0xff);
@ -732,7 +731,7 @@ impl Sending {
})?; })?;
( (
OutgoingKind::Appservice(Box::<ServerName>::try_from(server).map_err(|_| { OutgoingKind::Appservice(ServerName::parse(server).map_err(|_| {
Error::bad_database("Invalid server string in server_currenttransaction") Error::bad_database("Invalid server string in server_currenttransaction")
})?), })?),
if value.is_empty() { if value.is_empty() {
@ -771,7 +770,7 @@ impl Sending {
})?; })?;
( (
OutgoingKind::Normal(Box::<ServerName>::try_from(server).map_err(|_| { OutgoingKind::Normal(ServerName::parse(server).map_err(|_| {
Error::bad_database("Invalid server string in server_currenttransaction") Error::bad_database("Invalid server string in server_currenttransaction")
})?), })?),
if value.is_empty() { if value.is_empty() {

@ -8,7 +8,7 @@ use ruma::{
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, RoomAliasId, UInt, DeviceId, DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, RoomAliasId, UInt,
UserId, UserId,
}; };
use std::{collections::BTreeMap, convert::TryFrom, mem, sync::Arc}; use std::{collections::BTreeMap, convert::TryInto, mem, sync::Arc};
use tracing::warn; use tracing::warn;
use super::abstraction::Tree; use super::abstraction::Tree;
@ -62,9 +62,8 @@ impl Users {
rooms: &super::rooms::Rooms, rooms: &super::rooms::Rooms,
globals: &super::globals::Globals, globals: &super::globals::Globals,
) -> Result<bool> { ) -> Result<bool> {
let admin_room_alias_id = let admin_room_alias_id = RoomAliasId::parse(format!("#admins:{}", globals.server_name()))
Box::<RoomAliasId>::try_from(format!("#admins:{}", globals.server_name())) .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?;
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?;
let admin_room_id = rooms.id_from_alias(&admin_room_alias_id)?.unwrap(); let admin_room_id = rooms.id_from_alias(&admin_room_alias_id)?.unwrap();
rooms.is_joined(user_id, &admin_room_id) rooms.is_joined(user_id, &admin_room_id)
@ -98,11 +97,9 @@ impl Users {
})?; })?;
Ok(Some(( Ok(Some((
Box::<UserId>::try_from(utils::string_from_bytes(user_bytes).map_err( UserId::parse(utils::string_from_bytes(user_bytes).map_err(|_| {
|_| { Error::bad_database("User ID in token_userdeviceid is invalid unicode.")
Error::bad_database("User ID in token_userdeviceid is invalid unicode.") })?)
},
)?)
.map_err(|_| { .map_err(|_| {
Error::bad_database("User ID in token_userdeviceid is invalid.") Error::bad_database("User ID in token_userdeviceid is invalid.")
})?, })?,
@ -117,7 +114,7 @@ impl Users {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn iter(&self) -> impl Iterator<Item = Result<Box<UserId>>> + '_ { pub fn iter(&self) -> impl Iterator<Item = Result<Box<UserId>>> + '_ {
self.userid_password.iter().map(|(bytes, _)| { self.userid_password.iter().map(|(bytes, _)| {
Box::<UserId>::try_from(utils::string_from_bytes(&bytes).map_err(|_| { UserId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("User ID in userid_password is invalid unicode.") Error::bad_database("User ID in userid_password is invalid unicode.")
})?) })?)
.map_err(|_| Error::bad_database("User ID in userid_password is invalid.")) .map_err(|_| Error::bad_database("User ID in userid_password is invalid."))
@ -189,7 +186,7 @@ impl Users {
.map(|bytes| { .map(|bytes| {
let s = utils::string_from_bytes(&bytes) let s = utils::string_from_bytes(&bytes)
.map_err(|_| Error::bad_database("Avatar URL in db is invalid."))?; .map_err(|_| Error::bad_database("Avatar URL in db is invalid."))?;
Box::<MxcUri>::try_from(s) s.try_into()
.map_err(|_| Error::bad_database("Avatar URL in db is invalid.")) .map_err(|_| Error::bad_database("Avatar URL in db is invalid."))
}) })
.transpose() .transpose()
@ -686,7 +683,7 @@ impl Users {
} }
}) })
.map(|(_, bytes)| { .map(|(_, bytes)| {
Box::<UserId>::try_from(utils::string_from_bytes(&bytes).map_err(|_| { UserId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("User ID in devicekeychangeid_userid is invalid unicode.") Error::bad_database("User ID in devicekeychangeid_userid is invalid unicode.")
})?) })?)
.map_err(|_| Error::bad_database("User ID in devicekeychangeid_userid is invalid.")) .map_err(|_| Error::bad_database("User ID in devicekeychangeid_userid is invalid."))

@ -13,7 +13,7 @@ use serde_json::{
json, json,
value::{to_raw_value, RawValue as RawJsonValue}, value::{to_raw_value, RawValue as RawJsonValue},
}; };
use std::{cmp::Ordering, collections::BTreeMap, convert::TryFrom, ops::Deref}; use std::{cmp::Ordering, collections::BTreeMap, convert::TryInto, ops::Deref};
use tracing::warn; use tracing::warn;
/// Content hashes of a PDU. /// Content hashes of a PDU.
@ -337,12 +337,13 @@ pub(crate) fn gen_event_id_canonical_json(
Error::BadServerResponse("Invalid PDU in server response") Error::BadServerResponse("Invalid PDU in server response")
})?; })?;
let event_id = Box::<EventId>::try_from(&*format!( let event_id = format!(
"${}", "${}",
// Anything higher than version3 behaves the same // Anything higher than version3 behaves the same
ruma::signatures::reference_hash(&value, &RoomVersionId::V6) ruma::signatures::reference_hash(&value, &RoomVersionId::V6)
.expect("ruma can calculate reference hashes") .expect("ruma can calculate reference hashes")
)) )
.try_into()
.expect("ruma's reference hashes are valid event ids"); .expect("ruma's reference hashes are valid event ids");
Ok((event_id, value)) Ok((event_id, value))

@ -20,7 +20,6 @@ use {
}, },
ruma::api::{AuthScheme, IncomingRequest}, ruma::api::{AuthScheme, IncomingRequest},
std::collections::BTreeMap, std::collections::BTreeMap,
std::convert::TryFrom,
std::io::Cursor, std::io::Cursor,
tracing::{debug, warn}, tracing::{debug, warn},
}; };
@ -103,8 +102,7 @@ where
.unwrap() .unwrap()
}, },
|string| { |string| {
Box::<UserId>::try_from(string.expect("parsing to string always works")) UserId::parse(string.expect("parsing to string always works")).unwrap()
.unwrap()
}, },
); );
@ -171,7 +169,7 @@ where
} }
}; };
let origin = match Box::<ServerName>::try_from(origin_str) { let origin = match ServerName::parse(origin_str) {
Ok(s) => s, Ok(s) => s,
_ => { _ => {
warn!( warn!(

@ -544,12 +544,11 @@ pub fn get_server_keys_route(db: DatabaseGuard) -> Json<String> {
return Json("Federation is disabled.".to_owned()); return Json("Federation is disabled.".to_owned());
} }
let mut verify_keys = BTreeMap::new(); let mut verify_keys: BTreeMap<Box<ServerSigningKeyId>, VerifyKey> = BTreeMap::new();
verify_keys.insert( verify_keys.insert(
Box::<ServerSigningKeyId>::try_from( format!("ed25519:{}", db.globals.keypair().version())
format!("ed25519:{}", db.globals.keypair().version()).as_str(), .try_into()
) .expect("found invalid server signing keys in DB"),
.expect("found invalid server signing keys in DB"),
VerifyKey { VerifyKey {
key: base64::encode_config(db.globals.keypair().public_key(), base64::STANDARD_NO_PAD), key: base64::encode_config(db.globals.keypair().public_key(), base64::STANDARD_NO_PAD),
}, },
@ -730,7 +729,7 @@ pub async fn send_transaction_message_route(
// 0. Check the server is in the room // 0. Check the server is in the room
let room_id = match value let room_id = match value
.get("room_id") .get("room_id")
.and_then(|id| Box::<RoomId>::try_from(id.as_str()?).ok()) .and_then(|id| RoomId::parse(id.as_str()?).ok())
{ {
Some(id) => id, Some(id) => id,
None => { None => {
@ -2354,10 +2353,10 @@ pub fn get_event_route(
.and_then(|val| val.as_str()) .and_then(|val| val.as_str())
.ok_or_else(|| Error::bad_database("Invalid event in database"))?; .ok_or_else(|| Error::bad_database("Invalid event in database"))?;
let room_id = Box::<RoomId>::try_from(room_id_str) let room_id = <&RoomId>::try_from(room_id_str)
.map_err(|_| Error::bad_database("Invalid room id field in event in database"))?; .map_err(|_| Error::bad_database("Invalid room id field in event in database"))?;
if !db.rooms.server_in_room(sender_servername, &room_id)? { if !db.rooms.server_in_room(sender_servername, room_id)? {
return Err(Error::BadRequest(ErrorKind::NotFound, "Event not found.")); return Err(Error::BadRequest(ErrorKind::NotFound, "Event not found."));
} }
@ -2408,7 +2407,7 @@ pub fn get_missing_events_route(
.and_then(|val| val.as_str()) .and_then(|val| val.as_str())
.ok_or_else(|| Error::bad_database("Invalid event in database"))?; .ok_or_else(|| Error::bad_database("Invalid event in database"))?;
let event_room_id = Box::<RoomId>::try_from(room_id_str) let event_room_id = <&RoomId>::try_from(room_id_str)
.map_err(|_| Error::bad_database("Invalid room id field in event in database"))?; .map_err(|_| Error::bad_database("Invalid room id field in event in database"))?;
if event_room_id != body.room_id { if event_room_id != body.room_id {
@ -2476,14 +2475,14 @@ pub fn get_event_authorization_route(
.and_then(|val| val.as_str()) .and_then(|val| val.as_str())
.ok_or_else(|| Error::bad_database("Invalid event in database"))?; .ok_or_else(|| Error::bad_database("Invalid event in database"))?;
let room_id = Box::<RoomId>::try_from(room_id_str) let room_id = <&RoomId>::try_from(room_id_str)
.map_err(|_| Error::bad_database("Invalid room id field in event in database"))?; .map_err(|_| Error::bad_database("Invalid room id field in event in database"))?;
if !db.rooms.server_in_room(sender_servername, &room_id)? { if !db.rooms.server_in_room(sender_servername, room_id)? {
return Err(Error::BadRequest(ErrorKind::NotFound, "Event not found.")); return Err(Error::BadRequest(ErrorKind::NotFound, "Event not found."));
} }
let auth_chain_ids = get_auth_chain(&room_id, vec![Arc::from(&*body.event_id)], &db)?; let auth_chain_ids = get_auth_chain(room_id, vec![Arc::from(&*body.event_id)], &db)?;
Ok(get_event_authorization::v1::Response { Ok(get_event_authorization::v1::Response {
auth_chain: auth_chain_ids auth_chain: auth_chain_ids
@ -2948,7 +2947,7 @@ pub async fn create_invite_route(
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Failed to sign event."))?; .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Failed to sign event."))?;
// Generate event id // Generate event id
let event_id = Box::<EventId>::try_from(&*format!( let event_id = EventId::parse(format!(
"${}", "${}",
ruma::signatures::reference_hash(&signed_event, &body.room_version) ruma::signatures::reference_hash(&signed_event, &body.room_version)
.expect("ruma can calculate reference hashes") .expect("ruma can calculate reference hashes")
@ -3224,7 +3223,7 @@ pub(crate) async fn fetch_required_signing_keys(
let fetch_res = fetch_signing_keys( let fetch_res = fetch_signing_keys(
db, db,
&Box::<ServerName>::try_from(&**signature_server).map_err(|_| { signature_server.as_str().try_into().map_err(|_| {
Error::BadServerResponse("Invalid servername in signatures of server response pdu.") Error::BadServerResponse("Invalid servername in signatures of server response pdu.")
})?, })?,
signature_ids, signature_ids,
@ -3262,19 +3261,20 @@ fn get_server_keys_from_cache(
Error::BadServerResponse("Invalid PDU in server response") Error::BadServerResponse("Invalid PDU in server response")
})?; })?;
let event_id = Box::<EventId>::try_from(&*format!( let event_id = format!(
"${}", "${}",
ruma::signatures::reference_hash(&value, room_version) ruma::signatures::reference_hash(&value, room_version)
.expect("ruma can calculate reference hashes") .expect("ruma can calculate reference hashes")
)) );
.expect("ruma's reference hashes are valid event ids"); let event_id = <&EventId>::try_from(event_id.as_str())
.expect("ruma's reference hashes are valid event ids");
if let Some((time, tries)) = db if let Some((time, tries)) = db
.globals .globals
.bad_event_ratelimiter .bad_event_ratelimiter
.read() .read()
.unwrap() .unwrap()
.get(&event_id) .get(event_id)
{ {
// Exponential backoff // Exponential backoff
let mut min_elapsed_duration = Duration::from_secs(30) * (*tries) * (*tries); let mut min_elapsed_duration = Duration::from_secs(30) * (*tries) * (*tries);
@ -3308,7 +3308,7 @@ fn get_server_keys_from_cache(
let contains_all_ids = let contains_all_ids =
|keys: &BTreeMap<String, String>| signature_ids.iter().all(|id| keys.contains_key(id)); |keys: &BTreeMap<String, String>| signature_ids.iter().all(|id| keys.contains_key(id));
let origin = &Box::<ServerName>::try_from(&**signature_server).map_err(|_| { let origin = <&ServerName>::try_from(signature_server.as_str()).map_err(|_| {
Error::BadServerResponse("Invalid servername in signatures of server response pdu.") Error::BadServerResponse("Invalid servername in signatures of server response pdu.")
})?; })?;
@ -3327,7 +3327,7 @@ fn get_server_keys_from_cache(
if !contains_all_ids(&result) { if !contains_all_ids(&result) {
trace!("Signing key not loaded for {}", origin); trace!("Signing key not loaded for {}", origin);
servers.insert(origin.clone(), BTreeMap::new()); servers.insert(origin.to_owned(), BTreeMap::new());
} }
pub_key_map.insert(origin.to_string(), result); pub_key_map.insert(origin.to_string(), result);

Loading…
Cancel
Save