Skip to main content

tuwunel_service/integrity/
mediaid_user.rs

1//! The rows of `mediaid_user`.
2//!
3//! A key is a media id and the user who uploaded it. One check covers the key layout
4//! that came before, the other rows that do not name this server's media.
5
6use std::{fmt, str::from_utf8};
7
8use futures::Stream;
9use ruma::{MxcUri, OwnedMxcUri, OwnedUserId, UserId};
10use tuwunel_core::{
11	Result,
12	itertools::Itertools,
13	utils::{BoolExt, TryReadyExt},
14};
15use tuwunel_database::{KeyBuf, SEP, keyval::Buffer};
16
17use super::{Check, Report, ScanExt, tally};
18use crate::Services;
19
20type Value = Buffer<48>;
21
22pub(super) static LEGACY: Check = Check {
23	name: "media-uploader-keys",
24	about: "Every mediaid_user key has a separator",
25	quiescent: false,
26	walk: |services, limit| tally(legacy(services), limit),
27};
28
29pub(super) static REMOTE: Check = Check {
30	name: "remote-media-uploaders",
31	about: "Every separated mediaid_user key pairs a valid MXC URI here with a user id",
32	quiescent: false,
33	walk: |services, limit| tally(remote(services), limit),
34};
35
36/// A `mediaid_user` key from before keys carried the uploader.
37///
38/// Such a key holds only the media id, with the uploader in the value alone, so a
39/// read by the media id's prefix misses the row.
40pub enum Legacy {
41	/// The value names the uploader, so the key can carry it.
42	Upgradable {
43		/// The stored key.
44		key: KeyBuf,
45
46		/// The uploader the value names.
47		user_id: OwnedUserId,
48	},
49
50	/// The value is not a user id, so nothing can rebuild the row.
51	Unowned {
52		/// The stored key.
53		key: KeyBuf,
54
55		/// The stored value.
56		value: Value,
57	},
58}
59
60/// A `mediaid_user` row with no business being in the column.
61///
62/// The column records who uploaded this server's media, which rules out media
63/// of another server and whatever is not media at all.
64pub enum Remote {
65	/// The media belongs to another server.
66	Foreign {
67		/// The stored key.
68		key: KeyBuf,
69
70		/// The media.
71		mxc: OwnedMxcUri,
72	},
73
74	/// The media id is not a valid MXC URI.
75	Invalid {
76		/// The stored key.
77		key: KeyBuf,
78
79		/// The media id.
80		mxc: OwnedMxcUri,
81	},
82
83	/// The key has a separator but is not exactly a media id and a valid user id.
84	Unparsable {
85		/// The stored key.
86		key: KeyBuf,
87	},
88}
89
90/// Streams each `mediaid_user` key with no separator.
91///
92/// The defect is visible in the row alone, so any later row is unaffected by a
93/// repair of this one. A key with a separator is never reported here, whatever
94/// surrounds it.
95#[tracing::instrument(level = "trace", skip_all)]
96pub fn legacy(services: &Services) -> impl Stream<Item = Result<Legacy>> + Send + '_ {
97	services.db["mediaid_user"]
98		.raw_scan()
99		.scanned(&services.server)
100		.ready_try_filter_map(|(key, value)| Ok(classify_legacy(key, value)))
101}
102
103/// Classifies one row as [`legacy`] does.
104///
105/// A repair reads the row a finding names again and passes it back through here
106/// before writing, so detection and repair share one predicate.
107pub(crate) fn classify_legacy(key: &[u8], value: &[u8]) -> Option<Legacy> {
108	key.contains(&SEP)
109		.or(|| legacy_finding(key, value))
110}
111
112/// Streams each separated `mediaid_user` key that fails to name this server's media.
113///
114/// A key with no separator is left to [`legacy`], which reports every such row, so
115/// no row is reported twice. The server test reads configuration and the media id,
116/// so any later row is unaffected by a repair of this one.
117#[tracing::instrument(level = "trace", skip_all)]
118pub fn remote(services: &Services) -> impl Stream<Item = Result<Remote>> + Send + '_ {
119	services.db["mediaid_user"]
120		.raw_scan()
121		.scanned(&services.server)
122		.ready_try_filter(|(key, _)| key.contains(&SEP))
123		.ready_try_filter_map(|(key, _)| Ok(remote_finding(services, key)))
124}
125
126impl Report for Legacy {
127	fn kind(&self) -> &'static str {
128		match self {
129			| Self::Upgradable { .. } => "legacy-key",
130			| Self::Unowned { .. } => "unowned-key",
131		}
132	}
133}
134
135impl fmt::Display for Legacy {
136	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137		match self {
138			| Self::Upgradable { key, user_id } => {
139				write!(f, "{} has no uploader in its key; {user_id} owns it", key.escape_ascii())
140			},
141			| Self::Unowned { key, .. } => {
142				write!(f, "{} has no uploader in key or value", key.escape_ascii())
143			},
144		}
145	}
146}
147
148impl Report for Remote {
149	fn kind(&self) -> &'static str {
150		match self {
151			| Self::Foreign { .. } => "remote-media",
152			| Self::Invalid { .. } => "invalid-mxc",
153			| Self::Unparsable { .. } => "unparsable-key",
154		}
155	}
156}
157
158impl fmt::Display for Remote {
159	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160		match self {
161			| Self::Foreign { mxc, .. } => write!(f, "{mxc} is another server's media"),
162			| Self::Invalid { mxc, .. } => write!(f, "{mxc} is not a valid MXC URI"),
163			| Self::Unparsable { key } => write!(f, "key cannot be read: {}", key.escape_ascii()),
164		}
165	}
166}
167
168fn legacy_finding(key: &[u8], value: &[u8]) -> Legacy {
169	let user_id = from_utf8(value)
170		.ok()
171		.and_then(|value| <&UserId>::try_from(value).ok());
172
173	match user_id {
174		| None => Legacy::Unowned {
175			key: KeyBuf::from_slice(key),
176			value: Value::from_slice(value),
177		},
178		| Some(user_id) => Legacy::Upgradable {
179			key: KeyBuf::from_slice(key),
180			user_id: user_id.to_owned(),
181		},
182	}
183}
184
185fn remote_finding(services: &Services, key: &[u8]) -> Option<Remote> {
186	let Some(mxc) = media_id(key) else {
187		return Some(Remote::Unparsable { key: KeyBuf::from_slice(key) });
188	};
189
190	let key = || KeyBuf::from_slice(key);
191
192	match mxc.parts() {
193		| Err(_) => Some(Remote::Invalid { key: key(), mxc: mxc.to_owned() }),
194		| Ok(parts) if !services.globals.server_is_ours(parts.server_name) =>
195			Some(Remote::Foreign { key: key(), mxc: mxc.to_owned() }),
196		| Ok(_) => None,
197	}
198}
199
200/// Reads the media id of a key holding exactly a media id and a user id.
201///
202/// The key is split here rather than decoded by its tuple type, because a decoder
203/// asserts the record count in debug builds and a damaged key is what this walk
204/// is for.
205fn media_id(key: &[u8]) -> Option<&MxcUri> {
206	let (mxc, user_id) = key.split(|&byte| byte == SEP).collect_tuple()?;
207	let mxc = from_utf8(mxc).ok()?;
208	let user_id = from_utf8(user_id).ok()?;
209
210	<&UserId>::try_from(user_id)
211		.is_ok()
212		.then(|| mxc.into())
213}