Skip to main content

tuwunel_service/rooms/state_accessor/
erased.rs

1//! Applies sender-erasure pruning to client and federation event views.
2//!
3//! The helpers retain original events when erasure is disabled or redaction
4//! inputs cannot be resolved. Membership lookup failures are treated as not
5//! joined and can therefore trigger pruning.
6
7use futures::FutureExt;
8use ruma::{
9	CanonicalJsonObject, CanonicalJsonValue, EventId, RoomId, ServerName, UserId,
10	canonical_json::redact_in_place, events::room::member::MembershipState,
11};
12use tuwunel_core::{
13	implement,
14	utils::{
15		BoolExt,
16		result::{FlatOk, LogErr},
17	},
18};
19use tuwunel_matrix::{Event, Pdu};
20
21/// Applies MSC4025 pruning to an event served over federation.
22///
23/// The event is pruned when its sender is erased and `origin` had no joined
24/// user at the event. This check composes with history visibility rather than
25/// replacing it, and unresolved redaction inputs leave the event intact.
26#[implement(super::Service)]
27pub async fn erased_for_server(
28	&self,
29	origin: &ServerName,
30	mut pdu: CanonicalJsonObject,
31) -> CanonicalJsonObject {
32	if !self
33		.services
34		.config
35		.rooms
36		.redaction
37		.enforce_erasure_over_federation
38	{
39		return pdu;
40	}
41
42	let sender = pdu
43		.get("sender")
44		.and_then(CanonicalJsonValue::as_str)
45		.map(<&UserId>::try_from)
46		.flat_ok();
47
48	let Some(sender) = sender else {
49		return pdu;
50	};
51
52	if !self.services.users.is_erased(sender).await {
53		return pdu;
54	}
55
56	let event_id = pdu
57		.get("event_id")
58		.and_then(CanonicalJsonValue::as_str)
59		.map(<&EventId>::try_from)
60		.flat_ok();
61
62	let Some(event_id) = event_id else {
63		return pdu;
64	};
65
66	if self.server_joined_at_pdu(origin, event_id).await {
67		return pdu;
68	}
69
70	let room_id = pdu
71		.get("room_id")
72		.and_then(CanonicalJsonValue::as_str)
73		.map(<&RoomId>::try_from)
74		.flat_ok();
75
76	let Some(room_id) = room_id else {
77		return pdu;
78	};
79
80	let Ok(rules) = self
81		.services
82		.state
83		.get_room_version_rules(room_id)
84		.await
85		.log_err()
86	else {
87		return pdu;
88	};
89
90	redact_in_place(&mut pdu, &rules.redaction, None)
91		.log_err()
92		.ok();
93
94	pdu
95}
96
97/// MSC4025: the pruned clone of `pdu` for recipient `user_id`, or `None` to
98/// serve the original.
99///
100/// A prune that cannot read the room version's redaction rules also yields
101/// `None`, so an unprunable event is served intact rather than withheld.
102#[implement(super::Service)]
103pub async fn erased_view(&self, user_id: &UserId, pdu: &Pdu) -> Option<Pdu> {
104	self.erased_for(user_id, pdu)
105		.map(|erased| erased.then_async(|| self.pruned(pdu)))
106		.flatten()
107		.await
108		.flatten()
109}
110
111/// MSC4025: whether `pdu` serves pruned to `user_id`: its sender is erased
112/// and the recipient was not joined in the room state at the event.
113///
114/// The erasure check runs first and alone: it is a point get that is almost
115/// always false, while the membership read is a state lookup that only an
116/// erased sender needs.
117#[implement(super::Service)]
118pub async fn erased_for(&self, user_id: &UserId, pdu: &Pdu) -> bool {
119	self.services.users.is_erased(pdu.sender()).await
120		&& self
121			.user_membership_at_pdu(user_id, pdu)
122			.map(|membership| membership.ne(&MembershipState::Join))
123			.await
124}
125
126/// Prune per the room version's redaction rules. The pruned form carries no
127/// `redacted_because`; no redaction event exists for a serve-time erasure.
128#[implement(super::Service)]
129async fn pruned(&self, pdu: &Pdu) -> Option<Pdu> {
130	let rules = self
131		.services
132		.state
133		.get_room_version_rules(pdu.room_id())
134		.await
135		.log_err()
136		.ok()?;
137
138	pdu.redacted(&rules.redaction).log_err().ok()
139}