tuwunel_service/integrity/
userid_password.rs1use 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
31pub enum Finding {
36 Hashed {
38 user_id: Id,
40 },
41
42 Unparsable {
44 key: KeyBuf,
46 },
47}
48
49#[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
62pub(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}