|
|
|
@ -45,7 +45,7 @@ use ruma::{
|
|
|
|
|
receipt::ReceiptType,
|
|
|
|
|
serde::Raw,
|
|
|
|
|
signatures::{CanonicalJsonObject, CanonicalJsonValue},
|
|
|
|
|
state_res::{self, Event, EventMap, RoomVersion, StateMap},
|
|
|
|
|
state_res::{self, Event, RoomVersion, StateMap},
|
|
|
|
|
to_device::DeviceIdOrAllDevices,
|
|
|
|
|
uint, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName,
|
|
|
|
|
ServerSigningKeyId, UserId,
|
|
|
|
@ -612,7 +612,7 @@ pub async fn send_transaction_message_route(
|
|
|
|
|
// TODO: This could potentially also be some sort of trie (suffix tree) like structure so
|
|
|
|
|
// that once an auth event is known it would know (using indexes maybe) all of the auth
|
|
|
|
|
// events that it references.
|
|
|
|
|
let mut auth_cache = EventMap::new();
|
|
|
|
|
// let mut auth_cache = EventMap::new();
|
|
|
|
|
|
|
|
|
|
for pdu in &body.pdus {
|
|
|
|
|
// We do not add the event_id field to the pdu here because of signature and hashes checks
|
|
|
|
@ -627,15 +627,7 @@ pub async fn send_transaction_message_route(
|
|
|
|
|
let start_time = Instant::now();
|
|
|
|
|
resolved_map.insert(
|
|
|
|
|
event_id.clone(),
|
|
|
|
|
handle_incoming_pdu(
|
|
|
|
|
&body.origin,
|
|
|
|
|
&event_id,
|
|
|
|
|
value,
|
|
|
|
|
true,
|
|
|
|
|
&db,
|
|
|
|
|
&pub_key_map,
|
|
|
|
|
&mut auth_cache,
|
|
|
|
|
)
|
|
|
|
|
handle_incoming_pdu(&body.origin, &event_id, value, true, &db, &pub_key_map)
|
|
|
|
|
.await
|
|
|
|
|
.map(|_| ()),
|
|
|
|
|
);
|
|
|
|
@ -820,7 +812,6 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
is_timeline_event: bool,
|
|
|
|
|
db: &'a Database,
|
|
|
|
|
pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>,
|
|
|
|
|
auth_cache: &'a mut EventMap<Arc<PduEvent>>,
|
|
|
|
|
) -> AsyncRecursiveResult<'a, Option<Vec<u8>>, String> {
|
|
|
|
|
Box::pin(async move {
|
|
|
|
|
// TODO: For RoomVersion6 we must check that Raw<..> is canonical do we anywhere?: https://matrix.org/docs/spec/rooms/v6#canonical-json
|
|
|
|
@ -908,13 +899,7 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
// 5. Reject "due to auth events" if can't get all the auth events or some of the auth events are also rejected "due to auth events"
|
|
|
|
|
// EDIT: Step 5 is not applied anymore because it failed too often
|
|
|
|
|
debug!("Fetching auth events for {}", incoming_pdu.event_id);
|
|
|
|
|
fetch_and_handle_events(
|
|
|
|
|
db,
|
|
|
|
|
origin,
|
|
|
|
|
&incoming_pdu.auth_events,
|
|
|
|
|
pub_key_map,
|
|
|
|
|
auth_cache,
|
|
|
|
|
)
|
|
|
|
|
fetch_and_handle_events(db, origin, &incoming_pdu.auth_events, pub_key_map)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
|
|
|
|
|
@ -927,7 +912,11 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
// Build map of auth events
|
|
|
|
|
let mut auth_events = BTreeMap::new();
|
|
|
|
|
for id in &incoming_pdu.auth_events {
|
|
|
|
|
let auth_event = auth_cache.get(id).ok_or_else(|| {
|
|
|
|
|
let auth_event = db
|
|
|
|
|
.rooms
|
|
|
|
|
.get_pdu(id)
|
|
|
|
|
.map_err(|e| e.to_string())?
|
|
|
|
|
.ok_or_else(|| {
|
|
|
|
|
"Auth event not found, event failed recursive auth checks.".to_string()
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
@ -963,10 +952,10 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
let previous_create = if incoming_pdu.auth_events.len() == 1
|
|
|
|
|
&& incoming_pdu.prev_events == incoming_pdu.auth_events
|
|
|
|
|
{
|
|
|
|
|
auth_cache
|
|
|
|
|
.get(&incoming_pdu.auth_events[0])
|
|
|
|
|
.cloned()
|
|
|
|
|
.filter(|maybe_create| **maybe_create == create_event)
|
|
|
|
|
db.rooms
|
|
|
|
|
.get_pdu(&incoming_pdu.auth_events[0])
|
|
|
|
|
.map_err(|e| e.to_string())?
|
|
|
|
|
.filter(|maybe_create| **maybe_create == *create_event)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
@ -1008,7 +997,6 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
|
|
|
|
|
debug!("Requesting state at event.");
|
|
|
|
|
let mut state_at_incoming_event = None;
|
|
|
|
|
let mut incoming_auth_events = Vec::new();
|
|
|
|
|
|
|
|
|
|
if incoming_pdu.prev_events.len() == 1 {
|
|
|
|
|
let prev_event = &incoming_pdu.prev_events[0];
|
|
|
|
@ -1031,7 +1019,7 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
state_vec.push(prev_event.clone());
|
|
|
|
|
}
|
|
|
|
|
state_at_incoming_event = Some(
|
|
|
|
|
fetch_and_handle_events(db, origin, &state_vec, pub_key_map, auth_cache)
|
|
|
|
|
fetch_and_handle_events(db, origin, &state_vec, pub_key_map)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|_| "Failed to fetch state events locally".to_owned())?
|
|
|
|
|
.into_iter()
|
|
|
|
@ -1069,14 +1057,8 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
{
|
|
|
|
|
Ok(res) => {
|
|
|
|
|
debug!("Fetching state events at event.");
|
|
|
|
|
let state_vec = match fetch_and_handle_events(
|
|
|
|
|
&db,
|
|
|
|
|
origin,
|
|
|
|
|
&res.pdu_ids,
|
|
|
|
|
pub_key_map,
|
|
|
|
|
auth_cache,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
let state_vec =
|
|
|
|
|
match fetch_and_handle_events(&db, origin, &res.pdu_ids, pub_key_map).await
|
|
|
|
|
{
|
|
|
|
|
Ok(state) => state,
|
|
|
|
|
Err(_) => return Err("Failed to fetch state events.".to_owned()),
|
|
|
|
@ -1106,13 +1088,7 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug!("Fetching auth chain events at event.");
|
|
|
|
|
incoming_auth_events = match fetch_and_handle_events(
|
|
|
|
|
&db,
|
|
|
|
|
origin,
|
|
|
|
|
&res.auth_chain_ids,
|
|
|
|
|
pub_key_map,
|
|
|
|
|
auth_cache,
|
|
|
|
|
)
|
|
|
|
|
match fetch_and_handle_events(&db, origin, &res.auth_chain_ids, pub_key_map)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(state) => state,
|
|
|
|
@ -1181,15 +1157,12 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
let mut leaf_state = db
|
|
|
|
|
.rooms
|
|
|
|
|
.state_full(pdu_shortstatehash)
|
|
|
|
|
.map_err(|_| "Failed to ask db for room state.".to_owned())?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k, Arc::new(v)))
|
|
|
|
|
.collect::<StateMap<_>>();
|
|
|
|
|
.map_err(|_| "Failed to ask db for room state.".to_owned())?;
|
|
|
|
|
|
|
|
|
|
if let Some(state_key) = &leaf_pdu.state_key {
|
|
|
|
|
// Now it's the state after
|
|
|
|
|
let key = (leaf_pdu.kind.clone(), state_key.clone());
|
|
|
|
|
leaf_state.insert(key, Arc::new(leaf_pdu));
|
|
|
|
|
leaf_state.insert(key, leaf_pdu);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fork_states.insert(leaf_state);
|
|
|
|
@ -1209,10 +1182,7 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
let current_state = db
|
|
|
|
|
.rooms
|
|
|
|
|
.room_state_full(&room_id)
|
|
|
|
|
.map_err(|_| "Failed to load room state.".to_owned())?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k, Arc::new(v)))
|
|
|
|
|
.collect::<BTreeMap<_, _>>();
|
|
|
|
|
.map_err(|_| "Failed to load room state.".to_owned())?;
|
|
|
|
|
|
|
|
|
|
fork_states.insert(current_state.clone());
|
|
|
|
|
|
|
|
|
@ -1249,13 +1219,7 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
for map in &fork_states {
|
|
|
|
|
let mut state_auth = vec![];
|
|
|
|
|
for auth_id in map.values().flat_map(|pdu| &pdu.auth_events) {
|
|
|
|
|
match fetch_and_handle_events(
|
|
|
|
|
&db,
|
|
|
|
|
origin,
|
|
|
|
|
&[auth_id.clone()],
|
|
|
|
|
pub_key_map,
|
|
|
|
|
auth_cache,
|
|
|
|
|
)
|
|
|
|
|
match fetch_and_handle_events(&db, origin, &[auth_id.clone()], pub_key_map)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
// This should always contain exactly one element when Ok
|
|
|
|
@ -1265,31 +1229,9 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
auth_cache.extend(
|
|
|
|
|
map.iter()
|
|
|
|
|
.map(|pdu| (pdu.1.event_id.clone(), pdu.1.clone())),
|
|
|
|
|
);
|
|
|
|
|
auth_events.push(state_auth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add everything we will need to event_map
|
|
|
|
|
auth_cache.extend(
|
|
|
|
|
auth_events
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|pdus| pdus.iter().map(|pdu| (pdu.event_id.clone(), pdu.clone())))
|
|
|
|
|
.flatten(),
|
|
|
|
|
);
|
|
|
|
|
auth_cache.extend(
|
|
|
|
|
incoming_auth_events
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|pdu| (pdu.event_id().clone(), pdu)),
|
|
|
|
|
);
|
|
|
|
|
auth_cache.extend(
|
|
|
|
|
state_after
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(_, pdu)| (pdu.event_id().clone(), pdu)),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
match state_res::StateResolution::resolve(
|
|
|
|
|
&room_id,
|
|
|
|
|
room_version_id,
|
|
|
|
@ -1305,7 +1247,13 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|pdus| pdus.into_iter().map(|pdu| pdu.event_id().clone()).collect())
|
|
|
|
|
.collect(),
|
|
|
|
|
auth_cache,
|
|
|
|
|
&|id| {
|
|
|
|
|
let res = db.rooms.get_pdu(id);
|
|
|
|
|
if let Err(e) = &res {
|
|
|
|
|
error!("LOOK AT ME Failed to fetch event: {}", e);
|
|
|
|
|
}
|
|
|
|
|
res.ok().flatten()
|
|
|
|
|
},
|
|
|
|
|
) {
|
|
|
|
|
Ok(new_state) => new_state,
|
|
|
|
|
Err(_) => {
|
|
|
|
@ -1365,21 +1313,16 @@ pub fn handle_incoming_pdu<'a>(
|
|
|
|
|
/// Find the event and auth it. Once the event is validated (steps 1 - 8)
|
|
|
|
|
/// it is appended to the outliers Tree.
|
|
|
|
|
///
|
|
|
|
|
/// a. Look in the auth_cache
|
|
|
|
|
/// b. Look in the main timeline (pduid_pdu tree)
|
|
|
|
|
/// c. Look at outlier pdu tree
|
|
|
|
|
/// d. Ask origin server over federation
|
|
|
|
|
/// e. TODO: Ask other servers over federation?
|
|
|
|
|
///
|
|
|
|
|
/// If the event is unknown to the `auth_cache` it is added. This guarantees that any
|
|
|
|
|
/// event we need to know of will be present.
|
|
|
|
|
/// a. Look in the main timeline (pduid_pdu tree)
|
|
|
|
|
/// b. Look at outlier pdu tree
|
|
|
|
|
/// c. Ask origin server over federation
|
|
|
|
|
/// d. TODO: Ask other servers over federation?
|
|
|
|
|
//#[tracing::instrument(skip(db, key_map, auth_cache))]
|
|
|
|
|
pub(crate) fn fetch_and_handle_events<'a>(
|
|
|
|
|
db: &'a Database,
|
|
|
|
|
origin: &'a ServerName,
|
|
|
|
|
events: &'a [EventId],
|
|
|
|
|
pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>,
|
|
|
|
|
auth_cache: &'a mut EventMap<Arc<PduEvent>>,
|
|
|
|
|
) -> AsyncRecursiveResult<'a, Vec<Arc<PduEvent>>, Error> {
|
|
|
|
|
Box::pin(async move {
|
|
|
|
|
let back_off = |id| match db.globals.bad_event_ratelimiter.write().unwrap().entry(id) {
|
|
|
|
@ -1403,31 +1346,17 @@ pub(crate) fn fetch_and_handle_events<'a>(
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// a. Look at auth cache
|
|
|
|
|
let pdu = match auth_cache.get(id) {
|
|
|
|
|
Some(pdu) => {
|
|
|
|
|
// We already have the auth chain for events in cache
|
|
|
|
|
pdu.clone()
|
|
|
|
|
}
|
|
|
|
|
// b. Look in the main timeline (pduid_pdu tree)
|
|
|
|
|
// c. Look at outlier pdu tree
|
|
|
|
|
|
|
|
|
|
// a. Look in the main timeline (pduid_pdu tree)
|
|
|
|
|
// b. Look at outlier pdu tree
|
|
|
|
|
// (get_pdu checks both)
|
|
|
|
|
None => match db.rooms.get_pdu(&id)? {
|
|
|
|
|
let pdu = match db.rooms.get_pdu(&id)? {
|
|
|
|
|
Some(pdu) => {
|
|
|
|
|
trace!("Found {} in db", id);
|
|
|
|
|
// We need to fetch the auth chain
|
|
|
|
|
let _ = fetch_and_handle_events(
|
|
|
|
|
db,
|
|
|
|
|
origin,
|
|
|
|
|
&pdu.auth_events,
|
|
|
|
|
pub_key_map,
|
|
|
|
|
auth_cache,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
Arc::new(pdu)
|
|
|
|
|
pdu
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
// d. Ask origin server over federation
|
|
|
|
|
// c. Ask origin server over federation
|
|
|
|
|
debug!("Fetching {} over federation.", id);
|
|
|
|
|
match db
|
|
|
|
|
.sending
|
|
|
|
@ -1450,7 +1379,6 @@ pub(crate) fn fetch_and_handle_events<'a>(
|
|
|
|
|
false,
|
|
|
|
|
db,
|
|
|
|
|
pub_key_map,
|
|
|
|
|
auth_cache,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
@ -1460,9 +1388,15 @@ pub(crate) fn fetch_and_handle_events<'a>(
|
|
|
|
|
CanonicalJsonValue::String(event_id.into()),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Arc::new(serde_json::from_value(
|
|
|
|
|
serde_json::to_value(value).expect("canonicaljsonobject is valid value"),
|
|
|
|
|
).expect("This is possible because handle_incoming_pdu worked"))
|
|
|
|
|
Arc::new(
|
|
|
|
|
serde_json::from_value(
|
|
|
|
|
serde_json::to_value(value)
|
|
|
|
|
.expect("canonicaljsonobject is valid value"),
|
|
|
|
|
)
|
|
|
|
|
.expect(
|
|
|
|
|
"This is possible because handle_incoming_pdu worked",
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
warn!("Authentication of event {} failed: {:?}", id, e);
|
|
|
|
@ -1478,9 +1412,7 @@ pub(crate) fn fetch_and_handle_events<'a>(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
auth_cache.entry(id.clone()).or_insert_with(|| pdu.clone());
|
|
|
|
|
pdus.push(pdu);
|
|
|
|
|
}
|
|
|
|
|
Ok(pdus)
|
|
|
|
@ -1838,7 +1770,7 @@ pub fn get_event_authorization_route(
|
|
|
|
|
.difference(&auth_chain_ids)
|
|
|
|
|
.cloned(),
|
|
|
|
|
);
|
|
|
|
|
auth_chain_ids.extend(pdu.auth_events.into_iter());
|
|
|
|
|
auth_chain_ids.extend(pdu.auth_events.clone().into_iter());
|
|
|
|
|
|
|
|
|
|
let pdu_json = PduEvent::convert_to_outgoing_federation_event(
|
|
|
|
|
db.rooms.get_pdu_json(&event_id)?.unwrap(),
|
|
|
|
@ -1901,7 +1833,7 @@ pub fn get_room_state_route(
|
|
|
|
|
.difference(&auth_chain_ids)
|
|
|
|
|
.cloned(),
|
|
|
|
|
);
|
|
|
|
|
auth_chain_ids.extend(pdu.auth_events.into_iter());
|
|
|
|
|
auth_chain_ids.extend(pdu.auth_events.clone().into_iter());
|
|
|
|
|
|
|
|
|
|
let pdu_json = PduEvent::convert_to_outgoing_federation_event(
|
|
|
|
|
db.rooms.get_pdu_json(&event_id)?.unwrap(),
|
|
|
|
@ -1954,7 +1886,7 @@ pub fn get_room_state_ids_route(
|
|
|
|
|
.difference(&auth_chain_ids)
|
|
|
|
|
.cloned(),
|
|
|
|
|
);
|
|
|
|
|
auth_chain_ids.extend(pdu.auth_events.into_iter());
|
|
|
|
|
auth_chain_ids.extend(pdu.auth_events.clone().into_iter());
|
|
|
|
|
} else {
|
|
|
|
|
warn!("Could not find pdu mentioned in auth events.");
|
|
|
|
|
}
|
|
|
|
@ -2022,7 +1954,7 @@ pub fn create_join_event_template_route(
|
|
|
|
|
let create_prev_event = if prev_events.len() == 1
|
|
|
|
|
&& Some(&prev_events[0]) == create_event.as_ref().map(|c| &c.event_id)
|
|
|
|
|
{
|
|
|
|
|
create_event.map(Arc::new)
|
|
|
|
|
create_event
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
@ -2066,10 +1998,10 @@ pub fn create_join_event_template_route(
|
|
|
|
|
let mut unsigned = BTreeMap::new();
|
|
|
|
|
|
|
|
|
|
if let Some(prev_pdu) = db.rooms.room_state_get(&body.room_id, &kind, &state_key)? {
|
|
|
|
|
unsigned.insert("prev_content".to_owned(), prev_pdu.content);
|
|
|
|
|
unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone());
|
|
|
|
|
unsigned.insert(
|
|
|
|
|
"prev_sender".to_owned(),
|
|
|
|
|
serde_json::to_value(prev_pdu.sender).expect("UserId::to_value always works"),
|
|
|
|
|
serde_json::to_value(&prev_pdu.sender).expect("UserId::to_value always works"),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -2161,7 +2093,7 @@ pub async fn create_join_event_route(
|
|
|
|
|
))?;
|
|
|
|
|
|
|
|
|
|
let pub_key_map = RwLock::new(BTreeMap::new());
|
|
|
|
|
let mut auth_cache = EventMap::new();
|
|
|
|
|
// let mut auth_cache = EventMap::new();
|
|
|
|
|
|
|
|
|
|
// We do not add the event_id field to the pdu here because of signature and hashes checks
|
|
|
|
|
let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(&body.pdu) {
|
|
|
|
@ -2184,15 +2116,7 @@ pub async fn create_join_event_route(
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Origin field is invalid."))?;
|
|
|
|
|
|
|
|
|
|
let pdu_id = handle_incoming_pdu(
|
|
|
|
|
&origin,
|
|
|
|
|
&event_id,
|
|
|
|
|
value,
|
|
|
|
|
true,
|
|
|
|
|
&db,
|
|
|
|
|
&pub_key_map,
|
|
|
|
|
&mut auth_cache,
|
|
|
|
|
)
|
|
|
|
|
let pdu_id = handle_incoming_pdu(&origin, &event_id, value, true, &db, &pub_key_map)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|_| {
|
|
|
|
|
Error::BadRequest(
|
|
|
|
@ -2220,7 +2144,7 @@ pub async fn create_join_event_route(
|
|
|
|
|
.difference(&auth_chain_ids)
|
|
|
|
|
.cloned(),
|
|
|
|
|
);
|
|
|
|
|
auth_chain_ids.extend(pdu.auth_events.into_iter());
|
|
|
|
|
auth_chain_ids.extend(pdu.auth_events.clone().into_iter());
|
|
|
|
|
} else {
|
|
|
|
|
warn!("Could not find pdu mentioned in auth events.");
|
|
|
|
|
}
|
|
|
|
|