Support v1/v2 prev_events/auth_events outgoing federation format. (#12)
Support v1/v2 prev_events/auth_events when handling outlier pdu. (#12) Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
20
Cargo.lock
generated
20
Cargo.lock
generated
@@ -3406,7 +3406,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma"
|
name = "ruma"
|
||||||
version = "0.12.6"
|
version = "0.12.6"
|
||||||
source = "git+https://github.com/matrix-construct/ruma?rev=8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6#8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6"
|
source = "git+https://github.com/matrix-construct/ruma?rev=93f28d777073e60687e8013e59d8d16b5adbdf9a#93f28d777073e60687e8013e59d8d16b5adbdf9a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"assign",
|
"assign",
|
||||||
"js_int",
|
"js_int",
|
||||||
@@ -3425,7 +3425,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-appservice-api"
|
name = "ruma-appservice-api"
|
||||||
version = "0.12.2"
|
version = "0.12.2"
|
||||||
source = "git+https://github.com/matrix-construct/ruma?rev=8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6#8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6"
|
source = "git+https://github.com/matrix-construct/ruma?rev=93f28d777073e60687e8013e59d8d16b5adbdf9a#93f28d777073e60687e8013e59d8d16b5adbdf9a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"js_int",
|
"js_int",
|
||||||
"ruma-common",
|
"ruma-common",
|
||||||
@@ -3437,7 +3437,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-client-api"
|
name = "ruma-client-api"
|
||||||
version = "0.20.4"
|
version = "0.20.4"
|
||||||
source = "git+https://github.com/matrix-construct/ruma?rev=8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6#8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6"
|
source = "git+https://github.com/matrix-construct/ruma?rev=93f28d777073e60687e8013e59d8d16b5adbdf9a#93f28d777073e60687e8013e59d8d16b5adbdf9a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"as_variant",
|
"as_variant",
|
||||||
"assign",
|
"assign",
|
||||||
@@ -3460,7 +3460,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-common"
|
name = "ruma-common"
|
||||||
version = "0.15.4"
|
version = "0.15.4"
|
||||||
source = "git+https://github.com/matrix-construct/ruma?rev=8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6#8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6"
|
source = "git+https://github.com/matrix-construct/ruma?rev=93f28d777073e60687e8013e59d8d16b5adbdf9a#93f28d777073e60687e8013e59d8d16b5adbdf9a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"as_variant",
|
"as_variant",
|
||||||
"base64",
|
"base64",
|
||||||
@@ -3493,7 +3493,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-events"
|
name = "ruma-events"
|
||||||
version = "0.30.5"
|
version = "0.30.5"
|
||||||
source = "git+https://github.com/matrix-construct/ruma?rev=8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6#8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6"
|
source = "git+https://github.com/matrix-construct/ruma?rev=93f28d777073e60687e8013e59d8d16b5adbdf9a#93f28d777073e60687e8013e59d8d16b5adbdf9a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"as_variant",
|
"as_variant",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
@@ -3519,7 +3519,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-federation-api"
|
name = "ruma-federation-api"
|
||||||
version = "0.11.2"
|
version = "0.11.2"
|
||||||
source = "git+https://github.com/matrix-construct/ruma?rev=8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6#8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6"
|
source = "git+https://github.com/matrix-construct/ruma?rev=93f28d777073e60687e8013e59d8d16b5adbdf9a#93f28d777073e60687e8013e59d8d16b5adbdf9a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"headers",
|
"headers",
|
||||||
@@ -3541,7 +3541,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-identifiers-validation"
|
name = "ruma-identifiers-validation"
|
||||||
version = "0.10.1"
|
version = "0.10.1"
|
||||||
source = "git+https://github.com/matrix-construct/ruma?rev=8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6#8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6"
|
source = "git+https://github.com/matrix-construct/ruma?rev=93f28d777073e60687e8013e59d8d16b5adbdf9a#93f28d777073e60687e8013e59d8d16b5adbdf9a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"js_int",
|
"js_int",
|
||||||
"thiserror 2.0.14",
|
"thiserror 2.0.14",
|
||||||
@@ -3550,7 +3550,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-macros"
|
name = "ruma-macros"
|
||||||
version = "0.15.2"
|
version = "0.15.2"
|
||||||
source = "git+https://github.com/matrix-construct/ruma?rev=8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6#8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6"
|
source = "git+https://github.com/matrix-construct/ruma?rev=93f28d777073e60687e8013e59d8d16b5adbdf9a#93f28d777073e60687e8013e59d8d16b5adbdf9a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"proc-macro-crate",
|
"proc-macro-crate",
|
||||||
@@ -3565,7 +3565,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-push-gateway-api"
|
name = "ruma-push-gateway-api"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
source = "git+https://github.com/matrix-construct/ruma?rev=8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6#8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6"
|
source = "git+https://github.com/matrix-construct/ruma?rev=93f28d777073e60687e8013e59d8d16b5adbdf9a#93f28d777073e60687e8013e59d8d16b5adbdf9a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"js_int",
|
"js_int",
|
||||||
"ruma-common",
|
"ruma-common",
|
||||||
@@ -3577,7 +3577,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-signatures"
|
name = "ruma-signatures"
|
||||||
version = "0.17.1"
|
version = "0.17.1"
|
||||||
source = "git+https://github.com/matrix-construct/ruma?rev=8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6#8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6"
|
source = "git+https://github.com/matrix-construct/ruma?rev=93f28d777073e60687e8013e59d8d16b5adbdf9a#93f28d777073e60687e8013e59d8d16b5adbdf9a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64",
|
"base64",
|
||||||
"ed25519-dalek",
|
"ed25519-dalek",
|
||||||
|
|||||||
@@ -317,7 +317,7 @@ default-features = false
|
|||||||
|
|
||||||
[workspace.dependencies.ruma]
|
[workspace.dependencies.ruma]
|
||||||
git = "https://github.com/matrix-construct/ruma"
|
git = "https://github.com/matrix-construct/ruma"
|
||||||
rev = "8bc15ba4f145e7b995d36e82e8624c3ac3ce0ef6"
|
rev = "93f28d777073e60687e8013e59d8d16b5adbdf9a"
|
||||||
features = [
|
features = [
|
||||||
"__compat",
|
"__compat",
|
||||||
"appservice-api-c",
|
"appservice-api-c",
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
use ruma::{CanonicalJsonObject, CanonicalJsonValue, RoomVersionId};
|
use ruma::{
|
||||||
|
CanonicalJsonObject, CanonicalJsonValue, EventId, RoomVersionId,
|
||||||
|
room_version_rules::{EventsReferenceFormatVersion, RoomVersionRules},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{is_equal_to, matrix::room_version};
|
use crate::{
|
||||||
|
Result, err, extract_variant, is_equal_to,
|
||||||
|
matrix::{PduEvent, room_version},
|
||||||
|
};
|
||||||
|
|
||||||
pub fn into_outgoing_federation(
|
pub fn into_outgoing_federation(
|
||||||
mut pdu_json: CanonicalJsonObject,
|
mut pdu_json: CanonicalJsonObject,
|
||||||
@@ -35,5 +41,67 @@ pub fn into_outgoing_federation(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if matches!(room_rules.events_reference_format, EventsReferenceFormatVersion::V1) {
|
||||||
|
if let Some(value) = pdu_json.get_mut("auth_events") {
|
||||||
|
mutate_outgoing_reference_format(value);
|
||||||
|
}
|
||||||
|
if let Some(value) = pdu_json.get_mut("prev_events") {
|
||||||
|
mutate_outgoing_reference_format(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pdu_json
|
pdu_json
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn mutate_outgoing_reference_format(value: &mut CanonicalJsonValue) {
|
||||||
|
value
|
||||||
|
.as_array_mut()
|
||||||
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
|
.for_each(|value| {
|
||||||
|
if let Some(event_id) = value.as_str().map(ToOwned::to_owned) {
|
||||||
|
*value = CanonicalJsonValue::Array(vec![
|
||||||
|
CanonicalJsonValue::String(event_id),
|
||||||
|
CanonicalJsonValue::Object([(String::new(), "".into())].into()),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_incoming_federation(
|
||||||
|
event_id: &EventId,
|
||||||
|
pdu_json: &mut CanonicalJsonObject,
|
||||||
|
room_rules: &RoomVersionRules,
|
||||||
|
) -> Result<PduEvent> {
|
||||||
|
if matches!(room_rules.events_reference_format, EventsReferenceFormatVersion::V1) {
|
||||||
|
if let Some(value) = pdu_json.get_mut("auth_events") {
|
||||||
|
mutate_incoming_reference_format(value);
|
||||||
|
}
|
||||||
|
if let Some(value) = pdu_json.get_mut("prev_events") {
|
||||||
|
mutate_incoming_reference_format(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pdu_json.insert("event_id".to_owned(), CanonicalJsonValue::String(event_id.into()));
|
||||||
|
|
||||||
|
serde_json::from_value::<PduEvent>(serde_json::to_value(&pdu_json)?)
|
||||||
|
.map_err(|e| err!(Request(BadJson(debug_warn!("Event is not a valid PDU: {e}")))))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mutate_incoming_reference_format(value: &mut CanonicalJsonValue) {
|
||||||
|
value
|
||||||
|
.as_array_mut()
|
||||||
|
.into_iter()
|
||||||
|
.flat_map(|vec| vec.iter_mut())
|
||||||
|
.for_each(|value| {
|
||||||
|
let event_id = value
|
||||||
|
.as_array()
|
||||||
|
.into_iter()
|
||||||
|
.find_map(|vec| vec.first())
|
||||||
|
.and_then(|val| extract_variant!(val, CanonicalJsonValue::String))
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
*value = CanonicalJsonValue::String(event_id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
use futures::{StreamExt, TryFutureExt};
|
use futures::{StreamExt, TryFutureExt};
|
||||||
use ruma::{
|
use ruma::{
|
||||||
CanonicalJsonObject, CanonicalJsonValue, EventId, RoomId, RoomVersionId, ServerName,
|
CanonicalJsonObject, EventId, RoomId, RoomVersionId, ServerName, events::TimelineEventType,
|
||||||
events::TimelineEventType,
|
|
||||||
};
|
};
|
||||||
use tuwunel_core::{
|
use tuwunel_core::{
|
||||||
Err, Result, debug, debug_info, err, implement,
|
Err, Result, debug, debug_info, err, implement,
|
||||||
matrix::{Event, PduEvent, event::TypeExt, room_version},
|
matrix::{Event, PduEvent, event::TypeExt, room_version},
|
||||||
|
pdu::format::from_incoming_federation,
|
||||||
ref_at, state_res, trace,
|
ref_at, state_res, trace,
|
||||||
utils::{future::TryExtExt, stream::IterStream},
|
utils::{future::TryExtExt, stream::IterStream},
|
||||||
warn,
|
warn,
|
||||||
@@ -66,12 +66,11 @@ pub(super) async fn handle_outlier_pdu(
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Now that we have checked the signature and hashes we can add the eventID and
|
let room_rules = room_version::rules(room_version)?;
|
||||||
// convert to our PduEvent type
|
|
||||||
pdu_json.insert("event_id".to_owned(), CanonicalJsonValue::String(event_id.to_string()));
|
|
||||||
|
|
||||||
let event = serde_json::from_value::<PduEvent>(serde_json::to_value(&pdu_json)?)
|
// Now that we have checked the signature and hashes we can make mutations and
|
||||||
.map_err(|e| err!(Request(BadJson(debug_warn!("Event is not a valid PDU: {e}")))))?;
|
// convert to our PduEvent type.
|
||||||
|
let event = from_incoming_federation(event_id, &mut pdu_json, &room_rules)?;
|
||||||
|
|
||||||
check_room_id(room_id, &event)?;
|
check_room_id(room_id, &event)?;
|
||||||
|
|
||||||
@@ -89,7 +88,6 @@ pub(super) async fn handle_outlier_pdu(
|
|||||||
// auth events
|
// auth events
|
||||||
debug!("Checking based on auth events");
|
debug!("Checking based on auth events");
|
||||||
|
|
||||||
let room_rules = room_version::rules(room_version)?;
|
|
||||||
let is_hydra = !room_rules
|
let is_hydra = !room_rules
|
||||||
.event_format
|
.event_format
|
||||||
.allow_room_create_in_auth_events;
|
.allow_room_create_in_auth_events;
|
||||||
|
|||||||
Reference in New Issue
Block a user