Skip to content

Commit 06a27fb

Browse files
committed
matches/exec: create path integrity check before execution
Fixes #518 Create function check_path_integrity which checks the integrity of the path, with the following criteria: - Only absolute paths - Only non-empty paths - Only directories in path If the PATH env var does not pass the criteria, an error is returned warning about the specific path part that caused that error. Tests: - Added tests for both SingleExecMatcher and MultiExecMatcher - Covered all PATH validation scenarios: * Valid absolute directories * Empty path segments * Relative path segments * File paths instead of directories - Ensured safe environment variable handling with unsafe blocks - Maintained consistent test patterns with existing serial tests - Verified correct error handling for invalid PATH configurations To avoid making the function that performs the check public, tests were also added that verify that there is no error with a valid PATH.
1 parent d55e2f9 commit 06a27fb

File tree

2 files changed

+392
-3
lines changed

2 files changed

+392
-3
lines changed

src/find/matchers/exec.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,35 @@
44
// license that can be found in the LICENSE file or at
55
// https://opensource.org/licenses/MIT.
66

7+
use super::{Matcher, MatcherIO, WalkEntry};
78
use std::cell::RefCell;
9+
use std::env;
810
use std::error::Error;
911
use std::ffi::OsString;
1012
use std::io::{stderr, Write};
1113
use std::path::Path;
1214
use std::process::Command;
1315

14-
use super::{Matcher, MatcherIO, WalkEntry};
16+
fn check_path_integrity() -> Result<(), Box<dyn Error>> {
17+
let path_dirs = env::var("PATH")?;
18+
for dir_entry in env::split_paths(&path_dirs) {
19+
// We can securely unwrap (or expect) the value of dir_entry string
20+
// conversion on message error cause the env::var returns an VarError
21+
// variant that indicates if the variable (in this case PATH) contains
22+
// invalid Unicode data.
23+
let dir_entry_str = dir_entry.to_str().expect("Unexpected conversion error");
24+
if !dir_entry.is_absolute() || dir_entry.is_file() || dir_entry_str.is_empty() {
25+
return Err(format!(
26+
"The PATH environment variable contains non-absolute paths, \
27+
files, or empty paths. Segment that caused the error: '{}'",
28+
dir_entry_str
29+
)
30+
.into());
31+
}
32+
}
33+
34+
Ok(())
35+
}
1536

1637
enum Arg {
1738
FileArg(Vec<OsString>),
@@ -30,6 +51,10 @@ impl SingleExecMatcher {
3051
args: &[&str],
3152
exec_in_parent_dir: bool,
3253
) -> Result<Self, Box<dyn Error>> {
54+
if exec_in_parent_dir {
55+
check_path_integrity()?;
56+
}
57+
3358
let transformed_args = args
3459
.iter()
3560
.map(|&a| {
@@ -112,6 +137,10 @@ impl MultiExecMatcher {
112137
args: &[&str],
113138
exec_in_parent_dir: bool,
114139
) -> Result<Self, Box<dyn Error>> {
140+
if exec_in_parent_dir {
141+
check_path_integrity()?;
142+
}
143+
115144
let transformed_args = args.iter().map(OsString::from).collect();
116145

117146
Ok(Self {

0 commit comments

Comments
 (0)