Files
pruefwerk/compliance-core/src/models/sbom.rs
T
sharang 5b4d8bbe5f
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Successful in 1m16s
CI / Deploy MCP (push) Successful in 3m18s
CI / Deploy Dashboard (push) Successful in 7m6s
CI / Deploy Agent (push) Successful in 9m28s
feat(csaf): Siemens devices by order number and firmware against Siemens advisories (SPS 4a) (#1077)
2026-11-28 13:56:53 +00:00

242 lines
9.6 KiB
Rust

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VulnRef {
pub id: String,
pub source: String,
pub severity: Option<String>,
pub url: Option<String>,
/// Exploitation of the advisory's CVE (KEV, EPSS, SSVC; #493).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub exploitation: Option<super::cve::Exploitation>,
/// Other ids of the advisory (a GHSA's `CVE-…`), as OSV lists them.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub aliases: Vec<String>,
}
impl VulnRef {
/// The id the vulnerability is listed under: its CVE when one is known
/// (see [`super::cve::primary_id`]).
pub fn cve(&self) -> &str {
super::cve::primary_id(&self.id, &self.aliases)
}
}
/// The `package_manager` of a component read from a Yocto/OpenEmbedded recipe
/// in a layer tree, without a build.
pub const YOCTO_RECIPE_MANAGER: &str = "yocto-recipe";
/// The scanner (`Finding::scanner`) of the findings that say why a target's
/// SBOM is empty or incomplete: code without a manifest, and manifests whose
/// dependencies were not resolved (#979).
pub const SBOM_COVERAGE_SCANNER: &str = "sbom-coverage";
/// Where a [`YOCTO_RECIPE_MANAGER`] component comes from, as the SBOM view,
/// the exports and the evidence say it. Layer overrides, PACKAGECONFIG and
/// what the image installs are not resolved, so the inventory lists more than
/// an image contains.
pub const YOCTO_RECIPE_SOURCE: &str = "Yocto recipe inventory (not the image SBOM)";
/// How a component is known to be in the product's image.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InImageBasis {
/// An image recipe names it (IMAGE_INSTALL, a package group it installs),
/// read from the recipes without BitBake: what the image declares, not
/// what a build resolved.
Declared,
/// The build's own SPDX lists one of its packages in the image (Yocto's
/// create-spdx): what the build installed.
Built,
}
impl InImageBasis {
/// Its stored name (`declared`).
pub fn as_str(self) -> &'static str {
match self {
Self::Declared => "declared",
Self::Built => "built",
}
}
/// How the exports and the evidence say it.
pub fn describe(self) -> &'static str {
match self {
Self::Declared => "In the image (declared in the recipes, not resolved by bitbake)",
Self::Built => "In the image (installed by the build, from its SPDX)",
}
}
}
/// The images a component is in, and how that is known.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct InImage {
pub basis: InImageBasis,
/// The image recipes (`pump-image`) that install it, or the
/// configuration file that does for every image (`conf/local.conf.sample`).
pub images: Vec<String>,
}
impl InImage {
/// `In the image (declared …): pump-image, rescue-image`.
pub fn describe(&self) -> String {
format!("{}: {}", self.basis.describe(), self.images.join(", "))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SbomEntry {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<bson::oid::ObjectId>,
pub repo_id: String,
pub name: String,
pub version: String,
pub package_manager: String,
/// The version requirement a manifest declares where it pins no exact version (`^1.2.0`,
/// `>=5.1.2`, a branch). Kept apart from `version`, which CVE matching joins on: a range must
/// never be read as the version in use.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version_requirement: Option<String>,
pub license: Option<String>,
pub purl: Option<String>,
pub known_vulnerabilities: Vec<VulnRef>,
/// Where the component was found, when more than its name tells: the
/// manifests and DLLs one NuGet package was read from (`Hmi/packages.config`,
/// `lib/Ionic.Zip.dll (DLL metadata: DotNetZip Library 1.9.1.5)`). Kept when
/// duplicates of one package are merged.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub evidence: Vec<String>,
/// Hashes of the file the component is distributed as, from its lockfile
/// (npm's `integrity`, NuGet's `contentHash`, Cargo's `checksum`; #508).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub hashes: Vec<ComponentHash>,
/// Where that file is downloaded from: the lockfile's URL, or the public
/// registry's when the project uses no other.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub download_url: Option<String>,
/// The web page of whoever makes and maintains the component: its
/// project's homepage or repository, from its registry or purl (BSI
/// TR-03183-2's component creator; #508). A URL only, never a person.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub manufacturer_url: Option<String>,
/// The CPE 2.3 name NVD knows the component by, where it has no purl an
/// advisory database could look up (a Yocto recipe's CVE_PRODUCT).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cpe: Option<String>,
/// The vendor's order number of a device (Siemens MLFB
/// `6ES7511-1AK02-0AB0`; [`super::PlcDevice`]), which the vendor's CSAF
/// advisories name it by.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub order_number: Option<String>,
/// The images the component is in, where that is known (a Yocto recipe
/// an image recipe installs); `None` when it is not, and on components
/// stored before it was recorded.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub in_image: Option<InImage>,
/// The licence a person confirmed for the component
/// ([`super::license::LicenseConclusion`]). Never stored with it: a scan
/// rewrites the components, so it is attached when they are read.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub concluded: Option<super::license::LicenseConclusion>,
/// What the workspace's licence policy says about it
/// ([`super::license_policy::evaluate`]). Never stored: worked out when
/// the list is read.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub policy: Option<super::license_policy::LicenseVerdict>,
#[serde(with = "super::serde_helpers::bson_datetime")]
pub created_at: DateTime<Utc>,
#[serde(with = "super::serde_helpers::bson_datetime")]
pub updated_at: DateTime<Utc>,
}
/// A hash of a component's distributed file, as CycloneDX writes it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ComponentHash {
/// CycloneDX's algorithm name: `SHA-512`, `SHA-384`, `SHA-256`, `SHA-1`.
pub alg: String,
/// The digest, lowercase hex.
pub content: String,
}
impl SbomEntry {
/// The licence that counts: the confirmed one, else the detected one.
pub fn effective_license(&self) -> Option<&str> {
match &self.concluded {
Some(c) => Some(c.display()),
None => self.license.as_deref().filter(|l| !l.trim().is_empty()),
}
}
pub fn new(repo_id: String, name: String, version: String, package_manager: String) -> Self {
let now = Utc::now();
Self {
id: None,
repo_id,
name,
version,
package_manager,
version_requirement: None,
license: None,
purl: None,
known_vulnerabilities: Vec::new(),
evidence: Vec::new(),
hashes: Vec::new(),
download_url: None,
manufacturer_url: None,
cpe: None,
order_number: None,
in_image: None,
concluded: None,
policy: None,
created_at: now,
updated_at: now,
}
}
}
/// How well a scan's SBOM meets the SBOM standards (#496), as sbomqs scores
/// the CycloneDX document Prüfwerk exports for the target. Supporting evidence
/// for the CRA SBOM requirement (cra-ai-23), not a source of findings.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SbomQuality {
/// The scoring tool (`sbomqs`).
pub tool: String,
/// Its version (`v2.1.2`).
pub tool_version: String,
/// The format scored (`cyclonedx 1.5`).
pub spec: String,
/// Components the document lists, as the tool counted them.
pub components: u32,
/// One score per profile, in the order they were asked for.
pub profiles: Vec<SbomQualityProfile>,
}
/// One profile's score.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SbomQualityProfile {
/// The profile as Prüfwerk asks for it (`bsi-v2.1`, `ntia-2021`).
pub id: String,
/// Its name as the tool reports it (`BSI TR-03183-2 v2.1`).
pub name: String,
/// 0 to 10.
pub score: f64,
/// The tool's grade (`A` to `F`).
pub grade: String,
/// The profile's checks the SBOM does not fully meet.
pub failed: Vec<SbomQualityCheck>,
}
/// One check of a profile that the SBOM does not fully meet.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SbomQualityCheck {
/// The tool's key (`comp_supplier`).
pub key: String,
/// What the tool found (`supplier missing for all 3 components`).
pub description: String,
/// 0 to 10.
pub score: f64,
/// Whether the profile requires it (otherwise it is additional).
pub required: bool,
}