Skip to main content

tuwunel_service/integrity/
roomuserid_joined.rs

1//! The keys of `roomuserid_joined`.
2//!
3//! A key joins a room id and a user id on one separator. A conduwuit-era writer
4//! could double it.
5
6use std::fmt;
7
8use futures::Stream;
9use tuwunel_core::{Result, utils::TryReadyExt};
10use tuwunel_database::{KeyBuf, SEP, keyval::Buffer};
11
12use super::{Check, Report, ScanExt, tally};
13use crate::Services;
14
15type Single = Buffer<64>;
16type Count = Buffer<8>;
17
18pub(super) static CHECK: Check = Check {
19	name: "joined-keys",
20	about: "Every roomuserid_joined key has one separator where its room id ends",
21	quiescent: false,
22	walk: |services, limit| tally(scan(services), limit),
23};
24
25/// A `roomuserid_joined` key whose first separator is not a lone one.
26///
27/// Neither shape decodes as a room and a user. A doubled separator can be closed
28/// up, while a key with none does not say where its room id ends.
29pub enum Finding {
30	/// The separator after the room id is written twice.
31	Doubled {
32		/// The stored key.
33		key: KeyBuf,
34
35		/// The key with its first separator written once.
36		single: Single,
37
38		/// The stored value, which belongs under the corrected key.
39		value: Count,
40	},
41
42	/// The key has no separator at all.
43	Unseparated {
44		/// The stored key.
45		key: KeyBuf,
46	},
47}
48
49/// Streams each `roomuserid_joined` key with a malformed first separator.
50///
51/// Only the first separator is examined. The defect is visible in the row alone,
52/// so any later row is unaffected by a repair of this one.
53#[tracing::instrument(level = "trace", skip_all)]
54pub fn scan(services: &Services) -> impl Stream<Item = Result<Finding>> + Send + '_ {
55	services.db["roomuserid_joined"]
56		.raw_scan()
57		.scanned(&services.server)
58		.ready_try_filter_map(|(key, value)| Ok(classify(key, value)))
59}
60
61/// Classifies one row as [`scan`] does.
62///
63/// A repair reads the row a finding names again and passes it back through here
64/// before writing, so detection and repair share one predicate.
65pub(crate) fn classify(key: &[u8], value: &[u8]) -> Option<Finding> {
66	let Some(at) = key.iter().position(|&byte| byte == SEP) else {
67		return Some(Finding::Unseparated { key: KeyBuf::from_slice(key) });
68	};
69
70	let (room_id, rest) = key.split_at(at);
71
72	rest.starts_with(&[SEP, SEP]).then(|| {
73		let single = room_id
74			.iter()
75			.chain(rest.iter().skip(1))
76			.copied()
77			.collect();
78
79		Finding::Doubled {
80			key: KeyBuf::from_slice(key),
81			single,
82			value: Count::from_slice(value),
83		}
84	})
85}
86
87impl Report for Finding {
88	fn kind(&self) -> &'static str {
89		match self {
90			| Self::Doubled { .. } => "doubled-separator",
91			| Self::Unseparated { .. } => "missing-separator",
92		}
93	}
94}
95
96impl fmt::Display for Finding {
97	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98		match self {
99			| Self::Doubled { key, .. } => {
100				write!(f, "doubled separator in {}", key.escape_ascii())
101			},
102			| Self::Unseparated { key } => {
103				write!(f, "no separator in {}", key.escape_ascii())
104			},
105		}
106	}
107}