Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bug while using --compiled-rules without namespaces. #201

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions cli/src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ pub fn compile() -> Command {
}

pub fn exec_compile(args: &ArgMatches) -> anyhow::Result<()> {
let rules_path =
args.get_many::<(String, PathBuf)>("[NAMESPACE:]RULES_PATH").unwrap();
let rules_path = args
.get_many::<(Option<String>, PathBuf)>("[NAMESPACE:]RULES_PATH")
.unwrap();

let output_path = args.get_one::<PathBuf>("output").unwrap();

Expand Down
26 changes: 14 additions & 12 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,15 @@ fn meta_file_value_parser(
/// `[NAMESPACE:]PATH`.
///
/// Returns the namespace and the path. If the namespace is not provided
/// returns "default".
/// returns [`None`].
fn path_with_namespace_parser(
input: &str,
) -> Result<(String, PathBuf), anyhow::Error> {
let (namespace, path) =
if let Some((namespace, path)) = input.split_once(':') {
(namespace, path)
} else {
("default", input)
};
let path = PathBuf::from(path);
Ok((namespace.to_string(), path))
) -> Result<(Option<String>, PathBuf), anyhow::Error> {
if let Some((namespace, path)) = input.split_once(':') {
Ok((Some(namespace.to_string()), PathBuf::from(path)))
} else {
Ok((None, PathBuf::from(input)))
}
}

pub fn compile_rules<'a, P>(
Expand All @@ -124,7 +121,7 @@ pub fn compile_rules<'a, P>(
args: &ArgMatches,
) -> Result<Rules, anyhow::Error>
where
P: Iterator<Item = &'a (String, PathBuf)>,
P: Iterator<Item = &'a (Option<String>, PathBuf)>,
{
let mut compiler: Compiler<'_> = Compiler::new();

Expand Down Expand Up @@ -170,7 +167,12 @@ where
w.filter("**/*.yar");
w.filter("**/*.yara");

compiler.new_namespace(namespace.as_str());
compiler.new_namespace(
namespace
.as_ref()
.map(|namespace| namespace.as_str())
.unwrap_or("default"),
);

if let Err(err) = w.walk(
|file_path| {
Expand Down
7 changes: 4 additions & 3 deletions cli/src/commands/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ pub fn scan() -> Command {
}

pub fn exec_scan(args: &ArgMatches) -> anyhow::Result<()> {
let mut rules_path =
args.get_many::<(String, PathBuf)>("[NAMESPACE:]RULES_PATH").unwrap();
let mut rules_path = args
.get_many::<(Option<String>, PathBuf)>("[NAMESPACE:]RULES_PATH")
.unwrap();

let target_path = args.get_one::<PathBuf>("TARGET_PATH").unwrap();
let compiled_rules = args.get_flag("compiled-rules");
Expand Down Expand Up @@ -204,7 +205,7 @@ pub fn exec_scan(args: &ArgMatches) -> anyhow::Result<()> {

let (namespace, rules_path) = rules_path.next().unwrap();

if !namespace.is_empty() {
if namespace.is_some() {
bail!(
"can't use namespace with '{}'",
Paint::bold("--compiled-rules")
Expand Down
Loading