-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck-release-vulnerabilities.test.ts
More file actions
243 lines (234 loc) · 6.99 KB
/
Copy pathcheck-release-vulnerabilities.test.ts
File metadata and controls
243 lines (234 loc) · 6.99 KB
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
import assert from "node:assert/strict";
import { test } from "node:test";
import { evaluate, renderSummary } from "./check-release-vulnerabilities.ts";
const finding = {
FixedVersion: "2",
InstalledVersion: "1",
PkgName: "lib",
Severity: "HIGH",
VulnerabilityID: "CVE-1",
};
const report = { Results: [{ Vulnerabilities: [finding] }] };
const policy = { exceptions: [], schemaVersion: 2 };
const digest = `sha256:${"a".repeat(64)}`;
const subject = { digest, platform: "linux/amd64", reference: `registry.example/server@${digest}` };
const now = new Date("2026-09-01T00:00:00Z");
await test("rejects a high vulnerability without a disposition", () => {
assert.deepEqual(evaluate("server", report, policy).rejected, ["server|CVE-1|lib|1"]);
});
await test("reports but does not reject a high vulnerability with no upstream fix", () => {
// The four upstream images in security/release-images.json cannot be patched at all, so
// an unfixable finding blocks a release without offering anyone an action. It still has
// to be counted, because the evidence bundle is signed and must stay complete.
for (const unfixable of [
{ ...finding, FixedVersion: "" },
{ ...finding, FixedVersion: " " },
Object.fromEntries(Object.entries(finding).filter(([field]) => field !== "FixedVersion")),
]) {
const result = evaluate("server", { Results: [{ Vulnerabilities: [unfixable] }] }, policy);
assert.deepEqual(result.rejected, []);
assert.deepEqual(result.highCritical, ["server|CVE-1|lib|1"]);
assert.deepEqual(result.errors, []);
}
// A mixed report rejects only the half upstream has published a fix for.
const mixed = evaluate(
"server",
{
Results: [
{
Vulnerabilities: [
finding,
{ ...finding, PkgName: "other", VulnerabilityID: "CVE-2", FixedVersion: "" },
{ ...finding, PkgName: "third", VulnerabilityID: "CVE-3", Severity: "CRITICAL" },
],
},
],
},
policy,
);
assert.deepEqual(mixed.rejected, ["server|CVE-1|lib|1", "server|CVE-3|third|1"]);
assert.deepEqual(mixed.highCritical, [
"server|CVE-1|lib|1",
"server|CVE-2|other|1",
"server|CVE-3|third|1",
]);
});
await test("rejects a report whose fixed version is not a string", () => {
assert.throws(
() =>
evaluate(
"server",
{ Results: [{ Vulnerabilities: [{ ...finding, FixedVersion: 2 }] }] },
policy,
),
/FixedVersion must be a string/,
);
});
await test("accepts an owned, justified, unexpired exception", () => {
const exceptions = [
{
digest,
evidence: "https://github.com/example/project/issues/1",
expires: "2026-10-01T00:00:00Z",
image: "server",
installedVersion: "1",
justification: "not reachable in the deployed configuration",
owner: "@security",
package: "lib",
platform: "linux/amd64",
status: "not_affected",
vulnerability: "CVE-1",
},
];
assert.deepEqual(
evaluate(
"server",
{ ...report, ArtifactName: subject.reference },
{ ...policy, exceptions },
now,
subject,
).rejected,
[],
);
assert.deepEqual(
evaluate(
"server",
{ ...report, ArtifactName: subject.reference },
{ ...policy, exceptions: [{ ...exceptions[0], installedVersion: "0" }] },
now,
subject,
).rejected,
["server|CVE-1|lib|1"],
);
});
await test("matches an exception without its digest, but still on every other field", () => {
const exception = {
digest,
evidence: "https://github.com/example/project/issues/1",
expires: "2026-10-01T00:00:00Z",
image: "server",
installedVersion: "1",
justification: "not reachable in the deployed configuration",
owner: "@security",
package: "lib",
platform: "linux/amd64",
status: "not_affected",
vulnerability: "CVE-1",
};
const rejectedWith = (override: Partial<typeof exception>): string[] =>
evaluate(
"server",
{ ...report, ArtifactName: subject.reference },
{ ...policy, exceptions: [{ ...exception, ...override }] },
now,
subject,
).rejected;
// The release digest does not exist until the images are tagged, so matching on it made a
// pre-release exception impossible to author. An exception reviewed against one digest now
// covers the same package at the same version wherever it is scanned.
assert.deepEqual(rejectedWith({ digest: `sha256:${"b".repeat(64)}` }), []);
// Every other field still binds.
for (const override of [
{ image: "other" },
{ platform: "linux/arm64" },
{ package: "other" },
{ installedVersion: "0" },
{ vulnerability: "CVE-2" },
] satisfies Partial<typeof exception>[])
assert.deepEqual(rejectedWith(override), ["server|CVE-1|lib|1"], JSON.stringify(override));
// Two exceptions that differ only by digest are now one exception, twice.
assert.match(
evaluate(
"server",
{ ...report, ArtifactName: subject.reference },
{
...policy,
exceptions: [exception, { ...exception, digest: `sha256:${"b".repeat(64)}` }],
},
now,
subject,
).errors.join("\n"),
/duplicate exception: server\|linux\/amd64\|CVE-1\|lib\|1/,
);
});
await test("rejects expired and malformed exceptions", () => {
const exceptions = [
{
digest,
evidence: "https://github.com/example/project/issues/1",
expires: "2020-01-01T00:00:00Z",
image: "server",
installedVersion: "1",
justification: "reviewed",
owner: "@security",
package: "lib",
platform: "linux/amd64",
status: "affected",
vulnerability: "CVE-1",
},
];
assert.match(
evaluate("server", report, { ...policy, exceptions }, new Date("2021-01-01")).errors[0] ?? "",
/expired/,
);
assert.throws(() => evaluate("server", {}, policy), /malformed Trivy report/);
assert.throws(
() => evaluate("server", { Results: [{ Vulnerabilities: [{ Severity: "HIGH" }] }] }, policy),
/missing InstalledVersion/,
);
assert.match(
evaluate("server", report, {
...policy,
exceptions: [exceptions[0], exceptions[0]],
}).errors.join("\n"),
/duplicate exception/,
);
assert.match(
evaluate(
"server",
report,
{
...policy,
exceptions: [{ ...exceptions[0], expires: "2027-01-01T00:00:00Z" }],
},
now,
).errors.join("\n"),
/90-day limit/,
);
assert.match(
evaluate(
"server",
{ ...report, ArtifactName: "registry.example/server@sha256:wrong" },
policy,
new Date(),
subject,
).errors.join("\n"),
/Trivy report is for/,
);
});
await test("names the rejected findings in the run summary", () => {
const rendered = renderSummary(
{ digest, image: "server", platform: "linux/amd64" },
evaluate(
"server",
{
Results: [
{
Vulnerabilities: [finding, { ...finding, VulnerabilityID: "CVE-2", FixedVersion: "" }],
},
],
},
policy,
),
);
assert.match(rendered, /server \(linux\/amd64\): fail/);
assert.match(rendered, /2 HIGH\/CRITICAL, 1 rejected/);
assert.match(rendered, /\| CVE-1 \| lib \| 1 \|/);
assert.doesNotMatch(rendered, /\| CVE-2 \|/);
const clean = renderSummary(
{ digest, image: "server", platform: "linux/amd64" },
evaluate("server", { Results: [] }, policy),
);
assert.match(clean, /server \(linux\/amd64\): pass/);
assert.doesNotMatch(clean, /\| Vulnerability \|/);
});