@craswitch/mcp-server 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +99 -2
- package/dist/cli.js +8 -0
- package/dist/core/badge.js +156 -0
- package/dist/core/crypto-utils.js +199 -0
- package/dist/core/enisa.js +909 -0
- package/dist/core/evidence.js +350 -0
- package/dist/core/feeds.js +1175 -0
- package/dist/core/guardrails.js +537 -0
- package/dist/core/index.js +16 -0
- package/dist/core/lockfiles.js +1107 -0
- package/dist/core/sbom.js +191 -0
- package/dist/core/types.js +918 -0
- package/dist/core/vex.js +722 -0
- package/dist/core/x402.js +467 -0
- package/dist/crypto.js +214 -0
- package/dist/http-client.js +336 -0
- package/dist/index.js +17 -0
- package/dist/kev-baseline.js +186 -0
- package/dist/server.js +1075 -0
- package/package.json +39 -4
- package/server.json +63 -0
|
@@ -0,0 +1,1175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exploit feed normalizers (CISA KEV, ENISA EUVD, OSV/GHSA) and deterministic
|
|
3
|
+
* production-PURL + semver range matcher with `.cra-vex.json` suppression support.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
import { canonicalJson, sha256Hex } from './crypto-utils.js';
|
|
19
|
+
import { buildPurl, normalizeEcosystemName, normalizePypiName, parsePurl } from './lockfiles.js';
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Semver / Ecosystem Version Comparison
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
function parseVersion(raw ) {
|
|
45
|
+
let cleaned = raw.trim();
|
|
46
|
+
if (/^[vV]\d/.test(cleaned)) {
|
|
47
|
+
cleaned = cleaned.slice(1);
|
|
48
|
+
}
|
|
49
|
+
// Strip build metadata (+...)
|
|
50
|
+
const plusIdx = cleaned.indexOf('+');
|
|
51
|
+
if (plusIdx !== -1) {
|
|
52
|
+
cleaned = cleaned.slice(0, plusIdx);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let corePart = cleaned;
|
|
56
|
+
let prePart = '';
|
|
57
|
+
const dashIdx = cleaned.indexOf('-');
|
|
58
|
+
if (dashIdx !== -1) {
|
|
59
|
+
corePart = cleaned.slice(0, dashIdx);
|
|
60
|
+
prePart = cleaned.slice(dashIdx + 1);
|
|
61
|
+
} else {
|
|
62
|
+
// Handle Python-style prereleases like 1.2.3rc1, 1.2.3a1, 1.2.3b2
|
|
63
|
+
const pyPre = /^(\d+(?:\.\d+)*)(a|b|rc|alpha|beta|dev)(\d*)$/i.exec(cleaned);
|
|
64
|
+
if (pyPre && pyPre[1] && pyPre[2]) {
|
|
65
|
+
corePart = pyPre[1];
|
|
66
|
+
prePart = pyPre[3] ? `${pyPre[2].toLowerCase()}.${pyPre[3]}` : pyPre[2].toLowerCase();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const core = corePart
|
|
71
|
+
.split('.')
|
|
72
|
+
.map((seg) => {
|
|
73
|
+
const m = /^\d+/.exec(seg);
|
|
74
|
+
return m ? Number.parseInt(m[0], 10) : 0;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const prerelease = prePart
|
|
78
|
+
? prePart.split('.').map((seg) => (/^\d+$/.test(seg) ? Number.parseInt(seg, 10) : seg.toLowerCase()))
|
|
79
|
+
: [];
|
|
80
|
+
|
|
81
|
+
return { core, prerelease };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Deterministic version comparator returning -1, 0, or 1.
|
|
86
|
+
*/
|
|
87
|
+
export function compareVersions(a , b ) {
|
|
88
|
+
const va = parseVersion(a);
|
|
89
|
+
const vb = parseVersion(b);
|
|
90
|
+
const maxCore = Math.max(va.core.length, vb.core.length);
|
|
91
|
+
for (let i = 0; i < maxCore; i++) {
|
|
92
|
+
const na = va.core[i] ?? 0;
|
|
93
|
+
const nb = vb.core[i] ?? 0;
|
|
94
|
+
if (na !== nb) return na < nb ? -1 : 1;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const aHasPre = va.prerelease.length > 0;
|
|
98
|
+
const bHasPre = vb.prerelease.length > 0;
|
|
99
|
+
if (!aHasPre && !bHasPre) return 0;
|
|
100
|
+
if (aHasPre && !bHasPre) return -1;
|
|
101
|
+
if (!aHasPre && bHasPre) return 1;
|
|
102
|
+
|
|
103
|
+
const maxPre = Math.max(va.prerelease.length, vb.prerelease.length);
|
|
104
|
+
for (let i = 0; i < maxPre; i++) {
|
|
105
|
+
const pa = va.prerelease[i];
|
|
106
|
+
const pb = vb.prerelease[i];
|
|
107
|
+
if (pa === undefined) return -1;
|
|
108
|
+
if (pb === undefined) return 1;
|
|
109
|
+
if (pa === pb) continue;
|
|
110
|
+
if (typeof pa === 'number' && typeof pb === 'number') {
|
|
111
|
+
return pa < pb ? -1 : 1;
|
|
112
|
+
}
|
|
113
|
+
if (typeof pa === 'number') return -1;
|
|
114
|
+
if (typeof pb === 'number') return 1;
|
|
115
|
+
return pa < pb ? -1 : 1;
|
|
116
|
+
}
|
|
117
|
+
return 0;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function normalizeVersionToken(v ) {
|
|
121
|
+
const trimmed = v.trim();
|
|
122
|
+
return /^[vV]\d/.test(trimmed) ? trimmed.slice(1) : trimmed;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Checks whether `version` falls within an AffectedPackageRange (`introduced` inclusive,
|
|
127
|
+
* `fixed` exclusive, `lastAffected` inclusive, or listed in `exactVersions`).
|
|
128
|
+
*/
|
|
129
|
+
export function isVersionInRange(
|
|
130
|
+
version ,
|
|
131
|
+
range ,
|
|
132
|
+
) {
|
|
133
|
+
const normVer = normalizeVersionToken(version);
|
|
134
|
+
|
|
135
|
+
if (range.exactVersions && range.exactVersions.length > 0) {
|
|
136
|
+
for (const exact of range.exactVersions) {
|
|
137
|
+
if (normalizeVersionToken(exact) === normVer || compareVersions(version, exact) === 0) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (
|
|
144
|
+
range.introduced === undefined &&
|
|
145
|
+
range.fixed === undefined &&
|
|
146
|
+
range.lastAffected === undefined
|
|
147
|
+
) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (range.introduced !== undefined && range.introduced !== '0' && range.introduced !== '*') {
|
|
152
|
+
if (compareVersions(version, range.introduced) < 0) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (range.fixed !== undefined && range.fixed.trim().length > 0) {
|
|
158
|
+
if (compareVersions(version, range.fixed) >= 0) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (range.lastAffected !== undefined && range.lastAffected.trim().length > 0) {
|
|
164
|
+
if (compareVersions(version, range.lastAffected) > 0) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ---------------------------------------------------------------------------
|
|
173
|
+
// Helpers for Feed Normalization
|
|
174
|
+
// ---------------------------------------------------------------------------
|
|
175
|
+
|
|
176
|
+
function extractUrlsFromText(text ) {
|
|
177
|
+
const matches = text.match(/https?:\/\/[^\s;,)"'>]+/g);
|
|
178
|
+
return matches ? Array.from(new Set(matches)) : [];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function extractCveId(candidates ) {
|
|
182
|
+
for (const cand of candidates) {
|
|
183
|
+
if (!cand) continue;
|
|
184
|
+
const m = /\bCVE-\d{4}-\d{4,}\b/i.exec(cand);
|
|
185
|
+
if (m) return m[0].toUpperCase();
|
|
186
|
+
}
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function extractEuvdId(candidates ) {
|
|
191
|
+
for (const cand of candidates) {
|
|
192
|
+
if (!cand) continue;
|
|
193
|
+
const m = /\bEUVD-\d{4}-\d+\b/i.exec(cand);
|
|
194
|
+
if (m) return m[0].toUpperCase();
|
|
195
|
+
}
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function splitAliases(raw ) {
|
|
200
|
+
if (Array.isArray(raw)) {
|
|
201
|
+
return raw.filter((x) => typeof x === 'string' && x.trim().length > 0);
|
|
202
|
+
}
|
|
203
|
+
if (typeof raw === 'string') {
|
|
204
|
+
return raw
|
|
205
|
+
.split(/[\r\n,;\s]+/)
|
|
206
|
+
.map((s) => s.trim())
|
|
207
|
+
.filter((s) => s.length > 0);
|
|
208
|
+
}
|
|
209
|
+
return [];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function normalizeStringArray(raw ) {
|
|
213
|
+
if (!Array.isArray(raw)) return [];
|
|
214
|
+
return Array.from(
|
|
215
|
+
new Set(
|
|
216
|
+
raw
|
|
217
|
+
.filter((x) => typeof x === 'string' && x.trim().length > 0)
|
|
218
|
+
.map((x) => x.trim()),
|
|
219
|
+
),
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function normalizeAffectedRanges(raw ) {
|
|
224
|
+
if (!Array.isArray(raw)) return [];
|
|
225
|
+
const out = [];
|
|
226
|
+
for (const entry of raw) {
|
|
227
|
+
if (!entry || typeof entry !== 'object') continue;
|
|
228
|
+
const obj = entry ;
|
|
229
|
+
const eco =
|
|
230
|
+
typeof obj.ecosystem === 'string' ? normalizeEcosystemName(obj.ecosystem) : null;
|
|
231
|
+
const name = typeof obj.name === 'string' ? obj.name.trim() : '';
|
|
232
|
+
if (!eco || !name) continue;
|
|
233
|
+
const range = {
|
|
234
|
+
ecosystem: eco,
|
|
235
|
+
name,
|
|
236
|
+
};
|
|
237
|
+
if (typeof obj.namespace === 'string' && obj.namespace.trim().length > 0) {
|
|
238
|
+
range.namespace = obj.namespace.trim();
|
|
239
|
+
}
|
|
240
|
+
if (typeof obj.introduced === 'string' && obj.introduced.trim().length > 0) {
|
|
241
|
+
range.introduced = obj.introduced.trim();
|
|
242
|
+
}
|
|
243
|
+
if (typeof obj.fixed === 'string' && obj.fixed.trim().length > 0) {
|
|
244
|
+
range.fixed = obj.fixed.trim();
|
|
245
|
+
}
|
|
246
|
+
const rawLastAffected =
|
|
247
|
+
typeof obj.lastAffected === 'string'
|
|
248
|
+
? obj.lastAffected
|
|
249
|
+
: typeof obj.last_affected === 'string'
|
|
250
|
+
? obj.last_affected
|
|
251
|
+
: '';
|
|
252
|
+
if (rawLastAffected.trim().length > 0) {
|
|
253
|
+
range.lastAffected = rawLastAffected.trim();
|
|
254
|
+
}
|
|
255
|
+
if (Array.isArray(obj.exactVersions)) {
|
|
256
|
+
const exact = normalizeStringArray(obj.exactVersions);
|
|
257
|
+
if (exact.length > 0) range.exactVersions = exact;
|
|
258
|
+
}
|
|
259
|
+
out.push(range);
|
|
260
|
+
}
|
|
261
|
+
return out;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function splitPackageName(
|
|
265
|
+
ecosystem ,
|
|
266
|
+
rawName ,
|
|
267
|
+
) {
|
|
268
|
+
const trimmed = rawName.trim();
|
|
269
|
+
if (ecosystem === 'npm' && trimmed.startsWith('@') && trimmed.includes('/')) {
|
|
270
|
+
const idx = trimmed.indexOf('/');
|
|
271
|
+
return {
|
|
272
|
+
namespace: trimmed.slice(0, idx).toLowerCase(),
|
|
273
|
+
name: trimmed.slice(idx + 1).toLowerCase(),
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
if (ecosystem === 'npm') {
|
|
277
|
+
return { name: trimmed.toLowerCase() };
|
|
278
|
+
}
|
|
279
|
+
if (ecosystem === 'pypi') {
|
|
280
|
+
return { name: normalizePypiName(trimmed) };
|
|
281
|
+
}
|
|
282
|
+
if (ecosystem === 'maven' && trimmed.includes(':')) {
|
|
283
|
+
const idx = trimmed.indexOf(':');
|
|
284
|
+
return {
|
|
285
|
+
namespace: trimmed.slice(0, idx),
|
|
286
|
+
name: trimmed.slice(idx + 1),
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
if (ecosystem === 'golang' && trimmed.includes('/')) {
|
|
290
|
+
const idx = trimmed.lastIndexOf('/');
|
|
291
|
+
return {
|
|
292
|
+
namespace: trimmed.slice(0, idx),
|
|
293
|
+
name: trimmed.slice(idx + 1),
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
return { name: trimmed };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// ---------------------------------------------------------------------------
|
|
300
|
+
// Public Feed Normalizers
|
|
301
|
+
// ---------------------------------------------------------------------------
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Normalizes a CISA Known Exploited Vulnerabilities (KEV) JSON catalog payload.
|
|
305
|
+
*/
|
|
306
|
+
export function normalizeCisaKev(
|
|
307
|
+
payload ,
|
|
308
|
+
purlEnrichment ,
|
|
309
|
+
) {
|
|
310
|
+
let list = [];
|
|
311
|
+
if (Array.isArray(payload)) {
|
|
312
|
+
list = payload;
|
|
313
|
+
} else if (payload && typeof payload === 'object') {
|
|
314
|
+
const obj = payload ;
|
|
315
|
+
if (Array.isArray(obj.vulnerabilities)) {
|
|
316
|
+
list = obj.vulnerabilities;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const items = [];
|
|
321
|
+
for (const raw of list) {
|
|
322
|
+
if (!raw || typeof raw !== 'object') continue;
|
|
323
|
+
const entry = raw ;
|
|
324
|
+
const rawCve =
|
|
325
|
+
typeof entry.cveID === 'string'
|
|
326
|
+
? entry.cveID
|
|
327
|
+
: typeof entry.cveId === 'string'
|
|
328
|
+
? entry.cveId
|
|
329
|
+
: typeof entry.id === 'string'
|
|
330
|
+
? entry.id
|
|
331
|
+
: '';
|
|
332
|
+
const cveId = extractCveId([rawCve]) ?? rawCve.trim().toUpperCase();
|
|
333
|
+
if (!cveId) continue;
|
|
334
|
+
|
|
335
|
+
const enrichment = purlEnrichment?.[cveId];
|
|
336
|
+
const purls = Array.from(
|
|
337
|
+
new Set([...normalizeStringArray(entry.purls), ...(enrichment?.purls ?? [])]),
|
|
338
|
+
);
|
|
339
|
+
const affectedRanges = [
|
|
340
|
+
...normalizeAffectedRanges(entry.affectedRanges),
|
|
341
|
+
...(enrichment?.affectedRanges ?? []),
|
|
342
|
+
];
|
|
343
|
+
|
|
344
|
+
const notesText = typeof entry.notes === 'string' ? entry.notes : '';
|
|
345
|
+
const refUrls = Array.from(
|
|
346
|
+
new Set([
|
|
347
|
+
`https://nvd.nist.gov/vuln/detail/${cveId}`,
|
|
348
|
+
...extractUrlsFromText(notesText),
|
|
349
|
+
...normalizeStringArray(entry.references),
|
|
350
|
+
]),
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
const vulnName = typeof entry.vulnerabilityName === 'string' ? entry.vulnerabilityName.trim() : '';
|
|
354
|
+
const shortDesc = typeof entry.shortDescription === 'string' ? entry.shortDescription.trim() : '';
|
|
355
|
+
const summary =
|
|
356
|
+
shortDesc || vulnName || `${String(entry.vendorProject ?? '')} ${String(entry.product ?? '')}`.trim() || cveId;
|
|
357
|
+
|
|
358
|
+
const dateAdded =
|
|
359
|
+
typeof entry.dateAdded === 'string' && entry.dateAdded.trim().length > 0
|
|
360
|
+
? entry.dateAdded.trim()
|
|
361
|
+
: new Date().toISOString().slice(0, 10);
|
|
362
|
+
|
|
363
|
+
const item = {
|
|
364
|
+
id: cveId,
|
|
365
|
+
cveId,
|
|
366
|
+
source: 'cisa_kev',
|
|
367
|
+
purls,
|
|
368
|
+
activelyExploited: true,
|
|
369
|
+
dateAdded,
|
|
370
|
+
summary,
|
|
371
|
+
references: refUrls,
|
|
372
|
+
rawHash: sha256Hex(canonicalJson(entry)),
|
|
373
|
+
};
|
|
374
|
+
if (affectedRanges.length > 0) {
|
|
375
|
+
item.affectedRanges = affectedRanges;
|
|
376
|
+
}
|
|
377
|
+
if (typeof entry.dueDate === 'string' && entry.dueDate.trim().length > 0) {
|
|
378
|
+
item.dueDate = entry.dueDate.trim();
|
|
379
|
+
}
|
|
380
|
+
items.push(item);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return items;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Normalizes an ENISA European Vulnerability Database (EUVD) JSON payload.
|
|
388
|
+
*/
|
|
389
|
+
export function normalizeEnisaEuvd(payload ) {
|
|
390
|
+
let list = [];
|
|
391
|
+
if (Array.isArray(payload)) {
|
|
392
|
+
list = payload;
|
|
393
|
+
} else if (payload && typeof payload === 'object') {
|
|
394
|
+
const obj = payload ;
|
|
395
|
+
if (Array.isArray(obj.items)) list = obj.items;
|
|
396
|
+
else if (Array.isArray(obj.vulnerabilities)) list = obj.vulnerabilities;
|
|
397
|
+
else if (Array.isArray(obj.data)) list = obj.data;
|
|
398
|
+
else if (typeof obj.id === 'string') list = [obj];
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const items = [];
|
|
402
|
+
for (const raw of list) {
|
|
403
|
+
if (!raw || typeof raw !== 'object') continue;
|
|
404
|
+
const entry = raw ;
|
|
405
|
+
const rawId = typeof entry.id === 'string' ? entry.id.trim() : '';
|
|
406
|
+
const aliases = splitAliases(entry.aliases);
|
|
407
|
+
const euvdId =
|
|
408
|
+
extractEuvdId([
|
|
409
|
+
typeof entry.euvdId === 'string' ? entry.euvdId : undefined,
|
|
410
|
+
rawId,
|
|
411
|
+
...aliases,
|
|
412
|
+
]) ?? (rawId.toUpperCase().startsWith('EUVD-') ? rawId.toUpperCase() : undefined);
|
|
413
|
+
const cveId = extractCveId([
|
|
414
|
+
typeof entry.cveId === 'string' ? entry.cveId : undefined,
|
|
415
|
+
rawId,
|
|
416
|
+
...aliases,
|
|
417
|
+
]);
|
|
418
|
+
|
|
419
|
+
const id = euvdId ?? cveId ?? rawId;
|
|
420
|
+
if (!id) continue;
|
|
421
|
+
|
|
422
|
+
const purls = normalizeStringArray(entry.purls);
|
|
423
|
+
const affectedRanges = normalizeAffectedRanges(entry.affectedRanges);
|
|
424
|
+
|
|
425
|
+
// Also parse optional enisaIdProduct array if it carries purls
|
|
426
|
+
if (Array.isArray(entry.enisaIdProduct)) {
|
|
427
|
+
for (const prod of entry.enisaIdProduct) {
|
|
428
|
+
if (!prod || typeof prod !== 'object') continue;
|
|
429
|
+
const pObj = prod ;
|
|
430
|
+
if (typeof pObj.purl === 'string' && pObj.purl.startsWith('pkg:')) {
|
|
431
|
+
purls.push(pObj.purl.trim());
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const rawRefs = entry.references;
|
|
437
|
+
const references = Array.from(
|
|
438
|
+
new Set([
|
|
439
|
+
...(typeof rawRefs === 'string' ? extractUrlsFromText(rawRefs) : normalizeStringArray(rawRefs)),
|
|
440
|
+
...(euvdId ? [`https://euvd.enisa.europa.eu/vulnerability/${euvdId}`] : []),
|
|
441
|
+
]),
|
|
442
|
+
);
|
|
443
|
+
|
|
444
|
+
const summary =
|
|
445
|
+
(typeof entry.description === 'string' && entry.description.trim()) ||
|
|
446
|
+
(typeof entry.summary === 'string' && entry.summary.trim()) ||
|
|
447
|
+
id;
|
|
448
|
+
|
|
449
|
+
const dateAdded =
|
|
450
|
+
(typeof entry.datePublished === 'string' && entry.datePublished.trim()) ||
|
|
451
|
+
(typeof entry.dateUpdated === 'string' && entry.dateUpdated.trim()) ||
|
|
452
|
+
(typeof entry.dateAdded === 'string' && entry.dateAdded.trim()) ||
|
|
453
|
+
new Date().toISOString().slice(0, 10);
|
|
454
|
+
|
|
455
|
+
const activelyExploited =
|
|
456
|
+
typeof entry.activelyExploited === 'boolean'
|
|
457
|
+
? entry.activelyExploited
|
|
458
|
+
: typeof entry.exploited === 'boolean'
|
|
459
|
+
? entry.exploited
|
|
460
|
+
: true;
|
|
461
|
+
|
|
462
|
+
const item = {
|
|
463
|
+
id,
|
|
464
|
+
source: 'enisa_euvd',
|
|
465
|
+
purls: Array.from(new Set(purls)),
|
|
466
|
+
activelyExploited,
|
|
467
|
+
dateAdded,
|
|
468
|
+
summary,
|
|
469
|
+
references,
|
|
470
|
+
rawHash: sha256Hex(canonicalJson(entry)),
|
|
471
|
+
};
|
|
472
|
+
if (cveId) item.cveId = cveId;
|
|
473
|
+
if (euvdId) item.euvdId = euvdId;
|
|
474
|
+
if (affectedRanges.length > 0) item.affectedRanges = affectedRanges;
|
|
475
|
+
items.push(item);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return items;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Normalizes OSV / GitHub Security Advisory (GHSA) records.
|
|
483
|
+
*/
|
|
484
|
+
export function normalizeOsvAdvisories(
|
|
485
|
+
payload ,
|
|
486
|
+
defaultSource = 'osv',
|
|
487
|
+
) {
|
|
488
|
+
let list = [];
|
|
489
|
+
if (Array.isArray(payload)) {
|
|
490
|
+
list = payload;
|
|
491
|
+
} else if (payload && typeof payload === 'object') {
|
|
492
|
+
const obj = payload ;
|
|
493
|
+
if (Array.isArray(obj.vulns)) list = obj.vulns;
|
|
494
|
+
else if (Array.isArray(obj.advisories)) list = obj.advisories;
|
|
495
|
+
else if (typeof obj.id === 'string') list = [obj];
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const items = [];
|
|
499
|
+
for (const raw of list) {
|
|
500
|
+
if (!raw || typeof raw !== 'object') continue;
|
|
501
|
+
const entry = raw ;
|
|
502
|
+
const id = typeof entry.id === 'string' ? entry.id.trim() : '';
|
|
503
|
+
if (!id) continue;
|
|
504
|
+
|
|
505
|
+
const aliases = splitAliases(entry.aliases);
|
|
506
|
+
const cveId = extractCveId([id, ...aliases]);
|
|
507
|
+
const euvdId = extractEuvdId([id, ...aliases]);
|
|
508
|
+
const source = id.toUpperCase().startsWith('GHSA-') ? 'ghsa' : defaultSource;
|
|
509
|
+
|
|
510
|
+
const purls = new Set ();
|
|
511
|
+
const affectedRanges = [];
|
|
512
|
+
|
|
513
|
+
if (Array.isArray(entry.affected)) {
|
|
514
|
+
for (const affRaw of entry.affected) {
|
|
515
|
+
if (!affRaw || typeof affRaw !== 'object') continue;
|
|
516
|
+
const aff = affRaw ;
|
|
517
|
+
const pkg =
|
|
518
|
+
aff.package && typeof aff.package === 'object'
|
|
519
|
+
? (aff.package )
|
|
520
|
+
: null;
|
|
521
|
+
if (!pkg) continue;
|
|
522
|
+
|
|
523
|
+
const rawEco = typeof pkg.ecosystem === 'string' ? pkg.ecosystem : '';
|
|
524
|
+
const rawName = typeof pkg.name === 'string' ? pkg.name : '';
|
|
525
|
+
if (!rawName) continue;
|
|
526
|
+
|
|
527
|
+
const eco = normalizeEcosystemName(rawEco);
|
|
528
|
+
if (!eco) {
|
|
529
|
+
const lowerEco = rawEco.trim().toLowerCase();
|
|
530
|
+
const extraSoftwareEco =
|
|
531
|
+
lowerEco === 'rubygems' || lowerEco === 'gem' || lowerEco === 'ruby'
|
|
532
|
+
? 'gem'
|
|
533
|
+
: lowerEco === 'nuget' || lowerEco === 'dotnet' || lowerEco === '.net'
|
|
534
|
+
? 'nuget'
|
|
535
|
+
: null;
|
|
536
|
+
if (!extraSoftwareEco) continue;
|
|
537
|
+
|
|
538
|
+
const cleanPkgName = rawName.trim();
|
|
539
|
+
const versions = normalizeStringArray(aff.versions);
|
|
540
|
+
if (versions.length > 0) {
|
|
541
|
+
for (const v of versions) {
|
|
542
|
+
purls.add(
|
|
543
|
+
`pkg:${extraSoftwareEco}/${encodeURIComponent(cleanPkgName)}@${encodeURIComponent(v.trim())}`,
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
} else {
|
|
547
|
+
purls.add(`pkg:${extraSoftwareEco}/${encodeURIComponent(cleanPkgName)}`);
|
|
548
|
+
}
|
|
549
|
+
continue;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
const { namespace, name } = splitPackageName(eco, rawName);
|
|
553
|
+
|
|
554
|
+
// Exact versions list -> canonical PURLs + exactVersions range
|
|
555
|
+
const versions = normalizeStringArray(aff.versions);
|
|
556
|
+
for (const v of versions) {
|
|
557
|
+
purls.add(buildPurl(eco, name, v, namespace));
|
|
558
|
+
}
|
|
559
|
+
if (versions.length > 0) {
|
|
560
|
+
affectedRanges.push({
|
|
561
|
+
ecosystem: eco,
|
|
562
|
+
name,
|
|
563
|
+
...(namespace ? { namespace } : {}),
|
|
564
|
+
exactVersions: versions,
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// Semver / ecosystem event ranges (skip GIT commit-SHA ranges)
|
|
569
|
+
if (Array.isArray(aff.ranges)) {
|
|
570
|
+
for (const rangeRaw of aff.ranges) {
|
|
571
|
+
if (!rangeRaw || typeof rangeRaw !== 'object') continue;
|
|
572
|
+
const rangeObj = rangeRaw ;
|
|
573
|
+
if (typeof rangeObj.type === 'string' && rangeObj.type.trim().toUpperCase() === 'GIT') {
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
if (!Array.isArray(rangeObj.events)) continue;
|
|
577
|
+
|
|
578
|
+
let currentIntroduced ;
|
|
579
|
+
for (const evRaw of rangeObj.events) {
|
|
580
|
+
if (!evRaw || typeof evRaw !== 'object') continue;
|
|
581
|
+
const ev = evRaw ;
|
|
582
|
+
if (typeof ev.introduced === 'string') {
|
|
583
|
+
if (currentIntroduced !== undefined) {
|
|
584
|
+
affectedRanges.push({
|
|
585
|
+
ecosystem: eco,
|
|
586
|
+
name,
|
|
587
|
+
...(namespace ? { namespace } : {}),
|
|
588
|
+
introduced: currentIntroduced,
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
currentIntroduced = ev.introduced;
|
|
592
|
+
} else if (typeof ev.fixed === 'string') {
|
|
593
|
+
affectedRanges.push({
|
|
594
|
+
ecosystem: eco,
|
|
595
|
+
name,
|
|
596
|
+
...(namespace ? { namespace } : {}),
|
|
597
|
+
introduced: currentIntroduced ?? '0',
|
|
598
|
+
fixed: ev.fixed,
|
|
599
|
+
});
|
|
600
|
+
currentIntroduced = undefined;
|
|
601
|
+
} else if (typeof ev.last_affected === 'string') {
|
|
602
|
+
affectedRanges.push({
|
|
603
|
+
ecosystem: eco,
|
|
604
|
+
name,
|
|
605
|
+
...(namespace ? { namespace } : {}),
|
|
606
|
+
introduced: currentIntroduced ?? '0',
|
|
607
|
+
lastAffected: ev.last_affected,
|
|
608
|
+
});
|
|
609
|
+
currentIntroduced = undefined;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
if (currentIntroduced !== undefined) {
|
|
613
|
+
affectedRanges.push({
|
|
614
|
+
ecosystem: eco,
|
|
615
|
+
name,
|
|
616
|
+
...(namespace ? { namespace } : {}),
|
|
617
|
+
introduced: currentIntroduced,
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
const references = [];
|
|
626
|
+
if (Array.isArray(entry.references)) {
|
|
627
|
+
for (const ref of entry.references) {
|
|
628
|
+
if (typeof ref === 'string') {
|
|
629
|
+
references.push(ref);
|
|
630
|
+
} else if (ref && typeof ref === 'object' && typeof (ref ).url === 'string') {
|
|
631
|
+
references.push((ref ).url );
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
const dbSpec =
|
|
637
|
+
entry.database_specific && typeof entry.database_specific === 'object'
|
|
638
|
+
? (entry.database_specific )
|
|
639
|
+
: {};
|
|
640
|
+
const activelyExploited =
|
|
641
|
+
entry.activelyExploited === true ||
|
|
642
|
+
dbSpec.actively_exploited === true ||
|
|
643
|
+
dbSpec.cisa_kev === true ||
|
|
644
|
+
dbSpec.kev === true;
|
|
645
|
+
|
|
646
|
+
const summary =
|
|
647
|
+
(typeof entry.summary === 'string' && entry.summary.trim()) ||
|
|
648
|
+
(typeof entry.details === 'string' && entry.details.trim().slice(0, 280)) ||
|
|
649
|
+
id;
|
|
650
|
+
|
|
651
|
+
const dateAdded =
|
|
652
|
+
(typeof entry.published === 'string' && entry.published.trim()) ||
|
|
653
|
+
(typeof entry.modified === 'string' && entry.modified.trim()) ||
|
|
654
|
+
new Date().toISOString();
|
|
655
|
+
|
|
656
|
+
const item = {
|
|
657
|
+
id,
|
|
658
|
+
source,
|
|
659
|
+
purls: Array.from(purls).sort(),
|
|
660
|
+
activelyExploited,
|
|
661
|
+
dateAdded,
|
|
662
|
+
summary,
|
|
663
|
+
references: Array.from(new Set(references)),
|
|
664
|
+
rawHash: sha256Hex(canonicalJson(entry)),
|
|
665
|
+
};
|
|
666
|
+
if (cveId) item.cveId = cveId;
|
|
667
|
+
if (euvdId) item.euvdId = euvdId;
|
|
668
|
+
if (affectedRanges.length > 0) item.affectedRanges = affectedRanges;
|
|
669
|
+
items.push(item);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
return items;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// ---------------------------------------------------------------------------
|
|
676
|
+
// Deterministic Exploit Matcher & .cra-vex.json Suppression Engine
|
|
677
|
+
// ---------------------------------------------------------------------------
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
function correlateFeedItems(feedItems ) {
|
|
693
|
+
const groups = new Set ();
|
|
694
|
+
const keyToGroup = new Map ();
|
|
695
|
+
|
|
696
|
+
for (const item of feedItems) {
|
|
697
|
+
const keys = [item.cveId, item.euvdId, item.id]
|
|
698
|
+
.filter((k) => typeof k === 'string' && k.trim().length > 0)
|
|
699
|
+
.map((k) => k.trim().toUpperCase());
|
|
700
|
+
|
|
701
|
+
const matchedGroups = [];
|
|
702
|
+
for (const k of keys) {
|
|
703
|
+
const found = keyToGroup.get(k);
|
|
704
|
+
if (found && !matchedGroups.includes(found)) {
|
|
705
|
+
matchedGroups.push(found);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
let group = matchedGroups[0];
|
|
710
|
+
if (!group) {
|
|
711
|
+
group = {
|
|
712
|
+
vulnId: item.cveId ?? item.euvdId ?? item.id,
|
|
713
|
+
cveId: item.cveId,
|
|
714
|
+
euvdId: item.euvdId,
|
|
715
|
+
ids: new Set (),
|
|
716
|
+
sources: new Set (),
|
|
717
|
+
purls: new Set (),
|
|
718
|
+
affectedRanges: [],
|
|
719
|
+
activelyExploited: false,
|
|
720
|
+
summary: item.summary,
|
|
721
|
+
references: new Set (),
|
|
722
|
+
};
|
|
723
|
+
groups.add(group);
|
|
724
|
+
} else if (matchedGroups.length > 1) {
|
|
725
|
+
// Transitive merge: absorb all secondary groups into the primary group
|
|
726
|
+
for (let i = 1; i < matchedGroups.length; i++) {
|
|
727
|
+
const other = matchedGroups[i];
|
|
728
|
+
if (!other || other === group) continue;
|
|
729
|
+
if (other.cveId && !group.cveId) {
|
|
730
|
+
group.cveId = other.cveId;
|
|
731
|
+
group.vulnId = other.cveId;
|
|
732
|
+
}
|
|
733
|
+
if (other.euvdId && !group.euvdId) {
|
|
734
|
+
group.euvdId = other.euvdId;
|
|
735
|
+
}
|
|
736
|
+
for (const otherId of other.ids) {
|
|
737
|
+
group.ids.add(otherId);
|
|
738
|
+
keyToGroup.set(otherId, group);
|
|
739
|
+
}
|
|
740
|
+
for (const src of other.sources) group.sources.add(src);
|
|
741
|
+
for (const p of other.purls) group.purls.add(p);
|
|
742
|
+
group.affectedRanges.push(...other.affectedRanges);
|
|
743
|
+
if (other.activelyExploited) group.activelyExploited = true;
|
|
744
|
+
if (!group.summary && other.summary) group.summary = other.summary;
|
|
745
|
+
for (const ref of other.references) group.references.add(ref);
|
|
746
|
+
groups.delete(other);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
if (item.cveId && !group.cveId) {
|
|
751
|
+
group.cveId = item.cveId;
|
|
752
|
+
group.vulnId = item.cveId;
|
|
753
|
+
}
|
|
754
|
+
if (item.euvdId && !group.euvdId) {
|
|
755
|
+
group.euvdId = item.euvdId;
|
|
756
|
+
}
|
|
757
|
+
for (const k of keys) {
|
|
758
|
+
group.ids.add(k);
|
|
759
|
+
keyToGroup.set(k, group);
|
|
760
|
+
}
|
|
761
|
+
group.sources.add(item.source);
|
|
762
|
+
for (const p of item.purls) group.purls.add(p);
|
|
763
|
+
if (item.affectedRanges) group.affectedRanges.push(...item.affectedRanges);
|
|
764
|
+
if (item.activelyExploited) group.activelyExploited = true;
|
|
765
|
+
if (!group.summary && item.summary) group.summary = item.summary;
|
|
766
|
+
for (const ref of item.references) group.references.add(ref);
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
return Array.from(groups);
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
function normalizePurlForComparison(purl ) {
|
|
773
|
+
const parsed = parsePurl(purl);
|
|
774
|
+
if (!parsed || !parsed.version) return purl.trim().toLowerCase();
|
|
775
|
+
const normVersion =
|
|
776
|
+
parsed.ecosystem === 'golang' ? normalizeVersionToken(parsed.version) : parsed.version;
|
|
777
|
+
return buildPurl(parsed.ecosystem, parsed.name, normVersion, parsed.namespace).toLowerCase();
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
function componentMatchesRange(comp , range ) {
|
|
781
|
+
if (comp.ecosystem !== range.ecosystem) return false;
|
|
782
|
+
|
|
783
|
+
const compName = comp.ecosystem === 'pypi' ? normalizePypiName(comp.name) : comp.name.toLowerCase();
|
|
784
|
+
const rangeParts = splitPackageName(range.ecosystem, range.name);
|
|
785
|
+
const rangeName =
|
|
786
|
+
range.ecosystem === 'pypi' ? normalizePypiName(rangeParts.name) : rangeParts.name.toLowerCase();
|
|
787
|
+
const rangeNs = (range.namespace ?? rangeParts.namespace)?.toLowerCase();
|
|
788
|
+
const compNs = comp.namespace?.toLowerCase();
|
|
789
|
+
|
|
790
|
+
if (compName !== rangeName) return false;
|
|
791
|
+
if (rangeNs !== undefined && compNs !== rangeNs) return false;
|
|
792
|
+
if (rangeNs === undefined && compNs !== undefined && comp.ecosystem !== 'golang') return false;
|
|
793
|
+
|
|
794
|
+
return isVersionInRange(comp.version, range);
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
function findMatchingSuppression(
|
|
798
|
+
vuln ,
|
|
799
|
+
matchedComponents ,
|
|
800
|
+
suppressions ,
|
|
801
|
+
nowMs ,
|
|
802
|
+
) {
|
|
803
|
+
for (const sup of suppressions) {
|
|
804
|
+
const supId = sup.vulnerabilityId.trim().toUpperCase();
|
|
805
|
+
if (!vuln.ids.has(supId)) continue;
|
|
806
|
+
|
|
807
|
+
// Check expiration
|
|
808
|
+
if (sup.expiresAt) {
|
|
809
|
+
const expMs = Date.parse(sup.expiresAt);
|
|
810
|
+
if (!Number.isNaN(expMs) && expMs <= nowMs) {
|
|
811
|
+
continue;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// If suppression omits purl, it applies to the entire vulnerability in this repo
|
|
816
|
+
if (!sup.purl || sup.purl.trim().length === 0) {
|
|
817
|
+
return sup;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
const supPurlRaw = sup.purl.trim().toLowerCase();
|
|
821
|
+
const supPurlNorm = normalizePurlForComparison(sup.purl);
|
|
822
|
+
const supParsed = parsePurl(sup.purl);
|
|
823
|
+
|
|
824
|
+
// Every matched component must be covered by the suppression purl (exact or versionless)
|
|
825
|
+
const allCovered = matchedComponents.every((comp) => {
|
|
826
|
+
const compNorm = normalizePurlForComparison(comp.purl);
|
|
827
|
+
if (compNorm === supPurlNorm || comp.purl.toLowerCase() === supPurlRaw) return true;
|
|
828
|
+
|
|
829
|
+
if (supParsed) {
|
|
830
|
+
const compParsed = parsePurl(comp.purl);
|
|
831
|
+
if (compParsed) {
|
|
832
|
+
if (compParsed.ecosystem !== supParsed.ecosystem) return false;
|
|
833
|
+
const compNs = (compParsed.namespace ?? '').toLowerCase();
|
|
834
|
+
const supNs = (supParsed.namespace ?? '').toLowerCase();
|
|
835
|
+
if (compNs !== supNs) return false;
|
|
836
|
+
const compName =
|
|
837
|
+
compParsed.ecosystem === 'pypi'
|
|
838
|
+
? normalizePypiName(compParsed.name)
|
|
839
|
+
: compParsed.name.toLowerCase();
|
|
840
|
+
const supName =
|
|
841
|
+
supParsed.ecosystem === 'pypi'
|
|
842
|
+
? normalizePypiName(supParsed.name)
|
|
843
|
+
: supParsed.name.toLowerCase();
|
|
844
|
+
if (compName !== supName) return false;
|
|
845
|
+
// Versionless suppression PURL matches any version of the package (including scoped npm)
|
|
846
|
+
if (!supParsed.version) return true;
|
|
847
|
+
if (!compParsed.version) return false;
|
|
848
|
+
const compVer =
|
|
849
|
+
compParsed.ecosystem === 'golang'
|
|
850
|
+
? normalizeVersionToken(compParsed.version)
|
|
851
|
+
: compParsed.version;
|
|
852
|
+
const supVer =
|
|
853
|
+
supParsed.ecosystem === 'golang'
|
|
854
|
+
? normalizeVersionToken(supParsed.version)
|
|
855
|
+
: supParsed.version;
|
|
856
|
+
return compVer.toLowerCase() === supVer.toLowerCase();
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// Fallback versionless prefix match when PURL couldn't be parsed
|
|
861
|
+
if (!supPurlRaw.includes('@') && compNorm.startsWith(`${supPurlRaw}@`)) return true;
|
|
862
|
+
return false;
|
|
863
|
+
});
|
|
864
|
+
|
|
865
|
+
if (allCovered) {
|
|
866
|
+
return sup;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
return undefined;
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
/**
|
|
873
|
+
* Deterministically matches production PURL components against normalized exploit feed items
|
|
874
|
+
* (correlating CISA KEV + ENISA EUVD + OSV/GHSA) and applies `.cra-vex.json` suppressions.
|
|
875
|
+
*/
|
|
876
|
+
export function matchExploits(
|
|
877
|
+
components ,
|
|
878
|
+
feedItems ,
|
|
879
|
+
suppressions = [],
|
|
880
|
+
options ,
|
|
881
|
+
) {
|
|
882
|
+
const includeNonExploited = options?.includeNonExploited ?? false;
|
|
883
|
+
const includeDev = options?.includeDev ?? false;
|
|
884
|
+
const nowMs =
|
|
885
|
+
options?.now instanceof Date
|
|
886
|
+
? options.now.getTime()
|
|
887
|
+
: typeof options?.now === 'string'
|
|
888
|
+
? Date.parse(options.now)
|
|
889
|
+
: Date.now();
|
|
890
|
+
|
|
891
|
+
const eligibleComponents = includeDev
|
|
892
|
+
? components
|
|
893
|
+
: components.filter((c) => c.scope !== 'dev');
|
|
894
|
+
|
|
895
|
+
const correlated = correlateFeedItems(feedItems);
|
|
896
|
+
const matches = [];
|
|
897
|
+
|
|
898
|
+
for (const vuln of correlated) {
|
|
899
|
+
if (!includeNonExploited && !vuln.activelyExploited) continue;
|
|
900
|
+
|
|
901
|
+
const normalizedFeedPurls = new Set ();
|
|
902
|
+
for (const p of vuln.purls) {
|
|
903
|
+
normalizedFeedPurls.add(normalizePurlForComparison(p));
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
const matchedMap = new Map ();
|
|
907
|
+
for (const comp of eligibleComponents) {
|
|
908
|
+
const compNormPurl = normalizePurlForComparison(comp.purl);
|
|
909
|
+
if (normalizedFeedPurls.has(compNormPurl)) {
|
|
910
|
+
matchedMap.set(comp.purl, comp);
|
|
911
|
+
continue;
|
|
912
|
+
}
|
|
913
|
+
for (const range of vuln.affectedRanges) {
|
|
914
|
+
if (componentMatchesRange(comp, range)) {
|
|
915
|
+
matchedMap.set(comp.purl, comp);
|
|
916
|
+
break;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
if (matchedMap.size === 0) continue;
|
|
922
|
+
|
|
923
|
+
const matchedComponents = Array.from(matchedMap.values()).sort((a, b) =>
|
|
924
|
+
a.purl.localeCompare(b.purl),
|
|
925
|
+
);
|
|
926
|
+
const suppression = findMatchingSuppression(vuln, matchedComponents, suppressions, nowMs);
|
|
927
|
+
|
|
928
|
+
const match = {
|
|
929
|
+
vulnId: vuln.vulnId,
|
|
930
|
+
sources: Array.from(vuln.sources).sort(),
|
|
931
|
+
matchedComponents,
|
|
932
|
+
summary: vuln.summary,
|
|
933
|
+
references: Array.from(vuln.references).sort(),
|
|
934
|
+
suppressed: suppression !== undefined,
|
|
935
|
+
};
|
|
936
|
+
if (vuln.cveId) match.cveId = vuln.cveId;
|
|
937
|
+
if (vuln.euvdId) match.euvdId = vuln.euvdId;
|
|
938
|
+
if (suppression) match.suppression = suppression;
|
|
939
|
+
|
|
940
|
+
matches.push(match);
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
return matches.sort((a, b) => a.vulnId.localeCompare(b.vulnId));
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
// ---------------------------------------------------------------------------
|
|
947
|
+
// 4-State Triage Machine (EU CRA Article 14 Guardrail)
|
|
948
|
+
// ---------------------------------------------------------------------------
|
|
949
|
+
|
|
950
|
+
const TRIAGE_SLA_HOURS = 4;
|
|
951
|
+
const HOUR_MS = 60 * 60 * 1000;
|
|
952
|
+
const DAY_MS = 24 * HOUR_MS;
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
|
|
971
|
+
|
|
972
|
+
|
|
973
|
+
function purlCoversComponents(
|
|
974
|
+
specPurl ,
|
|
975
|
+
components ,
|
|
976
|
+
mode = 'every',
|
|
977
|
+
) {
|
|
978
|
+
if (components.length === 0) return true;
|
|
979
|
+
const specRaw = specPurl.trim().toLowerCase();
|
|
980
|
+
const specNorm = normalizePurlForComparison(specPurl);
|
|
981
|
+
const specParsed = parsePurl(specPurl);
|
|
982
|
+
|
|
983
|
+
const matchesComponent = (comp ) => {
|
|
984
|
+
const compNorm = normalizePurlForComparison(comp.purl);
|
|
985
|
+
if (compNorm === specNorm || comp.purl.toLowerCase() === specRaw) return true;
|
|
986
|
+
if (specParsed) {
|
|
987
|
+
const compParsed = parsePurl(comp.purl);
|
|
988
|
+
if (compParsed) {
|
|
989
|
+
if (compParsed.ecosystem !== specParsed.ecosystem) return false;
|
|
990
|
+
if ((compParsed.namespace ?? '').toLowerCase() !== (specParsed.namespace ?? '').toLowerCase()) {
|
|
991
|
+
return false;
|
|
992
|
+
}
|
|
993
|
+
const compName =
|
|
994
|
+
compParsed.ecosystem === 'pypi'
|
|
995
|
+
? normalizePypiName(compParsed.name)
|
|
996
|
+
: compParsed.name.toLowerCase();
|
|
997
|
+
const specName =
|
|
998
|
+
specParsed.ecosystem === 'pypi'
|
|
999
|
+
? normalizePypiName(specParsed.name)
|
|
1000
|
+
: specParsed.name.toLowerCase();
|
|
1001
|
+
if (compName !== specName) return false;
|
|
1002
|
+
if (!specParsed.version) return true;
|
|
1003
|
+
if (!compParsed.version) return false;
|
|
1004
|
+
const cv =
|
|
1005
|
+
compParsed.ecosystem === 'golang'
|
|
1006
|
+
? normalizeVersionToken(compParsed.version)
|
|
1007
|
+
: compParsed.version;
|
|
1008
|
+
const sv =
|
|
1009
|
+
specParsed.ecosystem === 'golang'
|
|
1010
|
+
? normalizeVersionToken(specParsed.version)
|
|
1011
|
+
: specParsed.version;
|
|
1012
|
+
return cv.toLowerCase() === sv.toLowerCase();
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
return false;
|
|
1016
|
+
};
|
|
1017
|
+
|
|
1018
|
+
return mode === 'some'
|
|
1019
|
+
? components.some(matchesComponent)
|
|
1020
|
+
: components.every(matchesComponent);
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
function matchVulnIds(match ) {
|
|
1024
|
+
const ids = new Set ();
|
|
1025
|
+
if (match.vulnId) ids.add(match.vulnId.trim().toUpperCase());
|
|
1026
|
+
if (match.cveId) ids.add(match.cveId.trim().toUpperCase());
|
|
1027
|
+
if (match.euvdId) ids.add(match.euvdId.trim().toUpperCase());
|
|
1028
|
+
return ids;
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
/**
|
|
1032
|
+
* Evaluates an exploit match through the CRA-Switch v2 4-State Triage Machine:
|
|
1033
|
+
* - Raw lockfile/SBOM match ALWAYS resolves to `triage_required` (4h internal Triage SLA,
|
|
1034
|
+
* NO statutory 24h ENISA clock started).
|
|
1035
|
+
* - Only an explicit `triageEvents` record with `determination === 'confirmed_exploited'`
|
|
1036
|
+
* transitions to `confirmed_exploited` and starts the 24h/72h/14d statutory clock from
|
|
1037
|
+
* `statutoryClockStartedAt || triagedAt`.
|
|
1038
|
+
* - Active non-expired `.cra-vex.json` suppression OR `determination === 'not_affected'`
|
|
1039
|
+
* resolves to `not_affected`.
|
|
1040
|
+
*/
|
|
1041
|
+
export function resolveAlertTriageState(
|
|
1042
|
+
match ,
|
|
1043
|
+
suppressions = [],
|
|
1044
|
+
triageEvents = [],
|
|
1045
|
+
nowIso ,
|
|
1046
|
+
) {
|
|
1047
|
+
const nowStr = nowIso && !Number.isNaN(Date.parse(nowIso)) ? nowIso : new Date().toISOString();
|
|
1048
|
+
const nowMs = Date.parse(nowStr);
|
|
1049
|
+
const vulnIds = matchVulnIds(match);
|
|
1050
|
+
|
|
1051
|
+
// Locate latest applicable triageEvent for this vulnerability/components
|
|
1052
|
+
const matchingEvents = triageEvents.filter((ev) => {
|
|
1053
|
+
if (!vulnIds.has(ev.vulnerabilityId.trim().toUpperCase())) return false;
|
|
1054
|
+
if (ev.purl && ev.purl.trim().length > 0) {
|
|
1055
|
+
// Confirming active exploitation on ANY matched component version triggers the statutory clock,
|
|
1056
|
+
// while marking `not_affected` still requires covering ALL matched component versions.
|
|
1057
|
+
const matchMode = ev.determination === 'confirmed_exploited' ? 'some' : 'every';
|
|
1058
|
+
return purlCoversComponents(ev.purl, match.matchedComponents, matchMode);
|
|
1059
|
+
}
|
|
1060
|
+
return true;
|
|
1061
|
+
});
|
|
1062
|
+
matchingEvents.sort((a, b) => Date.parse(a.triagedAt) - Date.parse(b.triagedAt));
|
|
1063
|
+
const latestEvent = matchingEvents[matchingEvents.length - 1];
|
|
1064
|
+
|
|
1065
|
+
// Locate active non-expired suppression
|
|
1066
|
+
let activeSuppression = match.suppressed ? match.suppression : undefined;
|
|
1067
|
+
if (activeSuppression?.expiresAt) {
|
|
1068
|
+
const expMs = Date.parse(activeSuppression.expiresAt);
|
|
1069
|
+
if (!Number.isNaN(expMs) && expMs <= nowMs) {
|
|
1070
|
+
activeSuppression = undefined;
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
if (!activeSuppression) {
|
|
1074
|
+
for (const sup of suppressions) {
|
|
1075
|
+
if (!vulnIds.has(sup.vulnerabilityId.trim().toUpperCase())) continue;
|
|
1076
|
+
if (sup.expiresAt) {
|
|
1077
|
+
const expMs = Date.parse(sup.expiresAt);
|
|
1078
|
+
if (!Number.isNaN(expMs) && expMs <= nowMs) continue;
|
|
1079
|
+
}
|
|
1080
|
+
if (sup.purl && sup.purl.trim().length > 0) {
|
|
1081
|
+
if (!purlCoversComponents(sup.purl, match.matchedComponents)) continue;
|
|
1082
|
+
}
|
|
1083
|
+
activeSuppression = sup;
|
|
1084
|
+
break;
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
const detectedAt =
|
|
1089
|
+
latestEvent?.detectedAt && !Number.isNaN(Date.parse(latestEvent.detectedAt))
|
|
1090
|
+
? latestEvent.detectedAt
|
|
1091
|
+
: nowStr;
|
|
1092
|
+
const detectedMs = Date.parse(detectedAt);
|
|
1093
|
+
const triageSlaDueAt = new Date(detectedMs + TRIAGE_SLA_HOURS * HOUR_MS).toISOString();
|
|
1094
|
+
|
|
1095
|
+
// Precedence 1: Explicit human confirmation of active exploitation in shipped product
|
|
1096
|
+
if (latestEvent && latestEvent.determination === 'confirmed_exploited') {
|
|
1097
|
+
const confirmedAt = latestEvent.statutoryClockStartedAt ?? latestEvent.triagedAt;
|
|
1098
|
+
const confirmedMs = Date.parse(confirmedAt);
|
|
1099
|
+
const safeConfirmedMs = Number.isNaN(confirmedMs) ? nowMs : confirmedMs;
|
|
1100
|
+
const safeConfirmedIso = new Date(safeConfirmedMs).toISOString();
|
|
1101
|
+
return {
|
|
1102
|
+
triageState: 'confirmed_exploited',
|
|
1103
|
+
detectedAt,
|
|
1104
|
+
triageSlaDueAt,
|
|
1105
|
+
statutoryClockStarted: true,
|
|
1106
|
+
confirmedAt: safeConfirmedIso,
|
|
1107
|
+
triagedBy: latestEvent.triagedBy,
|
|
1108
|
+
triageEvent: latestEvent,
|
|
1109
|
+
earlyWarningDueAt: new Date(safeConfirmedMs + 24 * HOUR_MS).toISOString(),
|
|
1110
|
+
detailedNotificationDueAt: new Date(safeConfirmedMs + 72 * HOUR_MS).toISOString(),
|
|
1111
|
+
finalReportDueAt: new Date(safeConfirmedMs + 14 * DAY_MS).toISOString(),
|
|
1112
|
+
};
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
// Precedence 2: Active VEX suppression or explicit `not_affected` triage determination
|
|
1116
|
+
if (activeSuppression || latestEvent?.determination === 'not_affected') {
|
|
1117
|
+
return {
|
|
1118
|
+
triageState: 'not_affected',
|
|
1119
|
+
detectedAt,
|
|
1120
|
+
triageSlaDueAt,
|
|
1121
|
+
statutoryClockStarted: false,
|
|
1122
|
+
...(latestEvent?.triagedBy ? { triagedBy: latestEvent.triagedBy } : {}),
|
|
1123
|
+
...(latestEvent ? { triageEvent: latestEvent } : {}),
|
|
1124
|
+
...(activeSuppression ? { suppression: activeSuppression } : {}),
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
// Default (CRITICAL GUARDRAIL): Raw match or watchlist -> `triage_required`, NO statutory clock
|
|
1129
|
+
return {
|
|
1130
|
+
triageState: 'triage_required',
|
|
1131
|
+
detectedAt,
|
|
1132
|
+
triageSlaDueAt,
|
|
1133
|
+
statutoryClockStarted: false,
|
|
1134
|
+
...(latestEvent?.triagedBy ? { triagedBy: latestEvent.triagedBy } : {}),
|
|
1135
|
+
...(latestEvent ? { triageEvent: latestEvent } : {}),
|
|
1136
|
+
};
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/**
|
|
1140
|
+
* Derives the repository's 4-state posture level:
|
|
1141
|
+
* `confirmed_exploited` > `triage_required` > `not_affected` (>=1 suppression) > `clean` > `unscanned`.
|
|
1142
|
+
*/
|
|
1143
|
+
export function deriveRepoStatusLevel(
|
|
1144
|
+
openAlerts ,
|
|
1145
|
+
suppressionCount ,
|
|
1146
|
+
hasRelease ,
|
|
1147
|
+
) {
|
|
1148
|
+
if (!hasRelease) {
|
|
1149
|
+
return 'unscanned';
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
const activeAlerts = openAlerts.filter(
|
|
1153
|
+
(a) => a.status === undefined || a.status === 'open',
|
|
1154
|
+
);
|
|
1155
|
+
|
|
1156
|
+
let hasConfirmed = false;
|
|
1157
|
+
let hasTriageRequired = false;
|
|
1158
|
+
let hasNotAffectedAlert = false;
|
|
1159
|
+
|
|
1160
|
+
for (const alert of activeAlerts) {
|
|
1161
|
+
if (alert.triageState === 'confirmed_exploited') {
|
|
1162
|
+
hasConfirmed = true;
|
|
1163
|
+
} else if (alert.triageState === 'not_affected') {
|
|
1164
|
+
hasNotAffectedAlert = true;
|
|
1165
|
+
} else {
|
|
1166
|
+
// Raw/unconfirmed alerts default to `triage_required`, never `confirmed_exploited`
|
|
1167
|
+
hasTriageRequired = true;
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
if (hasConfirmed) return 'confirmed_exploited';
|
|
1172
|
+
if (hasTriageRequired) return 'triage_required';
|
|
1173
|
+
if (suppressionCount >= 1 || hasNotAffectedAlert) return 'not_affected';
|
|
1174
|
+
return 'clean';
|
|
1175
|
+
}
|