Skip to main content

tuwunel_service/rooms/timeline/
redact.rs

1//! Replaces accepted timeline events with their room-version redacted form.
2//!
3//! Redaction can retain the original JSON for operators and removes searchable
4//! or relational content before overwriting the accepted row. These side
5//! effects are coordinated under the caller's room timeline guard.
6
7use std::mem::take;
8
9use futures::future::join;
10use ruma::{
11	CanonicalJsonObject, CanonicalJsonValue, EventId, RoomId,
12	canonical_json::{RedactedBecause, redact_in_place},
13	events::TimelineEventType,
14	room_version_rules::RedactionRules,
15};
16use tuwunel_core::{
17	Result, err, implement,
18	utils::{BoolExt, OptionExt, result::NotFound, to_canonical_object},
19};
20use tuwunel_matrix::{event::Event, pdu::PRIVATE_UNSIGNED};
21
22use super::Position;
23use crate::{
24	integrity::leading_json,
25	rooms::{
26		short::ShortRoomId,
27		threads::{thread_bundle_mut, thread_root},
28		timeline::RoomMutexGuard,
29	},
30};
31
32/// What recording a redaction on the event it redacts would do.
33///
34/// The unrecorded redactions check decides each redaction through this without
35/// writing, where `restore_redaction` decides and writes under the room's lock.
36pub(crate) enum Restoration {
37	/// Recording it would rewrite the event's row.
38	Due,
39
40	/// Recording it would leave the event as it is.
41	Moot,
42
43	/// A timeline mapping the decision reads does not decode as a PDU id.
44	Malformed,
45
46	/// The event's row is missing or does not decode as the repair reads it, the
47	/// event does not take the redaction, or the redaction does not convert to
48	/// canonical JSON.
49	Unreadable,
50}
51
52/// Replaces an accepted PDU with its room-version redacted form.
53///
54/// Failure to resolve the event's accepted PDU ID is treated as a successful
55/// no-op. Original retention, search removal, relation deletion, and removal
56/// from unread counts occur before the accepted row is replaced, so the
57/// operation is not atomic if a later step fails. A redacted thread reply is
58/// removed from its root's `m.thread` summary (the count, and the latest event
59/// when it was the latest) in the same write as the accepted row, which also
60/// takes a thread left with no replies off the thread list; a redacted thread
61/// root keeps its summary.
62#[implement(super::Service)]
63#[tracing::instrument(name = "redact", level = "debug", skip(self))]
64pub async fn redact_pdu<Pdu: Event + Send + Sync>(
65	&self,
66	event_id: &EventId,
67	reason: &Pdu,
68	shortroomid: ShortRoomId,
69	state_lock: &RoomMutexGuard,
70) -> Result {
71	let Ok(pdu_id) = self.get_pdu_id(event_id).await else {
72		// If event does not exist, just noop
73		// TODO this is actually wrong!
74		return Ok(());
75	};
76
77	let pdu = self
78		.get_pdu_json_from_id(&pdu_id)
79		.await
80		.map_err(|e| {
81			err!(Database(error!(?pdu_id, ?event_id, ?e, "PDU ID points to invalid PDU.")))
82		})?;
83
84	self.services
85		.retention
86		.save_original_pdu(event_id, &pdu, state_lock)
87		.await;
88
89	let body = pdu["content"]
90		.as_object()
91		.and_then(|obj| obj.get("body"))
92		.and_then(|body| body.as_str());
93
94	if let Some(body) = body {
95		self.services
96			.search
97			.deindex_pdu(shortroomid, &pdu_id, body);
98	}
99
100	let room_id: &RoomId = pdu.get("room_id").try_into()?;
101
102	let room_version_id = self
103		.services
104		.state
105		.get_room_version(room_id)
106		.await?;
107
108	let room_version_rules = room_version_id.rules().ok_or_else(|| {
109		err!(Request(UnsupportedRoomVersion(
110			"Cannot redact event for unknown room version {room_version_id:?}."
111		)))
112	})?;
113
114	self.services
115		.pdu_metadata
116		.delete_typed_relation(&pdu_id, &pdu)
117		.await;
118
119	self.services
120		.pusher
121		.redact_pdu(&pdu_id, event_id, &pdu)
122		.await;
123
124	// Read before redaction strips `m.relates_to`.
125	let root_event_id = pdu.get("content").and_then(thread_root);
126	let keep_thread = root_event_id.as_deref() != Some(event_id);
127	let pdu = redact_keeping_thread(
128		pdu,
129		keep_thread,
130		&room_version_rules.redaction,
131		redacted_because(reason.to_canonical_object()),
132	)?;
133
134	let root = root_event_id
135		.as_deref()
136		.map_async(|root| {
137			self.services
138				.threads
139				.redacted_reply_root(root, &pdu_id, event_id)
140		})
141		.await
142		.flatten();
143
144	self.replace_redacted_pdu(&pdu_id, &pdu, root)
145		.await
146}
147
148/// Records an applied redaction on an accepted PDU that does not carry it.
149///
150/// A database migrated from another homeserver implementation can hold an event
151/// whose content a redaction already removed but which lacks `redacted_because`,
152/// so it reads as unredacted. When the content already equals its redacted
153/// form, the row is rewritten in the form `redact_pdu` leaves, skipping the
154/// steps that need the original content. As with `redact_pdu`, the latest of
155/// several redactions is the one recorded. Returns whether the row was
156/// rewritten.
157#[implement(super::Service)]
158#[tracing::instrument(level = "debug", skip(self, reason, rules, _state_lock))]
159pub async fn restore_redaction<Pdu: Event + Send + Sync>(
160	&self,
161	event_id: &EventId,
162	reason: &Pdu,
163	rules: &RedactionRules,
164	_state_lock: &RoomMutexGuard,
165) -> Result<bool> {
166	let Some(pdu_id) = self.get_pdu_id(event_id).await.optional()? else {
167		return Ok(false);
168	};
169
170	let pdu = self.get_pdu_json_from_id(&pdu_id).await?;
171
172	if !restorable(&pdu, reason.room_id()) {
173		return Ok(false);
174	}
175
176	if let Some(recorded) = recorded_redaction(&pdu)
177		&& !self
178			.redaction_precedes(recorded, reason.event_id())
179			.await?
180	{
181		return Ok(false);
182	}
183
184	let because = to_canonical_object(reason.as_pdu()).map(redacted_because)?;
185	let Some(pdu) = already_redacted(pdu, rules, because)? else {
186		return Ok(false);
187	};
188
189	self.replace_redacted_pdu(&pdu_id, &pdu, None)
190		.await
191		.map(|()| true)
192}
193
194/// Whether the redaction a row records comes before `later` in the timeline.
195///
196/// A missing or malformed record, or a missing timeline position, is kept.
197/// Other lookup failures are returned to the caller.
198#[implement(super::Service)]
199async fn redaction_precedes(
200	&self,
201	recorded: &CanonicalJsonValue,
202	later: &EventId,
203) -> Result<bool> {
204	let Some(recorded) = recorded_event_id(recorded) else {
205		return Ok(false);
206	};
207
208	let (recorded, later) = join(self.get_pdu_count(recorded), self.get_pdu_count(later)).await;
209	let precedes = recorded
210		.optional()?
211		.zip(later.optional()?)
212		.is_some_and(|(recorded, later)| recorded < later);
213
214	Ok(precedes)
215}
216
217/// Decides as [`Service::restore_redaction`] does, writing nothing.
218///
219/// No lock is taken, so the answer describes the rows as they stand. Each
220/// timeline mapping is decoded without trusting it, and the event's row is parsed
221/// as a release build of the stored-event decoder parses it, without a debug
222/// build's assertion that no further record follows the event's. So a mapping
223/// the primitive panics on reads as malformed and a row that decoder fails on as
224/// unreadable; only a failed read is an error.
225#[implement(super::Service)]
226#[tracing::instrument(level = "debug", skip_all)]
227pub(crate) async fn redaction_restoration<Pdu: Event + Send + Sync>(
228	&self,
229	event_id: &EventId,
230	reason: &Pdu,
231	rules: &RedactionRules,
232) -> Result<Restoration> {
233	let pdu_id = match self.pdu_position(event_id).await? {
234		| Position::Absent => return Ok(Restoration::Moot),
235		| Position::Damaged => return Ok(Restoration::Malformed),
236		| Position::At(pdu_id) => pdu_id,
237	};
238
239	let pdu = self
240		.db
241		.pduid_pdu
242		.get(&pdu_id)
243		.await
244		.optional()?
245		.and_then(|pdu| leading_json::<CanonicalJsonObject>(&pdu).ok());
246
247	let Some(pdu) = pdu else {
248		return Ok(Restoration::Unreadable);
249	};
250
251	if !restorable(&pdu, reason.room_id()) {
252		return Ok(Restoration::Moot);
253	}
254
255	if let Some(recorded) = recorded_redaction(&pdu) {
256		let Some(precedes) = self
257			.precedes_checked(recorded, reason.event_id())
258			.await?
259		else {
260			return Ok(Restoration::Malformed);
261		};
262
263		if !precedes {
264			return Ok(Restoration::Moot);
265		}
266	}
267
268	let Ok(because) = to_canonical_object(reason.as_pdu()).map(redacted_because) else {
269		return Ok(Restoration::Unreadable);
270	};
271
272	let restoration = match already_redacted(pdu, rules, because) {
273		| Ok(Some(_)) => Restoration::Due,
274		| Ok(None) => Restoration::Moot,
275		| Err(_) => Restoration::Unreadable,
276	};
277
278	Ok(restoration)
279}
280
281/// Whether the redaction a row records comes before `later`, as
282/// `redaction_precedes` decides it.
283///
284/// Each timeline mapping is decoded untrusted, and `None` means one does not
285/// decode, where that would panic.
286#[implement(super::Service)]
287#[tracing::instrument(level = "debug", skip_all)]
288async fn precedes_checked(
289	&self,
290	recorded: &CanonicalJsonValue,
291	later: &EventId,
292) -> Result<Option<bool>> {
293	let Some(recorded) = recorded_event_id(recorded) else {
294		return Ok(Some(false));
295	};
296
297	let (recorded, later) = join(self.pdu_position(recorded), self.pdu_position(later)).await;
298	let precedes = match (recorded?, later?) {
299		| (Position::Damaged, _) | (_, Position::Damaged) => None,
300		| (Position::At(recorded), Position::At(later)) =>
301			Some(recorded.pdu_count() < later.pdu_count()),
302		| _ => Some(false),
303	};
304
305	Ok(precedes)
306}
307
308/// Whether a stored event in the room is of a type a redaction can apply to.
309///
310/// This server and the implementations it migrates from refuse to redact a
311/// create or server ACL event whatever the sender's power, so no such row was
312/// redacted.
313fn restorable(pdu: &CanonicalJsonObject, room_id: &RoomId) -> bool {
314	let in_room = pdu
315		.get("room_id")
316		.and_then(CanonicalJsonValue::as_str)
317		.is_some_and(|id| id == room_id.as_str());
318
319	let redactable = pdu
320		.get("type")
321		.and_then(CanonicalJsonValue::as_str)
322		.map(TimelineEventType::from)
323		.is_none_or(|kind| {
324			!matches!(kind, TimelineEventType::RoomCreate | TimelineEventType::RoomServerAcl)
325		});
326
327	in_room && redactable
328}
329
330fn recorded_redaction(pdu: &CanonicalJsonObject) -> Option<&CanonicalJsonValue> {
331	pdu.get("unsigned")
332		.and_then(CanonicalJsonValue::as_object)
333		.and_then(|unsigned| unsigned.get("redacted_because"))
334}
335
336fn recorded_event_id(recorded: &CanonicalJsonValue) -> Option<&EventId> {
337	recorded
338		.as_object()
339		.and_then(|recorded| recorded.get("event_id"))
340		.and_then(CanonicalJsonValue::as_str)
341		.and_then(|id| <&EventId>::try_from(id).ok())
342}
343
344/// The redacted form of an event whose content the redaction already removed.
345///
346/// `None` when redacting would still remove content, so no redaction has been
347/// applied to it.
348fn already_redacted(
349	pdu: CanonicalJsonObject,
350	rules: &RedactionRules,
351	because: RedactedBecause,
352) -> Result<Option<CanonicalJsonObject>> {
353	let content = pdu.get("content").cloned();
354	let redacted = redact_keeping_thread(pdu, true, rules, because)?;
355	let unchanged = redacted.get("content") == content.as_ref();
356
357	Ok(unchanged.then_some(redacted))
358}
359
360/// Redacts an event, keeping the unsigned fields private to its sender and, when
361/// asked, its thread summary.
362///
363/// The redaction rewrites `unsigned` wholesale. The private fields still go to
364/// the sender alone, since every serving path strips them for anyone else.
365fn redact_keeping_thread(
366	mut pdu: CanonicalJsonObject,
367	keep_thread: bool,
368	rules: &RedactionRules,
369	because: RedactedBecause,
370) -> Result<CanonicalJsonObject> {
371	let thread = keep_thread.and_then(|| thread_bundle_mut(&mut pdu).map(take));
372	let private = PRIVATE_UNSIGNED.map(|key| unsigned_mut(&mut pdu)?.remove_entry(key));
373
374	redact_in_place(&mut pdu, rules, Some(because))
375		.map_err(|err| err!("invalid event: {err}"))?;
376
377	let relations = thread.map(|thread| {
378		let relations = [("m.thread".into(), CanonicalJsonValue::Object(thread))].into();
379
380		("m.relations".into(), CanonicalJsonValue::Object(relations))
381	});
382
383	if let Some(unsigned) = unsigned_mut(&mut pdu) {
384		unsigned.extend(private.into_iter().flatten().chain(relations));
385	}
386
387	Ok(pdu)
388}
389
390fn unsigned_mut(pdu: &mut CanonicalJsonObject) -> Option<&mut CanonicalJsonObject> {
391	pdu.get_mut("unsigned")
392		.and_then(CanonicalJsonValue::as_object_mut)
393}
394
395/// The redaction as recorded on the event it redacts, without its own `unsigned`.
396///
397/// That field holds what the server knows about the redaction, such as the
398/// sender's transaction id, which is not for the readers of the redacted event.
399fn redacted_because(mut reason: CanonicalJsonObject) -> RedactedBecause {
400	reason.remove("unsigned");
401	RedactedBecause::from_json(reason)
402}
403
404#[cfg(test)]
405mod tests {
406	use ruma::{RoomVersionId, room_id};
407	use serde_json::{from_value, json};
408
409	use super::*;
410
411	#[test]
412	fn redaction_keeps_the_targets_private_fields_alone() {
413		let target = object(json!({
414			"type": "m.room.message",
415			"content": { "body": "hello", "msgtype": "m.text" },
416			"unsigned": {
417				"transaction_id": "target-txn",
418				"org.matrix.msc4140.delay_id": "syd_target",
419				"foreign.reference": "$redaction",
420			},
421		}));
422
423		let reason = object(json!({
424			"type": "m.room.redaction",
425			"content": { "redacts": "$target" },
426			"unsigned": { "transaction_id": "redactor-txn" },
427		}));
428
429		let redacted = redact_keeping_thread(target, true, &rules(), redacted_because(reason))
430			.expect("redactable event");
431
432		let expected = object(json!({
433			"transaction_id": "target-txn",
434			"org.matrix.msc4140.delay_id": "syd_target",
435			"redacted_because": {
436				"type": "m.room.redaction",
437				"content": { "redacts": "$target" },
438			},
439		}));
440
441		assert_eq!(unsigned(&redacted), &expected);
442	}
443
444	#[test]
445	fn unredacted_content_is_not_an_already_redacted() {
446		let target = object(json!({
447			"type": "m.room.message",
448			"content": { "body": "hello", "msgtype": "m.text" },
449		}));
450
451		let applied = already_redacted(target, &rules(), because()).expect("redactable event");
452
453		assert!(applied.is_none());
454	}
455
456	#[test]
457	fn emptied_content_is_restored_alone_in_unsigned() {
458		let target = object(json!({
459			"type": "m.room.message",
460			"content": {},
461			"unsigned": { "foreign.reference": "$redaction" },
462		}));
463
464		let redacted = already_redacted(target, &rules(), because())
465			.expect("redactable event")
466			.expect("redaction already applied");
467
468		let unsigned = unsigned(&redacted);
469
470		assert_eq!(unsigned.len(), 1);
471		assert!(unsigned.contains_key("redacted_because"));
472	}
473
474	#[test]
475	fn kept_content_is_an_already_redacted() {
476		let target = object(json!({
477			"type": "m.room.member",
478			"state_key": "@alice:example.org",
479			"content": { "membership": "join" },
480		}));
481
482		let applied = already_redacted(target, &rules(), because()).expect("redactable event");
483
484		assert!(applied.is_some());
485	}
486
487	#[test]
488	fn restored_row_matches_a_native_redaction() {
489		let original = object(json!({
490			"type": "m.room.message",
491			"room_id": "!room:example.org",
492			"sender": "@alice:example.org",
493			"content": { "body": "hello", "msgtype": "m.text" },
494			"unsigned": { "transaction_id": "target-txn" },
495		}));
496
497		let imported = object(json!({
498			"type": "m.room.message",
499			"room_id": "!room:example.org",
500			"sender": "@alice:example.org",
501			"content": {},
502			"unsigned": { "foreign.reference": "$redaction", "transaction_id": "target-txn" },
503		}));
504
505		let native =
506			redact_keeping_thread(original, true, &rules(), because()).expect("redactable event");
507
508		let restored = already_redacted(imported, &rules(), because())
509			.expect("redactable event")
510			.expect("redaction already applied");
511
512		assert_eq!(restored, native);
513	}
514
515	#[test]
516	fn restored_root_keeps_its_thread_summary() {
517		let relations =
518			json!({ "m.thread": { "count": 2, "latest_event": { "event_id": "$reply" } } });
519
520		let target = object(json!({
521			"type": "m.room.message",
522			"content": {},
523			"unsigned": { "m.relations": relations },
524		}));
525
526		let redacted = already_redacted(target, &rules(), because())
527			.expect("redactable event")
528			.expect("redaction already applied");
529
530		assert_eq!(
531			unsigned(&redacted).get("m.relations"),
532			Some(&CanonicalJsonValue::Object(object(relations)))
533		);
534	}
535
536	#[test]
537	fn unredactable_or_foreign_room_rows_are_left_alone() {
538		let room_id = room_id!("!room:example.org");
539		let unrecorded = object(json!({ "room_id": room_id, "content": {} }));
540		let create = object(json!({
541			"type": "m.room.create",
542			"room_id": room_id,
543			"content": { "room_version": "11" },
544		}));
545
546		let elsewhere = object(json!({ "room_id": "!other:example.org", "content": {} }));
547
548		assert!(restorable(&unrecorded, room_id));
549		assert!(!restorable(&create, room_id));
550		assert!(!restorable(&elsewhere, room_id));
551	}
552
553	#[test]
554	fn recorded_redaction_is_found() {
555		let unrecorded = object(json!({ "content": {} }));
556		let recorded = object(json!({
557			"unsigned": { "redacted_because": { "event_id": "$redaction" } },
558		}));
559
560		let nameless = object(json!({ "unsigned": { "redacted_because": {} } }));
561
562		assert!(recorded_redaction(&unrecorded).is_none());
563		assert!(recorded_redaction(&recorded).is_some());
564		assert!(recorded_redaction(&nameless).is_some());
565	}
566
567	fn object(value: serde_json::Value) -> CanonicalJsonObject {
568		from_value(value).expect("canonical JSON object")
569	}
570
571	fn unsigned(pdu: &CanonicalJsonObject) -> &CanonicalJsonObject {
572		pdu.get("unsigned")
573			.and_then(CanonicalJsonValue::as_object)
574			.expect("unsigned object")
575	}
576
577	fn rules() -> RedactionRules {
578		RoomVersionId::V11
579			.rules()
580			.expect("known room version")
581			.redaction
582	}
583
584	fn because() -> RedactedBecause {
585		redacted_because(object(json!({
586			"type": "m.room.redaction",
587			"content": { "redacts": "$target" },
588		})))
589	}
590}