@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
package/dist/core/vex.js
ADDED
|
@@ -0,0 +1,722 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenVEX v0.2.0 builder, `.cra-vex.json` config parser, and WebCrypto Ed25519
|
|
3
|
+
* keygen / canonical JSON signing / verification (zero dependencies).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { SCHEMA_VERSIONS } from './types.js';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
base64ToBytes,
|
|
22
|
+
base64UrlToBytes,
|
|
23
|
+
bytesToBase64,
|
|
24
|
+
bytesToBase64Url,
|
|
25
|
+
canonicalJson,
|
|
26
|
+
utf8ToBytes,
|
|
27
|
+
} from './crypto-utils.js';
|
|
28
|
+
import { resolveAlertTriageState } from './feeds.js';
|
|
29
|
+
|
|
30
|
+
const VALID_JUSTIFICATIONS = new Set ([
|
|
31
|
+
'component_not_present',
|
|
32
|
+
'vulnerable_code_not_present',
|
|
33
|
+
'vulnerable_code_not_in_execute_path',
|
|
34
|
+
'vulnerable_code_cannot_be_controlled_by_adversary',
|
|
35
|
+
'inline_mitigations_already_exist',
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
// ASN.1 DER SubjectPublicKeyInfo (SPKI) 12-byte prefix for raw 32-byte Ed25519 public keys
|
|
39
|
+
const ED25519_SPKI_PREFIX = new Uint8Array([
|
|
40
|
+
0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00,
|
|
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
|
+
const BLOCKED_WEBHOOK_SUFFIXES = [
|
|
82
|
+
'.localhost',
|
|
83
|
+
'.local',
|
|
84
|
+
'.internal',
|
|
85
|
+
'.localdomain',
|
|
86
|
+
'.home.arpa',
|
|
87
|
+
'.lan',
|
|
88
|
+
] ;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Validates an outbound alert webhook URL against SSRF vectors.
|
|
92
|
+
* Requires `https://` public FQDN hostnames (or `dryrun://` ONLY when `allowDryRun: true`);
|
|
93
|
+
* rejects `http://`, non-HTTP schemes, localhost, `.local`/`.internal`/`.localhost` domains,
|
|
94
|
+
* single-label hostnames, embedded credentials, and all raw IPv4 / IPv6 literals.
|
|
95
|
+
*/
|
|
96
|
+
export function validateWebhookUrl(
|
|
97
|
+
rawUrl ,
|
|
98
|
+
opts ,
|
|
99
|
+
) {
|
|
100
|
+
const trimmed = rawUrl.trim();
|
|
101
|
+
if (!trimmed) {
|
|
102
|
+
return { valid: false, reason: 'webhookUrl must not be empty' };
|
|
103
|
+
}
|
|
104
|
+
let parsed ;
|
|
105
|
+
try {
|
|
106
|
+
parsed = new URL(trimmed);
|
|
107
|
+
} catch {
|
|
108
|
+
return { valid: false, reason: 'webhookUrl must be a valid URL' };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const protocol = parsed.protocol.toLowerCase();
|
|
112
|
+
if (protocol === 'dryrun:') {
|
|
113
|
+
if (opts?.allowDryRun === true) {
|
|
114
|
+
return { valid: true };
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
valid: false,
|
|
118
|
+
reason: 'webhookUrl scheme "dryrun:" is only allowed when allowDryRun is enabled',
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (protocol !== 'https:') {
|
|
122
|
+
return {
|
|
123
|
+
valid: false,
|
|
124
|
+
reason: `webhookUrl scheme "${protocol}" is not allowed; must use https://`,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (parsed.username || parsed.password) {
|
|
129
|
+
return { valid: false, reason: 'webhookUrl must not contain embedded credentials' };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const rawHost = parsed.hostname.toLowerCase();
|
|
133
|
+
const host = rawHost.replace(/\.+$/, '');
|
|
134
|
+
if (!host) {
|
|
135
|
+
return { valid: false, reason: 'webhookUrl must include a hostname' };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Reject raw IPv6 literals (`[::1]`, `[::ffff:169.254.169.254]`, etc.)
|
|
139
|
+
if (host.startsWith('[') || host.endsWith(']') || host.includes(':')) {
|
|
140
|
+
return { valid: false, reason: 'webhookUrl must not target raw IPv6 literals' };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Reject raw IPv4 literals (WHATWG URL normalizes decimal/hex/octal IPs to dotted IPv4)
|
|
144
|
+
if (/^\d+(?:\.\d+)*$/.test(host)) {
|
|
145
|
+
return { valid: false, reason: 'webhookUrl must not target raw IPv4 literals' };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (host === 'localhost' || host === 'ip6-localhost' || host === 'ip6-loopback') {
|
|
149
|
+
return { valid: false, reason: 'webhookUrl must not target localhost' };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
for (const suffix of BLOCKED_WEBHOOK_SUFFIXES) {
|
|
153
|
+
if (host.endsWith(suffix)) {
|
|
154
|
+
return { valid: false, reason: `webhookUrl must not target internal domain (${suffix})` };
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Reject single-label hostnames (e.g. `metadata`, `redis`, `intranet`) or malformed labels
|
|
159
|
+
if (!host.includes('.') || host.startsWith('.') || host.includes('..')) {
|
|
160
|
+
return { valid: false, reason: 'webhookUrl hostname must be a public FQDN' };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return { valid: true };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Validates and returns a safe outbound webhook URL, or throws an Error on SSRF/invalid URLs.
|
|
168
|
+
*/
|
|
169
|
+
export function validateSafeWebhookUrl(
|
|
170
|
+
rawUrl ,
|
|
171
|
+
opts ,
|
|
172
|
+
) {
|
|
173
|
+
const check = validateWebhookUrl(rawUrl, opts);
|
|
174
|
+
if (!check.valid) {
|
|
175
|
+
throw new Error(`Unsafe webhookUrl: ${check.reason ?? 'disallowed URL'}`);
|
|
176
|
+
}
|
|
177
|
+
return rawUrl.trim();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function isSafeWebhookUrl(rawUrl , opts ) {
|
|
181
|
+
return validateWebhookUrl(rawUrl, opts).valid;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const VALID_DETERMINATIONS = new Set ([
|
|
185
|
+
'confirmed_exploited',
|
|
186
|
+
'not_affected',
|
|
187
|
+
'watchlist',
|
|
188
|
+
]);
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Parses and validates a repository `.cra-vex.json` (v1 or v2) configuration file.
|
|
192
|
+
* Remains 100% backward-compatible with v1 while supporting v2 triageEvents,
|
|
193
|
+
* coordinatorCsirt, euMemberStatesDistributed, and evidenceExport settings.
|
|
194
|
+
*/
|
|
195
|
+
export function parseCraVexConfig(
|
|
196
|
+
rawJson ,
|
|
197
|
+
sourcePath = '.cra-vex.json',
|
|
198
|
+
opts ,
|
|
199
|
+
) {
|
|
200
|
+
let parsed ;
|
|
201
|
+
try {
|
|
202
|
+
parsed = JSON.parse(rawJson);
|
|
203
|
+
} catch (err) {
|
|
204
|
+
throw new Error(
|
|
205
|
+
`Invalid .cra-vex.json JSON: ${err instanceof Error ? err.message : String(err)}`,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
210
|
+
throw new Error('.cra-vex.json must be a JSON object');
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const obj = parsed ;
|
|
214
|
+
const rawVersion = obj.version;
|
|
215
|
+
let version ;
|
|
216
|
+
if (rawVersion === '1' || rawVersion === '2') {
|
|
217
|
+
version = rawVersion;
|
|
218
|
+
} else {
|
|
219
|
+
throw new Error(
|
|
220
|
+
`Unsupported .cra-vex.json version: expected "1" or "2", got ${JSON.stringify(rawVersion)}`,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const rawSuppressions =
|
|
225
|
+
obj.suppressions === undefined && version === '2' ? [] : obj.suppressions;
|
|
226
|
+
if (!Array.isArray(rawSuppressions)) {
|
|
227
|
+
throw new Error('.cra-vex.json must include a "suppressions" array');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const suppressions = [];
|
|
231
|
+
for (let i = 0; i < rawSuppressions.length; i++) {
|
|
232
|
+
const rawSup = rawSuppressions[i];
|
|
233
|
+
if (!rawSup || typeof rawSup !== 'object' || Array.isArray(rawSup)) {
|
|
234
|
+
throw new Error(`Invalid suppression at index ${i}: expected object`);
|
|
235
|
+
}
|
|
236
|
+
const sup = rawSup ;
|
|
237
|
+
const vulnerabilityId =
|
|
238
|
+
typeof sup.vulnerabilityId === 'string' ? sup.vulnerabilityId.trim() : '';
|
|
239
|
+
if (!vulnerabilityId) {
|
|
240
|
+
throw new Error(`Suppression at index ${i} requires a non-empty vulnerabilityId`);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const status = sup.status;
|
|
244
|
+
if (status !== 'not_affected' && status !== 'fixed') {
|
|
245
|
+
throw new Error(
|
|
246
|
+
`Suppression at index ${i} has invalid status ${JSON.stringify(status)}; expected "not_affected" or "fixed"`,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
let justification ;
|
|
251
|
+
if (sup.justification !== undefined) {
|
|
252
|
+
if (
|
|
253
|
+
typeof sup.justification !== 'string' ||
|
|
254
|
+
!VALID_JUSTIFICATIONS.has(sup.justification )
|
|
255
|
+
) {
|
|
256
|
+
throw new Error(
|
|
257
|
+
`Suppression at index ${i} has invalid OpenVEX justification: ${JSON.stringify(sup.justification)}`,
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
justification = sup.justification ;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (sup.expiresAt !== undefined) {
|
|
264
|
+
if (typeof sup.expiresAt !== 'string' || Number.isNaN(Date.parse(sup.expiresAt))) {
|
|
265
|
+
throw new Error(`Suppression at index ${i} has invalid ISO expiresAt timestamp`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const record = {
|
|
270
|
+
vulnerabilityId,
|
|
271
|
+
status,
|
|
272
|
+
sourcePath:
|
|
273
|
+
typeof sup.sourcePath === 'string' && sup.sourcePath.trim().length > 0
|
|
274
|
+
? sup.sourcePath.trim()
|
|
275
|
+
: sourcePath,
|
|
276
|
+
};
|
|
277
|
+
if (typeof sup.purl === 'string' && sup.purl.trim().length > 0) {
|
|
278
|
+
record.purl = sup.purl.trim();
|
|
279
|
+
}
|
|
280
|
+
if (justification) record.justification = justification;
|
|
281
|
+
if (typeof sup.impactStatement === 'string' && sup.impactStatement.trim().length > 0) {
|
|
282
|
+
record.impactStatement = sup.impactStatement.trim();
|
|
283
|
+
}
|
|
284
|
+
if (typeof sup.expiresAt === 'string') {
|
|
285
|
+
record.expiresAt = sup.expiresAt;
|
|
286
|
+
}
|
|
287
|
+
suppressions.push(record);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const config = {
|
|
291
|
+
version,
|
|
292
|
+
suppressions,
|
|
293
|
+
};
|
|
294
|
+
if (typeof obj.securityContactEmail === 'string' && obj.securityContactEmail.trim().length > 0) {
|
|
295
|
+
config.securityContactEmail = obj.securityContactEmail.trim();
|
|
296
|
+
}
|
|
297
|
+
if (typeof obj.coordinatorCsirt === 'string' && obj.coordinatorCsirt.trim().length > 0) {
|
|
298
|
+
config.coordinatorCsirt = obj.coordinatorCsirt.trim();
|
|
299
|
+
}
|
|
300
|
+
if (obj.euMemberStatesDistributed !== undefined) {
|
|
301
|
+
if (!Array.isArray(obj.euMemberStatesDistributed)) {
|
|
302
|
+
throw new Error('Invalid .cra-vex.json euMemberStatesDistributed: expected string array');
|
|
303
|
+
}
|
|
304
|
+
const states = obj.euMemberStatesDistributed
|
|
305
|
+
.filter((s) => typeof s === 'string' && s.trim().length > 0)
|
|
306
|
+
.map((s) => s.trim().toUpperCase());
|
|
307
|
+
if (states.length > 0) {
|
|
308
|
+
config.euMemberStatesDistributed = Array.from(new Set(states));
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (obj.webhookUrl !== undefined) {
|
|
312
|
+
if (typeof obj.webhookUrl !== 'string') {
|
|
313
|
+
throw new Error('Invalid .cra-vex.json webhookUrl: expected string');
|
|
314
|
+
}
|
|
315
|
+
const trimmedUrl = obj.webhookUrl.trim();
|
|
316
|
+
if (trimmedUrl.length > 0) {
|
|
317
|
+
try {
|
|
318
|
+
config.webhookUrl = validateSafeWebhookUrl(trimmedUrl, opts);
|
|
319
|
+
} catch (err) {
|
|
320
|
+
throw new Error(
|
|
321
|
+
`Invalid .cra-vex.json webhookUrl: ${err instanceof Error ? err.message : String(err)}`,
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (obj.triageEvents !== undefined) {
|
|
328
|
+
if (!Array.isArray(obj.triageEvents)) {
|
|
329
|
+
throw new Error('Invalid .cra-vex.json triageEvents: expected array');
|
|
330
|
+
}
|
|
331
|
+
const triageEvents = [];
|
|
332
|
+
for (let i = 0; i < obj.triageEvents.length; i++) {
|
|
333
|
+
const rawEv = obj.triageEvents[i];
|
|
334
|
+
if (!rawEv || typeof rawEv !== 'object' || Array.isArray(rawEv)) {
|
|
335
|
+
throw new Error(`Invalid triageEvent at index ${i}: expected object`);
|
|
336
|
+
}
|
|
337
|
+
const ev = rawEv ;
|
|
338
|
+
const vulnerabilityId =
|
|
339
|
+
typeof ev.vulnerabilityId === 'string' ? ev.vulnerabilityId.trim() : '';
|
|
340
|
+
if (!vulnerabilityId) {
|
|
341
|
+
throw new Error(`triageEvent at index ${i} requires a non-empty vulnerabilityId`);
|
|
342
|
+
}
|
|
343
|
+
if (typeof ev.detectedAt !== 'string' || Number.isNaN(Date.parse(ev.detectedAt))) {
|
|
344
|
+
throw new Error(`triageEvent at index ${i} requires a valid ISO detectedAt timestamp`);
|
|
345
|
+
}
|
|
346
|
+
if (typeof ev.triagedAt !== 'string' || Number.isNaN(Date.parse(ev.triagedAt))) {
|
|
347
|
+
throw new Error(`triageEvent at index ${i} requires a valid ISO triagedAt timestamp`);
|
|
348
|
+
}
|
|
349
|
+
if (
|
|
350
|
+
typeof ev.determination !== 'string' ||
|
|
351
|
+
!VALID_DETERMINATIONS.has(ev.determination )
|
|
352
|
+
) {
|
|
353
|
+
throw new Error(
|
|
354
|
+
`triageEvent at index ${i} has invalid determination ${JSON.stringify(ev.determination)}`,
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
const triagedBy = typeof ev.triagedBy === 'string' ? ev.triagedBy.trim() : '';
|
|
358
|
+
if (!triagedBy) {
|
|
359
|
+
throw new Error(`triageEvent at index ${i} requires a non-empty triagedBy`);
|
|
360
|
+
}
|
|
361
|
+
if (
|
|
362
|
+
ev.statutoryClockStartedAt !== undefined &&
|
|
363
|
+
(typeof ev.statutoryClockStartedAt !== 'string' ||
|
|
364
|
+
Number.isNaN(Date.parse(ev.statutoryClockStartedAt)))
|
|
365
|
+
) {
|
|
366
|
+
throw new Error(`triageEvent at index ${i} has invalid ISO statutoryClockStartedAt`);
|
|
367
|
+
}
|
|
368
|
+
const parsedEv = {
|
|
369
|
+
vulnerabilityId,
|
|
370
|
+
detectedAt: ev.detectedAt.trim(),
|
|
371
|
+
triagedAt: ev.triagedAt.trim(),
|
|
372
|
+
determination: ev.determination ,
|
|
373
|
+
triagedBy,
|
|
374
|
+
};
|
|
375
|
+
if (typeof ev.purl === 'string' && ev.purl.trim().length > 0) {
|
|
376
|
+
parsedEv.purl = ev.purl.trim();
|
|
377
|
+
}
|
|
378
|
+
if (typeof ev.statutoryClockStartedAt === 'string') {
|
|
379
|
+
parsedEv.statutoryClockStartedAt = ev.statutoryClockStartedAt.trim();
|
|
380
|
+
}
|
|
381
|
+
if (typeof ev.note === 'string' && ev.note.trim().length > 0) {
|
|
382
|
+
parsedEv.note = ev.note.trim();
|
|
383
|
+
}
|
|
384
|
+
triageEvents.push(parsedEv);
|
|
385
|
+
}
|
|
386
|
+
config.triageEvents = triageEvents;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (obj.evidenceExport !== undefined) {
|
|
390
|
+
if (!obj.evidenceExport || typeof obj.evidenceExport !== 'object' || Array.isArray(obj.evidenceExport)) {
|
|
391
|
+
throw new Error('Invalid .cra-vex.json evidenceExport: expected object');
|
|
392
|
+
}
|
|
393
|
+
const ee = obj.evidenceExport ;
|
|
394
|
+
const exportCfg = {};
|
|
395
|
+
if (typeof ee.gitRepo === 'string' && ee.gitRepo.trim().length > 0) {
|
|
396
|
+
exportCfg.gitRepo = ee.gitRepo.trim();
|
|
397
|
+
}
|
|
398
|
+
if (typeof ee.gitReleaseTagPrefix === 'string' && ee.gitReleaseTagPrefix.trim().length > 0) {
|
|
399
|
+
exportCfg.gitReleaseTagPrefix = ee.gitReleaseTagPrefix.trim();
|
|
400
|
+
}
|
|
401
|
+
if (typeof ee.s3Endpoint === 'string' && ee.s3Endpoint.trim().length > 0) {
|
|
402
|
+
try {
|
|
403
|
+
exportCfg.s3Endpoint = validateSafeWebhookUrl(ee.s3Endpoint.trim(), opts);
|
|
404
|
+
} catch (err) {
|
|
405
|
+
throw new Error(
|
|
406
|
+
`Invalid .cra-vex.json evidenceExport.s3Endpoint: ${err instanceof Error ? err.message : String(err)}`,
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (typeof ee.s3Bucket === 'string' && ee.s3Bucket.trim().length > 0) {
|
|
411
|
+
exportCfg.s3Bucket = ee.s3Bucket.trim();
|
|
412
|
+
}
|
|
413
|
+
if (typeof ee.s3Region === 'string' && ee.s3Region.trim().length > 0) {
|
|
414
|
+
exportCfg.s3Region = ee.s3Region.trim();
|
|
415
|
+
}
|
|
416
|
+
if (typeof ee.s3Prefix === 'string' && ee.s3Prefix.trim().length > 0) {
|
|
417
|
+
exportCfg.s3Prefix = ee.s3Prefix.trim();
|
|
418
|
+
}
|
|
419
|
+
if (ee.objectLockDays !== undefined) {
|
|
420
|
+
if (
|
|
421
|
+
typeof ee.objectLockDays !== 'number' ||
|
|
422
|
+
!Number.isInteger(ee.objectLockDays) ||
|
|
423
|
+
ee.objectLockDays <= 0
|
|
424
|
+
) {
|
|
425
|
+
throw new Error('Invalid .cra-vex.json evidenceExport.objectLockDays: expected positive integer');
|
|
426
|
+
}
|
|
427
|
+
exportCfg.objectLockDays = ee.objectLockDays;
|
|
428
|
+
}
|
|
429
|
+
config.evidenceExport = exportCfg;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return config;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Builds an OpenVEX v0.2.0 document from exploit matches and/or suppressions.
|
|
437
|
+
*/
|
|
438
|
+
export function buildOpenVexDocument(input ) {
|
|
439
|
+
const timestamp = input.timestamp ?? new Date().toISOString();
|
|
440
|
+
const releaseTag = input.releaseTag ?? 'latest';
|
|
441
|
+
const defaultProductId = `pkg:github/${input.repoFullName}@${releaseTag}`;
|
|
442
|
+
const statements = [];
|
|
443
|
+
const emittedVulnIds = new Set ();
|
|
444
|
+
const triageStateMap = new Map ();
|
|
445
|
+
if (input.triageStates) {
|
|
446
|
+
for (const [k, v] of Object.entries(input.triageStates)) {
|
|
447
|
+
const normKey = k.trim().toUpperCase();
|
|
448
|
+
if (normKey) triageStateMap.set(normKey, v);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
for (const match of input.matches ?? []) {
|
|
453
|
+
const aliases = Array.from(
|
|
454
|
+
new Set([match.cveId, match.euvdId].filter((x) => Boolean(x) && x !== match.vulnId)),
|
|
455
|
+
);
|
|
456
|
+
const products =
|
|
457
|
+
match.matchedComponents.length > 0
|
|
458
|
+
? match.matchedComponents.map((comp) => ({
|
|
459
|
+
'@id': comp.purl,
|
|
460
|
+
identifiers: { purl: comp.purl },
|
|
461
|
+
}))
|
|
462
|
+
: [{ '@id': defaultProductId, identifiers: { purl: defaultProductId } }];
|
|
463
|
+
|
|
464
|
+
const vulnObj = {
|
|
465
|
+
name: match.vulnId,
|
|
466
|
+
};
|
|
467
|
+
if (match.cveId) {
|
|
468
|
+
vulnObj['@id'] = `https://nvd.nist.gov/vuln/detail/${match.cveId}`;
|
|
469
|
+
}
|
|
470
|
+
if (aliases.length > 0) {
|
|
471
|
+
vulnObj.aliases = aliases;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (match.suppressed && match.suppression) {
|
|
475
|
+
const stmt = {
|
|
476
|
+
vulnerability: vulnObj,
|
|
477
|
+
products,
|
|
478
|
+
status: match.suppression.status,
|
|
479
|
+
impact_statement:
|
|
480
|
+
match.suppression.impactStatement ??
|
|
481
|
+
`Suppressed via ${match.suppression.sourcePath} repository attestation.`,
|
|
482
|
+
timestamp,
|
|
483
|
+
};
|
|
484
|
+
if (match.suppression.status === 'not_affected') {
|
|
485
|
+
stmt.justification =
|
|
486
|
+
match.suppression.justification ?? 'vulnerable_code_not_in_execute_path';
|
|
487
|
+
}
|
|
488
|
+
statements.push(stmt);
|
|
489
|
+
} else {
|
|
490
|
+
const explicitState =
|
|
491
|
+
triageStateMap.get(match.vulnId.trim().toUpperCase()) ??
|
|
492
|
+
(match.cveId ? triageStateMap.get(match.cveId.trim().toUpperCase()) : undefined) ??
|
|
493
|
+
(match.euvdId ? triageStateMap.get(match.euvdId.trim().toUpperCase()) : undefined);
|
|
494
|
+
const resolved = resolveAlertTriageState(
|
|
495
|
+
match,
|
|
496
|
+
input.suppressions ?? [],
|
|
497
|
+
input.triageEvents ?? [],
|
|
498
|
+
timestamp,
|
|
499
|
+
);
|
|
500
|
+
const effectiveState = explicitState ?? resolved.triageState;
|
|
501
|
+
|
|
502
|
+
if (effectiveState === 'not_affected') {
|
|
503
|
+
const status = resolved.suppression?.status ?? 'not_affected';
|
|
504
|
+
const stmt = {
|
|
505
|
+
vulnerability: vulnObj,
|
|
506
|
+
products,
|
|
507
|
+
status,
|
|
508
|
+
impact_statement:
|
|
509
|
+
resolved.suppression?.impactStatement ??
|
|
510
|
+
resolved.triageEvent?.note ??
|
|
511
|
+
'Triaged as not affected in shipped product.',
|
|
512
|
+
timestamp,
|
|
513
|
+
};
|
|
514
|
+
if (status === 'not_affected') {
|
|
515
|
+
stmt.justification =
|
|
516
|
+
resolved.suppression?.justification ?? 'vulnerable_code_not_in_execute_path';
|
|
517
|
+
}
|
|
518
|
+
statements.push(stmt);
|
|
519
|
+
} else if (effectiveState === 'confirmed_exploited') {
|
|
520
|
+
statements.push({
|
|
521
|
+
vulnerability: vulnObj,
|
|
522
|
+
products,
|
|
523
|
+
status: 'affected',
|
|
524
|
+
action_statement:
|
|
525
|
+
'Confirmed active exploitation in shipped product. Review ENISA Article 14 draft dossier and submit Early Warning within 24 hours of confirmation.',
|
|
526
|
+
timestamp,
|
|
527
|
+
});
|
|
528
|
+
} else {
|
|
529
|
+
statements.push({
|
|
530
|
+
vulnerability: vulnObj,
|
|
531
|
+
products,
|
|
532
|
+
status: 'under_investigation',
|
|
533
|
+
action_statement:
|
|
534
|
+
'Under internal security triage (4-hour internal SLA). Statutory 24-hour ENISA Article 14 clock has not started pending human confirmation of active exploitation in the shipped product.',
|
|
535
|
+
timestamp,
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
emittedVulnIds.add(match.vulnId.toUpperCase());
|
|
541
|
+
if (match.cveId) emittedVulnIds.add(match.cveId.toUpperCase());
|
|
542
|
+
if (match.euvdId) emittedVulnIds.add(match.euvdId.toUpperCase());
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
const docTsMs = Date.parse(timestamp);
|
|
546
|
+
for (const sup of input.suppressions ?? []) {
|
|
547
|
+
if (emittedVulnIds.has(sup.vulnerabilityId.toUpperCase())) continue;
|
|
548
|
+
if (sup.expiresAt) {
|
|
549
|
+
const expMs = Date.parse(sup.expiresAt);
|
|
550
|
+
if (!Number.isNaN(expMs) && !Number.isNaN(docTsMs) && expMs <= docTsMs) {
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
const prodId = sup.purl ?? defaultProductId;
|
|
555
|
+
const stmt = {
|
|
556
|
+
vulnerability: {
|
|
557
|
+
name: sup.vulnerabilityId,
|
|
558
|
+
...(sup.vulnerabilityId.toUpperCase().startsWith('CVE-')
|
|
559
|
+
? { '@id': `https://nvd.nist.gov/vuln/detail/${sup.vulnerabilityId.toUpperCase()}` }
|
|
560
|
+
: {}),
|
|
561
|
+
},
|
|
562
|
+
products: [{ '@id': prodId, identifiers: { purl: prodId } }],
|
|
563
|
+
status: sup.status,
|
|
564
|
+
impact_statement:
|
|
565
|
+
sup.impactStatement ?? `Declared in repository ${sup.sourcePath}.`,
|
|
566
|
+
timestamp,
|
|
567
|
+
};
|
|
568
|
+
if (sup.status === 'not_affected') {
|
|
569
|
+
stmt.justification = sup.justification ?? 'vulnerable_code_not_in_execute_path';
|
|
570
|
+
}
|
|
571
|
+
statements.push(stmt);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
return {
|
|
575
|
+
'@context': SCHEMA_VERSIONS.openvex,
|
|
576
|
+
'@id':
|
|
577
|
+
input.id ??
|
|
578
|
+
`https://craswitch.dev/vex/${encodeURIComponent(input.repoFullName)}/${encodeURIComponent(releaseTag)}/${timestamp}`,
|
|
579
|
+
author: input.author ?? 'CRA-Switch Automated Attestation',
|
|
580
|
+
role: input.role ?? 'EU CRA Article 14 Exploit-Watch & OpenVEX Publisher',
|
|
581
|
+
timestamp,
|
|
582
|
+
version: input.version ?? 1,
|
|
583
|
+
statements,
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// ---------------------------------------------------------------------------
|
|
588
|
+
// WebCrypto Ed25519 Keypair, Signing, and Verification
|
|
589
|
+
// ---------------------------------------------------------------------------
|
|
590
|
+
|
|
591
|
+
function stripSignature(doc ) {
|
|
592
|
+
const copy = {};
|
|
593
|
+
for (const [k, v] of Object.entries(doc)) {
|
|
594
|
+
if (k === 'signature') continue;
|
|
595
|
+
copy[k] = v;
|
|
596
|
+
}
|
|
597
|
+
return copy ;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function buildSpkiFromRawPublicKey(raw32 ) {
|
|
601
|
+
const spki = new Uint8Array(ED25519_SPKI_PREFIX.length + raw32.length);
|
|
602
|
+
spki.set(ED25519_SPKI_PREFIX, 0);
|
|
603
|
+
spki.set(raw32, ED25519_SPKI_PREFIX.length);
|
|
604
|
+
return spki;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
async function importPrivateKey(privateKey ) {
|
|
608
|
+
if (typeof privateKey !== 'string') {
|
|
609
|
+
return crypto.subtle.importKey('jwk', privateKey, { name: 'Ed25519' }, false, ['sign']);
|
|
610
|
+
}
|
|
611
|
+
const trimmed = privateKey.trim();
|
|
612
|
+
if (trimmed.startsWith('{')) {
|
|
613
|
+
const jwk = JSON.parse(trimmed) ;
|
|
614
|
+
return crypto.subtle.importKey('jwk', jwk, { name: 'Ed25519' }, false, ['sign']);
|
|
615
|
+
}
|
|
616
|
+
const pkcs8Bytes = base64ToBytes(trimmed);
|
|
617
|
+
return crypto.subtle.importKey('pkcs8', pkcs8Bytes, { name: 'Ed25519' }, false, ['sign']);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
async function importPublicKey(publicKey ) {
|
|
621
|
+
if (typeof publicKey !== 'string') {
|
|
622
|
+
return crypto.subtle.importKey('jwk', publicKey, { name: 'Ed25519' }, false, ['verify']);
|
|
623
|
+
}
|
|
624
|
+
const trimmed = publicKey.trim();
|
|
625
|
+
if (trimmed.startsWith('{')) {
|
|
626
|
+
const jwk = JSON.parse(trimmed) ;
|
|
627
|
+
return crypto.subtle.importKey('jwk', jwk, { name: 'Ed25519' }, false, ['verify']);
|
|
628
|
+
}
|
|
629
|
+
const keyData = trimmed.startsWith('ed25519:') ? trimmed.slice('ed25519:'.length) : trimmed;
|
|
630
|
+
const bytes = base64ToBytes(keyData);
|
|
631
|
+
const spkiBytes = bytes.length === 32 ? buildSpkiFromRawPublicKey(bytes) : bytes;
|
|
632
|
+
return crypto.subtle.importKey('spki', spkiBytes, { name: 'Ed25519' }, false, ['verify']);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* Generates an Ed25519 keypair using WebCrypto.
|
|
637
|
+
* Returns base64 PKCS8 private key, base64 SPKI public key, self-verifying keyId
|
|
638
|
+
* (`ed25519:<base64url-spki>`), and JWK exports.
|
|
639
|
+
*/
|
|
640
|
+
export async function generateEd25519Keypair() {
|
|
641
|
+
const pair = (await crypto.subtle.generateKey(
|
|
642
|
+
{ name: 'Ed25519' },
|
|
643
|
+
true,
|
|
644
|
+
['sign', 'verify'],
|
|
645
|
+
)) ;
|
|
646
|
+
|
|
647
|
+
const pkcs8 = new Uint8Array(await crypto.subtle.exportKey('pkcs8', pair.privateKey));
|
|
648
|
+
const spki = new Uint8Array(await crypto.subtle.exportKey('spki', pair.publicKey));
|
|
649
|
+
const privateKeyJwk = (await crypto.subtle.exportKey('jwk', pair.privateKey)) ;
|
|
650
|
+
const publicKeyJwk = (await crypto.subtle.exportKey('jwk', pair.publicKey)) ;
|
|
651
|
+
|
|
652
|
+
const privateKeyB64 = bytesToBase64(pkcs8);
|
|
653
|
+
const publicKeyB64 = bytesToBase64(spki);
|
|
654
|
+
const keyId = `ed25519:${bytesToBase64Url(spki)}`;
|
|
655
|
+
|
|
656
|
+
return {
|
|
657
|
+
privateKeyB64,
|
|
658
|
+
publicKeyB64,
|
|
659
|
+
keyId,
|
|
660
|
+
privateKeyJwk,
|
|
661
|
+
publicKeyJwk,
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* Signs an OpenVEX document over its canonical sorted JSON payload (excluding `signature`)
|
|
667
|
+
* using WebCrypto Ed25519.
|
|
668
|
+
*/
|
|
669
|
+
export async function signOpenVex(
|
|
670
|
+
doc ,
|
|
671
|
+
privateKeyB64 ,
|
|
672
|
+
keyId ,
|
|
673
|
+
signedAt ,
|
|
674
|
+
) {
|
|
675
|
+
const key = await importPrivateKey(privateKeyB64);
|
|
676
|
+
const unsignedDoc = stripSignature(doc);
|
|
677
|
+
const payloadBytes = utf8ToBytes(canonicalJson(unsignedDoc));
|
|
678
|
+
const signatureBuffer = await crypto.subtle.sign('Ed25519', key, payloadBytes);
|
|
679
|
+
const signatureValue = bytesToBase64Url(new Uint8Array(signatureBuffer));
|
|
680
|
+
|
|
681
|
+
return {
|
|
682
|
+
...unsignedDoc,
|
|
683
|
+
signature: {
|
|
684
|
+
algorithm: 'Ed25519',
|
|
685
|
+
keyId,
|
|
686
|
+
value: signatureValue,
|
|
687
|
+
signedAt: signedAt ?? new Date().toISOString(),
|
|
688
|
+
},
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Verifies an Ed25519-signed OpenVEX document over its canonical sorted JSON payload.
|
|
694
|
+
* If `publicKeyB64` is omitted, extracts the Ed25519 public key from a self-verifying
|
|
695
|
+
* `doc.signature.keyId` (`ed25519:<base64url-spki-or-raw>`).
|
|
696
|
+
*/
|
|
697
|
+
export async function verifyOpenVex(
|
|
698
|
+
doc ,
|
|
699
|
+
publicKeyB64 ,
|
|
700
|
+
) {
|
|
701
|
+
try {
|
|
702
|
+
if (!doc || typeof doc !== 'object' || !doc.signature) return false;
|
|
703
|
+
const sig = doc.signature;
|
|
704
|
+
if (sig.algorithm !== 'Ed25519' || !sig.value || !sig.keyId) return false;
|
|
705
|
+
|
|
706
|
+
const keySource =
|
|
707
|
+
publicKeyB64 !== undefined
|
|
708
|
+
? publicKeyB64
|
|
709
|
+
: sig.keyId.startsWith('ed25519:')
|
|
710
|
+
? sig.keyId
|
|
711
|
+
: null;
|
|
712
|
+
|
|
713
|
+
if (!keySource) return false;
|
|
714
|
+
|
|
715
|
+
const key = await importPublicKey(keySource);
|
|
716
|
+
const sigBytes = base64UrlToBytes(sig.value);
|
|
717
|
+
const payloadBytes = utf8ToBytes(canonicalJson(stripSignature(doc)));
|
|
718
|
+
return await crypto.subtle.verify('Ed25519', key, sigBytes, payloadBytes);
|
|
719
|
+
} catch {
|
|
720
|
+
return false;
|
|
721
|
+
}
|
|
722
|
+
}
|