| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#[cfg(unix)] |
| 7 |
#[test] |
| 8 |
fn write_private_temp_creates_owner_only_file() { |
| 9 |
use std::os::unix::fs::PermissionsExt; |
| 10 |
|
| 11 |
let dir = std::env::temp_dir().join(format!("go-tst-{}", uuid::Uuid::new_v4().simple())); |
| 12 |
std::fs::create_dir_all(&dir).unwrap(); |
| 13 |
let path = dir.join("secret.txt"); |
| 14 |
|
| 15 |
crate::commands::write_private_temp(&path, b"sensitive body").unwrap(); |
| 16 |
|
| 17 |
let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777; |
| 18 |
assert_eq!(mode, 0o600, "temp file must be owner read/write only"); |
| 19 |
assert_eq!(std::fs::read(&path).unwrap(), b"sensitive body"); |
| 20 |
|
| 21 |
std::fs::remove_dir_all(&dir).ok(); |
| 22 |
} |
| 23 |
|
| 24 |
#[cfg(unix)] |
| 25 |
#[test] |
| 26 |
fn harden_helpers_restrict_dir_and_file() { |
| 27 |
use std::os::unix::fs::PermissionsExt; |
| 28 |
|
| 29 |
let dir = std::env::temp_dir().join(format!("go-tst-{}", uuid::Uuid::new_v4().simple())); |
| 30 |
std::fs::create_dir_all(&dir).unwrap(); |
| 31 |
|
| 32 |
let path = dir.join("blob.bin"); |
| 33 |
std::fs::write(&path, b"decrypted attachment").unwrap(); |
| 34 |
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); |
| 35 |
|
| 36 |
crate::commands::harden_temp_dir(&dir); |
| 37 |
crate::commands::harden_temp_file(&path); |
| 38 |
|
| 39 |
let dir_mode = std::fs::metadata(&dir).unwrap().permissions().mode() & 0o777; |
| 40 |
let file_mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777; |
| 41 |
assert_eq!( |
| 42 |
dir_mode, 0o700, |
| 43 |
"temp dir must be owner-only (not traversable by others)" |
| 44 |
); |
| 45 |
assert_eq!(file_mode, 0o600, "temp file must be owner-only"); |
| 46 |
|
| 47 |
std::fs::remove_dir_all(&dir).ok(); |
| 48 |
} |
| 49 |
|