Convert uses of Box<ServerName> to a ref

This commit is contained in:
Devin Ragotzy 2021-01-14 14:39:56 -05:00
parent d108a735a4
commit 52392628e9
7 changed files with 92 additions and 43 deletions

View file

@ -70,7 +70,7 @@ pub async fn get_alias_helper(
.sending .sending
.send_federation_request( .send_federation_request(
&db.globals, &db.globals,
room_alias.server_name().to_owned(), room_alias.server_name(),
federation::query::get_room_information::v1::Request { room_alias }, federation::query::get_room_information::v1::Request { room_alias },
) )
.await?; .await?;

View file

@ -137,7 +137,7 @@ pub async fn get_public_rooms_filtered_helper(
.sending .sending
.send_federation_request( .send_federation_request(
&db.globals, &db.globals,
other_server.to_owned(), other_server,
federation::directory::get_public_rooms_filtered::v1::Request { federation::directory::get_public_rooms_filtered::v1::Request {
limit, limit,
since: since.as_deref(), since: since.as_deref(),

View file

@ -77,7 +77,7 @@ pub async fn get_content_route(
.sending .sending
.send_federation_request( .send_federation_request(
&db.globals, &db.globals,
body.server_name.clone(), &body.server_name,
get_content::Request { get_content::Request {
allow_remote: false, allow_remote: false,
server_name: &body.server_name, server_name: &body.server_name,
@ -126,7 +126,7 @@ pub async fn get_content_thumbnail_route(
.sending .sending
.send_federation_request( .send_federation_request(
&db.globals, &db.globals,
body.server_name.clone(), &body.server_name,
get_content_thumbnail::Request { get_content_thumbnail::Request {
allow_remote: false, allow_remote: false,
height: body.height, height: body.height,

View file

@ -468,7 +468,7 @@ async fn join_room_by_id_helper(
.sending .sending
.send_federation_request( .send_federation_request(
&db.globals, &db.globals,
remote_server.clone(), remote_server,
federation::membership::create_join_event_template::v1::Request { federation::membership::create_join_event_template::v1::Request {
room_id, room_id,
user_id: sender_user, user_id: sender_user,
@ -547,7 +547,7 @@ async fn join_room_by_id_helper(
.sending .sending
.send_federation_request( .send_federation_request(
&db.globals, &db.globals,
remote_server.clone(), 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: &event_id,

View file

@ -303,7 +303,7 @@ impl Sending {
server_server::send_request( server_server::send_request(
&globals, &globals,
server.clone(), &*server,
send_transaction_message::v1::Request { send_transaction_message::v1::Request {
origin: globals.server_name(), origin: globals.server_name(),
pdus: &pdu_jsons, pdus: &pdu_jsons,
@ -348,7 +348,7 @@ impl Sending {
pub async fn send_federation_request<T: OutgoingRequest>( pub async fn send_federation_request<T: OutgoingRequest>(
&self, &self,
globals: &crate::database::globals::Globals, globals: &crate::database::globals::Globals,
destination: Box<ServerName>, destination: &ServerName,
request: T, request: T,
) -> Result<T::IncomingResponse> ) -> Result<T::IncomingResponse>
where where

View file

@ -287,7 +287,7 @@ impl state_res::Event for PduEvent {
/// Generates a correct eventId for the incoming pdu. /// Generates a correct eventId for the incoming pdu.
/// ///
/// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap<String, CanonicalJsonValue>`. /// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap<String, CanonicalJsonValue>`.
pub(crate) fn process_incoming_pdu( pub(crate) fn gen_event_id_canonical_json(
pdu: &Raw<ruma::events::pdu::Pdu>, pdu: &Raw<ruma::events::pdu::Pdu>,
) -> (EventId, CanonicalJsonObject) { ) -> (EventId, CanonicalJsonObject) {
let value = serde_json::from_str(pdu.json().get()).expect("A Raw<...> is always valid JSON"); let value = serde_json::from_str(pdu.json().get()).expect("A Raw<...> is always valid JSON");

View file

@ -20,11 +20,12 @@ use ruma::{
OutgoingRequest, OutgoingRequest,
}, },
directory::{IncomingFilter, IncomingRoomNetwork}, directory::{IncomingFilter, IncomingRoomNetwork},
events::pdu::Pdu,
serde::to_canonical_value, serde::to_canonical_value,
signatures::{CanonicalJsonObject, CanonicalJsonValue, PublicKeyMap}, signatures::{CanonicalJsonObject, CanonicalJsonValue, PublicKeyMap},
EventId, RoomId, RoomVersionId, ServerName, ServerSigningKeyId, UserId, EventId, RoomId, RoomVersionId, ServerName, ServerSigningKeyId, UserId,
}; };
use state_res::{Event, StateMap}; use state_res::{Event, EventMap, StateMap};
use std::{ use std::{
collections::{BTreeMap, BTreeSet}, collections::{BTreeMap, BTreeSet},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
@ -36,7 +37,7 @@ use std::{
pub async fn send_request<T: OutgoingRequest>( pub async fn send_request<T: OutgoingRequest>(
globals: &crate::database::globals::Globals, globals: &crate::database::globals::Globals,
destination: Box<ServerName>, destination: &ServerName,
request: T, request: T,
) -> Result<T::IncomingResponse> ) -> Result<T::IncomingResponse>
where where
@ -50,7 +51,7 @@ where
.actual_destination_cache .actual_destination_cache
.read() .read()
.unwrap() .unwrap()
.get(&destination) .get(destination)
.cloned(); .cloned();
let (actual_destination, host) = if let Some(result) = maybe_result { let (actual_destination, host) = if let Some(result) = maybe_result {
@ -61,7 +62,7 @@ where
.actual_destination_cache .actual_destination_cache
.write() .write()
.unwrap() .unwrap()
.insert(destination.clone(), result.clone()); .insert(Box::<ServerName>::from(destination), result.clone());
result result
}; };
@ -278,9 +279,9 @@ async fn find_actual_destination(
(actual_destination, host) (actual_destination, host)
} }
async fn query_srv_record<'a>( async fn query_srv_record(
globals: &crate::database::globals::Globals, globals: &crate::database::globals::Globals,
hostname: &'a str, hostname: &str,
) -> Option<String> { ) -> Option<String> {
if let Ok(Some(host_port)) = globals if let Ok(Some(host_port)) = globals
.dns_resolver() .dns_resolver()
@ -572,11 +573,9 @@ pub async fn send_transaction_message_route<'a>(
// 1. Is a valid event, otherwise it is dropped. // 1. Is a valid event, otherwise it is dropped.
// Ruma/PduEvent/StateEvent satisfies this // Ruma/PduEvent/StateEvent satisfies this
// We do not add the event_id field to the pdu here because of signature and hashes checks // We do not add the event_id field to the pdu here because of signature and hashes checks
// TODO: ruma may solve this but our `process_incoming_pdu` needs to return a Result then let (event_id, value) = crate::pdu::gen_event_id_canonical_json(pdu);
let (event_id, value) = crate::pdu::process_incoming_pdu(pdu);
// 2. Passes signature checks, otherwise event is dropped. let server_name = &body.body.origin;
// 3. Passes hash checks, otherwise it is redacted before being processed further.
let server_name = body.body.origin.clone();
let mut pub_key_map = BTreeMap::new(); let mut pub_key_map = BTreeMap::new();
if let Some(sig) = value.get("signatures") { if let Some(sig) = value.get("signatures") {
match sig { match sig {
@ -588,7 +587,7 @@ pub async fn send_transaction_message_route<'a>(
.sending .sending
.send_federation_request( .send_federation_request(
&db.globals, &db.globals,
Box::<ServerName>::try_from(key.to_string()).unwrap(), <&ServerName>::try_from(key.as_str()).unwrap(),
get_server_keys::v2::Request::new(), get_server_keys::v2::Request::new(),
) )
.await?; .await?;
@ -616,6 +615,9 @@ pub async fn send_transaction_message_route<'a>(
continue; continue;
} }
// Ruma/PduEvent satisfies - 1. Is a valid event, otherwise it is dropped.
// 2. Passes signature checks, otherwise event is dropped.
// 3. Passes hash checks, otherwise it is redacted before being processed further.
let mut val = match signature_and_hash_check(&pub_key_map, value) { let mut val = match signature_and_hash_check(&pub_key_map, value) {
Ok(pdu) => pdu, Ok(pdu) => pdu,
Err(e) => { Err(e) => {
@ -625,15 +627,20 @@ pub async fn send_transaction_message_route<'a>(
}; };
// Now that we have checked the signature and hashes we can add the eventID and convert // Now that we have checked the signature and hashes we can add the eventID and convert
// to our PduEvent type // to our PduEvent type also finally verifying the first step listed above
val.insert( val.insert(
"event_id".to_owned(), "event_id".to_owned(),
to_canonical_value(&event_id).expect("EventId is a valid CanonicalJsonValue"), to_canonical_value(&event_id).expect("EventId is a valid CanonicalJsonValue"),
); );
let pdu = serde_json::from_value::<PduEvent>( let pdu = match serde_json::from_value::<PduEvent>(
serde_json::to_value(val).expect("CanonicalJsonObj is a valid JsonValue"), serde_json::to_value(val).expect("CanonicalJsonObj is a valid JsonValue"),
) ) {
.expect("Pdu is valid Canonical JSON Map"); Ok(pdu) => pdu,
Err(_) => {
resolved_map.insert(event_id, Err("Event is not a valid PDU".into()));
continue;
}
};
// If we have no idea about this room skip the PDU // If we have no idea about this room skip the PDU
if !db.rooms.exists(&pdu.room_id)? { if !db.rooms.exists(&pdu.room_id)? {
@ -644,18 +651,15 @@ pub async fn send_transaction_message_route<'a>(
let event = Arc::new(pdu.clone()); let event = Arc::new(pdu.clone());
dbg!(&*event); dbg!(&*event);
// Fetch any unknown prev_events or retrieve them from the DB // Fetch any unknown prev_events or retrieve them from the DB
let previous = let previous = match fetch_events(&db, server_name, &pub_key_map, &pdu.prev_events).await {
match fetch_events(&db, server_name.clone(), &pub_key_map, &pdu.prev_events).await { Ok(mut evs) if evs.len() == 1 => Some(Arc::new(evs.remove(0))),
Ok(mut evs) if evs.len() == 1 => Some(Arc::new(evs.remove(0))), _ => None,
_ => None, };
};
// 4. Passes authorization rules based on the event's auth events, otherwise it is rejected. // 4. Passes authorization rules based on the event's auth events, otherwise it is rejected.
// Recursively gather all auth events checking that the previous auth events are valid. // Recursively gather all auth events checking that the previous auth events are valid.
let auth_events: Vec<PduEvent> = let auth_events: Vec<PduEvent> =
match fetch_check_auth_events(&db, server_name.clone(), &pub_key_map, &pdu.prev_events) match fetch_check_auth_events(&db, server_name, &pub_key_map, &pdu.prev_events).await {
.await
{
Ok(events) => events, Ok(events) => events,
Err(_) => { Err(_) => {
resolved_map.insert( resolved_map.insert(
@ -707,7 +711,7 @@ pub async fn send_transaction_message_route<'a>(
.sending .sending
.send_federation_request( .send_federation_request(
&db.globals, &db.globals,
server_name.clone(), server_name,
get_room_state_ids::v1::Request { get_room_state_ids::v1::Request {
room_id: pdu.room_id(), room_id: pdu.room_id(),
event_id: pdu.event_id(), event_id: pdu.event_id(),
@ -716,8 +720,7 @@ pub async fn send_transaction_message_route<'a>(
.await .await
{ {
Ok(res) => { Ok(res) => {
let state = let state = fetch_events(&db, server_name, &pub_key_map, &res.pdu_ids).await?;
fetch_events(&db, server_name.clone(), &pub_key_map, &res.pdu_ids).await?;
// Sanity check: there are no conflicting events in the state we received // Sanity check: there are no conflicting events in the state we received
let mut seen = BTreeSet::new(); let mut seen = BTreeSet::new();
for ev in &state { for ev in &state {
@ -734,7 +737,7 @@ pub async fn send_transaction_message_route<'a>(
( (
state, state,
fetch_events(&db, server_name.clone(), &pub_key_map, &res.auth_chain_ids) fetch_events(&db, server_name, &pub_key_map, &res.auth_chain_ids)
.await? .await?
.into_iter() .into_iter()
.map(Arc::new) .map(Arc::new)
@ -881,6 +884,52 @@ pub async fn send_transaction_message_route<'a>(
Ok(dbg!(send_transaction_message::v1::Response { pdus: resolved_map }).into()) Ok(dbg!(send_transaction_message::v1::Response { pdus: resolved_map }).into())
} }
async fn auth_each_event(
db: &Database,
value: CanonicalJsonObject,
event_id: EventId,
pub_key_map: &PublicKeyMap,
server_name: &ServerName,
auth_cache: EventMap<Arc<PduEvent>>,
) -> std::result::Result<PduEvent, String> {
// Ruma/PduEvent satisfies - 1. Is a valid event, otherwise it is dropped.
// 2. Passes signature checks, otherwise event is dropped.
// 3. Passes hash checks, otherwise it is redacted before being processed further.
let mut val = signature_and_hash_check(&pub_key_map, value)?;
// Now that we have checked the signature and hashes we can add the eventID and convert
// to our PduEvent type also finally verifying the first step listed above
val.insert(
"event_id".to_owned(),
to_canonical_value(&event_id).expect("EventId is a valid CanonicalJsonValue"),
);
let pdu = serde_json::from_value::<PduEvent>(
serde_json::to_value(val).expect("CanonicalJsonObj is a valid JsonValue"),
)
.map_err(|_| "Event is not a valid PDU".to_string())?;
// If we have no idea about this room skip the PDU
if !db.rooms.exists(&pdu.room_id).map_err(|e| e.to_string())? {
return Err("Room is unknown to this server".into());
}
// Fetch any unknown prev_events or retrieve them from the DB
let previous = match fetch_events(&db, server_name, &pub_key_map, &pdu.prev_events).await {
Ok(mut evs) if evs.len() == 1 => Some(Arc::new(evs.remove(0))),
_ => None,
};
// 4. Passes authorization rules based on the event's auth events, otherwise it is rejected.
// Recursively gather all auth events checking that the previous auth events are valid.
let auth_events: Vec<PduEvent> =
match fetch_check_auth_events(&db, server_name, &pub_key_map, &pdu.prev_events).await {
Ok(events) => events,
Err(_) => return Err("Failed to recursively gather auth events".into()),
};
Ok(pdu)
}
fn signature_and_hash_check( fn signature_and_hash_check(
pub_key_map: &ruma::signatures::PublicKeyMap, pub_key_map: &ruma::signatures::PublicKeyMap,
value: CanonicalJsonObject, value: CanonicalJsonObject,
@ -909,7 +958,7 @@ fn signature_and_hash_check(
/// events `auth_events`. If the chain is found to have missing events it fails. /// events `auth_events`. If the chain is found to have missing events it fails.
async fn fetch_check_auth_events( async fn fetch_check_auth_events(
db: &Database, db: &Database,
origin: Box<ServerName>, origin: &ServerName,
key_map: &PublicKeyMap, key_map: &PublicKeyMap,
event_ids: &[EventId], event_ids: &[EventId],
) -> Result<Vec<PduEvent>> { ) -> Result<Vec<PduEvent>> {
@ -929,13 +978,13 @@ async fn fetch_check_auth_events(
.sending .sending
.send_federation_request( .send_federation_request(
&db.globals, &db.globals,
origin.clone(), origin,
get_event::v1::Request { event_id: &ev_id }, get_event::v1::Request { event_id: &ev_id },
) )
.await .await
{ {
Ok(res) => { Ok(res) => {
let (event_id, value) = crate::pdu::process_incoming_pdu(&res.pdu); let (event_id, value) = crate::pdu::gen_event_id_canonical_json(&res.pdu);
match signature_and_hash_check(key_map, value) { match signature_and_hash_check(key_map, value) {
Ok(mut val) => { Ok(mut val) => {
val.insert( val.insert(
@ -970,7 +1019,7 @@ async fn fetch_check_auth_events(
/// effect the state of the room /// effect the state of the room
async fn fetch_events( async fn fetch_events(
db: &Database, db: &Database,
origin: Box<ServerName>, origin: &ServerName,
key_map: &PublicKeyMap, key_map: &PublicKeyMap,
events: &[EventId], events: &[EventId],
) -> Result<Vec<PduEvent>> { ) -> Result<Vec<PduEvent>> {
@ -982,13 +1031,13 @@ async fn fetch_events(
.sending .sending
.send_federation_request( .send_federation_request(
&db.globals, &db.globals,
origin.clone(), origin,
get_event::v1::Request { event_id: id }, get_event::v1::Request { event_id: id },
) )
.await .await
{ {
Ok(res) => { Ok(res) => {
let (event_id, value) = crate::pdu::process_incoming_pdu(&res.pdu); let (event_id, value) = crate::pdu::gen_event_id_canonical_json(&res.pdu);
match signature_and_hash_check(key_map, value) { match signature_and_hash_check(key_map, value) {
Ok(mut val) => { Ok(mut val) => {
// TODO: add to our DB somehow? // TODO: add to our DB somehow?