tuwunel_service/integrity/
redactions.rs1use 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
35const ORIGIN_MARKER: &str = "unembed_unsigned_info";
37
38const ORIGIN_COLUMN: &str = "servernamekeyid_response";
40
41type Version = Option<Mapped<RoomVersionId>>;
43
44#[derive(Deserialize)]
49struct Kind {
50 #[serde(rename = "type")]
51 kind: TimelineEventType,
52}
53
54pub enum Finding {
60 Unrecorded {
63 redaction: OwnedEventId,
65
66 target: OwnedEventId,
68 },
69
70 Unparsable {
72 key: KeyBuf,
74 },
75
76 UnknownRoom {
78 redaction: OwnedEventId,
80
81 room_id: OwnedRoomId,
83 },
84
85 MalformedMapping {
89 redaction: OwnedEventId,
91 },
92
93 Undecidable {
96 redaction: OwnedEventId,
98
99 target: OwnedEventId,
101 },
102}
103
104pub(crate) type Redaction = Result<PduEvent, Unparsable>;
111
112pub(crate) struct Unparsable {
116 pub(crate) key: KeyBuf,
118
119 pub(crate) error: serde_json::Error,
121}
122
123#[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 .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#[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#[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
266fn 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}