@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,1107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure TypeScript lockfile & CycloneDX SBOM parsers with PURL normalization.
|
|
3
|
+
* Supports: package-lock.json (v1/v2/v3), pnpm-lock.yaml (v6/v9), Cargo.lock,
|
|
4
|
+
* go.sum, requirements.txt, poetry.lock, pom.xml, and cyclonedx.json.
|
|
5
|
+
* Enforces SCAN_LIMITS (maxLockfileBytes, maxLockfilesPerRepo).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { SCAN_LIMITS } from './types.js';
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
import { utf8ToBytes } from './crypto-utils.js';
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
const SCOPE_PRIORITY = {
|
|
41
|
+
prod: 3,
|
|
42
|
+
optional: 2,
|
|
43
|
+
unknown: 1,
|
|
44
|
+
dev: 0,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Normalizes a PyPI package name per PEP 503 (lowercase, runs of [-_.] -> '-').
|
|
49
|
+
*/
|
|
50
|
+
export function normalizePypiName(name ) {
|
|
51
|
+
return name.trim().toLowerCase().replace(/[-_.]+/g, '-');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Builds a canonical Package URL (PURL).
|
|
56
|
+
*/
|
|
57
|
+
export function buildPurl(
|
|
58
|
+
ecosystem ,
|
|
59
|
+
name ,
|
|
60
|
+
version ,
|
|
61
|
+
namespace ,
|
|
62
|
+
) {
|
|
63
|
+
const cleanVersion = version.trim();
|
|
64
|
+
let normNs = namespace?.trim();
|
|
65
|
+
let normName = name.trim();
|
|
66
|
+
|
|
67
|
+
if (ecosystem === 'npm') {
|
|
68
|
+
if (!normNs && normName.startsWith('@') && normName.includes('/')) {
|
|
69
|
+
const slashIdx = normName.indexOf('/');
|
|
70
|
+
normNs = normName.slice(0, slashIdx);
|
|
71
|
+
normName = normName.slice(slashIdx + 1);
|
|
72
|
+
}
|
|
73
|
+
normName = normName.toLowerCase();
|
|
74
|
+
if (normNs) {
|
|
75
|
+
normNs = normNs.toLowerCase();
|
|
76
|
+
const encodedNs = normNs.startsWith('@')
|
|
77
|
+
? `%40${encodeURIComponent(normNs.slice(1))}`
|
|
78
|
+
: encodeURIComponent(normNs);
|
|
79
|
+
return `pkg:npm/${encodedNs}/${encodeURIComponent(normName)}@${encodeURIComponent(cleanVersion)}`;
|
|
80
|
+
}
|
|
81
|
+
return `pkg:npm/${encodeURIComponent(normName)}@${encodeURIComponent(cleanVersion)}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (ecosystem === 'pypi') {
|
|
85
|
+
normName = normalizePypiName(normName);
|
|
86
|
+
return `pkg:pypi/${encodeURIComponent(normName)}@${encodeURIComponent(cleanVersion)}`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (ecosystem === 'cargo') {
|
|
90
|
+
return `pkg:cargo/${encodeURIComponent(normName)}@${encodeURIComponent(cleanVersion)}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (ecosystem === 'maven') {
|
|
94
|
+
if (!normNs && normName.includes(':')) {
|
|
95
|
+
const colonIdx = normName.indexOf(':');
|
|
96
|
+
normNs = normName.slice(0, colonIdx);
|
|
97
|
+
normName = normName.slice(colonIdx + 1);
|
|
98
|
+
}
|
|
99
|
+
if (normNs) {
|
|
100
|
+
return `pkg:maven/${encodeURIComponent(normNs)}/${encodeURIComponent(normName)}@${encodeURIComponent(cleanVersion)}`;
|
|
101
|
+
}
|
|
102
|
+
return `pkg:maven/${encodeURIComponent(normName)}@${encodeURIComponent(cleanVersion)}`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// golang: preserve slash-delimited namespace path segments
|
|
106
|
+
if (!normNs && normName.includes('/')) {
|
|
107
|
+
const lastSlash = normName.lastIndexOf('/');
|
|
108
|
+
normNs = normName.slice(0, lastSlash);
|
|
109
|
+
normName = normName.slice(lastSlash + 1);
|
|
110
|
+
}
|
|
111
|
+
if (normNs) {
|
|
112
|
+
const encodedNs = normNs
|
|
113
|
+
.split('/')
|
|
114
|
+
.map((seg) => encodeURIComponent(seg))
|
|
115
|
+
.join('/');
|
|
116
|
+
return `pkg:golang/${encodedNs}/${encodeURIComponent(normName)}@${encodeURIComponent(cleanVersion)}`;
|
|
117
|
+
}
|
|
118
|
+
return `pkg:golang/${encodeURIComponent(normName)}@${encodeURIComponent(cleanVersion)}`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function safeDecodeUriComponent(segment ) {
|
|
122
|
+
try {
|
|
123
|
+
return decodeURIComponent(segment);
|
|
124
|
+
} catch {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Parses a canonical PURL string into its structured fields.
|
|
131
|
+
*/
|
|
132
|
+
export function parsePurl(purl ) {
|
|
133
|
+
const trimmed = purl.trim();
|
|
134
|
+
if (!trimmed.startsWith('pkg:')) return null;
|
|
135
|
+
const withoutScheme = trimmed.slice(4);
|
|
136
|
+
// Strip qualifiers (?...) and subpath (#...)
|
|
137
|
+
const beforeSubpath = withoutScheme.split('#')[0] ?? '';
|
|
138
|
+
const beforeQualifiers = beforeSubpath.split('?')[0] ?? '';
|
|
139
|
+
const firstSlash = beforeQualifiers.indexOf('/');
|
|
140
|
+
if (firstSlash === -1) return null;
|
|
141
|
+
|
|
142
|
+
const rawType = beforeQualifiers.slice(0, firstSlash).toLowerCase();
|
|
143
|
+
const ecosystem = normalizeEcosystemName(rawType);
|
|
144
|
+
if (!ecosystem) return null;
|
|
145
|
+
|
|
146
|
+
const rest = beforeQualifiers.slice(firstSlash + 1);
|
|
147
|
+
const lastSlash = rest.lastIndexOf('/');
|
|
148
|
+
const atIdx = rest.lastIndexOf('@');
|
|
149
|
+
// Treat `@` as a version delimiter only when it appears after the last `/`
|
|
150
|
+
// so versionless scoped npm PURLs like `pkg:npm/@scope/pkg` parse cleanly.
|
|
151
|
+
const hasVersion = atIdx > lastSlash && atIdx !== -1;
|
|
152
|
+
const pathPart = hasVersion ? rest.slice(0, atIdx) : rest;
|
|
153
|
+
const rawVersion = hasVersion ? rest.slice(atIdx + 1) : undefined;
|
|
154
|
+
const version = rawVersion !== undefined ? safeDecodeUriComponent(rawVersion) : undefined;
|
|
155
|
+
if (rawVersion !== undefined && version === null) return null;
|
|
156
|
+
|
|
157
|
+
if (!pathPart) return null;
|
|
158
|
+
|
|
159
|
+
const decodedSegments = [];
|
|
160
|
+
for (const seg of pathPart.split('/')) {
|
|
161
|
+
const decoded = safeDecodeUriComponent(seg);
|
|
162
|
+
if (decoded === null) return null;
|
|
163
|
+
decodedSegments.push(decoded);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (decodedSegments.length === 1) {
|
|
167
|
+
const rawName = decodedSegments[0] ?? '';
|
|
168
|
+
if (!rawName) return null;
|
|
169
|
+
const name =
|
|
170
|
+
ecosystem === 'pypi'
|
|
171
|
+
? normalizePypiName(rawName)
|
|
172
|
+
: ecosystem === 'npm'
|
|
173
|
+
? rawName.toLowerCase()
|
|
174
|
+
: rawName;
|
|
175
|
+
return {
|
|
176
|
+
ecosystem,
|
|
177
|
+
name,
|
|
178
|
+
version: version ?? undefined,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const rawName = decodedSegments[decodedSegments.length - 1] ?? '';
|
|
183
|
+
const rawNs = decodedSegments.slice(0, -1).join('/');
|
|
184
|
+
if (!rawName) return null;
|
|
185
|
+
const name =
|
|
186
|
+
ecosystem === 'pypi'
|
|
187
|
+
? normalizePypiName(rawName)
|
|
188
|
+
: ecosystem === 'npm'
|
|
189
|
+
? rawName.toLowerCase()
|
|
190
|
+
: rawName;
|
|
191
|
+
const namespace = ecosystem === 'npm' ? rawNs.toLowerCase() : rawNs;
|
|
192
|
+
return {
|
|
193
|
+
ecosystem,
|
|
194
|
+
namespace,
|
|
195
|
+
name,
|
|
196
|
+
version: version ?? undefined,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function normalizeEcosystemName(raw ) {
|
|
201
|
+
const lower = raw.trim().toLowerCase();
|
|
202
|
+
if (lower === 'npm') return 'npm';
|
|
203
|
+
if (lower === 'cargo' || lower === 'crates.io' || lower === 'rust') return 'cargo';
|
|
204
|
+
if (lower === 'golang' || lower === 'go') return 'golang';
|
|
205
|
+
if (lower === 'pypi' || lower === 'python' || lower === 'pip') return 'pypi';
|
|
206
|
+
if (lower === 'maven' || lower === 'java') return 'maven';
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Detects supported lockfile or CycloneDX SBOM kind from a file path.
|
|
212
|
+
*/
|
|
213
|
+
export function detectLockfileKind(filePath ) {
|
|
214
|
+
const normalized = filePath.replace(/\\/g, '/').trim();
|
|
215
|
+
const baseName = normalized.slice(normalized.lastIndexOf('/') + 1);
|
|
216
|
+
const lowerBase = baseName.toLowerCase();
|
|
217
|
+
|
|
218
|
+
if (lowerBase === 'package-lock.json' || lowerBase === 'npm-shrinkwrap.json') {
|
|
219
|
+
return 'package-lock.json';
|
|
220
|
+
}
|
|
221
|
+
if (lowerBase === 'pnpm-lock.yaml') {
|
|
222
|
+
return 'pnpm-lock.yaml';
|
|
223
|
+
}
|
|
224
|
+
if (lowerBase === 'cargo.lock') {
|
|
225
|
+
return 'Cargo.lock';
|
|
226
|
+
}
|
|
227
|
+
if (lowerBase === 'go.sum') {
|
|
228
|
+
return 'go.sum';
|
|
229
|
+
}
|
|
230
|
+
if (
|
|
231
|
+
lowerBase === 'requirements.txt' ||
|
|
232
|
+
(lowerBase.startsWith('requirements') && lowerBase.endsWith('.txt'))
|
|
233
|
+
) {
|
|
234
|
+
return 'requirements.txt';
|
|
235
|
+
}
|
|
236
|
+
if (lowerBase === 'poetry.lock') {
|
|
237
|
+
return 'poetry.lock';
|
|
238
|
+
}
|
|
239
|
+
if (lowerBase === 'pom.xml') {
|
|
240
|
+
return 'pom.xml';
|
|
241
|
+
}
|
|
242
|
+
if (
|
|
243
|
+
lowerBase === 'cyclonedx.json' ||
|
|
244
|
+
lowerBase === 'bom.json' ||
|
|
245
|
+
lowerBase === 'sbom.cdx.json' ||
|
|
246
|
+
lowerBase.endsWith('.cdx.json') ||
|
|
247
|
+
lowerBase.endsWith('cyclonedx.json')
|
|
248
|
+
) {
|
|
249
|
+
return 'cyclonedx.json';
|
|
250
|
+
}
|
|
251
|
+
if (
|
|
252
|
+
lowerBase === 'spdx.json' ||
|
|
253
|
+
lowerBase.endsWith('.spdx.json') ||
|
|
254
|
+
lowerBase.endsWith('spdx.json')
|
|
255
|
+
) {
|
|
256
|
+
return 'spdx.json';
|
|
257
|
+
}
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Deduplicates components by canonical PURL, retaining the highest-priority
|
|
263
|
+
* dependency scope (`prod > optional > unknown > dev`), and sorts deterministically.
|
|
264
|
+
*/
|
|
265
|
+
export function deduplicateComponents(components ) {
|
|
266
|
+
const map = new Map ();
|
|
267
|
+
for (const comp of components) {
|
|
268
|
+
const existing = map.get(comp.purl);
|
|
269
|
+
if (!existing) {
|
|
270
|
+
map.set(comp.purl, comp);
|
|
271
|
+
} else if (SCOPE_PRIORITY[comp.scope] > SCOPE_PRIORITY[existing.scope]) {
|
|
272
|
+
map.set(comp.purl, comp);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return Array.from(map.values()).sort((a, b) => a.purl.localeCompare(b.purl));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function makeComponent(
|
|
279
|
+
ecosystem ,
|
|
280
|
+
name ,
|
|
281
|
+
version ,
|
|
282
|
+
scope ,
|
|
283
|
+
sourceFile ,
|
|
284
|
+
namespace ,
|
|
285
|
+
) {
|
|
286
|
+
let finalNs = namespace?.trim() || undefined;
|
|
287
|
+
let finalName = name.trim();
|
|
288
|
+
|
|
289
|
+
if (ecosystem === 'npm' && !finalNs && finalName.startsWith('@') && finalName.includes('/')) {
|
|
290
|
+
const idx = finalName.indexOf('/');
|
|
291
|
+
finalNs = finalName.slice(0, idx).toLowerCase();
|
|
292
|
+
finalName = finalName.slice(idx + 1).toLowerCase();
|
|
293
|
+
} else if (ecosystem === 'npm') {
|
|
294
|
+
finalName = finalName.toLowerCase();
|
|
295
|
+
if (finalNs) finalNs = finalNs.toLowerCase();
|
|
296
|
+
} else if (ecosystem === 'pypi') {
|
|
297
|
+
finalName = normalizePypiName(finalName);
|
|
298
|
+
} else if (ecosystem === 'golang' && !finalNs && finalName.includes('/')) {
|
|
299
|
+
const idx = finalName.lastIndexOf('/');
|
|
300
|
+
finalNs = finalName.slice(0, idx);
|
|
301
|
+
finalName = finalName.slice(idx + 1);
|
|
302
|
+
} else if (ecosystem === 'maven' && !finalNs && finalName.includes(':')) {
|
|
303
|
+
const idx = finalName.indexOf(':');
|
|
304
|
+
finalNs = finalName.slice(0, idx);
|
|
305
|
+
finalName = finalName.slice(idx + 1);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const purl = buildPurl(ecosystem, finalName, version.trim(), finalNs);
|
|
309
|
+
const comp = {
|
|
310
|
+
purl,
|
|
311
|
+
ecosystem,
|
|
312
|
+
name: finalName,
|
|
313
|
+
version: version.trim(),
|
|
314
|
+
scope,
|
|
315
|
+
sourceFile,
|
|
316
|
+
};
|
|
317
|
+
if (finalNs) {
|
|
318
|
+
comp.namespace = finalNs;
|
|
319
|
+
}
|
|
320
|
+
return comp;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// ---------------------------------------------------------------------------
|
|
324
|
+
// Individual Format Parsers
|
|
325
|
+
// ---------------------------------------------------------------------------
|
|
326
|
+
|
|
327
|
+
function extractNpmNameFromPath(pkgPath ) {
|
|
328
|
+
const marker = 'node_modules/';
|
|
329
|
+
const idx = pkgPath.lastIndexOf(marker);
|
|
330
|
+
if (idx === -1) return null;
|
|
331
|
+
const sub = pkgPath.slice(idx + marker.length);
|
|
332
|
+
return sub.length > 0 ? sub : null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function parsePackageLockJson(content , sourceFile ) {
|
|
336
|
+
let parsed ;
|
|
337
|
+
try {
|
|
338
|
+
parsed = JSON.parse(content);
|
|
339
|
+
} catch (err) {
|
|
340
|
+
throw new Error(
|
|
341
|
+
`Invalid package-lock.json in ${sourceFile}: ${err instanceof Error ? err.message : String(err)}`,
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
345
|
+
throw new Error(`Invalid package-lock.json root in ${sourceFile}`);
|
|
346
|
+
}
|
|
347
|
+
const root = parsed ;
|
|
348
|
+
const results = [];
|
|
349
|
+
|
|
350
|
+
// Lockfile v2 / v3 format (`packages` map)
|
|
351
|
+
if (root.packages && typeof root.packages === 'object' && !Array.isArray(root.packages)) {
|
|
352
|
+
const packages = root.packages ;
|
|
353
|
+
for (const [pkgPath, rawEntry] of Object.entries(packages)) {
|
|
354
|
+
if (pkgPath === '' || !rawEntry || typeof rawEntry !== 'object') continue;
|
|
355
|
+
const entry = rawEntry ;
|
|
356
|
+
if (entry.link === true) continue;
|
|
357
|
+
const version = typeof entry.version === 'string' ? entry.version.trim() : '';
|
|
358
|
+
if (!version) continue;
|
|
359
|
+
|
|
360
|
+
const name =
|
|
361
|
+
typeof entry.name === 'string' && entry.name.trim().length > 0
|
|
362
|
+
? entry.name.trim()
|
|
363
|
+
: extractNpmNameFromPath(pkgPath);
|
|
364
|
+
if (!name) continue;
|
|
365
|
+
|
|
366
|
+
let scope = 'prod';
|
|
367
|
+
if (entry.dev === true || entry.devOptional === true) {
|
|
368
|
+
scope = 'dev';
|
|
369
|
+
} else if (entry.optional === true) {
|
|
370
|
+
scope = 'optional';
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
results.push(makeComponent('npm', name, version, scope, sourceFile));
|
|
374
|
+
}
|
|
375
|
+
return deduplicateComponents(results);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Fallback for lockfile v1 format (`dependencies` tree)
|
|
379
|
+
const walkDeps = (depsObj , parentScope ) => {
|
|
380
|
+
for (const [name, rawEntry] of Object.entries(depsObj)) {
|
|
381
|
+
if (!rawEntry || typeof rawEntry !== 'object') continue;
|
|
382
|
+
const entry = rawEntry ;
|
|
383
|
+
const version = typeof entry.version === 'string' ? entry.version.trim() : '';
|
|
384
|
+
let scope = parentScope;
|
|
385
|
+
if (entry.dev === true) {
|
|
386
|
+
scope = 'dev';
|
|
387
|
+
} else if (entry.optional === true && scope !== 'dev') {
|
|
388
|
+
scope = 'optional';
|
|
389
|
+
}
|
|
390
|
+
if (version) {
|
|
391
|
+
results.push(makeComponent('npm', name, version, scope, sourceFile));
|
|
392
|
+
}
|
|
393
|
+
if (entry.dependencies && typeof entry.dependencies === 'object') {
|
|
394
|
+
walkDeps(entry.dependencies , scope);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
if (root.dependencies && typeof root.dependencies === 'object') {
|
|
400
|
+
walkDeps(root.dependencies , 'prod');
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
return deduplicateComponents(results);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function parsePnpmPackageKey(rawKey ) {
|
|
407
|
+
let key = rawKey.trim();
|
|
408
|
+
// Strip surrounding YAML quotes
|
|
409
|
+
if ((key.startsWith("'") && key.endsWith("'")) || (key.startsWith('"') && key.endsWith('"'))) {
|
|
410
|
+
key = key.slice(1, -1);
|
|
411
|
+
}
|
|
412
|
+
// Strip leading slash (pnpm v5/v6)
|
|
413
|
+
if (key.startsWith('/')) {
|
|
414
|
+
key = key.slice(1);
|
|
415
|
+
}
|
|
416
|
+
// Strip peer dependency suffixes: `(react@18.2.0)` or `_react@18.2.0`
|
|
417
|
+
const parenIdx = key.indexOf('(');
|
|
418
|
+
if (parenIdx !== -1) {
|
|
419
|
+
key = key.slice(0, parenIdx);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Support both `name@version` (v6/v9) and `name/version` (v5)
|
|
423
|
+
const searchStart = key.startsWith('@') ? key.indexOf('/') + 1 : 0;
|
|
424
|
+
if (key.startsWith('@') && searchStart === 0) return null;
|
|
425
|
+
|
|
426
|
+
const atIdx = key.indexOf('@', searchStart);
|
|
427
|
+
if (atIdx !== -1) {
|
|
428
|
+
const name = key.slice(0, atIdx);
|
|
429
|
+
let version = key.slice(atIdx + 1);
|
|
430
|
+
const underscoreIdx = version.indexOf('_');
|
|
431
|
+
if (underscoreIdx !== -1) {
|
|
432
|
+
version = version.slice(0, underscoreIdx);
|
|
433
|
+
}
|
|
434
|
+
if (!name || !version) return null;
|
|
435
|
+
return { name, version };
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// v5 format: `lodash/4.17.21` or `@scope/pkg/1.2.3`
|
|
439
|
+
const slashIdx = key.indexOf('/', searchStart);
|
|
440
|
+
if (slashIdx !== -1) {
|
|
441
|
+
const name = key.slice(0, slashIdx);
|
|
442
|
+
let version = key.slice(slashIdx + 1);
|
|
443
|
+
const underscoreIdx = version.indexOf('_');
|
|
444
|
+
if (underscoreIdx !== -1) {
|
|
445
|
+
version = version.slice(0, underscoreIdx);
|
|
446
|
+
}
|
|
447
|
+
if (!name || !version) return null;
|
|
448
|
+
return { name, version };
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return null;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function parsePnpmLockYaml(content , sourceFile ) {
|
|
455
|
+
const lines = content.split(/\r?\n/);
|
|
456
|
+
const results = [];
|
|
457
|
+
const devDirectNames = new Set ();
|
|
458
|
+
const prodDirectNames = new Set ();
|
|
459
|
+
|
|
460
|
+
// Pass 1: detect direct dependency groups in top-level or importers section
|
|
461
|
+
let topSection = '';
|
|
462
|
+
let importerSubSection = '';
|
|
463
|
+
for (const rawLine of lines) {
|
|
464
|
+
const line = rawLine.replace(/\t/g, ' ');
|
|
465
|
+
if (line.trim() === '' || line.trim().startsWith('#')) continue;
|
|
466
|
+
const indent = line.length - line.trimStart().length;
|
|
467
|
+
const trimmed = line.trim();
|
|
468
|
+
|
|
469
|
+
if (indent === 0) {
|
|
470
|
+
topSection = trimmed.endsWith(':') ? trimmed.slice(0, -1) : trimmed;
|
|
471
|
+
importerSubSection = '';
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
if (topSection === 'dependencies' || topSection === 'devDependencies') {
|
|
476
|
+
if (indent === 2 && trimmed.endsWith(':')) {
|
|
477
|
+
const pkgName = trimmed.slice(0, -1).replace(/^['"]|['"]$/g, '');
|
|
478
|
+
if (topSection === 'devDependencies') devDirectNames.add(pkgName);
|
|
479
|
+
else prodDirectNames.add(pkgName);
|
|
480
|
+
}
|
|
481
|
+
} else if (topSection === 'importers') {
|
|
482
|
+
if (indent === 4 && trimmed.endsWith(':')) {
|
|
483
|
+
importerSubSection = trimmed.slice(0, -1);
|
|
484
|
+
} else if (indent === 6 && trimmed.endsWith(':')) {
|
|
485
|
+
const pkgName = trimmed.slice(0, -1).replace(/^['"]|['"]$/g, '');
|
|
486
|
+
if (importerSubSection === 'devDependencies') devDirectNames.add(pkgName);
|
|
487
|
+
else if (importerSubSection === 'dependencies') prodDirectNames.add(pkgName);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// Pass 2: parse `packages:` section entries
|
|
493
|
+
let inPackages = false;
|
|
494
|
+
let currentPkg = null;
|
|
495
|
+
|
|
496
|
+
const flushCurrent = () => {
|
|
497
|
+
if (!currentPkg) return;
|
|
498
|
+
let scope = 'prod';
|
|
499
|
+
if (currentPkg.dev) {
|
|
500
|
+
scope = 'dev';
|
|
501
|
+
} else if (currentPkg.optional) {
|
|
502
|
+
scope = 'optional';
|
|
503
|
+
} else if (devDirectNames.has(currentPkg.name) && !prodDirectNames.has(currentPkg.name)) {
|
|
504
|
+
scope = 'dev';
|
|
505
|
+
}
|
|
506
|
+
results.push(makeComponent('npm', currentPkg.name, currentPkg.version, scope, sourceFile));
|
|
507
|
+
currentPkg = null;
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
for (const rawLine of lines) {
|
|
511
|
+
const line = rawLine.replace(/\t/g, ' ');
|
|
512
|
+
const trimmed = line.trim();
|
|
513
|
+
if (trimmed === '' || trimmed.startsWith('#')) continue;
|
|
514
|
+
const indent = line.length - line.trimStart().length;
|
|
515
|
+
|
|
516
|
+
if (indent === 0) {
|
|
517
|
+
if (inPackages) flushCurrent();
|
|
518
|
+
inPackages = trimmed === 'packages:';
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (!inPackages) continue;
|
|
523
|
+
|
|
524
|
+
if (indent === 2 && trimmed.endsWith(':')) {
|
|
525
|
+
flushCurrent();
|
|
526
|
+
const keyPart = trimmed.slice(0, -1);
|
|
527
|
+
const parsed = parsePnpmPackageKey(keyPart);
|
|
528
|
+
if (parsed) {
|
|
529
|
+
currentPkg = { name: parsed.name, version: parsed.version, dev: false, optional: false };
|
|
530
|
+
}
|
|
531
|
+
} else if (currentPkg && indent >= 4) {
|
|
532
|
+
if (trimmed === 'dev: true') {
|
|
533
|
+
currentPkg.dev = true;
|
|
534
|
+
} else if (trimmed === 'optional: true') {
|
|
535
|
+
currentPkg.optional = true;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
flushCurrent();
|
|
540
|
+
|
|
541
|
+
return deduplicateComponents(results);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function unquoteTomlString(raw ) {
|
|
545
|
+
const trimmed = raw.trim();
|
|
546
|
+
if (
|
|
547
|
+
(trimmed.startsWith('"') && trimmed.endsWith('"')) ||
|
|
548
|
+
(trimmed.startsWith("'") && trimmed.endsWith("'"))
|
|
549
|
+
) {
|
|
550
|
+
return trimmed.slice(1, -1);
|
|
551
|
+
}
|
|
552
|
+
return trimmed;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function parseCargoLock(content , sourceFile ) {
|
|
556
|
+
const lines = content.split(/\r?\n/);
|
|
557
|
+
const results = [];
|
|
558
|
+
let inPackage = false;
|
|
559
|
+
let name = '';
|
|
560
|
+
let version = '';
|
|
561
|
+
|
|
562
|
+
const flush = () => {
|
|
563
|
+
if (inPackage && name && version) {
|
|
564
|
+
results.push(makeComponent('cargo', name, version, 'prod', sourceFile));
|
|
565
|
+
}
|
|
566
|
+
name = '';
|
|
567
|
+
version = '';
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
for (const rawLine of lines) {
|
|
571
|
+
const trimmed = rawLine.trim();
|
|
572
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
573
|
+
|
|
574
|
+
if (trimmed === '[[package]]') {
|
|
575
|
+
flush();
|
|
576
|
+
inPackage = true;
|
|
577
|
+
continue;
|
|
578
|
+
}
|
|
579
|
+
if (trimmed.startsWith('[')) {
|
|
580
|
+
flush();
|
|
581
|
+
inPackage = false;
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
if (!inPackage) continue;
|
|
585
|
+
|
|
586
|
+
const eqIdx = trimmed.indexOf('=');
|
|
587
|
+
if (eqIdx === -1) continue;
|
|
588
|
+
const key = trimmed.slice(0, eqIdx).trim();
|
|
589
|
+
const value = unquoteTomlString(trimmed.slice(eqIdx + 1));
|
|
590
|
+
if (key === 'name') name = value;
|
|
591
|
+
else if (key === 'version') version = value;
|
|
592
|
+
}
|
|
593
|
+
flush();
|
|
594
|
+
|
|
595
|
+
return deduplicateComponents(results);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function parseGoSum(content , sourceFile ) {
|
|
599
|
+
const lines = content.split(/\r?\n/);
|
|
600
|
+
const results = [];
|
|
601
|
+
|
|
602
|
+
for (const rawLine of lines) {
|
|
603
|
+
const trimmed = rawLine.trim();
|
|
604
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
605
|
+
const parts = trimmed.split(/\s+/);
|
|
606
|
+
if (parts.length < 2) continue;
|
|
607
|
+
const modPath = parts[0] ?? '';
|
|
608
|
+
let version = parts[1] ?? '';
|
|
609
|
+
if (!modPath || !version) continue;
|
|
610
|
+
|
|
611
|
+
if (version.endsWith('/go.mod')) {
|
|
612
|
+
version = version.slice(0, -'/go.mod'.length);
|
|
613
|
+
}
|
|
614
|
+
if (!version) continue;
|
|
615
|
+
results.push(makeComponent('golang', modPath, version, 'prod', sourceFile));
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
return deduplicateComponents(results);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function parseRequirementsTxt(content , sourceFile ) {
|
|
622
|
+
const lines = content.split(/\r?\n/);
|
|
623
|
+
const results = [];
|
|
624
|
+
|
|
625
|
+
// Join backslash-continued lines
|
|
626
|
+
const joinedLines = [];
|
|
627
|
+
let buffer = '';
|
|
628
|
+
for (const rawLine of lines) {
|
|
629
|
+
const stripped = rawLine.trimEnd();
|
|
630
|
+
if (stripped.endsWith('\\')) {
|
|
631
|
+
buffer += stripped.slice(0, -1).trim() + ' ';
|
|
632
|
+
} else {
|
|
633
|
+
joinedLines.push((buffer + stripped).trim());
|
|
634
|
+
buffer = '';
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
if (buffer.trim()) joinedLines.push(buffer.trim());
|
|
638
|
+
|
|
639
|
+
const reqRegex = /^([A-Za-z0-9][A-Za-z0-9._-]*)(?:\[[^\]]*\])?\s*(===|==|~=|>=)\s*([A-Za-z0-9.*+!_-]+)/;
|
|
640
|
+
|
|
641
|
+
for (const line of joinedLines) {
|
|
642
|
+
if (!line || line.startsWith('#') || line.startsWith('-')) continue;
|
|
643
|
+
// Strip inline comments and environment markers
|
|
644
|
+
const withoutComment = (line.split('#')[0] ?? '').trim();
|
|
645
|
+
const withoutMarker = (withoutComment.split(';')[0] ?? '').trim();
|
|
646
|
+
if (!withoutMarker) continue;
|
|
647
|
+
// Strip trailing `--hash=...`
|
|
648
|
+
const withoutOptions = (withoutMarker.split(/\s+--/)[0] ?? '').trim();
|
|
649
|
+
const match = reqRegex.exec(withoutOptions);
|
|
650
|
+
if (!match) continue;
|
|
651
|
+
const pkgName = match[1];
|
|
652
|
+
const version = match[3];
|
|
653
|
+
if (!pkgName || !version) continue;
|
|
654
|
+
results.push(makeComponent('pypi', pkgName, version, 'prod', sourceFile));
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
return deduplicateComponents(results);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function parsePoetryLock(content , sourceFile ) {
|
|
661
|
+
const lines = content.split(/\r?\n/);
|
|
662
|
+
const results = [];
|
|
663
|
+
let inPackage = false;
|
|
664
|
+
let name = '';
|
|
665
|
+
let version = '';
|
|
666
|
+
let category = '';
|
|
667
|
+
let groups = [];
|
|
668
|
+
let optional = false;
|
|
669
|
+
|
|
670
|
+
const flush = () => {
|
|
671
|
+
if (inPackage && name && version) {
|
|
672
|
+
let scope = 'prod';
|
|
673
|
+
const lowerCat = category.toLowerCase();
|
|
674
|
+
if (
|
|
675
|
+
lowerCat === 'dev' ||
|
|
676
|
+
(groups.length > 0 && groups.every((g) => g === 'dev' || g === 'test' || g === 'lint'))
|
|
677
|
+
) {
|
|
678
|
+
scope = 'dev';
|
|
679
|
+
} else if (optional) {
|
|
680
|
+
scope = 'optional';
|
|
681
|
+
}
|
|
682
|
+
results.push(makeComponent('pypi', name, version, scope, sourceFile));
|
|
683
|
+
}
|
|
684
|
+
name = '';
|
|
685
|
+
version = '';
|
|
686
|
+
category = '';
|
|
687
|
+
groups = [];
|
|
688
|
+
optional = false;
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
for (const rawLine of lines) {
|
|
692
|
+
const trimmed = rawLine.trim();
|
|
693
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
694
|
+
|
|
695
|
+
if (trimmed === '[[package]]') {
|
|
696
|
+
flush();
|
|
697
|
+
inPackage = true;
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
if (trimmed.startsWith('[')) {
|
|
701
|
+
// Sub-tables like `[package.dependencies]` belong to the current package but shouldn't overwrite name/version
|
|
702
|
+
if (!trimmed.startsWith('[package.')) {
|
|
703
|
+
flush();
|
|
704
|
+
inPackage = false;
|
|
705
|
+
} else {
|
|
706
|
+
// Flush current package header fields before sub-table so sub-table keys aren't confused
|
|
707
|
+
flush();
|
|
708
|
+
inPackage = false;
|
|
709
|
+
}
|
|
710
|
+
continue;
|
|
711
|
+
}
|
|
712
|
+
if (!inPackage) continue;
|
|
713
|
+
|
|
714
|
+
const eqIdx = trimmed.indexOf('=');
|
|
715
|
+
if (eqIdx === -1) continue;
|
|
716
|
+
const key = trimmed.slice(0, eqIdx).trim();
|
|
717
|
+
const rawVal = trimmed.slice(eqIdx + 1).trim();
|
|
718
|
+
|
|
719
|
+
if (key === 'name') name = unquoteTomlString(rawVal);
|
|
720
|
+
else if (key === 'version') version = unquoteTomlString(rawVal);
|
|
721
|
+
else if (key === 'category') category = unquoteTomlString(rawVal);
|
|
722
|
+
else if (key === 'optional') optional = rawVal === 'true';
|
|
723
|
+
else if (key === 'groups' && rawVal.startsWith('[') && rawVal.endsWith(']')) {
|
|
724
|
+
groups = rawVal
|
|
725
|
+
.slice(1, -1)
|
|
726
|
+
.split(',')
|
|
727
|
+
.map((item) => unquoteTomlString(item).toLowerCase())
|
|
728
|
+
.filter((item) => item.length > 0);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
flush();
|
|
732
|
+
|
|
733
|
+
return deduplicateComponents(results);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
export const MAX_POM_DEPENDENCIES = 5_000;
|
|
737
|
+
|
|
738
|
+
function parsePomXml(content , sourceFile ) {
|
|
739
|
+
// Strip XML comments
|
|
740
|
+
const cleaned = content.replace(/<!--[\s\S]*?-->/g, '');
|
|
741
|
+
const props = new Map ();
|
|
742
|
+
|
|
743
|
+
// Strip non-project dependency sections (<parent>, <build>, <dependencyManagement>,
|
|
744
|
+
// <pluginManagement>, <reporting>) before resolving project.version and scanning dependencies.
|
|
745
|
+
const withoutAuxiliarySections = cleaned
|
|
746
|
+
.replace(/<parent>[\s\S]*?<\/parent>/g, '')
|
|
747
|
+
.replace(/<dependencyManagement>[\s\S]*?<\/dependencyManagement>/g, '')
|
|
748
|
+
.replace(/<pluginManagement>[\s\S]*?<\/pluginManagement>/g, '')
|
|
749
|
+
.replace(/<build>[\s\S]*?<\/build>/g, '')
|
|
750
|
+
.replace(/<reporting>[\s\S]*?<\/reporting>/g, '');
|
|
751
|
+
|
|
752
|
+
// Extract project version if present at top level (ignoring <parent>, <build>, <dependencies>)
|
|
753
|
+
const withoutDeps = withoutAuxiliarySections.replace(/<dependencies>[\s\S]*?<\/dependencies>/g, '');
|
|
754
|
+
const projectVerMatch = /<version>([^<]+)<\/version>/.exec(withoutDeps);
|
|
755
|
+
if (projectVerMatch?.[1]) {
|
|
756
|
+
props.set('project.version', projectVerMatch[1].trim());
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
// Extract <properties>...</properties>
|
|
760
|
+
const propsBlockMatch = /<properties>([\s\S]*?)<\/properties>/.exec(withoutAuxiliarySections);
|
|
761
|
+
if (propsBlockMatch?.[1]) {
|
|
762
|
+
const propRegex = /<([A-Za-z0-9._-]+)>([^<]*)<\/\1>/g;
|
|
763
|
+
let m ;
|
|
764
|
+
while ((m = propRegex.exec(propsBlockMatch[1])) !== null) {
|
|
765
|
+
if (m[1] && m[2] !== undefined) {
|
|
766
|
+
props.set(m[1].trim(), m[2].trim());
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
const resolveProps = (val ) =>
|
|
772
|
+
val.replace(/\$\{([^}]+)\}/g, (_, key ) => props.get(key.trim()) ?? `\${${key}}`);
|
|
773
|
+
|
|
774
|
+
const results = [];
|
|
775
|
+
const depRegex = /<dependency>([\s\S]*?)<\/dependency>/g;
|
|
776
|
+
let match ;
|
|
777
|
+
let depTagCount = 0;
|
|
778
|
+
|
|
779
|
+
while ((match = depRegex.exec(withoutAuxiliarySections)) !== null) {
|
|
780
|
+
depTagCount++;
|
|
781
|
+
if (depTagCount > MAX_POM_DEPENDENCIES) {
|
|
782
|
+
throw new Error(
|
|
783
|
+
`pom.xml ${sourceFile} exceeds MAX_POM_DEPENDENCIES (${MAX_POM_DEPENDENCIES})`,
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
const block = match[1] ?? '';
|
|
787
|
+
const groupMatch = /<groupId>([^<]+)<\/groupId>/.exec(block);
|
|
788
|
+
const artifactMatch = /<artifactId>([^<]+)<\/artifactId>/.exec(block);
|
|
789
|
+
const versionMatch = /<version>([^<]+)<\/version>/.exec(block);
|
|
790
|
+
const scopeMatch = /<scope>([^<]+)<\/scope>/.exec(block);
|
|
791
|
+
const optionalMatch = /<optional>([^<]+)<\/optional>/.exec(block);
|
|
792
|
+
|
|
793
|
+
const groupId = groupMatch?.[1] ? resolveProps(groupMatch[1].trim()) : '';
|
|
794
|
+
const artifactId = artifactMatch?.[1] ? resolveProps(artifactMatch[1].trim()) : '';
|
|
795
|
+
const version = versionMatch?.[1] ? resolveProps(versionMatch[1].trim()) : '';
|
|
796
|
+
|
|
797
|
+
if (!groupId || !artifactId || !version || version.includes('${')) continue;
|
|
798
|
+
|
|
799
|
+
const rawScope = scopeMatch?.[1]?.trim().toLowerCase() ?? 'compile';
|
|
800
|
+
const isOptional = optionalMatch?.[1]?.trim().toLowerCase() === 'true';
|
|
801
|
+
|
|
802
|
+
let scope = 'prod';
|
|
803
|
+
if (rawScope === 'test' || rawScope === 'provided' || rawScope === 'system') {
|
|
804
|
+
scope = 'dev';
|
|
805
|
+
} else if (isOptional) {
|
|
806
|
+
scope = 'optional';
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
results.push(makeComponent('maven', artifactId, version, scope, sourceFile, groupId));
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
return deduplicateComponents(results);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
function parseCycloneDxJson(content , sourceFile ) {
|
|
816
|
+
let parsed ;
|
|
817
|
+
try {
|
|
818
|
+
parsed = JSON.parse(content);
|
|
819
|
+
} catch (err) {
|
|
820
|
+
throw new Error(
|
|
821
|
+
`Invalid CycloneDX JSON in ${sourceFile}: ${err instanceof Error ? err.message : String(err)}`,
|
|
822
|
+
);
|
|
823
|
+
}
|
|
824
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
825
|
+
throw new Error(`Invalid CycloneDX JSON root in ${sourceFile}`);
|
|
826
|
+
}
|
|
827
|
+
const root = parsed ;
|
|
828
|
+
if (root.bomFormat !== 'CycloneDX') {
|
|
829
|
+
throw new Error(`Expected bomFormat "CycloneDX" in ${sourceFile}`);
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
const rawComponents = Array.isArray(root.components) ? root.components : [];
|
|
833
|
+
const results = [];
|
|
834
|
+
|
|
835
|
+
for (const item of rawComponents) {
|
|
836
|
+
if (!item || typeof item !== 'object') continue;
|
|
837
|
+
const comp = item ;
|
|
838
|
+
const rawScope = typeof comp.scope === 'string' ? comp.scope.toLowerCase() : 'required';
|
|
839
|
+
let scope = 'prod';
|
|
840
|
+
if (rawScope === 'excluded') scope = 'dev';
|
|
841
|
+
else if (rawScope === 'optional') scope = 'optional';
|
|
842
|
+
|
|
843
|
+
if (typeof comp.purl === 'string' && comp.purl.trim().length > 0) {
|
|
844
|
+
const parsedPurl = parsePurl(comp.purl);
|
|
845
|
+
if (parsedPurl && parsedPurl.version) {
|
|
846
|
+
results.push(
|
|
847
|
+
makeComponent(
|
|
848
|
+
parsedPurl.ecosystem,
|
|
849
|
+
parsedPurl.name,
|
|
850
|
+
parsedPurl.version,
|
|
851
|
+
scope,
|
|
852
|
+
sourceFile,
|
|
853
|
+
parsedPurl.namespace,
|
|
854
|
+
),
|
|
855
|
+
);
|
|
856
|
+
continue;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// Fallback: build from group/name/version if ecosystem property is present
|
|
861
|
+
const name = typeof comp.name === 'string' ? comp.name.trim() : '';
|
|
862
|
+
const version = typeof comp.version === 'string' ? comp.version.trim() : '';
|
|
863
|
+
const group = typeof comp.group === 'string' ? comp.group.trim() : undefined;
|
|
864
|
+
if (!name || !version) continue;
|
|
865
|
+
|
|
866
|
+
let ecosystem = null;
|
|
867
|
+
if (Array.isArray(comp.properties)) {
|
|
868
|
+
for (const prop of comp.properties) {
|
|
869
|
+
if (
|
|
870
|
+
prop &&
|
|
871
|
+
typeof prop === 'object' &&
|
|
872
|
+
(prop ).name === 'cra-switch:ecosystem' &&
|
|
873
|
+
typeof (prop ).value === 'string'
|
|
874
|
+
) {
|
|
875
|
+
ecosystem = normalizeEcosystemName((prop ).value );
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
if (ecosystem) {
|
|
880
|
+
results.push(makeComponent(ecosystem, name, version, scope, sourceFile, group));
|
|
881
|
+
} else if (typeof comp.purl === 'string' && comp.purl.trim().startsWith('pkg:')) {
|
|
882
|
+
results.push({
|
|
883
|
+
purl: comp.purl.trim(),
|
|
884
|
+
ecosystem: 'generic' ,
|
|
885
|
+
name,
|
|
886
|
+
version,
|
|
887
|
+
...(group ? { namespace: group } : {}),
|
|
888
|
+
scope,
|
|
889
|
+
sourceFile,
|
|
890
|
+
});
|
|
891
|
+
} else {
|
|
892
|
+
const genericPurl = group
|
|
893
|
+
? `pkg:generic/${encodeURIComponent(group)}/${encodeURIComponent(name)}@${encodeURIComponent(version)}`
|
|
894
|
+
: `pkg:generic/${encodeURIComponent(name)}@${encodeURIComponent(version)}`;
|
|
895
|
+
results.push({
|
|
896
|
+
purl: genericPurl,
|
|
897
|
+
ecosystem: 'generic' ,
|
|
898
|
+
name,
|
|
899
|
+
version,
|
|
900
|
+
...(group ? { namespace: group } : {}),
|
|
901
|
+
scope,
|
|
902
|
+
sourceFile,
|
|
903
|
+
});
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
return deduplicateComponents(results);
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* Parses an SPDX 2.3 (or 2.2) JSON SBOM, extracting `packages[].externalRefs`
|
|
912
|
+
* (`referenceType === 'purl'`) and falling back to `pkg:generic/<name>@<version>`.
|
|
913
|
+
* Enforces `SCAN_LIMITS.maxLockfileBytes`.
|
|
914
|
+
*/
|
|
915
|
+
export function parseSpdxSbom(rawJson , sourceFile = 'spdx.json') {
|
|
916
|
+
const byteLength = utf8ToBytes(rawJson).byteLength;
|
|
917
|
+
if (byteLength > SCAN_LIMITS.maxLockfileBytes) {
|
|
918
|
+
throw new Error(
|
|
919
|
+
`SPDX SBOM ${sourceFile} exceeds SCAN_LIMITS.maxLockfileBytes (${byteLength} > ${SCAN_LIMITS.maxLockfileBytes})`,
|
|
920
|
+
);
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
let parsed ;
|
|
924
|
+
try {
|
|
925
|
+
parsed = JSON.parse(rawJson);
|
|
926
|
+
} catch (err) {
|
|
927
|
+
throw new Error(
|
|
928
|
+
`Invalid SPDX JSON in ${sourceFile}: ${err instanceof Error ? err.message : String(err)}`,
|
|
929
|
+
);
|
|
930
|
+
}
|
|
931
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
932
|
+
throw new Error(`Invalid SPDX JSON root in ${sourceFile}`);
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
const root = parsed ;
|
|
936
|
+
const spdxVersion = typeof root.spdxVersion === 'string' ? root.spdxVersion.trim() : '';
|
|
937
|
+
const spdxId = typeof root.SPDXID === 'string' ? root.SPDXID.trim() : '';
|
|
938
|
+
if (!spdxVersion.toUpperCase().startsWith('SPDX-') && spdxId !== 'SPDXRef-DOCUMENT') {
|
|
939
|
+
throw new Error(`Expected SPDX document (spdxVersion "SPDX-2.x") in ${sourceFile}`);
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
const rawPackages = Array.isArray(root.packages) ? root.packages : [];
|
|
943
|
+
const docDescribes = new Set (
|
|
944
|
+
Array.isArray(root.documentDescribes)
|
|
945
|
+
? root.documentDescribes.filter((x) => typeof x === 'string')
|
|
946
|
+
: [],
|
|
947
|
+
);
|
|
948
|
+
|
|
949
|
+
const results = [];
|
|
950
|
+
for (const item of rawPackages) {
|
|
951
|
+
if (!item || typeof item !== 'object') continue;
|
|
952
|
+
const pkg = item ;
|
|
953
|
+
const pkgSpdxId = typeof pkg.SPDXID === 'string' ? pkg.SPDXID.trim() : '';
|
|
954
|
+
const pkgName = typeof pkg.name === 'string' ? pkg.name.trim() : '';
|
|
955
|
+
const pkgVersion = typeof pkg.versionInfo === 'string' ? pkg.versionInfo.trim() : '';
|
|
956
|
+
|
|
957
|
+
// Extract PURL from externalRefs (`referenceType === 'purl'`)
|
|
958
|
+
let rawPurl ;
|
|
959
|
+
if (Array.isArray(pkg.externalRefs)) {
|
|
960
|
+
for (const ref of pkg.externalRefs) {
|
|
961
|
+
if (!ref || typeof ref !== 'object') continue;
|
|
962
|
+
const r = ref ;
|
|
963
|
+
if (
|
|
964
|
+
typeof r.referenceType === 'string' &&
|
|
965
|
+
r.referenceType.trim().toLowerCase() === 'purl' &&
|
|
966
|
+
typeof r.referenceLocator === 'string' &&
|
|
967
|
+
r.referenceLocator.trim().startsWith('pkg:')
|
|
968
|
+
) {
|
|
969
|
+
rawPurl = r.referenceLocator.trim();
|
|
970
|
+
break;
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
// Skip top-level described document package when it has no external PURL and no versionInfo
|
|
976
|
+
if (!rawPurl && (!pkgName || !pkgVersion)) {
|
|
977
|
+
continue;
|
|
978
|
+
}
|
|
979
|
+
if (!rawPurl && pkgSpdxId && docDescribes.has(pkgSpdxId) && rawPackages.length > 1) {
|
|
980
|
+
// If root package has no purl and there are child packages, skip the root container entry
|
|
981
|
+
continue;
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
if (rawPurl) {
|
|
985
|
+
const parsedPurl = parsePurl(rawPurl);
|
|
986
|
+
if (parsedPurl) {
|
|
987
|
+
const version = parsedPurl.version ?? pkgVersion;
|
|
988
|
+
if (version) {
|
|
989
|
+
results.push(
|
|
990
|
+
makeComponent(
|
|
991
|
+
parsedPurl.ecosystem,
|
|
992
|
+
parsedPurl.name,
|
|
993
|
+
version,
|
|
994
|
+
'prod',
|
|
995
|
+
sourceFile,
|
|
996
|
+
parsedPurl.namespace,
|
|
997
|
+
),
|
|
998
|
+
);
|
|
999
|
+
continue;
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
// Preserve non-5-ecosystem PURLs (e.g. pkg:generic/..., pkg:deb/..., pkg:rpm/...)
|
|
1004
|
+
const fallbackName = pkgName || rawPurl.slice(4).split('@')[0] || 'unknown';
|
|
1005
|
+
const atIdx = rawPurl.lastIndexOf('@');
|
|
1006
|
+
const fallbackVer =
|
|
1007
|
+
pkgVersion || (atIdx > rawPurl.lastIndexOf('/') ? rawPurl.slice(atIdx + 1) : '0.0.0');
|
|
1008
|
+
results.push({
|
|
1009
|
+
purl: rawPurl,
|
|
1010
|
+
ecosystem: 'generic' ,
|
|
1011
|
+
name: fallbackName,
|
|
1012
|
+
version: fallbackVer,
|
|
1013
|
+
scope: 'prod',
|
|
1014
|
+
sourceFile,
|
|
1015
|
+
});
|
|
1016
|
+
continue;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
// Fallback: pkg:generic/<name>@<version>
|
|
1020
|
+
results.push({
|
|
1021
|
+
purl: `pkg:generic/${encodeURIComponent(pkgName)}@${encodeURIComponent(pkgVersion)}`,
|
|
1022
|
+
ecosystem: 'generic' ,
|
|
1023
|
+
name: pkgName,
|
|
1024
|
+
version: pkgVersion,
|
|
1025
|
+
scope: 'prod',
|
|
1026
|
+
sourceFile,
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
return deduplicateComponents(results);
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
// ---------------------------------------------------------------------------
|
|
1034
|
+
// Public Entrypoints
|
|
1035
|
+
// ---------------------------------------------------------------------------
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* Parses a single lockfile or CycloneDX/SPDX SBOM input, enforcing SCAN_LIMITS.maxLockfileBytes.
|
|
1039
|
+
*/
|
|
1040
|
+
export function parseLockfile(input ) {
|
|
1041
|
+
const byteLength = utf8ToBytes(input.content).byteLength;
|
|
1042
|
+
if (byteLength > SCAN_LIMITS.maxLockfileBytes) {
|
|
1043
|
+
throw new Error(
|
|
1044
|
+
`Lockfile ${input.path} exceeds SCAN_LIMITS.maxLockfileBytes (${byteLength} > ${SCAN_LIMITS.maxLockfileBytes})`,
|
|
1045
|
+
);
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
const kind = input.kind ?? detectLockfileKind(input.path);
|
|
1049
|
+
if (!kind) {
|
|
1050
|
+
throw new Error(`Unsupported lockfile kind for path: ${input.path}`);
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
switch (kind) {
|
|
1054
|
+
case 'package-lock.json':
|
|
1055
|
+
return parsePackageLockJson(input.content, input.path);
|
|
1056
|
+
case 'pnpm-lock.yaml':
|
|
1057
|
+
return parsePnpmLockYaml(input.content, input.path);
|
|
1058
|
+
case 'Cargo.lock':
|
|
1059
|
+
return parseCargoLock(input.content, input.path);
|
|
1060
|
+
case 'go.sum':
|
|
1061
|
+
return parseGoSum(input.content, input.path);
|
|
1062
|
+
case 'requirements.txt':
|
|
1063
|
+
return parseRequirementsTxt(input.content, input.path);
|
|
1064
|
+
case 'poetry.lock':
|
|
1065
|
+
return parsePoetryLock(input.content, input.path);
|
|
1066
|
+
case 'pom.xml':
|
|
1067
|
+
return parsePomXml(input.content, input.path);
|
|
1068
|
+
case 'cyclonedx.json':
|
|
1069
|
+
return parseCycloneDxJson(input.content, input.path);
|
|
1070
|
+
case 'spdx.json':
|
|
1071
|
+
return parseSpdxSbom(input.content, input.path);
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
/**
|
|
1076
|
+
* Parses a batch of lockfiles for a repository, enforcing SCAN_LIMITS.maxLockfilesPerRepo
|
|
1077
|
+
* and SCAN_LIMITS.maxLockfileBytes.
|
|
1078
|
+
*/
|
|
1079
|
+
export function parseLockfiles(
|
|
1080
|
+
inputs ,
|
|
1081
|
+
) {
|
|
1082
|
+
const limited = inputs.slice(0, SCAN_LIMITS.maxLockfilesPerRepo);
|
|
1083
|
+
const skippedFiles = inputs.slice(SCAN_LIMITS.maxLockfilesPerRepo).map((i) => i.path);
|
|
1084
|
+
const allComponents = [];
|
|
1085
|
+
const errors = [];
|
|
1086
|
+
let processedFiles = 0;
|
|
1087
|
+
|
|
1088
|
+
for (const input of limited) {
|
|
1089
|
+
try {
|
|
1090
|
+
const parsed = parseLockfile(input);
|
|
1091
|
+
allComponents.push(...parsed);
|
|
1092
|
+
processedFiles++;
|
|
1093
|
+
} catch (err) {
|
|
1094
|
+
errors.push({
|
|
1095
|
+
path: input.path,
|
|
1096
|
+
error: err instanceof Error ? err.message : String(err),
|
|
1097
|
+
});
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
return {
|
|
1102
|
+
components: deduplicateComponents(allComponents),
|
|
1103
|
+
processedFiles,
|
|
1104
|
+
skippedFiles,
|
|
1105
|
+
errors,
|
|
1106
|
+
};
|
|
1107
|
+
}
|