tuwunel_service/integrity/
moderation.rs1use std::{borrow::Cow, fmt, sync::Arc};
8
9use futures::{FutureExt, Stream, StreamExt, future::BoxFuture};
10use ruma::{MilliSecondsSinceUnixEpoch, UserId};
11use serde::Deserialize;
12use tuwunel_core::{
13 Result,
14 arrayvec::ArrayVec,
15 result::NotFound,
16 utils::{BoolExt, IterStream, TryReadyExt, stream::TryBroadbandExt},
17};
18use tuwunel_database::{Json, KeyBuf, Map, serialize_val};
19
20use super::{Check, Report, ScanExt, Summary, tally};
21use crate::{Services, users::Moderation};
22
23type Sources = ArrayVec<Source, { COLUMNS.len() }>;
24
25pub(super) static CHECK: Check = Check {
26 name: "foreign-moderation",
27 about: "Every active suspension and lock a foreign column records is copied into tuwunel's \
28 own",
29 quiescent: true,
30 walk,
31};
32
33const COLUMNS: [(&str, &str); 2] =
37 [("userid_suspension", "userid_suspended"), ("userid_lock", "userid_locked")];
38
39#[derive(Deserialize)]
44struct Foreign<'a> {
45 suspended: bool,
46 suspended_at: MilliSecondsSinceUnixEpoch,
47 #[serde(borrow)]
48 suspended_by: Cow<'a, str>,
49}
50
51pub struct Source {
56 pub name: &'static str,
58
59 pub map: Arc<Map>,
61
62 pub target: &'static str,
64}
65
66pub enum Finding {
71 Uncopied {
73 source: &'static str,
75
76 key: KeyBuf,
78
79 moderation: Moderation,
81 },
82
83 Unparsable {
85 source: &'static str,
87
88 key: KeyBuf,
90 },
91}
92
93pub(crate) enum Entry {
98 Active {
100 key: KeyBuf,
102
103 moderation: Moderation,
105 },
106
107 Unparsable {
109 key: KeyBuf,
111
112 error: serde_json::Error,
114 },
115}
116
117pub fn sources(services: &Services) -> impl Iterator<Item = Result<Source>> + Send + '_ {
121 COLUMNS.into_iter().filter_map(|(name, target)| {
122 services
123 .db
124 .open_cf(name)
125 .transpose()
126 .map(|map| map.map(|map| Source { name, map, target }))
127 })
128}
129
130#[tracing::instrument(
136 level = "trace",
137 skip_all,
138 fields(
139 source = source.name,
140 ),
141)]
142pub fn scan<'a>(
143 services: &'a Services,
144 source: &'a Source,
145) -> impl Stream<Item = Result<Finding>> + Send + 'a {
146 let target = &services.db[source.target];
147
148 entries(services, source)
149 .broad_and_then(async move |entry| {
150 let current = match &entry {
151 | Entry::Active { key, .. } => target.get(key).map(NotFound::optional).await?,
152 | Entry::Unparsable { .. } => None,
153 };
154
155 Ok(finding(source.name, entry, current.as_deref()))
156 })
157 .ready_try_filter_map(Ok)
158}
159
160#[tracing::instrument(
167 level = "trace",
168 skip_all,
169 fields(
170 source = source.name,
171 ),
172)]
173pub(crate) fn entries<'a>(
174 services: &'a Services,
175 source: &'a Source,
176) -> impl Stream<Item = Result<Entry>> + Send + 'a {
177 let fallback = services.globals.server_user.as_ref();
178
179 source
180 .map
181 .raw_scan()
182 .scanned(&services.server)
183 .ready_try_filter_map(move |(key, value)| Ok(entry(key, value, fallback)))
184}
185
186impl Report for Finding {
187 fn kind(&self) -> &'static str {
188 match self {
189 | Self::Uncopied { .. } => "uncopied-entry",
190 | Self::Unparsable { .. } => "unparsable-entry",
191 }
192 }
193}
194
195impl fmt::Display for Finding {
196 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197 match self {
198 | Self::Uncopied { source, key, .. } => {
199 write!(
200 f,
201 "active entry {} in {source} has no copy, or another",
202 key.escape_ascii()
203 )
204 },
205 | Self::Unparsable { source, key } => {
206 write!(f, "entry {} in {source} does not decode", key.escape_ascii())
207 },
208 }
209 }
210}
211
212fn walk(services: &Services, limit: usize) -> BoxFuture<'_, Result<Summary>> {
214 async move {
215 let opened: Sources = sources(services).collect::<Result<_>>()?;
216 let findings = opened
217 .iter()
218 .stream()
219 .flat_map(|source| scan(services, source));
220
221 tally(findings, limit).await
222 }
223 .boxed() }
225
226pub(super) fn finding(
231 source: &'static str,
232 entry: Entry,
233 current: Option<&[u8]>,
234) -> Option<Finding> {
235 let (key, moderation) = match entry {
236 | Entry::Active { key, moderation } => (key, moderation),
237 | Entry::Unparsable { key, .. } => return Some(Finding::Unparsable { source, key }),
238 };
239
240 let copied = current.is_some_and(|current| {
241 serialize_val(Json(&moderation)).is_ok_and(|expected| expected.as_slice().eq(current))
242 });
243
244 copied
245 .is_false()
246 .then_some(Finding::Uncopied { source, key, moderation })
247}
248
249pub(super) fn entry(key: &[u8], value: &[u8], fallback: &UserId) -> Option<Entry> {
254 let entry = match serde_json::from_slice::<Foreign<'_>>(value) {
255 | Ok(entry) => entry,
256 | Err(error) => {
257 return Some(Entry::Unparsable { key: KeyBuf::from_slice(key), error });
258 },
259 };
260
261 entry.suspended.then(|| Entry::Active {
262 key: KeyBuf::from_slice(key),
263 moderation: to_moderation(&entry, fallback),
264 })
265}
266
267fn to_moderation(entry: &Foreign<'_>, fallback: &UserId) -> Moderation {
272 Moderation {
273 when: entry.suspended_at,
274 by: UserId::parse(&*entry.suspended_by).unwrap_or_else(|_| fallback.to_owned()),
275 }
276}