Skip to content

Commit

Permalink
Merge pull request #56 from mrl5/issue-54
Browse files Browse the repository at this point in the history
fix(cpe-tag): errors when package version "0" or "9999" [#54]
  • Loading branch information
mrl5 authored Jul 23, 2022
2 parents c138690 + cff2ed2 commit 8fd7f50
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions crates/cpe-tag/src/searchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ use std::error::Error;
use std::path::Path;

pub fn grep(pattern: String, feed: &Path) -> Result<HashSet<String>, Box<dyn Error>> {
if pattern.is_empty() {
return Ok(HashSet::new());
}

let matcher = RegexMatcher::new_line_matcher(&pattern)?;
let mut matches: Vec<String> = vec![];

Expand All @@ -41,13 +45,7 @@ pub fn match_cpes<'a>(
re_pattern: &str,
) -> HashMap<&'a Package, HashSet<String>> {
let mut cpes = HashMap::new();
let re = Regex::new(re_pattern).unwrap();
let matches = feed
.iter()
.filter(|feed_entry| re.is_match(feed_entry))
.map(|x| x.to_string())
.collect();
cpes.insert(pkg, matches);
cpes.insert(pkg, get_matches(re_pattern, feed));
cpes
}

Expand All @@ -60,6 +58,18 @@ pub fn contains_cpe_json_key(line: &str) -> bool {
line.contains(CPE_KEYWORD_IN_FEED_LINE)
}

fn get_matches(re_pattern: &str, feed: &[Box<str>]) -> HashSet<String> {
if re_pattern.is_empty() {
return HashSet::new();
}

let re = Regex::new(re_pattern).unwrap();
feed.iter()
.filter(|feed_entry| re.is_match(feed_entry))
.map(|x| x.to_string())
.collect()
}

fn get_uniq_values(matches: Vec<String>) -> HashSet<String> {
let values: HashSet<String> = matches.iter().map(|m| scrap_cpe(m)).collect();
values
Expand All @@ -76,6 +86,19 @@ mod tests {
"cpe:2.3:a:xmlsoft:libxml2:2.9.10:*:*:*:*:*:*:*"
);
}
#[test]
fn it_should_ignore_empty_string_as_a_pattern() {
let test_matches = [
Box::from(
" \"cpe23Uri\" : \"cpe:2.3:a:busybox:busybox:1.29.3:*:*:*:*:*:*:*\"\r\n",
),
Box::from(
" \"cpe23Uri\" : \"cpe:2.3:a:xmlsoft:libxml2:2.9.10:*:*:*:*:*:*:*\"\r\n",
),
];

assert_eq!(get_matches("", &test_matches).len(), 0);
}

#[test]
fn it_should_return_unique_values() {
Expand Down

0 comments on commit 8fd7f50

Please sign in to comment.