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
|
//! ref: composer/tests/Composer/Test/Advisory/AuditorTest.php
use chrono::Utc;
use indexmap::IndexMap;
use shirabe::advisory::AnySecurityAdvisory;
use shirabe::advisory::Auditor;
use shirabe::advisory::PartialSecurityAdvisory;
use shirabe::advisory::SecurityAdvisory;
use shirabe_semver::constraint::SimpleConstraint;
fn constraint(operator: &str, version: &str) -> shirabe_semver::constraint::AnyConstraint {
SimpleConstraint::new(operator.to_string(), version.to_string(), None).into()
}
fn full_advisory() -> AnySecurityAdvisory {
let mut source: IndexMap<String, String> = IndexMap::new();
source.insert("name".to_string(), "foo".to_string());
source.insert("remoteId".to_string(), "remoteID".to_string());
AnySecurityAdvisory::Full(SecurityAdvisory::new(
"foo/bar".to_string(),
"123".to_string(),
constraint("=", "1.0.0.0"),
"test".to_string(),
vec![source],
Utc::now(),
None,
None,
None,
))
}
fn full_advisory_with_id(advisory_id: &str) -> AnySecurityAdvisory {
let mut source: IndexMap<String, String> = IndexMap::new();
source.insert("name".to_string(), "foo".to_string());
source.insert("remoteId".to_string(), "remoteID".to_string());
AnySecurityAdvisory::Full(SecurityAdvisory::new(
"foo/bar".to_string(),
advisory_id.to_string(),
constraint("=", "1.0.0.0"),
"test".to_string(),
vec![source],
Utc::now(),
None,
None,
None,
))
}
fn partial_advisory(advisory_id: &str) -> AnySecurityAdvisory {
AnySecurityAdvisory::Partial(PartialSecurityAdvisory::new(
"foo/bar".to_string(),
advisory_id.to_string(),
constraint("=", "1.0.0.0"),
))
}
fn ignore_list(pairs: Vec<(&str, Option<&str>)>) -> IndexMap<String, Option<String>> {
pairs
.into_iter()
.map(|(k, v)| (k.to_string(), v.map(String::from)))
.collect()
}
// These run the Auditor against a mocked HttpDownloader/IO and packages built from version
// constraints (parsed via a look-around regex the regex crate cannot compile).
#[test]
#[ignore = "requires PHPUnit getMockBuilder partial mock of ComposerRepository (hasSecurityAdvisories/getSecurityAdvisories) and BufferIO; no mocking infrastructure exists"]
fn test_audit() {
todo!()
}
#[test]
#[ignore = "requires getIOMock with expects() output expectations and getMockBuilder mock of ComposerRepository; no mocking infrastructure exists"]
fn test_audit_with_ignore() {
todo!()
}
#[test]
#[ignore = "requires getMockBuilder partial mock of RepositorySet (getMatchingSecurityAdvisories willReturnCallback); no mocking infrastructure exists"]
fn test_audit_with_ignore_unreachable() {
todo!()
}
#[test]
#[ignore = "requires getIOMock with expects() output expectations and getMockBuilder mock of ComposerRepository; no mocking infrastructure exists"]
fn test_audit_with_ignore_severity() {
todo!()
}
#[test]
fn test_needs_complete_advisory_load() {
let cases: Vec<(
IndexMap<String, Vec<AnySecurityAdvisory>>,
IndexMap<String, Option<String>>,
bool,
)> = vec![
// no filter or advisories
(IndexMap::new(), ignore_list(vec![]), false),
// packagist filters are IDs so work fine with partial advisories
(
IndexMap::new(),
ignore_list(vec![("PKSA-foo-bar", None)]),
false,
),
// packagist filters are IDs so work fine with partial advisories/2
(
{
let mut m: IndexMap<String, Vec<AnySecurityAdvisory>> = IndexMap::new();
m.insert(
"vendor1/package1".to_string(),
vec![full_advisory(), partial_advisory("1234")],
);
m
},
ignore_list(vec![("PKSA-foo-bar", Some("this is fine 🔥"))]),
false,
),
// no advisories no need to load any further
(
IndexMap::new(),
ignore_list(vec![("CVE-2025-1234", None)]),
false,
),
// no advisories no need to load any further/2
(
{
let mut m: IndexMap<String, Vec<AnySecurityAdvisory>> = IndexMap::new();
m.insert("vendor1/package1".to_string(), vec![]);
m
},
ignore_list(vec![("CVE-2025-1234", None)]),
false,
),
// CVE filter or other non-packagist ones might need to fully load for safety if partial advisories are present
(
{
let mut m: IndexMap<String, Vec<AnySecurityAdvisory>> = IndexMap::new();
m.insert(
"vendor1/package1".to_string(),
vec![full_advisory(), partial_advisory("1234")],
);
m
},
ignore_list(vec![("CVE-2025-1234", None)]),
true,
),
// filter does not trigger load if all advisories are fully loaded
(
{
let mut m: IndexMap<String, Vec<AnySecurityAdvisory>> = IndexMap::new();
m.insert("vendor1/package1".to_string(), vec![full_advisory()]);
m.insert(
"vendor1/package2".to_string(),
vec![full_advisory_with_id("1234")],
);
m
},
ignore_list(vec![("CVE-2025-1234", None)]),
false,
),
];
let auditor = Auditor;
for (advisories, ignore_list, expected) in cases {
assert_eq!(
expected,
auditor.needs_complete_advisory_load(&advisories, &ignore_list)
);
}
}
|