1use 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
32pub(crate) enum Restoration {
37 Due,
39
40 Moot,
42
43 Malformed,
45
46 Unreadable,
50}
51
52#[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 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 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#[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#[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#[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#[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
308fn 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
344fn 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
360fn 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
395fn 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}