Skip to content

Commit e4f021c

Browse files
committed
Fix: add detail version to best_os_cpe as other component
To make it possible to display "2.0 (SP12 x86_64)" and VERSION="2.0 (SP12)" as different operating systems instead of combining them the whole version is added as other component within the cpe of best_os_cpe.
1 parent b8d686c commit e4f021c

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

rust/src/openvasd/container_image_scanner/detection.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use tokio::{
88

99
use crate::container_image_scanner::image::extractor::{Locator, LocatorError};
1010

11-
#[derive(Debug)]
11+
#[derive(Debug, PartialEq, PartialOrd)]
1212
pub struct OperatingSystem {
1313
pub name: String,
1414
pub version: String,
@@ -65,7 +65,7 @@ where
6565
Err(OperatingSystemDetectionError::NotFound)
6666
}
6767

68-
struct OperatingSystemDetector<T> {
68+
pub(crate) struct OperatingSystemDetector<T> {
6969
reader: T,
7070
}
7171

@@ -77,7 +77,7 @@ where
7777
OperatingSystemDetector { reader }
7878
}
7979

80-
async fn detect_operating_system(
80+
pub(crate) async fn detect_operating_system(
8181
self,
8282
) -> Result<OperatingSystem, OperatingSystemDetectionError> {
8383
#[rustfmt::skip]

rust/src/openvasd/container_image_scanner/image/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use registry::docker_v2::fake::RegistryMock as DockerRegistryV2Mock;
1111

1212
pub mod packages;
1313

14-
#[derive(Clone, Debug, PartialEq, Eq)]
14+
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq)]
1515
pub struct Image {
1616
pub registry: String,
1717
pub image: Option<String>,

rust/src/openvasd/container_image_scanner/messages.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl<T> CustomerMessage<T> {
5252
}
5353
}
5454

55+
#[derive(Debug, PartialEq, PartialOrd)]
5556
pub enum DetailPair<'a> {
5657
OS(&'a OperatingSystem),
5758
OSCpe(&'a OperatingSystem),
@@ -79,7 +80,19 @@ impl<'a> DetailPair<'a> {
7980
DetailPair::Packages(items) => items.join(","),
8081
DetailPair::OSCpe(os) => {
8182
// as requested by customer.
82-
format!("cpe:/o:{}:{}:{}", os.name, os.name, os.version_id)
83+
format!(
84+
"cpe:/o:{}:{}:{}::~~~~~{}",
85+
os.name,
86+
os.name,
87+
os.version_id,
88+
os.version
89+
.chars()
90+
.map(|x| match x {
91+
x if x.is_ascii_alphanumeric() => x,
92+
_ => '_',
93+
})
94+
.collect::<String>()
95+
)
8396
}
8497
DetailPair::HostName(image) => image.to_string(),
8598
}
@@ -184,3 +197,39 @@ where
184197
tracing::warn!(%error, id, amount_of_results=results.len(), "Scan results lost.");
185198
}
186199
}
200+
201+
#[cfg(test)]
202+
mod test {
203+
204+
use crate::container_image_scanner::{
205+
detection::OperatingSystemDetector, messages::DetailPair,
206+
};
207+
208+
#[tokio::test]
209+
async fn test_different_cpe() {
210+
let content = r#"
211+
Name="EulerOS"
212+
VERSION="2.0 (SP12)"
213+
ID="euleros"
214+
VERSION_ID="2.0"
215+
"#;
216+
let os = OperatingSystemDetector::from(content)
217+
.detect_operating_system()
218+
.await
219+
.unwrap();
220+
let content = r#"
221+
Name="EulerOS"
222+
VERSION="2.0 (SP12 x86_64)"
223+
ID="euleros"
224+
VERSION_ID="2.0"
225+
"#;
226+
let os_2 = OperatingSystemDetector::from(content)
227+
.detect_operating_system()
228+
.await
229+
.unwrap();
230+
assert_ne!(
231+
DetailPair::OSCpe(&os).value(),
232+
DetailPair::OSCpe(&os_2).value()
233+
)
234+
}
235+
}

0 commit comments

Comments
 (0)