store: create private (0700) directories (L6)
This commit is contained in:
8 files changed
+76
-10
No files matched your search
@@ -802,7 +802,9 @@ data/ desktop, metainfo, gschema (all `.in` templates), icons, re
|
||||
first). It holds `version`, an optional plaintext `device` nsec,
|
||||
and `keys: [{pubkey, name, ncryptsec}]`. There is **no Secret Service and no
|
||||
`oo7`** dependency: the login keyring was never at-rest protection (unlocked at
|
||||
login for the whole session, keyed off the login password).
|
||||
login for the whole session, keyed off the login password). The data, config
|
||||
and cache directories — and the lock file's directory — are created **0700**
|
||||
through the shared `store::create_private_dir` (L6), not with the process umask.
|
||||
- **Vault mutations are serialized (M9).** `add_user_key` and `change_passphrase`
|
||||
hold a `tokio::sync::Mutex` (`writes`) across their whole body, because each
|
||||
encrypts and then updates state — interleaved, they could write a key under the
|
||||
|
||||
+1
-1
@@ -48,7 +48,7 @@ fn claim_at(path: &Path) -> std::io::Result<File> {
|
||||
if let Some(parent) = path.parent() {
|
||||
// The claim runs before the vault is opened, so on a fresh machine the
|
||||
// data directory may not exist yet.
|
||||
std::fs::create_dir_all(parent)?;
|
||||
crate::store::create_private_dir(parent)?;
|
||||
}
|
||||
|
||||
let mut options = std::fs::OpenOptions::new();
|
||||
|
||||
+1
-1
@@ -159,7 +159,7 @@ impl AppRegistry {
|
||||
return Ok(());
|
||||
}
|
||||
if let Some(parent) = self.path.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
crate::store::create_private_dir(parent)?;
|
||||
}
|
||||
let contents = serde_json::to_string_pretty(self)?;
|
||||
let temporary = self.path.with_extension("json.tmp");
|
||||
|
||||
+1
-1
@@ -106,7 +106,7 @@ impl ActivityLog {
|
||||
return;
|
||||
}
|
||||
if let Some(parent) = self.path.parent() {
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
let _ = crate::store::create_private_dir(parent);
|
||||
}
|
||||
if self.size() > MAX_BYTES {
|
||||
self.trim_unlocked();
|
||||
|
||||
+3
-4
@@ -6,8 +6,7 @@
|
||||
//! falls back to the initials an `AdwAvatar` draws.
|
||||
//!
|
||||
//! Cache layout (`glib::user_cache_dir()/<app id>/icons/`; the directory is
|
||||
//! created with the process umask, the files are mode 0600),
|
||||
//! keyed by the client's public key:
|
||||
//! created mode 0700, the files are mode 0600), keyed by the client's public key:
|
||||
//!
|
||||
//! - `<hex>.img` — the bytes that decoded, mode 0600.
|
||||
//! - `<hex>.url` — the URL last **attempted** (success *or* failure), mode 0600.
|
||||
@@ -616,7 +615,7 @@ impl IconStore {
|
||||
let Some(key) = Self::valid_key(key) else {
|
||||
return;
|
||||
};
|
||||
let _ = std::fs::create_dir_all(&self.dir);
|
||||
let _ = crate::store::create_private_dir(&self.dir);
|
||||
if write_private(&self.img_path(&key), bytes).is_err() {
|
||||
return;
|
||||
}
|
||||
@@ -630,7 +629,7 @@ impl IconStore {
|
||||
let Some(key) = Self::valid_key(key) else {
|
||||
return;
|
||||
};
|
||||
let _ = std::fs::create_dir_all(&self.dir);
|
||||
let _ = crate::store::create_private_dir(&self.dir);
|
||||
let _ = std::fs::remove_file(self.img_path(&key));
|
||||
let _ = write_private(&self.url_path(&key), url.as_bytes());
|
||||
self.textures.borrow_mut().remove(&key);
|
||||
|
||||
+1
-1
@@ -502,7 +502,7 @@ impl KeyStore {
|
||||
.parent()
|
||||
.filter(|parent| !parent.as_os_str().is_empty())
|
||||
.unwrap_or_else(|| std::path::Path::new("."));
|
||||
std::fs::create_dir_all(parent)?;
|
||||
crate::store::create_private_dir(parent)?;
|
||||
let contents = serde_json::to_string_pretty(&vault)?;
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
@@ -1,9 +1,74 @@
|
||||
//! Persistent state: the encrypted key vault, the application registry, and the
|
||||
//! activity log.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
pub mod apps;
|
||||
pub mod audit;
|
||||
pub mod icons;
|
||||
pub mod keys;
|
||||
pub mod nip05;
|
||||
pub mod profiles;
|
||||
|
||||
/// Create a directory (and its parents) that is private to the user, mode 0700.
|
||||
///
|
||||
/// The data, config and cache directories hold keys, the app registry, the
|
||||
/// activity log and per-key icon files; on a multi-user machine the process
|
||||
/// umask (typically 0755) would let another local user list them. A directory
|
||||
/// that already exists is tightened to 0700 as well. Fails closed on error.
|
||||
pub(crate) fn create_private_dir(path: &Path) -> std::io::Result<()> {
|
||||
let mut builder = std::fs::DirBuilder::new();
|
||||
builder.recursive(true);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::DirBuilderExt;
|
||||
builder.mode(0o700);
|
||||
}
|
||||
builder.create(path)?;
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn scratch() -> std::path::PathBuf {
|
||||
static NEXT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
|
||||
std::env::temp_dir().join(format!(
|
||||
"regent-private-dir-{}-{}",
|
||||
std::process::id(),
|
||||
NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn mode(path: &Path) -> u32 {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
std::fs::metadata(path).unwrap().permissions().mode() & 0o777
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_private_dir_is_created_0700_and_tightens_an_existing_one() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let dir = scratch();
|
||||
let nested = dir.join("a").join("b");
|
||||
create_private_dir(&nested).expect("create");
|
||||
assert!(nested.is_dir());
|
||||
assert_eq!(mode(&nested), 0o700);
|
||||
|
||||
// A directory that already exists with looser permissions is tightened.
|
||||
let loose = dir.join("loose");
|
||||
std::fs::create_dir(&loose).expect("create");
|
||||
std::fs::set_permissions(&loose, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
create_private_dir(&loose).expect("create");
|
||||
assert_eq!(mode(&loose), 0o700);
|
||||
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
@@ -388,7 +388,7 @@ impl Profiles {
|
||||
.parent()
|
||||
.filter(|parent| !parent.as_os_str().is_empty())
|
||||
.unwrap_or_else(|| std::path::Path::new("."));
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
let _ = crate::store::create_private_dir(parent);
|
||||
let entries = self.entries.lock().unwrap().clone();
|
||||
let Ok(contents) = serde_json::to_string_pretty(&entries) else {
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user