Merge pull request #1285 from gitbutlerapp/Set-user-only-permissions

Set files in data dir to user only (0o600)
This commit is contained in:
Scott Chacon 2023-10-04 15:23:09 +02:00 committed by GitHub
commit 002a52af57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -71,14 +71,24 @@ impl Storage {
#[cfg(test)]
mod tests {
use std::{fs, os::unix::prelude::PermissionsExt};
use super::*;
#[test]
fn test_get_or_create() {
let dir = tempfile::tempdir().unwrap();
let controller = Storage::from(&dir.path().to_path_buf());
let once = controller.get_or_create().unwrap();
let twice = controller.get_or_create().unwrap();
assert_eq!(once, twice);
// check permissions of the private key
let permissions = fs::metadata(dir.path().join("keys/ed25519"))
.unwrap()
.permissions();
let perms = format!("{:o}", permissions.mode());
assert_eq!(perms, "100600");
}
}

View File

@ -1,5 +1,6 @@
use std::{
fs,
os::unix::prelude::PermissionsExt,
path::{self, Path, PathBuf},
sync::{Arc, RwLock},
};
@ -65,6 +66,13 @@ impl Storage {
fs::create_dir_all(dir).map_err(Error::IO)?;
}
fs::write(file_path.clone(), content).map_err(Error::IO)?;
// Set the permissions to be user-only.
let metadata = fs::metadata(file_path.clone())?;
let mut permissions = metadata.permissions();
permissions.set_mode(0o600); // User read/write
fs::set_permissions(file_path.clone(), permissions)?;
Ok(())
}