Skip to main content

tuwunel_service/rooms/state_res/
mod.rs

1//! State resolution and checks on incoming PDUs according to the [Matrix](https://matrix.org/) specification.
2//!
3//! When creating or receiving a PDU (or event), a server must check whether it
4//! is valid and how it affects the room state. The purpose of this crate is to
5//! provide functions that solve that.
6//!
7//! # Checks performed on receipt of a PDU
8//!
9//! This crate used with [ruma-signatures] should allow to perform all the
10//! [necessary checks on receipt of a PDU].
11//!
12//! To respect the Matrix specification, the following functions should be
13//! called for a PDU:
14//!
15//! 1. `check_rules()` - The event should be dropped on error.
16//! 2. `ruma_signatures::verify_event()` - The event should be dropped on error.
17//!    The PDU should be redacted before checking the authorization rules if the
18//!    result is `Verified::Signatures`.
19//! 3. `check_state_independent_auth_rules()` - The event should be rejected on
20//!    error.
21//! 4. `check_state_dependent_auth_rules()` - This function must be called 3
22//!    times:
23//!     1. With the `auth_events` for the state, the event should be rejected on
24//!        error.
25//!     2. With the state before the event, the event should be rejected on
26//!        error.
27//!     3. With the current state of the room, the event should be "soft failed"
28//!        on error.
29//!
30//! # Room State Resolution
31//!
32//! Because of the distributed nature of Matrix, homeservers might not receive
33//! all events in the same order, which might cause the room state to diverge
34//! temporarily between homeservers. The purpose of [state resolution] is to
35//! make sure that all homeservers arrive at the same room state given the same
36//! events.
37//!
38//! The `resolve()` function allows to do that. It takes an iterator of state
39//! maps and applies the proper state resolution algorithm for the current room
40//! version to output the map of events in the current room state.
41//!
42//! # Event helper types
43//!
44//! The types from [ruma-events] use strict deserialization rules according to
45//! their definition in the specification, which means that they also validate
46//! fields that are not checked when receiving a PDU. Since it is not
47//! appropriate for servers to reject an event that passes those checks, this
48//! crate provides helper types in the [`events`] module, built around the
49//! `Event` trait, to deserialize lazily a handful of fields from the most
50//! common state events. Since these are the same types used for checking the
51//! authorization rules, they are guaranteed to not perform extra validation on
52//! unvalidated fields.
53//!
54//! The types from ruma-events are still appropriate to be used to create a new
55//! event, or to validate an event received from a client.
56//!
57//! [ruma-signatures]: https://crates.io/crates/ruma-signatures
58//! [necessary checks on receipt of a PDU]: https://spec.matrix.org/latest/server-server-api/#checks-performed-on-receipt-of-a-pdu
59//! [ruma-events]: https://crates.io/crates/ruma-events
60
61mod event_auth;
62pub mod events;
63mod fetch_state;
64mod resolve;
65#[cfg(test)]
66mod test_utils;
67pub mod topological_sort;
68
69use tuwunel_core::matrix::TypeStateKey;
70
71use self::{event_auth::check_state_dependent_auth_rules, fetch_state::FetchStateExt};
72pub use self::{
73	event_auth::{AuthTypes, auth_check, auth_types_for_event},
74	resolve::{AuthSet, ConflictMap, StateMap, resolve},
75	topological_sort::topological_sort,
76};