Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/green-pandas-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fnm": patch
---

Fix insecure world-writable directory warning
12 changes: 11 additions & 1 deletion src/commands/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ fn generate_symlink_path() -> String {
}

fn make_symlink(config: &FnmConfig) -> Result<std::path::PathBuf, Error> {
let base_dir = config.multishell_storage().ensure_exists_silently();
#[cfg(windows)]
let base_dir = { config.multishell_storage().ensure_exists_silently() };
#[cfg(not(windows))]
let base_dir = config
.multishell_storage()
.ensure_exists_silently_with_permissions(|permissions| {
use std::os::unix::fs::PermissionsExt;
// r/w only for owner
permissions.set_mode(0o700);
});

let mut temp_dir = base_dir.join(generate_symlink_path());

while temp_dir.exists() {
Expand Down
34 changes: 34 additions & 0 deletions src/path_ext.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use log::warn;
use std::fs::Permissions;

pub trait PathExt {
fn exists(&self) -> bool;
fn ensure_exists_silently(self) -> Self;
fn ensure_exists_silently_with_permissions<F>(self, permissions: F) -> Self
where
F: FnOnce(&mut Permissions);
}

impl<T: AsRef<std::path::Path>> PathExt for T {
fn exists(&self) -> bool {
std::path::Path::exists(self.as_ref())
}

/// Ensures a path is existing by creating it recursively
/// if it is missing. No error is emitted if the creation has failed.
fn ensure_exists_silently(self) -> Self {
Expand All @@ -13,4 +22,29 @@ impl<T: AsRef<std::path::Path>> PathExt for T {
}
self
}

fn ensure_exists_silently_with_permissions<F>(self, modify_permissions: F) -> Self
where
F: FnOnce(&mut Permissions),
{
if self.exists() {
return self;
}

if let Err(err) = std::fs::create_dir_all(self.as_ref()) {
warn!("Failed to create directory {:?}: {err}", self.as_ref());
}

let modified = self.as_ref().metadata().and_then(|x| {
let mut permissions = x.permissions();
modify_permissions(&mut permissions);
std::fs::set_permissions(self.as_ref(), permissions)
});

if let Err(err) = modified {
warn!("Failed to set permissions {:?}: {err}", self.as_ref());
}

self
}
}