From f997ddcd45417edfc6dcefa0dc1191fc4a5ed1f4 Mon Sep 17 00:00:00 2001 From: Tirthraj Chavan Date: Wed, 2 Dec 2026 15:04:06 +0530 Subject: [PATCH] fix(storage): set 0600 mode on search-index.lock --- .changeset/search-index-lock-mode.md | 4 ++ crates/nostream-storage/src/lmdb/mod.rs | 51 ++++++++++++++++++++----- 2 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 .changeset/search-index-lock-mode.md diff --git a/.changeset/search-index-lock-mode.md b/.changeset/search-index-lock-mode.md new file mode 100644 index 000000000..3ed054f2f --- /dev/null +++ b/.changeset/search-index-lock-mode.md @@ -0,0 +1,4 @@ +--- +--- + +fix(storage): set 0600 mode on search-index.lock (#1316) diff --git a/crates/nostream-storage/src/lmdb/mod.rs b/crates/nostream-storage/src/lmdb/mod.rs index 2b6f373bc..6ec606151 100644 --- a/crates/nostream-storage/src/lmdb/mod.rs +++ b/crates/nostream-storage/src/lmdb/mod.rs @@ -281,10 +281,15 @@ pub const REBUILD_LOCK_FILE: &str = "search-index.lock"; /// holder in another process. The lock goes with the returned file, and with the process. fn lock_rebuild_file(path: &Path) -> Result { let lock_path = path.join(REBUILD_LOCK_FILE); - let locked = std::fs::OpenOptions::new() - .create(true) - .truncate(false) - .write(true) + let mut options = std::fs::OpenOptions::new(); + options.create(true).truncate(false).write(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + // 0600, matching the LMDB dataset files beside it rather than taking the host umask. + options.mode(0o600); + } + let locked = options .open(&lock_path) .and_then(|file| file.lock().map(|()| file)); locked.map_err(|source| StorageError::RebuildLock { @@ -292,7 +297,6 @@ fn lock_rebuild_file(path: &Path) -> Result { source, }) } - /// An embedded LMDB event store with its metadata tail, built from the root a caller supplies. #[derive(Debug, Clone)] pub struct LmdbBackend { @@ -1463,9 +1467,10 @@ mod tests { ); } - /// The store directory holds the whole dataset, so it is the owner's alone: heed writes the - /// files 0600, and a directory the umask left group-writable would both widen the audience - /// and be refused by the migration's own target check on the next run. + /// The store directory holds the whole dataset, so it is the owner's alone: heed and the + /// rebuild lock write every file 0600, and a directory the umask left group-writable would both + /// widen the audience and be refused by the migration's own target check on the next run. + #[cfg(unix)] #[tokio::test] async fn the_store_directory_is_the_owners_alone() { use std::os::unix::fs::PermissionsExt; @@ -1473,12 +1478,40 @@ mod tests { let directory = tempfile::tempdir().expect("tempdir"); let root = directory.path().join("store"); LmdbBackend::open(&root).await.expect("the store opens"); - let mode = std::fs::metadata(crate::backend::store_dir(&root)) + let store_path = crate::backend::store_dir(&root); + let mode = std::fs::metadata(&store_path) .expect("the store directory exists") .permissions() .mode() & 0o777; assert_eq!(mode, 0o700, "group and other have no access"); + + let mut files = std::fs::read_dir(&store_path) + .expect("the store directory reads") + .map(|entry| entry.expect("dir entry")) + .collect::>(); + files.sort_by_key(std::fs::DirEntry::file_name); + assert!( + files + .iter() + .any(|entry| entry.file_name() == REBUILD_LOCK_FILE), + "the store creates {REBUILD_LOCK_FILE}" + ); + + for entry in files { + let file_mode = entry + .metadata() + .expect("store entry metadata") + .permissions() + .mode() + & 0o777; + assert_eq!( + file_mode, + 0o600, + "{} must be owner-only (0600)", + entry.path().display() + ); + } } #[tokio::test]