From 0288bb9659e4c0954d4f9e000f6698d8121fd76d Mon Sep 17 00:00:00 2001 From: tecnolgd Date: Sat, 14 Nov 2026 10:54:04 +0000 Subject: [PATCH 1/2] fix(tools): skip non-executable files in package::which Signed-off-by: tecnolgd --- tools/src/package.rs | 82 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/tools/src/package.rs b/tools/src/package.rs index 69f2ce04..75a54881 100644 --- a/tools/src/package.rs +++ b/tools/src/package.rs @@ -4,19 +4,48 @@ use crate::{binary, cases, cli::Args, config, error, process::Spec, storage, Res use std::collections::{BTreeMap, BTreeSet, VecDeque}; use std::ffi::{OsStr, OsString}; use std::fs; +use std::os::unix::fs::MetadataExt; use std::path::{Path, PathBuf}; use utils::wson::Value; pub fn which(name: &str) -> Option { let raw = Path::new(name); if raw.is_absolute() || raw.components().count() > 1 { - return raw.is_file().then(|| raw.into()); + // For direct paths, ensure it's a file and meets executable requirements. + if raw.is_file() { + #[cfg(unix)] + { + if let Ok(metadata) = std::fs::metadata(raw) { + if metadata.mode() & 0o111 != 0 { + return Some(raw.into()); + } + } + return None; + } + #[cfg(not(unix))] + { + return Some(raw.into()); + } + } + return None; } for directory in std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default()) { for suffix in if cfg!(windows) { vec!["", ".exe", ".cmd", ".bat"] } else { vec![""] } { let path = directory.join(format!("{name}{suffix}")); if path.is_file() { - return Some(path); + #[cfg(unix)] + { + if let Ok(metadata) = std::fs::metadata(&path) { + // Checks if any execute bit is set (owner, group, or others) + if metadata.mode() & 0o111 != 0 { + return Some(path); + } + } + } + #[cfg(not(unix))] + { + return Some(path); + } } } } @@ -1564,4 +1593,53 @@ mod tests { std::os::unix::fs::symlink("../outside", stage.join("alias")).unwrap(); assert!(archive_at_epoch(&stage, &output, "wave", 1).is_err()); } + + #[test] + fn which_skips_non_executable_files_and_preserves_executable_matches() { + let temp = tempfile::tempdir().unwrap(); + let first = temp.path().join("first"); + let second = temp.path().join("second"); + fs::create_dir_all(&first).unwrap(); + fs::create_dir_all(&second).unwrap(); + + let tool_name = if cfg!(windows) { "audit-tool.exe" } else { "audit-tool" }; + let non_exec = first.join(tool_name); + let exec = second.join(tool_name); + + fs::write(&non_exec, b"dummy content").unwrap(); + fs::write(&exec, b"#!/bin/sh\necho ok").unwrap(); + + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(&non_exec, fs::Permissions::from_mode(0o644)).unwrap(); + fs::set_permissions(&exec, fs::Permissions::from_mode(0o755)).unwrap(); + } + + let path_os = std::env::join_paths([&first, &second]).unwrap(); + let old_path = std::env::var_os("PATH"); + std::env::set_var("PATH", &path_os); + + // Search PATH: should skip `first` (0644) and hit `second` (0755) + let found = which("audit-tool"); + + // Restore PATH early so we don't pollute other test runs + if let Some(old) = old_path { + std::env::set_var("PATH", old); + } else { + std::env::remove_var("PATH"); + } + + #[cfg(unix)] + { + assert_eq!(found, Some(exec.clone())); + assert_eq!(which(non_exec.to_str().unwrap()), None); + assert_eq!(which(exec.to_str().unwrap()), Some(exec)); + } + + #[cfg(not(unix))] + { + assert!(found.is_some()); + } + } } From 900e697eb51aa790bfd030346ffbf60bc1596a3d Mon Sep 17 00:00:00 2001 From: tecnolgd Date: Sat, 14 Nov 2026 11:09:43 +0000 Subject: [PATCH 2/2] fix(tools): gate unix MetadataExt import behind cfg(unix) Signed-off-by: tecnolgd --- tools/src/package.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/src/package.rs b/tools/src/package.rs index 75a54881..ae597c83 100644 --- a/tools/src/package.rs +++ b/tools/src/package.rs @@ -4,7 +4,6 @@ use crate::{binary, cases, cli::Args, config, error, process::Spec, storage, Res use std::collections::{BTreeMap, BTreeSet, VecDeque}; use std::ffi::{OsStr, OsString}; use std::fs; -use std::os::unix::fs::MetadataExt; use std::path::{Path, PathBuf}; use utils::wson::Value; @@ -15,6 +14,7 @@ pub fn which(name: &str) -> Option { if raw.is_file() { #[cfg(unix)] { + use std::os::unix::fs::MetadataExt; if let Ok(metadata) = std::fs::metadata(raw) { if metadata.mode() & 0o111 != 0 { return Some(raw.into()); @@ -35,6 +35,7 @@ pub fn which(name: &str) -> Option { if path.is_file() { #[cfg(unix)] { + use std::os::unix::fs::MetadataExt; if let Ok(metadata) = std::fs::metadata(&path) { // Checks if any execute bit is set (owner, group, or others) if metadata.mode() & 0o111 != 0 {