use std::path::{Component, Path};
const SYSTEM_AND_ADMINISTRATORS: [&str; 2] = ["S-1-5-18", "S-1-5-32-544"];
#[cfg(windows)]
pub(super) fn owner_only(home: &Path, profile: Option<&Path>) -> std::io::Result<()> {
let home = std::fs::canonicalize(home)?;
let profile = profile.and_then(|p| std::fs::canonicalize(p).ok());
if profile.is_some_and(|p| inside(&home, &p)) {
return Ok(());
}
protect(&home)
}
#[cfg(windows)]
#[expect(
clippy::disallowed_methods,
reason = "the store protects its home folder before any run exists, with no process runner to hand"
)]
fn protect(folder: &Path) -> std::io::Result<()> {
use std::os::windows::process::CommandExt;
let headless = windows::Win32::System::Threading::CREATE_NO_WINDOW.0;
let whoami = std::process::Command::new("whoami")
.args(["/user", "/fo", "csv", "/nh"])
.creation_flags(headless)
.output()?;
let sid = account_sid(&String::from_utf8_lossy(&whoami.stdout)).ok_or_else(|| {
std::io::Error::other(format!(
"`whoami /user` didn't name the account: {}",
String::from_utf8_lossy(&whoami.stderr).trim()
))
})?;
let icacls = std::process::Command::new("icacls")
.arg(folder)
.args(grants(&sid))
.creation_flags(headless)
.output()?;
if icacls.status.success() {
Ok(())
} else {
let output = String::from_utf8_lossy(&icacls.stdout).trim().to_owned();
Err(std::io::Error::other(format!("icacls couldn't make it owner-only: {output}")))
}
}
fn inside(path: &Path, folder: &Path) -> bool {
let parts = |p: &Path| -> Vec<String> {
p.components()
.filter(|c| !matches!(c, Component::CurDir))
.map(|c| c.as_os_str().to_string_lossy().to_lowercase())
.flat_map(|c| c.split(['\\', '/']).filter(|s| !s.is_empty()).map(str::to_owned).collect::<Vec<_>>())
.collect()
};
let (path, folder) = (parts(path), parts(folder));
!folder.is_empty() && path.starts_with(&folder)
}
fn account_sid(whoami: &str) -> Option<String> {
let sid = whoami.trim().rsplit(',').next()?.trim().trim_matches('"');
sid.starts_with("S-1-").then(|| sid.to_owned())
}
fn grants(sid: &str) -> Vec<String> {
let mut args = vec!["/inheritance:r".to_owned()];
for who in std::iter::once(sid).chain(SYSTEM_AND_ADMINISTRATORS) {
args.extend(["/grant:r".to_owned(), format!("*{who}:(OI)(CI)F")]);
}
args.push("/Q".to_owned());
args
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_home_in_the_profile_is_inside_it_whatever_the_case_or_slash() {
let profile = Path::new("C:\\Users\\Ada");
assert!(inside(Path::new("c:\\users\\ada\\.isothrum"), profile));
assert!(inside(Path::new("C:/Users/Ada/AppData/isothrum"), profile));
assert!(inside(Path::new("/home/ada/.isothrum"), Path::new("/home/ada")));
assert!(!inside(Path::new("C:\\Users\\Adam\\.isothrum"), profile), "a sibling isn't inside");
assert!(!inside(Path::new("D:\\isothrum"), profile));
assert!(!inside(Path::new("D:\\isothrum"), Path::new("")));
}
#[test]
fn the_account_is_read_from_whoami() {
let sid = "S-1-5-21-1004336348-1177238915-682003330-1001";
assert_eq!(account_sid(&format!("\"desktop-7\\ada\",\"{sid}\"\r\n")).as_deref(), Some(sid));
assert_eq!(account_sid("ERROR: access denied"), None);
}
#[test]
fn the_dacl_is_protected_and_inherited_by_everything_under_it() {
assert_eq!(
grants("S-1-5-21-1"),
[
"/inheritance:r",
"/grant:r",
"*S-1-5-21-1:(OI)(CI)F",
"/grant:r",
"*S-1-5-18:(OI)(CI)F",
"/grant:r",
"*S-1-5-32-544:(OI)(CI)F",
"/Q"
]
);
}
#[cfg(windows)]
#[test]
fn a_protected_folder_keeps_only_its_own_grants() {
let dir = tempfile::tempdir().unwrap();
protect(dir.path()).unwrap();
let out = std::process::Command::new("icacls").arg(dir.path()).output().unwrap();
let acl = String::from_utf8_lossy(&out.stdout);
assert_eq!(acl.matches("(OI)(CI)(F)").count(), 3, "{acl}");
assert!(!acl.contains("(I)"), "nothing inherited: {acl}");
}
}