|
| 1 | +//! Writes and directories that hold key material or bearer tokens. |
| 2 | +
|
| 3 | +use std::io::Write; |
| 4 | +use std::path::Path; |
| 5 | + |
| 6 | +/// Write `contents` to `path` with owner-only permissions. |
| 7 | +/// |
| 8 | +/// On unix the mode is pinned at creation, so the file never exists with a |
| 9 | +/// permissive mode between open and chmod. `O_NOFOLLOW` refuses to write |
| 10 | +/// through a pre-planted symlink. `mode()` applies only when the file is |
| 11 | +/// created, so the mode is also set after the write to tighten a |
| 12 | +/// pre-existing loose file. |
| 13 | +pub(crate) fn write(path: &Path, contents: &[u8]) -> std::io::Result<()> { |
| 14 | + #[cfg(unix)] |
| 15 | + { |
| 16 | + use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; |
| 17 | + let mut f = std::fs::OpenOptions::new() |
| 18 | + .write(true) |
| 19 | + .create(true) |
| 20 | + .truncate(true) |
| 21 | + .mode(0o600) |
| 22 | + .custom_flags(libc::O_NOFOLLOW) |
| 23 | + .open(path)?; |
| 24 | + f.write_all(contents)?; |
| 25 | + f.set_permissions(std::fs::Permissions::from_mode(0o600))?; |
| 26 | + } |
| 27 | + #[cfg(not(unix))] |
| 28 | + std::fs::write(path, contents)?; |
| 29 | + Ok(()) |
| 30 | +} |
| 31 | + |
| 32 | +/// Create `path` (and missing parents) as an owner-only directory. |
| 33 | +/// |
| 34 | +/// An existing directory is also re-pinned: a `~/.gitlawb` created before |
| 35 | +/// this helper stays group- and world-listable otherwise. |
| 36 | +pub(crate) fn create_dir(path: &Path) -> std::io::Result<()> { |
| 37 | + #[cfg(unix)] |
| 38 | + { |
| 39 | + use std::os::unix::fs::{DirBuilderExt, PermissionsExt}; |
| 40 | + std::fs::DirBuilder::new() |
| 41 | + .recursive(true) |
| 42 | + .mode(0o700) |
| 43 | + .create(path)?; |
| 44 | + std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700))?; |
| 45 | + } |
| 46 | + #[cfg(not(unix))] |
| 47 | + std::fs::create_dir_all(path)?; |
| 48 | + Ok(()) |
| 49 | +} |
| 50 | + |
| 51 | +#[cfg(test)] |
| 52 | +mod tests { |
| 53 | + use tempfile::TempDir; |
| 54 | + |
| 55 | + #[test] |
| 56 | + #[cfg(unix)] |
| 57 | + fn write_creates_file_with_0600() { |
| 58 | + use std::os::unix::fs::PermissionsExt; |
| 59 | + let dir = TempDir::new().unwrap(); |
| 60 | + let path = dir.path().join("key.pem"); |
| 61 | + super::write(&path, b"secret").unwrap(); |
| 62 | + assert_eq!(std::fs::read(&path).unwrap(), b"secret"); |
| 63 | + assert_eq!( |
| 64 | + std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, |
| 65 | + 0o600 |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + #[test] |
| 70 | + #[cfg(unix)] |
| 71 | + fn write_tightens_preexisting_loose_file() { |
| 72 | + use std::os::unix::fs::PermissionsExt; |
| 73 | + let dir = TempDir::new().unwrap(); |
| 74 | + let path = dir.path().join("key.pem"); |
| 75 | + std::fs::write(&path, b"old").unwrap(); |
| 76 | + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); |
| 77 | + super::write(&path, b"new").unwrap(); |
| 78 | + assert_eq!(std::fs::read(&path).unwrap(), b"new"); |
| 79 | + assert_eq!( |
| 80 | + std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, |
| 81 | + 0o600 |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + #[cfg(unix)] |
| 87 | + fn write_refuses_symlink() { |
| 88 | + let dir = TempDir::new().unwrap(); |
| 89 | + let target = dir.path().join("target.pem"); |
| 90 | + std::fs::write(&target, b"planted").unwrap(); |
| 91 | + let link = dir.path().join("link.pem"); |
| 92 | + std::os::unix::fs::symlink(&target, &link).unwrap(); |
| 93 | + assert!(super::write(&link, b"secret").is_err()); |
| 94 | + assert_eq!(std::fs::read(&target).unwrap(), b"planted"); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + #[cfg(unix)] |
| 99 | + fn create_dir_modes_0700_and_tightens_existing() { |
| 100 | + use std::os::unix::fs::PermissionsExt; |
| 101 | + let dir = TempDir::new().unwrap(); |
| 102 | + let path = dir.path().join("a").join("b"); |
| 103 | + super::create_dir(&path).unwrap(); |
| 104 | + assert_eq!( |
| 105 | + std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, |
| 106 | + 0o700 |
| 107 | + ); |
| 108 | + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); |
| 109 | + super::create_dir(&path).unwrap(); |
| 110 | + assert_eq!( |
| 111 | + std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, |
| 112 | + 0o700 |
| 113 | + ); |
| 114 | + } |
| 115 | +} |
0 commit comments