From 45da681b21aaf94855f186af09cbf8fe2bb49f63 Mon Sep 17 00:00:00 2001 From: Wesley Shields Date: Sat, 18 Jan 2025 09:18:21 -0500 Subject: [PATCH] fix: don't strip prefix if path is exactly "." (#293) One of the things I often run is "yr scan -r rules.yara ." when I have a collection of samples and I want to scan all of them in the current directory. With the fix in #280 the path was being stripped to an empty string which would result in an error when trying to scan. Fix it by only performing the strip if the path is not equal to ".". --- cli/src/tests/mod.rs | 9 +++++++++ cli/src/walk.rs | 20 ++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/cli/src/tests/mod.rs b/cli/src/tests/mod.rs index e4e7e1a6..5bbe28ce 100644 --- a/cli/src/tests/mod.rs +++ b/cli/src/tests/mod.rs @@ -284,4 +284,13 @@ fn cli_issue_280() { .arg("./src/tests/testdata/") .assert() .success(); + + // Handle special case of just . for path argument. + Command::cargo_bin("yr") + .unwrap() + .arg("scan") + .arg("src/tests/testdata/foo.yar") + .arg(".") + .assert() + .success(); } diff --git a/cli/src/walk.rs b/cli/src/walk.rs index 9d89f6b8..f7486f64 100644 --- a/cli/src/walk.rs +++ b/cli/src/walk.rs @@ -203,12 +203,20 @@ impl<'a> Walker<'a> { // workaround for a bug in globwalk that causes a panic. // https://github.com/VirusTotal/yara-x/issues/280 // https://github.com/Gilnaa/globwalk/issues/28 - - #[cfg(not(target_os = "windows"))] - let path = self.path.strip_prefix("./").unwrap_or(self.path); - - #[cfg(target_os = "windows")] - let path = self.path.strip_prefix(r#".\"#).unwrap_or(self.path); + // + // Only perform the strip if the path is not exactly "." - this allows + // users to run "yr scan rules.yara ." to scan all the files in the + // current directory. + let path = if self.path.as_os_str().ne(".") { + #[cfg(not(target_os = "windows"))] + let path = self.path.strip_prefix("./").unwrap_or(self.path); + + #[cfg(target_os = "windows")] + let path = self.path.strip_prefix(r#".\"#).unwrap_or(self.path); + path + } else { + self.path + }; let mut builder = if self.filters.is_empty() { globwalk::GlobWalkerBuilder::from_patterns(path, &["**"])