From 9c45cb3e478a519182a7d9bf48af1529b2ccc457 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 31 Oct 2026 05:28:26 +0100 Subject: move warn_if_readable_by_others to files module --- src/files.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs index e27bde6..cd2f38c 100644 --- a/src/files.rs +++ b/src/files.rs @@ -167,6 +167,27 @@ fn sync_parent(path: &Path) { } } +/// A key is a secret: a file others may read is worth a warning (not an error: a +/// group may be deliberate, e.g. shared with a deployment tool). `what`: which key. +pub(crate) fn warn_if_readable_by_others(path: &Path, what: &str) { + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + if let Ok(metadata) = std::fs::metadata(path) { + let mode = metadata.permissions().mode(); + if mode & 0o077 != 0 { + tracing::warn!( + path = %path.display(), + mode = format!("{:o}", mode & 0o777), + "The {what} file is readable by others than its owner; chmod 600 it" + ); + } + } + } + #[cfg(not(unix))] + let _ = (path, what); +} + fn with_suffix(path: &Path, suffix: &str) -> PathBuf { let mut name = path.as_os_str().to_owned(); name.push(suffix); -- cgit v1.3.1