Skip to content

Commit 3caafd7

Browse files
committed
fix(workspace_trust): secure permissions on Unix
1 parent 068d835 commit 3caafd7

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

helix-loader/src/workspace_trust.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use helix_stdx::faccess::write_with_perms;
12
use std::{collections::HashSet, fs, path::PathBuf};
23

34
use crate::{data_dir, workspace_exclude_file, workspace_trust_file};
@@ -71,8 +72,7 @@ impl WorkspaceTrust {
7172
log::error!("Couldn't create helix's data directory: {:?}", e);
7273
};
7374
}
74-
// TO-DO: ensure only owner has write permission
75-
if let Err(e) = fs::write(workspace_trust_file(), trust_text) {
75+
if let Err(e) = write_with_perms(workspace_trust_file(), trust_text, 0o0644) {
7676
log::error!("Error during write of workspace_trust file: {:?}", e);
7777
}
7878
}
@@ -93,8 +93,7 @@ impl WorkspaceTrust {
9393
log::error!("Couldn't create helix's data directory: {:?}", e);
9494
};
9595
}
96-
// TO-DO: ensure only owner has write permission
97-
if let Err(e) = fs::write(workspace_exclude_file(), trust_text) {
96+
if let Err(e) = write_with_perms(workspace_exclude_file(), trust_text, 0o0644) {
9897
log::error!("Error during write of workspace_trust file: {:?}", e);
9998
}
10099
} else {

helix-stdx/src/faccess.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Functions for managine file metadata.
1+
//! Functions for managing file metadata.
22
//! From <https://github.com/Freaky/faccess>
33
44
use std::io;
@@ -95,6 +95,24 @@ mod imp {
9595
Ok(())
9696
}
9797

98+
/// Same as [`fs::write`][std::fs::write], but sets mode bits on Unix targets
99+
pub fn write_with_perms<P: AsRef<Path>, C: AsRef<[u8]>>(
100+
path: P,
101+
contents: C,
102+
perms: u16,
103+
) -> io::Result<()> {
104+
fn inner(path: &Path, contents: &[u8], perms: u16) -> io::Result<()> {
105+
let f = std::fs::File::create(path)?;
106+
#[cfg(target_family = "unix")]
107+
{
108+
let p = f.metadata()?.permissions().set_mode(perms);
109+
std::fs::set_permissions(f, p)?
110+
}
111+
f.write_all(contents)
112+
}
113+
inner(path.as_ref(), contents.as_ref(), perms)
114+
}
115+
98116
pub fn hardlink_count(p: &Path) -> std::io::Result<u64> {
99117
let metadata = p.metadata()?;
100118
Ok(metadata.nlink())

0 commit comments

Comments
 (0)