1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
use indexmap::IndexMap;
use crate::config::Config;
pub const FORMAT_TABLE: &str = "table";
pub const FORMAT_PLAIN: &str = "plain";
pub const FORMAT_JSON: &str = "json";
pub const FORMAT_SUMMARY: &str = "summary";
pub const FORMATS: [&str; 4] = [FORMAT_TABLE, FORMAT_PLAIN, FORMAT_JSON, FORMAT_SUMMARY];
pub const ABANDONED_IGNORE: &str = "ignore";
pub const ABANDONED_REPORT: &str = "report";
pub const ABANDONED_FAIL: &str = "fail";
pub const ABANDONEDS: [&str; 3] = [ABANDONED_IGNORE, ABANDONED_REPORT, ABANDONED_FAIL];
/// Mirrors `Auditor::FORMAT_*` constants.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AuditFormat {
#[default]
Table,
Plain,
Json,
Summary,
}
impl AuditFormat {
pub fn as_str(self) -> &'static str {
match self {
Self::Table => FORMAT_TABLE,
Self::Plain => FORMAT_PLAIN,
Self::Json => FORMAT_JSON,
Self::Summary => FORMAT_SUMMARY,
}
}
pub fn from_str(s: &str) -> Option<Self> {
match s {
FORMAT_TABLE => Some(Self::Table),
FORMAT_PLAIN => Some(Self::Plain),
FORMAT_JSON => Some(Self::Json),
FORMAT_SUMMARY => Some(Self::Summary),
_ => None,
}
}
}
/// Mirrors `Auditor::ABANDONED_*` constants.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AbandonedHandling {
Ignore,
Report,
#[default]
Fail,
}
impl AbandonedHandling {
pub fn as_str(self) -> &'static str {
match self {
Self::Ignore => ABANDONED_IGNORE,
Self::Report => ABANDONED_REPORT,
Self::Fail => ABANDONED_FAIL,
}
}
pub fn from_str(s: &str) -> Option<Self> {
match s {
ABANDONED_IGNORE => Some(Self::Ignore),
ABANDONED_REPORT => Some(Self::Report),
ABANDONED_FAIL => Some(Self::Fail),
_ => None,
}
}
}
/// Mirrors `Composer\Advisory\AuditConfig`.
#[derive(Debug, Clone)]
pub struct AuditConfig {
pub audit: bool,
pub audit_format: AuditFormat,
pub audit_abandoned: AbandonedHandling,
pub block_insecure: bool,
pub block_abandoned: bool,
pub ignore_unreachable: bool,
pub ignore_list_for_audit: IndexMap<String, Option<String>>,
pub ignore_list_for_blocking: IndexMap<String, Option<String>>,
pub ignore_severity_for_audit: IndexMap<String, Option<String>>,
pub ignore_severity_for_blocking: IndexMap<String, Option<String>>,
pub ignore_abandoned_for_audit: IndexMap<String, Option<String>>,
pub ignore_abandoned_for_blocking: IndexMap<String, Option<String>>,
}
struct ParsedIgnore {
audit: IndexMap<String, Option<String>>,
block: IndexMap<String, Option<String>>,
}
/// Mirrors `AuditConfig::parseIgnoreWithApply()`.
///
/// Supports these JSON shapes:
/// - `["CVE-1"]` — simple list, apply=all, reason=null
/// - `{"CVE-1": "reason"}` — with reason, apply=all
/// - `{"CVE-1": null}` — null reason, apply=all
/// - `{"CVE-1": {"apply": "audit|block|all", "reason": "..."}}` — detailed
fn parse_ignore_with_apply(config: &serde_json::Value) -> ParsedIgnore {
let mut for_audit: IndexMap<String, Option<String>> = IndexMap::new();
let mut for_block: IndexMap<String, Option<String>> = IndexMap::new();
match config {
serde_json::Value::Array(arr) => {
for item in arr {
if let Some(s) = item.as_str() {
for_audit.insert(s.to_string(), None);
for_block.insert(s.to_string(), None);
}
}
}
serde_json::Value::Object(obj) => {
for (key, value) in obj {
let (apply, reason) = match value {
serde_json::Value::String(r) => ("all", Some(r.clone())),
serde_json::Value::Null => ("all", None),
serde_json::Value::Object(detail) => {
let apply = detail
.get("apply")
.and_then(|v| v.as_str())
.unwrap_or("all");
if !matches!(apply, "audit" | "block" | "all") {
continue;
}
let reason = detail
.get("reason")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
(apply, reason)
}
_ => continue,
};
if apply == "audit" || apply == "all" {
for_audit.insert(key.clone(), reason.clone());
}
if apply == "block" || apply == "all" {
for_block.insert(key.clone(), reason);
}
}
}
_ => {}
}
ParsedIgnore {
audit: for_audit,
block: for_block,
}
}
impl AuditConfig {
/// Mirrors `AuditConfig::fromConfig()`.
pub fn from_config(config: &Config, audit: bool, audit_format: AuditFormat) -> Self {
let empty_arr = serde_json::Value::Array(vec![]);
let audit_val = config
.get("audit")
.unwrap_or_else(|| serde_json::Value::Object(Default::default()));
let ignore_list_val = audit_val
.get("ignore")
.cloned()
.unwrap_or_else(|| empty_arr.clone());
let ignore_list_parsed = parse_ignore_with_apply(&ignore_list_val);
let ignore_abandoned_val = audit_val
.get("ignore-abandoned")
.cloned()
.unwrap_or_else(|| empty_arr.clone());
let ignore_abandoned_parsed = parse_ignore_with_apply(&ignore_abandoned_val);
let ignore_severity_val = audit_val
.get("ignore-severity")
.cloned()
.unwrap_or_else(|| empty_arr.clone());
let ignore_severity_parsed = parse_ignore_with_apply(&ignore_severity_val);
let audit_abandoned = audit_val
.get("abandoned")
.and_then(|v| v.as_str())
.and_then(AbandonedHandling::from_str)
.unwrap_or_default();
let block_insecure = audit_val
.get("block-insecure")
.and_then(|v| v.as_bool())
.unwrap_or(true);
let block_abandoned = audit_val
.get("block-abandoned")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let ignore_unreachable = audit_val
.get("ignore-unreachable")
.and_then(|v| v.as_bool())
.unwrap_or(false);
Self {
audit,
audit_format,
audit_abandoned,
block_insecure,
block_abandoned,
ignore_unreachable,
ignore_list_for_audit: ignore_list_parsed.audit,
ignore_list_for_blocking: ignore_list_parsed.block,
ignore_severity_for_audit: ignore_severity_parsed.audit,
ignore_severity_for_blocking: ignore_severity_parsed.block,
ignore_abandoned_for_audit: ignore_abandoned_parsed.audit,
ignore_abandoned_for_blocking: ignore_abandoned_parsed.block,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_ignore_simple_array() {
let val = serde_json::json!(["CVE-2024-1234", "PKSA-0001"]);
let parsed = parse_ignore_with_apply(&val);
assert_eq!(parsed.audit.len(), 2);
assert_eq!(parsed.block.len(), 2);
assert_eq!(parsed.audit.get("CVE-2024-1234"), Some(&None));
}
#[test]
fn test_parse_ignore_object_with_reasons() {
let val = serde_json::json!({
"CVE-2024-1234": "manually patched",
"PKSA-0001": null
});
let parsed = parse_ignore_with_apply(&val);
assert_eq!(
parsed.audit.get("CVE-2024-1234"),
Some(&Some("manually patched".to_string()))
);
assert_eq!(parsed.audit.get("PKSA-0001"), Some(&None));
}
#[test]
fn test_parse_ignore_detailed_apply_audit_only() {
let val = serde_json::json!({
"CVE-2024-1234": { "apply": "audit", "reason": "test" }
});
let parsed = parse_ignore_with_apply(&val);
assert_eq!(parsed.audit.len(), 1);
assert_eq!(parsed.block.len(), 0);
}
#[test]
fn test_parse_ignore_detailed_apply_block_only() {
let val = serde_json::json!({
"CVE-2024-1234": { "apply": "block", "reason": "test" }
});
let parsed = parse_ignore_with_apply(&val);
assert_eq!(parsed.audit.len(), 0);
assert_eq!(parsed.block.len(), 1);
}
#[test]
fn test_parse_ignore_detailed_apply_all() {
let val = serde_json::json!({
"CVE-2024-1234": { "apply": "all", "reason": "test" }
});
let parsed = parse_ignore_with_apply(&val);
assert_eq!(parsed.audit.len(), 1);
assert_eq!(parsed.block.len(), 1);
}
#[test]
fn test_audit_config_defaults() {
let config = Config::default();
let audit_config = AuditConfig::from_config(&config, true, AuditFormat::Table);
assert!(audit_config.audit);
assert_eq!(audit_config.audit_format, AuditFormat::Table);
assert_eq!(audit_config.audit_abandoned, AbandonedHandling::Fail);
assert!(audit_config.block_insecure);
assert!(!audit_config.block_abandoned);
assert!(!audit_config.ignore_unreachable);
assert!(audit_config.ignore_list_for_audit.is_empty());
assert!(audit_config.ignore_severity_for_audit.is_empty());
}
#[test]
fn test_audit_config_from_config_with_audit_section() {
use std::collections::BTreeMap;
let mut config = Config::default();
config
.merge(&BTreeMap::from([(
"audit".to_string(),
serde_json::json!({
"ignore": ["CVE-2024-1234"],
"ignore-severity": {"low": "low severity not critical"},
"abandoned": "report",
"block-insecure": false,
"ignore-unreachable": true
}),
)]))
.unwrap();
let audit_config = AuditConfig::from_config(&config, true, AuditFormat::Summary);
assert_eq!(audit_config.audit_abandoned, AbandonedHandling::Report);
assert!(!audit_config.block_insecure);
assert!(audit_config.ignore_unreachable);
assert_eq!(audit_config.ignore_list_for_audit.len(), 1);
assert_eq!(audit_config.ignore_severity_for_audit.len(), 1);
}
}
|