Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

security(format): preserve permissions on atomic replacement (#2881)

Earl Co52794db32137
diff --git a/crates/format/src/atomic.rs b/crates/format/src/atomic.rs
index 1e4d06a6a..fc6d29dcd 100644
--- a/crates/format/src/atomic.rs
+++ b/crates/format/src/atomic.rs
@@ -28,11 +28,21 @@ fn parent_dir(path: &Path) -> PathBuf {
/// `fsync` the directory so the rename itself survives a power cut. If anything fails the temp file
/// is removed and `path` is untouched.
pub fn atomic_write(path: &Path, data: &[u8]) -> io::Result<()> {
+ // `rename` replaces the inode, so a new staging file must inherit an existing project's
+ // access mode instead of silently broadening a private file to the process default.
+ let permissions = match fs::metadata(path) {
+ Ok(meta) => Some(meta.permissions()),
+ Err(e) if e.kind() == io::ErrorKind::NotFound => None,
+ Err(e) => return Err(e),
+ };
let tmp = temp_path(path);
let r = (|| {
{
// closed before the rename
let mut f = OpenOptions::new().write(true).create_new(true).open(&tmp)?;
+ if let Some(permissions) = permissions.as_ref() {
+ f.set_permissions(permissions.clone())?;
+ }
f.write_all(data)?;
f.sync_all()?;
}
@@ -122,6 +132,21 @@ mod tests {
fs::remove_dir_all(&d).unwrap();
}
+ #[cfg(unix)]
+ #[test]
+ fn replacement_preserves_existing_file_permissions() {
+ use std::os::unix::fs::PermissionsExt;
+
+ let d = tmpdir("permissions");
+ let p = d.join("private.fcproj");
+ fs::write(&p, b"before").unwrap();
+ fs::set_permissions(&p, fs::Permissions::from_mode(0o600)).unwrap();
+ atomic_write(&p, b"after").unwrap();
+ assert_eq!(fs::read(&p).unwrap(), b"after");
+ assert_eq!(fs::metadata(&p).unwrap().permissions().mode() & 0o777, 0o600);
+ fs::remove_dir_all(&d).unwrap();
+ }
+
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn new_file_is_published_complete_without_clobbering_or_temp_leaks() {