Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions src/find/matchers/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// https://opensource.org/licenses/MIT.

use std::cell::RefCell;
use std::env;
use std::error::Error;
use std::ffi::OsString;
use std::io::{stderr, Write};
Expand All @@ -13,6 +14,27 @@ use std::process::Command;

use super::{Matcher, MatcherIO, WalkEntry};

fn check_path_integrity() -> Result<(), Box<dyn Error>> {
let path_dirs = env::var("PATH")?;
for dir_entry in env::split_paths(&path_dirs) {
// We can securely unwrap (or expect) the value of dir_entry string
// conversion on message error cause the env::var returns an VarError
// variant that indicates if the variable (in this case PATH) contains
// invalid Unicode data.
let dir_entry_str = dir_entry.to_str().expect("Unexpected conversion error");
if !dir_entry.is_absolute() || dir_entry.is_file() || dir_entry_str.is_empty() {
return Err(format!(
"The PATH environment variable contains non-absolute paths, \
files, or empty paths. Segment that caused the error: '{}'",
dir_entry_str
)
.into());
}
}

Ok(())
}

enum Arg {
FileArg(Vec<OsString>),
LiteralArg(OsString),
Expand All @@ -30,6 +52,10 @@ impl SingleExecMatcher {
args: &[&str],
exec_in_parent_dir: bool,
) -> Result<Self, Box<dyn Error>> {
if exec_in_parent_dir {
check_path_integrity()?;
}

let transformed_args = args
.iter()
.map(|&a| {
Expand Down Expand Up @@ -112,6 +138,10 @@ impl MultiExecMatcher {
args: &[&str],
exec_in_parent_dir: bool,
) -> Result<Self, Box<dyn Error>> {
if exec_in_parent_dir {
check_path_integrity()?;
}

let transformed_args = args.iter().map(OsString::from).collect();

Ok(Self {
Expand Down
Loading
Loading