Skip to main content

tuwunel_service/integrity/
readreceiptid_readreceipt.rs

1//! Leftover rows in `readreceiptid_readreceipt`.
2//!
3//! A key is a room id, a stream count and a user id, then a thread kind on any row
4//! written since receipts became thread-aware. An older writer could leave a user's
5//! previous receipt behind.
6
7use std::{fmt, mem::replace, str::from_utf8};
8
9use futures::Stream;
10use ruma::{RoomId, UserId};
11use tuwunel_core::{
12	Result,
13	smallstr::SmallString,
14	utils::{ReadyExt, TryReadyExt},
15};
16use tuwunel_database::{KeyBuf, SEP};
17
18use super::{Check, Report, ScanExt, tally};
19use crate::Services;
20
21type Id = SmallString<[u8; 48]>;
22type Holder = (Id, Id);
23type Row<'a> = (&'a RoomId, &'a UserId, bool);
24
25const COUNT: usize = size_of::<u64>();
26
27pub(super) static CHECK: Check = Check {
28	name: "read-receipts",
29	about: "Receipt keys read back, and none lacking a thread kind repeats the holder before it",
30	quiescent: false,
31	walk: |services, limit| tally(scan(services), limit),
32};
33
34/// A `readreceiptid_readreceipt` row left behind by an earlier receipt.
35///
36/// A row from before thread-aware receipts ends at its user. One directly after
37/// a row of the same room and user is an entry its writer failed to delete.
38pub enum Finding {
39	/// The row repeats the room and user of the row before it.
40	Repeated {
41		/// The stored key.
42		key: KeyBuf,
43
44		/// The room of both rows.
45		room_id: Id,
46
47		/// The user of both rows.
48		user_id: Id,
49	},
50
51	/// The key does not split into a room, a count and a user.
52	Unparsable {
53		/// The stored key.
54		key: KeyBuf,
55	},
56}
57
58/// Streams each repeated row and each key that cannot be read.
59///
60/// Only adjacent rows are compared, in key order, and the first row of a run is
61/// taken as the keeper. A key with a thread kind is compared against but never
62/// reported, because one user holds a row per thread, and a pre-thread row is kept
63/// even where a thread-aware row of the same count follows it. An unreadable key
64/// breaks adjacency, so the rows either side of it are not called repeats.
65#[tracing::instrument(level = "trace", skip_all)]
66pub fn scan(services: &Services) -> impl Stream<Item = Result<Finding>> + Send + '_ {
67	services.db["readreceiptid_readreceipt"]
68		.raw_scan()
69		.scanned(&services.server)
70		.ready_scan(None, |last, row| {
71			row.map(|(key, _)| {
72				let row = parse(key);
73				let repeated = advance(last, row);
74
75				finding(key, row, repeated)
76			})
77			.into()
78		})
79		.ready_try_filter_map(Ok)
80}
81
82impl Report for Finding {
83	fn kind(&self) -> &'static str {
84		match self {
85			| Self::Repeated { .. } => "repeated-receipt",
86			| Self::Unparsable { .. } => "unparsable-key",
87		}
88	}
89}
90
91impl fmt::Display for Finding {
92	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93		match self {
94			| Self::Repeated { room_id, user_id, .. } => {
95				write!(f, "repeated receipt of {user_id} in {room_id}")
96			},
97			| Self::Unparsable { key } => write!(f, "key cannot be read: {}", key.escape_ascii()),
98		}
99	}
100}
101
102/// Reads a key's room and user, and whether a thread kind follows the user.
103///
104/// The key is split here rather than decoded by its tuple type, because a decoder
105/// asserts the layout in debug builds and a damaged key is what this walk is for.
106/// A kind the receipt reader could not decode makes the whole key unreadable.
107fn parse(key: &[u8]) -> Option<Row<'_>> {
108	let (room_id, rest) = split(key)?;
109	let (_count, rest) = rest.split_first_chunk::<COUNT>()?;
110	let rest = rest.strip_prefix(&[SEP])?;
111	let (user_id, kind) =
112		split(rest).map_or((rest, None), |(user_id, kind)| (user_id, Some(kind)));
113
114	let room_id = from_utf8(room_id).ok()?.try_into().ok()?;
115	let user_id = from_utf8(user_id).ok()?.try_into().ok()?;
116
117	kind.is_none_or(readable)
118		.then_some((room_id, user_id, kind.is_some()))
119}
120
121fn split(bytes: &[u8]) -> Option<(&[u8], &[u8])> {
122	let at = bytes.iter().position(|&byte| byte == SEP)?;
123	let (head, tail) = bytes.split_at(at);
124
125	Some((head, tail.strip_prefix(&[SEP])?))
126}
127
128/// Whether a thread kind decodes as the receipt reader takes it.
129///
130/// The reader takes the kind as text, and tolerates one separator after it.
131fn readable(kind: &[u8]) -> bool { from_utf8(kind.strip_suffix(&[SEP]).unwrap_or(kind)).is_ok() }
132
133/// Moves the walk on to a row, reporting whether it has the last row's holder.
134///
135/// An unreadable row leaves no holder behind, which is how it breaks adjacency.
136fn advance(last: &mut Option<Holder>, row: Option<Row<'_>>) -> bool {
137	let previous = replace(last, row.map(holder));
138
139	previous.is_some_and(|previous| last.as_ref().eq(&Some(&previous)))
140}
141
142fn holder((room_id, user_id, _): Row<'_>) -> Holder {
143	(room_id.as_str().into(), user_id.as_str().into())
144}
145
146fn finding(key: &[u8], row: Option<Row<'_>>, repeated: bool) -> Option<Finding> {
147	match row {
148		| None => Some(Finding::Unparsable { key: KeyBuf::from_slice(key) }),
149		| Some((room_id, user_id, false)) if repeated => Some(Finding::Repeated {
150			key: KeyBuf::from_slice(key),
151			room_id: room_id.as_str().into(),
152			user_id: user_id.as_str().into(),
153		}),
154		| Some(_) => None,
155	}
156}