Skip to main content

plut_core/
finding.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use crate::config::Target;
6
7/// How loud a finding is.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
9#[serde(rename_all = "lowercase")]
10pub enum Severity {
11    #[default]
12    Info,
13    Warning,
14    Error,
15}
16
17impl Severity {
18    /// The lowercase name used in config and JSON.
19    #[must_use]
20    pub const fn as_str(self) -> &'static str {
21        match self {
22            Self::Info => "info",
23            Self::Warning => "warning",
24            Self::Error => "error",
25        }
26    }
27}
28
29/// How strongly the rule believes a finding is a real issue.
30#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
31#[serde(rename_all = "lowercase")]
32pub enum Confidence {
33    Low,
34    #[default]
35    Medium,
36    High,
37}
38
39impl Confidence {
40    /// The lowercase name used in JSON and the terminal.
41    #[must_use]
42    pub const fn as_str(self) -> &'static str {
43        match self {
44            Self::Low => "low",
45            Self::Medium => "medium",
46            Self::High => "high",
47        }
48    }
49}
50
51/// Which family a rule belongs to.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
53#[serde(rename_all = "lowercase")]
54pub enum Namespace {
55    /// A built-in `GB` rule.
56    #[default]
57    Gb,
58    /// A `LuneBlox` runtime rule.
59    Luneblox,
60    /// A Lute runtime rule.
61    Lute,
62    /// A project rule.
63    Custom,
64}
65
66impl Namespace {
67    /// The lowercase name used in JSON and SARIF.
68    #[must_use]
69    pub const fn as_str(self) -> &'static str {
70        match self {
71            Self::Gb => "gb",
72            Self::Luneblox => "luneblox",
73            Self::Lute => "lute",
74            Self::Custom => "custom",
75        }
76    }
77}
78
79/// A byte range in a source file.
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
81pub struct Span {
82    pub start: usize,
83    pub end: usize,
84}
85
86impl Span {
87    /// Build a span from two byte offsets.
88    #[must_use]
89    pub const fn new(start: usize, end: usize) -> Self {
90        Self { start, end }
91    }
92}
93
94/// One report from one rule.
95#[derive(Debug, Clone, Serialize, Deserialize, Default)]
96pub struct Finding {
97    /// Rule id, such as `GB201`.
98    pub rule: String,
99    /// Which family produced the rule.
100    #[serde(default)]
101    pub namespace: Namespace,
102    /// The runtimes this finding applies to.
103    #[serde(default)]
104    pub targets: Vec<Target>,
105    pub severity: Severity,
106    /// Path relative to the project root.
107    pub path: PathBuf,
108    /// 1-based line of `span.start`. Zero when the finding is about the file as a whole.
109    pub line: usize,
110    /// 1-based column of `span.start`. Zero when the finding is about the file as a whole.
111    pub column: usize,
112    pub span: Span,
113    pub message: String,
114    /// How strongly the rule believes the finding.
115    #[serde(default)]
116    pub confidence: Confidence,
117    /// The CWE this finding belongs to, when one applies.
118    #[serde(skip_serializing_if = "Option::is_none")]
119    pub cwe: Option<u32>,
120    #[serde(skip_serializing_if = "Option::is_none")]
121    pub help: Option<String>,
122    /// Exploit scenario, for reviewers and agents.
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub exploit: Option<String>,
125    /// Step-by-step remediation guidance.
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub remediation: Option<String>,
128    /// Machine-applicable edits. Empty means "suggestion only".
129    #[serde(default, skip_serializing_if = "Vec::is_empty")]
130    pub edits: Vec<TextEdit>,
131    /// How safe the edits are. Absent means review-required.
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub fix_safety: Option<FixSafety>,
134    /// Hash of the source the finding was produced from. Internal to `--fix`: it is
135    /// not part of the report schema.
136    #[serde(skip)]
137    pub source_hash: Option<String>,
138}
139
140/// How safe it is to apply the edits without a human reading the diff.
141#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
142#[serde(rename_all = "snake_case")]
143pub enum FixSafety {
144    /// No behaviour change by construction.
145    Safe,
146    /// A mechanical API migration, such as `wait` to `task.wait`.
147    Migration,
148    /// A patch that needs a human or an agent to review it first.
149    ReviewRequired,
150}
151
152/// A byte-range replacement suggested by a rule.
153#[derive(Debug, Clone, Default, Serialize, Deserialize)]
154pub struct TextEdit {
155    pub start: usize,
156    pub end: usize,
157    pub replacement: String,
158}
159
160/// A stable hash of a source text, used to detect a file that changed between the
161/// analysis and a fix. This is not a cryptographic hash; it only needs to change when
162/// the bytes change, and to stay the same across processes.
163#[must_use]
164pub fn source_hash(source: &str) -> String {
165    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
166
167    for byte in source.as_bytes() {
168        hash ^= u64::from(*byte);
169        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
170    }
171
172    format!("{hash:016x}")
173}