phoebus_os/installer-rs/crates/phoebus-core/src/secret.rs
Phoebus 38214ea9e7 chore: import Phoebus OS installer and Rust migration
Initial public import of the proven Bash installer (installer/) and the production Rust workspace (installer-rs/). The reference ISO, build output and local agent transcripts are gitignored.

Verified: installer/tests/run_tests.sh and cargo test --workspace both pass.
2026-09-20 02:07:33 -07:00

123 lines
3.3 KiB
Rust

//! Secret handling and redaction.
//!
//! `SecretString` never reveals its contents through `Debug`/`Display` and
//! zeroizes on drop. The `Redactor` is a process-wide (per-context) registry of
//! secret values that must not appear in logs. The command runner and logger
//! route every message through it, so callers cannot forget to redact.
use std::fmt;
use std::sync::{Arc, Mutex};
use zeroize::{Zeroize, ZeroizeOnDrop};
/// A string that is redacted in formatting and zeroized on drop.
#[derive(Clone, Zeroize, ZeroizeOnDrop)]
pub struct SecretString(String);
impl SecretString {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
/// Read the secret. Use only where the plaintext is genuinely required
/// (feeding a passphrase to cryptsetup over stdin, hashing a password).
pub fn expose(&self) -> &str {
&self.0
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn into_inner(mut self) -> String {
let out = self.0.clone();
self.0.zeroize();
out
}
}
impl fmt::Debug for SecretString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("<redacted>")
}
}
impl fmt::Display for SecretString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("<redacted>")
}
}
/// Registry of secret plaintexts that must be scrubbed from logs.
#[derive(Clone, Default)]
pub struct Redactor {
inner: Arc<Mutex<Vec<SecretString>>>,
}
impl Redactor {
pub fn new() -> Self {
Self::default()
}
/// Remember a secret so it is scrubbed from every future log line.
pub fn register(&self, secret: &SecretString) {
if secret.is_empty() {
return;
}
if let Ok(mut values) = self.inner.lock() {
// Avoid storing duplicates.
if !values.iter().any(|v| v.expose() == secret.expose()) {
values.push(secret.clone());
}
}
}
pub fn register_plain(&self, secret: &str) {
self.register(&SecretString::new(secret));
}
/// Replace every registered secret in `text` with `<redacted>`.
pub fn redact(&self, text: &str) -> String {
let Ok(values) = self.inner.lock() else {
return text.to_string();
};
let mut out = text.to_string();
for secret in values.iter() {
let plain = secret.expose();
if !plain.is_empty() {
out = out.replace(plain, "<redacted>");
}
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn debug_and_display_are_redacted() {
let s = SecretString::new("hunter2");
assert_eq!(format!("{s:?}"), "<redacted>");
assert_eq!(format!("{s}"), "<redacted>");
assert_eq!(s.expose(), "hunter2");
}
#[test]
fn redactor_scrubs_registered_values() {
let r = Redactor::new();
r.register_plain("hunter2");
assert_eq!(r.redact("pw=hunter2 end"), "pw=<redacted> end");
assert_eq!(r.redact("nothing"), "nothing");
}
#[test]
fn redactor_handles_multiple_and_dedup() {
let r = Redactor::new();
r.register_plain("a1");
r.register_plain("a1");
r.register_plain("bb");
assert_eq!(r.redact("a1 and bb"), "<redacted> and <redacted>");
}
}