Skip to content

Commit

Permalink
fix: don't strip prefix if path is exactly "." (#293)
Browse files Browse the repository at this point in the history
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 ".".
  • Loading branch information
wxsBSD authored Jan 18, 2025
1 parent c352f24 commit 45da681
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
9 changes: 9 additions & 0 deletions cli/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
20 changes: 14 additions & 6 deletions cli/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, &["**"])
Expand Down

0 comments on commit 45da681

Please sign in to comment.