CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Successful in 1m19s
CI / Deploy MCP (push) Successful in 5m2s
CI / Deploy Dashboard (push) Successful in 6m50s
CI / Deploy Agent (push) Successful in 9m12s
The SBOM page's component tab opens, for one target, with two bar charts (one hue, no legend; "Tabelle" shows the same numbers as tables): - "Welches Update entfernt die meisten CVEs": per CVE group of #949 with a fix, "openssl 3.0.11 -> 3.0.15" and "-3 CVEs", ranked by the CVEs the update fixes, then by the worst severity; top 10, then "Alle anzeigen". A bar opens that component's group card in place (breadcrumb "Komponenten > openssl 3.0.11", "Zurueck zum Diagramm"). The card is #949's own; only its visibility changes. - "Komponenten nach Oekosystem": per package manager the components and how many have a known vulnerability. A bar filters the component table below to it and puts it in the URL: the new additive route /sbom/ecosystem/:ecosystem?target=<id> renders the same page. The package-manager select now shows the active filter. Agent: GET /api/v1/targets/{id}/sbom-ecosystems counts the target's SBOM components per package manager in one aggregation (tenant database, org role gate as every route). The ranking and the filterable managers live in compliance-core (sbom_ecosystems), shared with /sbom/filters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
452 lines
18 KiB
Rust
452 lines
18 KiB
Rust
//! The "Komponenten" view of one target (#957), at the top of the SBOM
|
||
//! page's component tab: which update removes the most CVEs (from the CVE
|
||
//! groups of #949) and the components per ecosystem, as horizontal bars.
|
||
//! An update bar opens that component's CVE group card in place; an
|
||
//! ecosystem bar filters the component table below to it. "Tabelle" shows
|
||
//! the same numbers as plain tables.
|
||
|
||
use std::collections::HashSet;
|
||
|
||
use dioxus::prelude::*;
|
||
|
||
use compliance_core::cve_groups::{CveGroup, PackageKey};
|
||
use compliance_core::models::CveAlert;
|
||
use compliance_core::sbom_ecosystems::EcosystemCount;
|
||
|
||
use crate::components::severity_badge::SeverityBadge;
|
||
use crate::components::vex_panel::CveGroupCard;
|
||
use crate::i18n::texts::{common, component_overview as tx};
|
||
use crate::i18n::{format_count, use_lang};
|
||
use crate::infrastructure::sbom_ecosystems::fetch_sbom_ecosystems;
|
||
use crate::infrastructure::vex::{fetch_cve_groups, fetch_enisa_candidates, fetch_target_cves};
|
||
use crate::pages::sbom::pm_display_name;
|
||
|
||
/// How many update bars show before "Alle anzeigen".
|
||
const TOP_UPDATES: usize = 10;
|
||
|
||
/// The CVE groups an update helps with, the one removing the most CVEs
|
||
/// first (then the most severe, then by component). The groups and their
|
||
/// counts are #949's; this only filters and orders them.
|
||
pub fn rank_updates(groups: &[CveGroup]) -> Vec<&CveGroup> {
|
||
let mut ranked: Vec<&CveGroup> = groups
|
||
.iter()
|
||
.filter(|g| g.fix_version.is_some() && g.fixable > 0)
|
||
.collect();
|
||
ranked.sort_unstable_by(|a, b| {
|
||
b.fixable
|
||
.cmp(&a.fixable)
|
||
.then_with(|| b.counts.max().cmp(&a.counts.max()))
|
||
.then_with(|| a.key.cmp(&b.key))
|
||
});
|
||
ranked
|
||
}
|
||
|
||
/// An update's label: `openssl 3.0.11 → 3.0.15`.
|
||
fn update_label(group: &CveGroup) -> String {
|
||
let fix = group.fix_version.as_deref().unwrap_or_default();
|
||
format!("{} {} → {fix}", group.key.package, group.key.version)
|
||
}
|
||
|
||
/// The width of a bar of `value` against the largest one, `max`, as a CSS
|
||
/// percentage (at least a sliver, so a small bar stays visible).
|
||
fn bar_width(value: u64, max: u64) -> String {
|
||
// Lossy `as f64` is fine: counts this large do not exist, and the result
|
||
// is a CSS width.
|
||
let percent = if max == 0 {
|
||
0.0
|
||
} else {
|
||
value as f64 * 100.0 / max as f64
|
||
};
|
||
format!("width: {:.1}%", percent.max(2.0))
|
||
}
|
||
|
||
/// The "Komponenten" overview of target `target_id`. `ecosystem` is the
|
||
/// component table's package-manager filter; `on_ecosystem` sets it (an
|
||
/// empty name clears it).
|
||
#[component]
|
||
pub fn ComponentOverview(
|
||
target_id: ReadSignal<String>,
|
||
ecosystem: String,
|
||
on_ecosystem: EventHandler<String>,
|
||
) -> Element {
|
||
let lang = use_lang()();
|
||
let mut groups = use_resource(move || async move { fetch_cve_groups(target_id()).await.ok() });
|
||
let ecosystems =
|
||
use_resource(move || async move { fetch_sbom_ecosystems(target_id()).await.ok() });
|
||
let mut as_table = use_signal(|| false);
|
||
let mut opened = use_signal(|| Option::<PackageKey>::None);
|
||
// Another target picked on the page closes the open group card.
|
||
use_effect(move || {
|
||
target_id();
|
||
opened.set(None);
|
||
});
|
||
|
||
if let Some(key) = opened() {
|
||
let open_group = groups
|
||
.read()
|
||
.as_ref()
|
||
.and_then(|found| found.as_ref())
|
||
.and_then(|found| found.iter().find(|g| g.key == key).cloned());
|
||
return rsx! {
|
||
div { class: "co-view",
|
||
Breadcrumb {
|
||
selection: format!("{} {}", key.package, key.version),
|
||
on_back: move |()| opened.set(None),
|
||
}
|
||
match open_group {
|
||
Some(group) => rsx! {
|
||
OpenGroup {
|
||
target_id,
|
||
group,
|
||
on_saved: move |()| groups.restart(),
|
||
}
|
||
},
|
||
None if groups.read().is_none() => rsx! {
|
||
div { class: "loading", {common::LOADING.get(lang)} }
|
||
},
|
||
None => rsx! { p { class: "target-muted", {tx::GROUP_SETTLED.get(lang)} } },
|
||
}
|
||
}
|
||
};
|
||
}
|
||
if !ecosystem.is_empty() {
|
||
return rsx! {
|
||
div { class: "co-view",
|
||
Breadcrumb {
|
||
selection: pm_display_name(&ecosystem).to_string(),
|
||
on_back: move |()| on_ecosystem.call(String::new()),
|
||
}
|
||
}
|
||
};
|
||
}
|
||
|
||
rsx! {
|
||
div { class: "co-view",
|
||
div { class: "co-toggle", role: "group",
|
||
for (table, label) in [(false, tx::VIEW_CHART), (true, tx::VIEW_TABLE)] {
|
||
button {
|
||
class: if as_table() == table { "co-toggle-btn active" } else { "co-toggle-btn" },
|
||
"aria-pressed": if as_table() == table { "true" } else { "false" },
|
||
onclick: move |_| as_table.set(table),
|
||
{label.get(lang)}
|
||
}
|
||
}
|
||
}
|
||
div { class: "co-charts",
|
||
section { class: "card co-chart",
|
||
h3 { class: "co-chart-title", {tx::UPDATES_TITLE.get(lang)} }
|
||
p { class: "co-chart-hint", {tx::UPDATES_HINT.get(lang)} }
|
||
match &*groups.read() {
|
||
None => rsx! { div { class: "loading", {common::LOADING.get(lang)} } },
|
||
Some(None) => rsx! { p { class: "target-muted", {tx::LOAD_FAILED.get(lang)} } },
|
||
Some(Some(found)) => rsx! {
|
||
UpdateBars {
|
||
groups: found.clone(),
|
||
as_table: as_table(),
|
||
on_open: move |key: PackageKey| opened.set(Some(key)),
|
||
}
|
||
},
|
||
}
|
||
}
|
||
section { class: "card co-chart",
|
||
h3 { class: "co-chart-title", {tx::ECOSYSTEMS_TITLE.get(lang)} }
|
||
p { class: "co-chart-hint", {tx::ECOSYSTEMS_HINT.get(lang)} }
|
||
match &*ecosystems.read() {
|
||
None => rsx! { div { class: "loading", {common::LOADING.get(lang)} } },
|
||
Some(None) => rsx! { p { class: "target-muted", {tx::LOAD_FAILED.get(lang)} } },
|
||
Some(Some(found)) if found.is_empty() => rsx! {
|
||
p { class: "target-muted", {tx::NO_ECOSYSTEMS.get(lang)} }
|
||
},
|
||
Some(Some(found)) => rsx! {
|
||
EcosystemBars { ecosystems: found.clone(), as_table: as_table(), on_ecosystem }
|
||
},
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// "Komponenten › `selection`" with the way back to the charts.
|
||
#[component]
|
||
fn Breadcrumb(selection: String, on_back: EventHandler<()>) -> Element {
|
||
let lang = use_lang()();
|
||
rsx! {
|
||
nav { class: "co-breadcrumb", "aria-label": tx::BREADCRUMB.get(lang),
|
||
span { class: "co-breadcrumb-root", {tx::BREADCRUMB.get(lang)} }
|
||
span { class: "co-breadcrumb-sep", "aria-hidden": "true", "›" }
|
||
strong { "{selection}" }
|
||
button {
|
||
class: "btn btn-ghost btn-sm co-back",
|
||
onclick: move |_| on_back.call(()),
|
||
{tx::BACK.get(lang)}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// The update bars (or their table) of `groups`, the top 10 until "Alle
|
||
/// anzeigen"; `on_open` receives the component of a clicked bar.
|
||
#[component]
|
||
fn UpdateBars(groups: Vec<CveGroup>, as_table: bool, on_open: EventHandler<PackageKey>) -> Element {
|
||
let lang = use_lang()();
|
||
let mut show_all = use_signal(|| false);
|
||
let ranked = rank_updates(&groups);
|
||
if ranked.is_empty() {
|
||
return rsx! { p { class: "target-muted", {tx::NO_UPDATES.get(lang)} } };
|
||
}
|
||
let total = ranked.len();
|
||
let shown = if show_all() {
|
||
total
|
||
} else {
|
||
total.min(TOP_UPDATES)
|
||
};
|
||
let max = ranked.first().map_or(0, |g| g.fixable as u64);
|
||
rsx! {
|
||
if as_table {
|
||
div { class: "table-wrapper",
|
||
table { class: "co-table",
|
||
thead {
|
||
tr {
|
||
th { {tx::COL_UPDATE.get(lang)} }
|
||
th { class: "co-num", {tx::COL_REMOVES.get(lang)} }
|
||
th { {tx::COL_SEVERITY.get(lang)} }
|
||
}
|
||
}
|
||
tbody {
|
||
for group in ranked.iter().take(shown) {
|
||
{
|
||
let label = update_label(group);
|
||
let key = group.key.clone();
|
||
rsx! {
|
||
tr {
|
||
td {
|
||
button {
|
||
class: "co-link",
|
||
"aria-label": tx::update_action(&label, group.fixable, lang),
|
||
onclick: move |_| on_open.call(key.clone()),
|
||
"{label}"
|
||
}
|
||
}
|
||
td { class: "co-num", {tx::removes(group.fixable)} }
|
||
td { SeverityBadge { severity: group.counts.max().as_str().to_string() } }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
ol { class: "co-bars",
|
||
for group in ranked.iter().take(shown) {
|
||
{
|
||
let label = update_label(group);
|
||
let action = tx::update_action(&label, group.fixable, lang);
|
||
let key = group.key.clone();
|
||
rsx! {
|
||
li {
|
||
button {
|
||
class: "co-bar",
|
||
"aria-label": "{action}",
|
||
title: "{action}",
|
||
onclick: move |_| on_open.call(key.clone()),
|
||
span { class: "co-bar-label", "{label}" }
|
||
span { class: "co-bar-track",
|
||
span { class: "co-bar-fill", style: bar_width(group.fixable as u64, max) }
|
||
}
|
||
span { class: "co-bar-value", {tx::removes(group.fixable)} }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if total > TOP_UPDATES {
|
||
button {
|
||
class: "btn btn-ghost btn-sm co-more",
|
||
"aria-expanded": if show_all() { "true" } else { "false" },
|
||
onclick: move |_| show_all.toggle(),
|
||
if show_all() { {tx::TOP_ONLY.get(lang)} } else { {tx::show_all(total, lang)} }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// The ecosystem bars (or their table) of `ecosystems`; a click sends the
|
||
/// package manager to `on_ecosystem`.
|
||
#[component]
|
||
fn EcosystemBars(
|
||
ecosystems: Vec<EcosystemCount>,
|
||
as_table: bool,
|
||
on_ecosystem: EventHandler<String>,
|
||
) -> Element {
|
||
let lang = use_lang()();
|
||
let max = ecosystems.iter().map(|e| e.components).max().unwrap_or(0);
|
||
rsx! {
|
||
if as_table {
|
||
div { class: "table-wrapper",
|
||
table { class: "co-table",
|
||
thead {
|
||
tr {
|
||
th { {tx::COL_ECOSYSTEM.get(lang)} }
|
||
th { class: "co-num", {tx::COL_COMPONENTS.get(lang)} }
|
||
th { class: "co-num", {tx::COL_WITH_CVE.get(lang)} }
|
||
}
|
||
}
|
||
tbody {
|
||
for count in ecosystems.iter() {
|
||
{
|
||
let name = pm_display_name(&count.package_manager);
|
||
let action = tx::ecosystem_action(name, count.components, count.with_cve, lang);
|
||
let manager = count.package_manager.clone();
|
||
rsx! {
|
||
tr {
|
||
td {
|
||
button {
|
||
class: "co-link",
|
||
"aria-label": "{action}",
|
||
onclick: move |_| on_ecosystem.call(manager.clone()),
|
||
"{name}"
|
||
}
|
||
}
|
||
td { class: "co-num", {format_count(count.components, lang)} }
|
||
td { class: "co-num", {format_count(count.with_cve, lang)} }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
ol { class: "co-bars",
|
||
for count in ecosystems.iter() {
|
||
{
|
||
let name = pm_display_name(&count.package_manager);
|
||
let action = tx::ecosystem_action(name, count.components, count.with_cve, lang);
|
||
let manager = count.package_manager.clone();
|
||
rsx! {
|
||
li {
|
||
button {
|
||
class: "co-bar",
|
||
"aria-label": "{action}",
|
||
title: "{action}",
|
||
onclick: move |_| on_ecosystem.call(manager.clone()),
|
||
span { class: "co-bar-label", "{name}" }
|
||
span { class: "co-bar-track",
|
||
span { class: "co-bar-fill", style: bar_width(count.components, max) }
|
||
}
|
||
span { class: "co-bar-value",
|
||
{tx::ecosystem_value(count.components, count.with_cve, lang)}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// The CVE group card of #949 for `group`, with its alerts and the CRA
|
||
/// Article 14 candidates it needs, loaded when it opens.
|
||
#[component]
|
||
fn OpenGroup(
|
||
target_id: ReadSignal<String>,
|
||
group: CveGroup,
|
||
on_saved: EventHandler<()>,
|
||
) -> Element {
|
||
let lang = use_lang()();
|
||
let mut alerts = use_resource(move || async move { fetch_target_cves(target_id()).await.ok() });
|
||
let mut enisa = use_resource(move || async move {
|
||
fetch_enisa_candidates(target_id())
|
||
.await
|
||
.unwrap_or_default()
|
||
});
|
||
let ids: HashSet<&str> = group.alert_ids.iter().map(String::as_str).collect();
|
||
let members: Option<Vec<CveAlert>> = alerts.read().as_ref().map(|loaded| {
|
||
loaded
|
||
.iter()
|
||
.flatten()
|
||
.filter(|a| a.id.is_some_and(|id| ids.contains(id.to_hex().as_str())))
|
||
.cloned()
|
||
.collect()
|
||
});
|
||
let candidates = enisa.read().clone().unwrap_or_default();
|
||
match members {
|
||
None => rsx! { div { class: "loading", {common::LOADING.get(lang)} } },
|
||
Some(members) => rsx! {
|
||
CveGroupCard {
|
||
target_id: target_id(),
|
||
group,
|
||
alerts: members,
|
||
candidates,
|
||
on_saved: move |()| {
|
||
alerts.restart();
|
||
enisa.restart();
|
||
on_saved.call(());
|
||
},
|
||
}
|
||
},
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
use compliance_core::cve_groups::SeverityCounts;
|
||
|
||
fn group(package: &str, fixable: usize, fix: Option<&str>, counts: SeverityCounts) -> CveGroup {
|
||
CveGroup {
|
||
key: PackageKey {
|
||
package: package.into(),
|
||
version: "1.0.0".into(),
|
||
purl: None,
|
||
},
|
||
fix_version: fix.map(String::from),
|
||
fixable,
|
||
counts,
|
||
kev: false,
|
||
epss_max: None,
|
||
cve_ids: Vec::new(),
|
||
alert_ids: Vec::new(),
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn updates_rank_by_the_cves_they_remove_then_by_severity() {
|
||
let high = SeverityCounts {
|
||
high: 1,
|
||
..SeverityCounts::default()
|
||
};
|
||
let low = SeverityCounts {
|
||
low: 1,
|
||
..SeverityCounts::default()
|
||
};
|
||
let groups = vec![
|
||
group("lodash", 1, Some("4.17.21"), low),
|
||
group("vite", 3, Some("8.3.3"), low),
|
||
group("openssl", 1, Some("3.0.15"), high),
|
||
// No fix named: no update to rank.
|
||
group("left-pad", 0, None, high),
|
||
];
|
||
let ranked: Vec<&str> = rank_updates(&groups)
|
||
.iter()
|
||
.map(|g| g.key.package.as_str())
|
||
.collect();
|
||
assert_eq!(ranked, ["vite", "openssl", "lodash"]);
|
||
assert_eq!(update_label(&groups[1]), "vite 1.0.0 → 8.3.3");
|
||
}
|
||
|
||
#[test]
|
||
fn a_bar_is_as_wide_as_its_share_of_the_largest() {
|
||
assert_eq!(bar_width(3, 3), "width: 100.0%");
|
||
assert_eq!(bar_width(1, 4), "width: 25.0%");
|
||
assert_eq!(bar_width(1, 1000), "width: 2.0%");
|
||
assert_eq!(bar_width(0, 0), "width: 2.0%");
|
||
}
|
||
}
|