Skip to content

Commit 26a6612

Browse files
committed
fix(command): validate absolute binary executability
1 parent 6c66bcc commit 26a6612

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

crates/vite_command/src/lib.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::os::fd::{BorrowedFd, RawFd};
33
use std::{
44
collections::HashMap,
55
ffi::OsStr,
6-
path::Path,
76
process::{ExitStatus, Stdio},
87
};
98

@@ -31,15 +30,6 @@ pub fn resolve_bin(
3130
path_env: Option<&OsStr>,
3231
cwd: impl AsRef<AbsolutePath>,
3332
) -> Result<AbsolutePathBuf, Error> {
34-
let candidate = Path::new(bin_name);
35-
if candidate.is_absolute() {
36-
if candidate.exists() {
37-
return AbsolutePathBuf::new(candidate.to_path_buf())
38-
.ok_or_else(|| Error::CannotFindBinaryPath(bin_name.into()));
39-
}
40-
return Err(Error::CannotFindBinaryPath(bin_name.into()));
41-
}
42-
4333
let current_path;
4434
let path_env = match path_env {
4535
Some(p) => p,
@@ -371,6 +361,33 @@ mod tests {
371361
assert!(result.is_ok(), "Should run absolute binary path, but got error: {:?}", result);
372362
}
373363

364+
#[cfg(unix)]
365+
#[tokio::test]
366+
async fn test_run_command_rejects_non_executable_absolute_binary_path() {
367+
let temp_dir = create_temp_dir();
368+
let temp_dir_path =
369+
AbsolutePathBuf::new(temp_dir.path().canonicalize().unwrap().to_path_buf())
370+
.unwrap();
371+
let script_path = temp_dir_path.join("test-bin");
372+
std::fs::write(&script_path, "#!/bin/sh\nexit 0\n").unwrap();
373+
let envs = HashMap::new();
374+
let result = run_command(
375+
&script_path.as_path().display().to_string(),
376+
&[] as &[&str],
377+
&envs,
378+
&temp_dir_path,
379+
)
380+
.await;
381+
assert!(result.is_err(), "Should reject non-executable absolute binary path");
382+
assert_eq!(
383+
result.unwrap_err().to_string(),
384+
format!(
385+
"Cannot find binary path for command '{}'",
386+
script_path.as_path().display()
387+
)
388+
);
389+
}
390+
374391
#[tokio::test]
375392
async fn test_run_command_and_not_find_binary_path() {
376393
let temp_dir = create_temp_dir();

0 commit comments

Comments
 (0)