Skip to main content

tuwunel_service/integrity/
userid_password.rs

1//! Hashed sentinels in `userid_password`.
2//!
3//! An account with no password of its own stores the sentinel bare. An older writer
4//! could hash it like any other password.
5
6use std::{fmt, str::from_utf8};
7
8use futures::Stream;
9use tuwunel_core::{
10	Result,
11	smallstr::SmallString,
12	utils::{TryReadyExt, hash::verify_password},
13};
14use tuwunel_database::KeyBuf;
15
16use super::{Check, Report, ScanExt, tally};
17use crate::{
18	Services,
19	users::{PASSWORD_SENTINEL, is_password_hash},
20};
21
22type Id = SmallString<[u8; 48]>;
23
24pub(super) static CHECK: Check = Check {
25	name: "sentinel-passwords",
26	about: "Every userid_password row is text and none holds a hash of the sentinel",
27	quiescent: false,
28	walk: |services, limit| tally(scan(services), limit),
29};
30
31/// A `userid_password` row that is not what its writer stores.
32///
33/// The sentinel marks an account with no password of its own. Hashed, it reads as
34/// a real password rather than that marker.
35pub enum Finding {
36	/// The stored hash verifies against the sentinel.
37	Hashed {
38		/// The user owning the row.
39		user_id: Id,
40	},
41
42	/// The key or the value is not text.
43	Unparsable {
44		/// The stored key.
45		key: KeyBuf,
46	},
47}
48
49/// Streams each password row hashing the sentinel, and each that is not text.
50///
51/// A value that is text but no hash is not reported, so neither is a hash in a
52/// format this build cannot verify. Whether an identity provider is configured
53/// plays no part: the row is the same either way.
54#[tracing::instrument(level = "trace", skip_all)]
55pub fn scan(services: &Services) -> impl Stream<Item = Result<Finding>> + Send + '_ {
56	services.db["userid_password"]
57		.raw_scan()
58		.scanned(&services.server)
59		.ready_try_filter_map(|(key, value)| Ok(classify(key, value)))
60}
61
62/// Classifies one row as [`scan`] does.
63///
64/// A repair reads the row a finding names again and passes it back through here
65/// before writing, so detection and repair share one predicate.
66pub(crate) fn classify(key: &[u8], value: &[u8]) -> Option<Finding> {
67	match (from_utf8(key), from_utf8(value)) {
68		| (Ok(user_id), Ok(value)) =>
69			hashes_sentinel(value).then(|| Finding::Hashed { user_id: user_id.into() }),
70		| _ => Some(Finding::Unparsable { key: KeyBuf::from_slice(key) }),
71	}
72}
73
74impl Report for Finding {
75	fn kind(&self) -> &'static str {
76		match self {
77			| Self::Hashed { .. } => "hashed-sentinel",
78			| Self::Unparsable { .. } => "unparsable-row",
79		}
80	}
81}
82
83impl fmt::Display for Finding {
84	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85		match self {
86			| Self::Hashed { user_id } => {
87				write!(f, "password of {user_id} is a hash of the sentinel")
88			},
89			| Self::Unparsable { key } => write!(f, "row is not text: {}", key.escape_ascii()),
90		}
91	}
92}
93
94fn hashes_sentinel(value: &str) -> bool {
95	is_password_hash(value) && verify_password(PASSWORD_SENTINEL, value).is_ok()
96}