phoebus_os/installer-rs/crates/phoebus-ai/src/privacy.rs

106 lines
3.4 KiB
Rust

//! AI privacy boundary.
//!
//! A system-administration assistant needs *system* access, not the home
//! directory. This module makes that an enforced property of the context
//! builder rather than a prompt instruction: the default policy collects only
//! system facts, and paths under `/home`, `/root`, `~/.ssh`, `~/.gnupg`, and
//! credential directories are rejected by construction.
use std::collections::BTreeMap;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PrivacyPolicy {
/// Whether the user has explicitly granted workspace access. Default off.
pub allow_home: bool,
/// Whether raw application logs may be included. Default off.
pub allow_raw_logs: bool,
}
/// The data that may be sent to a model. Only system facts and explicitly
/// allowed excerpts.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ContextBundle {
pub system: BTreeMap<String, String>,
}
impl ContextBundle {
pub fn insert(&mut self, key: impl Into<String>, value: impl Into<String>) {
self.system.insert(key.into(), value.into());
}
/// Render the bundle for a model, applying redaction and an explicit
/// description of what is *not* being sent.
pub fn render(&self, redactor: &phoebus_core::Redactor) -> String {
let mut out = String::new();
out.push_str("System context (no personal files are included):\n");
for (k, v) in &self.system {
out.push_str(&format!("{k}: {}\n", redactor.redact(v)));
}
out.push_str(
"Excluded: home directory, SSH/GPG keys, browser data, credentials, \
password manager, wallets.\n",
);
out
}
}
/// Paths that must never be included in model context.
pub fn is_forbidden_path(path: &str) -> bool {
let p = path.trim();
let home_like = |s: &str| {
s == "/home" || s.starts_with("/home/") || s == "/root" || s.starts_with("/root/")
};
let sensitive = |s: &str| {
[
".ssh",
".gnupg",
".aws",
".config/gcloud",
".mozilla",
".config/BraveSoftware",
".password-store",
".local/share/keyrings",
".kube",
"wallet",
"Wallets",
]
.iter()
.any(|needle| s.contains(needle))
};
home_like(p) || sensitive(p)
}
#[cfg(test)]
mod tests {
use super::*;
use phoebus_core::{Redactor, SecretString};
#[test]
fn forbidden_paths_are_rejected() {
assert!(is_forbidden_path("/home/alice/Documents"));
assert!(is_forbidden_path("/root/.ssh/id_ed25519"));
assert!(is_forbidden_path("/home/bob/.gnupg/secring.gpg"));
assert!(!is_forbidden_path("/etc/systemd/system"));
assert!(!is_forbidden_path("/proc/loadavg"));
}
#[test]
fn default_policy_is_system_only() {
let p = PrivacyPolicy::default();
assert!(!p.allow_home);
assert!(!p.allow_raw_logs);
}
#[test]
fn render_states_what_is_excluded_and_redacts() {
let redactor = Redactor::new();
redactor.register(&SecretString::new("hunter2"));
let mut bundle = ContextBundle::default();
bundle.insert("failed_units", "0");
bundle.insert("note", "token=hunter2");
let text = bundle.render(&redactor);
assert!(text.contains("Excluded:"));
assert!(text.contains("<redacted>"));
assert!(!text.contains("hunter2"));
}
}