//! Tests for the private temp-file helpers that keep email previews and opened //! attachments unreadable to other local users on a shared (world-readable) //! system temp dir. See `commands::{harden_temp_dir, harden_temp_file, //! write_private_temp}`. #[cfg(unix)] #[test] fn write_private_temp_creates_owner_only_file() { use std::os::unix::fs::PermissionsExt; let dir = std::env::temp_dir().join(format!("go-tst-{}", uuid::Uuid::new_v4().simple())); std::fs::create_dir_all(&dir).unwrap(); let path = dir.join("secret.txt"); crate::commands::write_private_temp(&path, b"sensitive body").unwrap(); let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777; assert_eq!(mode, 0o600, "temp file must be owner read/write only"); assert_eq!(std::fs::read(&path).unwrap(), b"sensitive body"); std::fs::remove_dir_all(&dir).ok(); } #[cfg(unix)] #[test] fn harden_helpers_restrict_dir_and_file() { use std::os::unix::fs::PermissionsExt; let dir = std::env::temp_dir().join(format!("go-tst-{}", uuid::Uuid::new_v4().simple())); std::fs::create_dir_all(&dir).unwrap(); // Simulate a plain copy landing world-readable (default umask on /tmp). let path = dir.join("blob.bin"); std::fs::write(&path, b"decrypted attachment").unwrap(); std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); crate::commands::harden_temp_dir(&dir); crate::commands::harden_temp_file(&path); let dir_mode = std::fs::metadata(&dir).unwrap().permissions().mode() & 0o777; let file_mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777; assert_eq!( dir_mode, 0o700, "temp dir must be owner-only (not traversable by others)" ); assert_eq!(file_mode, 0o600, "temp file must be owner-only"); std::fs::remove_dir_all(&dir).ok(); }