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: change binary plugin to return val policy expr can understand #588

Merged
merged 1 commit into from
Nov 7, 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
2 changes: 1 addition & 1 deletion hipcheck/src/policy/config_to_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn parse_binary(
// Cap the weight at 65,533
let weight = binary.weight.try_into().unwrap_or(u16::MAX);
let threshold = binary.binary_file_threshold;
let expression = format!("(eq {} (count $))", threshold);
let expression = format!("(lte $ {})", threshold);

// Add the plugin
let plugin = PolicyPlugin::new(
Expand Down
2 changes: 1 addition & 1 deletion hipcheck/src/policy/test_example.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ analyze {

category "practices" weight=1 {
analysis "mitre/activity" policy="(lte $ 71)" weight=1
analysis "mitre/binary" policy="(eq 0 (count $))" weight=1
analysis "mitre/binary" policy="(lte $ 0)" weight=1
analysis "mitre/fuzz" policy="(eq #t $)" weight=1
analysis "mitre/identity" policy="(lte $ 0.2)" weight=1
analysis "mitre/review" policy="(lte $ 0.05)" weight=1
Expand Down
23 changes: 16 additions & 7 deletions plugins/binary/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ mod fs;
use crate::binary_detector::{detect_binary_files, BinaryFileDetector};

use clap::Parser;
use hipcheck_sdk::{prelude::*, types::Target};
use hipcheck_sdk::{
prelude::*,
types::{LocalGitRepo, Target},
};
use pathbuf::pathbuf;
use serde::Deserialize;

Expand Down Expand Up @@ -46,19 +49,25 @@ impl TryFrom<RawConfig> for Config {
}
}

#[query(default)]
async fn binary(engine: &mut PluginEngine, value: Target) -> Result<Vec<PathBuf>> {
#[query]
async fn files(_engine: &mut PluginEngine, value: LocalGitRepo) -> Result<Vec<PathBuf>> {
let bfd = DETECTOR.get().ok_or(Error::UnspecifiedQueryState)?;
let repo = pathbuf![&value.local.path];
let repo = pathbuf![&value.path];
let out: Vec<PathBuf> = detect_binary_files(&repo)
.map_err(|_| Error::UnspecifiedQueryState)?
.into_iter()
.filter(|f| bfd.is_likely_binary_file(f))
.collect();
out.iter().for_each(|f| {
Ok(out)
}

#[query(default)]
async fn binary(engine: &mut PluginEngine, value: Target) -> Result<usize> {
let paths = files(engine, value.local).await?;
paths.iter().for_each(|f| {
engine.record_concern(format!("Found binary file at '{}'", f.to_string_lossy()))
});
Ok(out)
Ok(paths.len())
}

#[derive(Clone, Debug, Default)]
Expand Down Expand Up @@ -103,7 +112,7 @@ impl Plugin for BinaryPlugin {
// If no policy vars, we have no default expr
Some(None) => Ok("".to_owned()),
// Use policy config vars to construct a default expr
Some(Some(policy_conf)) => Ok(format!("(lte (count $) {})", policy_conf)),
Some(Some(policy_conf)) => Ok(format!("(lte $ {})", policy_conf)),
}
}

Expand Down