Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/search-index-lock-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

fix(storage): set 0600 mode on search-index.lock (#1316)
51 changes: 42 additions & 9 deletions crates/nostream-storage/src/lmdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,22 @@ 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<std::fs::File, StorageError> {
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 {
path: lock_path,
source,
})
}

/// An embedded LMDB event store with its metadata tail, built from the root a caller supplies.
#[derive(Debug, Clone)]
pub struct LmdbBackend {
Expand Down Expand Up @@ -1463,22 +1467,51 @@ 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;

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::<Vec<_>>();
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]
Expand Down
Loading