Skip to main content

tuwunel_service/integrity/
redactions.rs

1//! Redactions a foreign database applied without recording them.
2//!
3//! Some origin releases keep only a reference to a redaction on the event it
4//! redacted, both when redacting and in a rewrite of every stored row. On this
5//! server such an event reads as unredacted while its content is already gone.
6
7use std::fmt;
8
9use futures::{Stream, StreamExt, TryStreamExt, stream::once};
10use ruma::{OwnedEventId, OwnedRoomId, RoomVersionId, events::TimelineEventType};
11use serde::Deserialize;
12use tuwunel_core::{
13	Result,
14	result::NotFound,
15	utils::{TryReadyExt, stream::TryBroadbandExt},
16};
17use tuwunel_database::KeyBuf;
18use tuwunel_matrix::{event::Event, pdu::PduEvent, room_version::rules as room_rules};
19
20use super::{Check, Report, ScanExt, per_room, tally};
21use crate::{
22	Services,
23	migrations::marker_present,
24	rooms::timeline::{Mapped, Restoration},
25};
26
27pub(super) static CHECK: Check = Check {
28	name: "unrecorded-redactions",
29	about: "Where an origin recording redactions by reference opened the database, every \
30	        redaction whose target's content is gone is recorded on the target",
31	quiescent: true,
32	walk: |services, limit| tally(scan(services), limit),
33};
34
35// Stamped by the origin when its rewrite of the stored redactions completes.
36const ORIGIN_MARKER: &str = "unembed_unsigned_info";
37
38// Created on every open by an origin release recording redactions by reference.
39const ORIGIN_COLUMN: &str = "servernamekeyid_response";
40
41// A room's version, read once for its redactions; none when it has no create event.
42type Version = Option<Mapped<RoomVersionId>>;
43
44/// The event type of a stored row, read without the rest of the event.
45///
46/// The walk reads every timeline row, so only a row this shows to be a
47/// redaction pays for the full parse.
48#[derive(Deserialize)]
49struct Kind {
50	#[serde(rename = "type")]
51	kind: TimelineEventType,
52}
53
54/// A redaction its target does not record although the target's content is gone.
55///
56/// The repair records each such redaction as a redaction here leaves it. A
57/// redaction this check cannot decide is reported apart, and the repair fails on
58/// it.
59pub enum Finding {
60	/// The target's content is in its redacted form, but its row records this
61	/// redaction nowhere, or only an earlier one.
62	Unrecorded {
63		/// The redaction.
64		redaction: OwnedEventId,
65
66		/// The event it redacts.
67		target: OwnedEventId,
68	},
69
70	/// The row is typed a redaction but does not parse as an event.
71	Unparsable {
72		/// The stored key.
73		key: KeyBuf,
74	},
75
76	/// The redaction's room has no version this server reads and supports.
77	UnknownRoom {
78		/// The redaction.
79		redaction: OwnedEventId,
80
81		/// Its room.
82		room_id: OwnedRoomId,
83	},
84
85	/// A row the decision reads is damaged: one leading from the room's current
86	/// state to its create event, or a timeline mapping, the create event's
87	/// included.
88	MalformedMapping {
89		/// The redaction.
90		redaction: OwnedEventId,
91	},
92
93	/// The target's row is missing or does not decode, the target does not take
94	/// the redaction, or the redaction does not convert to canonical JSON.
95	Undecidable {
96		/// The redaction.
97		redaction: OwnedEventId,
98
99		/// The event it redacts.
100		target: OwnedEventId,
101	},
102}
103
104/// A redaction event the timeline stores, or a row typed a redaction that does
105/// not parse as one.
106///
107/// The event is carried as it was parsed, while a row that does not parse keeps
108/// its key and the parse error, so the check can name the row and the repair can
109/// log why it failed.
110pub(crate) type Redaction = Result<PduEvent, Unparsable>;
111
112/// A timeline row typed a redaction that does not parse as an event.
113///
114/// Its key names the row in a finding, and its error is what the repair logs.
115pub(crate) struct Unparsable {
116	/// The stored key.
117	pub(crate) key: KeyBuf,
118
119	/// Why it does not parse.
120	pub(crate) error: serde_json::Error,
121}
122
123/// Streams each redaction recording would apply to its target, and each one this
124/// check cannot decide.
125///
126/// Only a database an origin recording redactions by reference has opened is
127/// walked, as only there does the repair run. Each redaction is decided as
128/// recording it would decide, against its target as it stands, its room's
129/// version read once for each run of redactions in one room.
130#[tracing::instrument(level = "trace", skip_all)]
131pub fn scan(services: &Services) -> impl Stream<Item = Result<Finding>> + Send + '_ {
132	let redactions = once(records_by_reference(services))
133		.map_ok(move |recorded| recorded.then(|| redactions(services)))
134		.ready_try_filter_map(Ok)
135		.try_flatten()
136		// Erases the stream's type: unboxed, the drop glue of what `per_room` builds
137		// on it has a symbol name over 1 MiB, longer than Apple's linker accepts.
138		.boxed();
139
140	per_room(redactions, PduEvent::room_id, move |room_id| version(services, room_id))
141		.broad_and_then(async |redaction| classify(services, redaction).await)
142		.ready_try_filter_map(Ok)
143}
144
145/// Whether the origin ever opened the database with a release that records
146/// redactions by reference.
147///
148/// The stamp lands only when the origin finishes rewriting the stored rows, while
149/// its redactions take the new form from the first open, so the column that
150/// release creates on every open answers for a rewrite never finished.
151#[tracing::instrument(level = "debug", skip_all)]
152pub(crate) async fn records_by_reference(services: &Services) -> Result<bool> {
153	if services.db.open_cf(ORIGIN_COLUMN)?.is_some() {
154		return Ok(true);
155	}
156
157	marker_present(services, ORIGIN_MARKER).await
158}
159
160/// Streams every redaction event the timeline stores.
161///
162/// The repair attempts each one and the check decides each one, so the two agree
163/// on which redactions there are.
164#[tracing::instrument(level = "trace", skip_all)]
165pub(crate) fn redactions(
166	services: &Services,
167) -> impl Stream<Item = Result<Redaction>> + Send + '_ {
168	services.db["pduid_pdu"]
169		.raw_scan()
170		.scanned(&services.server)
171		.ready_try_filter_map(|(key, value)| Ok(redaction(key, value)))
172}
173
174impl Report for Finding {
175	fn kind(&self) -> &'static str {
176		match self {
177			| Self::Unrecorded { .. } => "unrecorded-redaction",
178			| Self::Unparsable { .. } => "unparsable-redaction",
179			| Self::UnknownRoom { .. } => "unknown-room-version",
180			| Self::MalformedMapping { .. } => "malformed-mapping",
181			| Self::Undecidable { .. } => "undecidable-redaction",
182		}
183	}
184}
185
186impl fmt::Display for Finding {
187	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
188		match self {
189			| Self::Unrecorded { redaction, target } => {
190				write!(f, "{target} lost its content to {redaction} but does not record it")
191			},
192			| Self::Unparsable { key } => {
193				write!(f, "redaction {} does not parse as an event", key.escape_ascii())
194			},
195			| Self::UnknownRoom { redaction, room_id } => {
196				write!(f, "redaction {redaction} is in {room_id}, whose version is unknown")
197			},
198			| Self::MalformedMapping { redaction } =>
199				write!(f, "a row deciding redaction {redaction} is damaged"),
200			| Self::Undecidable { redaction, target } => write!(
201				f,
202				"whether {redaction} applies to {target} is unknown: a row it needs does not \
203				 read"
204			),
205		}
206	}
207}
208
209#[tracing::instrument(level = "debug", skip(services))]
210async fn version(services: &Services, room_id: OwnedRoomId) -> Result<Version> {
211	services
212		.state
213		.room_version_checked(&room_id)
214		.await
215		.present()
216}
217
218#[tracing::instrument(level = "debug", skip_all)]
219async fn classify(
220	services: &Services,
221	redaction: Result<(PduEvent, Version), Unparsable>,
222) -> Result<Option<Finding>> {
223	let (redaction, version) = match redaction {
224		| Ok(redaction) => redaction,
225		| Err(Unparsable { key, .. }) => return Ok(Some(Finding::Unparsable { key })),
226	};
227
228	let room_id = redaction.room_id();
229	let event_id = || redaction.event_id().to_owned();
230	let rules = match version {
231		| Some(Mapped::Event(version)) => room_rules(&version).ok(),
232		| Some(Mapped::Damaged) => {
233			return Ok(Some(Finding::MalformedMapping { redaction: event_id() }));
234		},
235		| None => None,
236	};
237
238	let Some(rules) = rules else {
239		let finding = Finding::UnknownRoom {
240			redaction: event_id(),
241			room_id: room_id.to_owned(),
242		};
243
244		return Ok(Some(finding));
245	};
246
247	let Some(target) = redaction.redacts_id(&rules) else {
248		return Ok(None);
249	};
250
251	let restoration = services
252		.timeline
253		.redaction_restoration(&target, &redaction, &rules.redaction)
254		.await?;
255
256	let finding = match restoration {
257		| Restoration::Due => Some(Finding::Unrecorded { redaction: event_id(), target }),
258		| Restoration::Moot => None,
259		| Restoration::Malformed => Some(Finding::MalformedMapping { redaction: event_id() }),
260		| Restoration::Unreadable => Some(Finding::Undecidable { redaction: event_id(), target }),
261	};
262
263	Ok(finding)
264}
265
266/// The redaction event a timeline row holds.
267///
268/// Any other row is `None`, as is one whose type cannot be read, since nothing
269/// can be restored from it.
270fn redaction(key: &[u8], value: &[u8]) -> Option<Redaction> {
271	serde_json::from_slice(value)
272		.is_ok_and(|probe: Kind| probe.kind.eq(&TimelineEventType::RoomRedaction))
273		.then(|| {
274			serde_json::from_slice(value)
275				.map_err(|error| Unparsable { key: KeyBuf::from_slice(key), error })
276		})
277}