From 4fdafa0ba16e8057b132d7c79f7e9628abcbab23 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Thu, 14 Jan 2021 09:49:18 +0200 Subject: [PATCH] Missing cfg (#600) * Check whether NPM root is owned by the current user (fix #599) * Missing cfg * Another missing config --- src/steps/node.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/steps/node.rs b/src/steps/node.rs index 8ee6a73d..6d1605af 100644 --- a/src/steps/node.rs +++ b/src/steps/node.rs @@ -5,9 +5,12 @@ use crate::terminal::print_separator; use crate::utils::{require, PathExt}; use crate::{error::SkipStep, execution_context::ExecutionContext}; use anyhow::Result; -use log::debug; - use directories::BaseDirs; +use log::debug; +#[cfg(unix)] +use nix::unistd::Uid; +#[cfg(unix)] +use std::os::unix::prelude::MetadataExt; use std::path::PathBuf; use std::process::Command; @@ -20,7 +23,7 @@ impl NPM { Self { command } } - #[cfg(not(target_os = "macos"))] + #[cfg(target_os = "linux")] fn root(&self) -> Result { Command::new(&self.command) .args(&["root", "-g"]) @@ -38,13 +41,17 @@ impl NPM { pub fn run_npm_upgrade(_base_dirs: &BaseDirs, run_type: RunType) -> Result<()> { let npm = require("npm").map(NPM::new)?; - #[cfg(not(target_os = "macos"))] + #[cfg(target_os = "linux")] { let npm_root = npm.root()?; - if !npm_root.is_descendant_of(_base_dirs.home_dir()) { + let metadata = std::fs::metadata(&npm_root)?; + let uid = Uid::effective(); + + if metadata.uid() != uid.as_raw() { return Err(SkipStep(format!( - "NPM root at {} isn't a decandent of the user's home directory", - npm_root.display() + "NPM root at {} is owned by {} which is not the current user", + npm_root.display(), + uid )) .into()); }