Files
pruefwerk/compliance-core/src/models/issue.rs
T
sharangandClaude Opus 5.5 87b1e281ed
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 2m42s
CI / Deploy Dashboard (push) Successful in 6m14s
CI / Deploy Agent (push) Successful in 8m42s
fix(vex): a CVE a scan no longer finds is not stated fixed (#994) (#1015)
A scan never states VEX `fixed`. When a scan stops finding a CVE, its tracker issue stays open and gets one comment; a person closes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q9inRcu428A2iNjDHPEAT9
2026-11-27 22:23:03 +00:00

206 lines
6.3 KiB
Rust

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum TrackerType {
GitHub,
GitLab,
Gitea,
Jira,
}
impl std::fmt::Display for TrackerType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::GitHub => write!(f, "github"),
Self::GitLab => write!(f, "gitlab"),
Self::Gitea => write!(f, "gitea"),
Self::Jira => write!(f, "jira"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum IssueStatus {
Open,
InProgress,
Closed,
Resolved,
}
impl std::fmt::Display for IssueStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Open => write!(f, "open"),
Self::InProgress => write!(f, "in_progress"),
Self::Closed => write!(f, "closed"),
Self::Resolved => write!(f, "resolved"),
}
}
}
impl IssueStatus {
/// Every status, open ones first.
pub const ALL: [Self; 4] = [Self::Open, Self::InProgress, Self::Closed, Self::Resolved];
/// Whether the ticket is done (closed or resolved); the other two are
/// open.
pub fn is_closed(&self) -> bool {
match self {
Self::Closed | Self::Resolved => true,
Self::Open | Self::InProgress => false,
}
}
}
/// What a ticket is about, as its `finding_id` says.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IssueSubject<'a> {
/// One finding, by its id (the hex of its `ObjectId`).
Finding(&'a str),
/// A known vulnerability in one target's components, by the target id.
Cve {
/// The target (repository) the CVE was found in.
target_id: &'a str,
},
}
/// What the `finding_id` of a CVE ticket starts with.
const CVE_KEY: &str = "cve:";
/// The start of the `finding_id` of every CVE ticket of one target. The whole
/// key is `cve:{target_id}:{package}:{cve}`: one ticket per target, package
/// and CVE.
///
/// # Examples
///
/// ```
/// use compliance_core::models::issue::cve_issue_prefix;
/// assert_eq!(cve_issue_prefix("abc"), "cve:abc:");
/// ```
pub fn cve_issue_prefix(target_id: &str) -> String {
format!("{CVE_KEY}{target_id}:")
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrackerIssue {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<bson::oid::ObjectId>,
pub finding_id: String,
pub tracker_type: TrackerType,
pub external_id: String,
pub external_url: String,
pub title: String,
pub status: IssueStatus,
/// The tracker user who closed the issue there (#953), while it stays
/// closed: a person's decision the scan does not undo on its own.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub closed_in_tracker: Option<String>,
/// When a complete scan first no longer found the issue's CVE (#994),
/// while it stays gone: the issue was told so once and stays open, since
/// a CVE that is gone is no proof of a fix.
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "super::serde_helpers::opt_bson_datetime"
)]
pub gone_noted_at: Option<DateTime<Utc>>,
#[serde(with = "super::serde_helpers::bson_datetime")]
pub created_at: DateTime<Utc>,
#[serde(with = "super::serde_helpers::bson_datetime")]
pub updated_at: DateTime<Utc>,
}
impl TrackerIssue {
pub fn new(
finding_id: String,
tracker_type: TrackerType,
external_id: String,
external_url: String,
title: String,
) -> Self {
let now = Utc::now();
Self {
id: None,
finding_id,
tracker_type,
external_id,
external_url,
title,
status: IssueStatus::Open,
closed_in_tracker: None,
gone_noted_at: None,
created_at: now,
updated_at: now,
}
}
/// What the ticket is about: its finding, or for a CVE ticket the target
/// the CVE is in. `None` when `finding_id` is neither (empty, for an
/// issue whose finding had no id).
pub fn subject(&self) -> Option<IssueSubject<'_>> {
if let Some(rest) = self.finding_id.strip_prefix(CVE_KEY) {
// `rest` is `{target_id}:{package}:{cve}`; `split` always yields
// a first piece, so only an empty one is refused.
return rest
.split(':')
.next()
.filter(|id| !id.is_empty())
.map(|target_id| IssueSubject::Cve { target_id });
}
let id = self.finding_id.as_str();
(id.len() == 24 && id.bytes().all(|b| b.is_ascii_hexdigit()))
.then_some(IssueSubject::Finding(id))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn issue(finding_id: &str) -> TrackerIssue {
TrackerIssue::new(
finding_id.to_string(),
TrackerType::Gitea,
"913".into(),
"https://gitea.invalid/o/r/issues/913".into(),
"[high] semgrep: hardcoded key".into(),
)
}
#[test]
fn a_finding_ticket_is_about_its_finding() {
let id = "65a1b2c3d4e5f60718293a4b";
assert_eq!(issue(id).subject(), Some(IssueSubject::Finding(id)));
}
#[test]
fn a_cve_ticket_is_about_its_target() {
let key = format!("{}openssl:CVE-2024-0001", cve_issue_prefix("tgt1"));
assert_eq!(
issue(&key).subject(),
Some(IssueSubject::Cve { target_id: "tgt1" })
);
}
#[test]
fn a_ticket_without_a_usable_id_is_about_nothing() {
assert_eq!(issue("").subject(), None);
assert_eq!(issue("cve::pkg:CVE-1").subject(), None);
assert_eq!(issue("not-an-object-id").subject(), None);
// The right length, but not hex.
assert_eq!(issue("zzzzzzzzzzzzzzzzzzzzzzzz").subject(), None);
}
#[test]
fn closed_and_resolved_are_closed() {
let closed: Vec<IssueStatus> = IssueStatus::ALL
.into_iter()
.filter(IssueStatus::is_closed)
.collect();
assert_eq!(closed, [IssueStatus::Closed, IssueStatus::Resolved]);
}
}