#![allow(dead_code)]
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
pub fn warn_if_world_readable(path: &Path, kind: &'static str) {
#[cfg(unix)]
{
let Ok(metadata) = std::fs::metadata(path) else {
return;
};
let mode = metadata.permissions().mode();
if mode & 0o077 == 0 {
return;
}
let safe_path = crate::network::redact_home(&path.display().to_string());
tracing::warn!(
event = "security.file_permissive_perms",
kind = kind,
path = %safe_path,
mode = format!("{:o}", mode & 0o777),
"file has permissive permissions; chmod 600 recommended"
);
}
#[cfg(not(unix))]
{
let _ = (path, kind);
}
}
#[cfg(test)]
#[cfg(unix)]
mod tests {
use super::*;
use std::io::Write;
#[test]
fn warn_only_emits_for_world_readable() {
let mut tmp = tempfile::NamedTempFile::new().unwrap();
write!(tmp, "x").unwrap();
let mut perms = tmp.as_file().metadata().unwrap().permissions();
perms.set_mode(0o600);
std::fs::set_permissions(tmp.path(), perms).unwrap();
warn_if_world_readable(tmp.path(), "test");
}
}