@trustify-da/trustify-da-javascript-client 0.3.0-ea.76e1fef → 0.3.0-ea.7ee4047

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.
Files changed (83) hide show
  1. package/README.md +199 -11
  2. package/dist/package.json +30 -26
  3. package/dist/src/analysis.d.ts +50 -22
  4. package/dist/src/analysis.js +70 -18
  5. package/dist/src/cli.js +188 -6
  6. package/dist/src/config.d.ts +120 -0
  7. package/dist/src/config.js +260 -0
  8. package/dist/src/cyclone_dx_sbom.d.ts +13 -0
  9. package/dist/src/cyclone_dx_sbom.js +50 -4
  10. package/dist/src/index.d.ts +27 -23
  11. package/dist/src/index.js +32 -41
  12. package/dist/src/license/index.d.ts +4 -4
  13. package/dist/src/license/index.js +13 -7
  14. package/dist/src/license/license_utils.js +4 -1
  15. package/dist/src/license/licenses_api.d.ts +3 -3
  16. package/dist/src/license/licenses_api.js +1 -1
  17. package/dist/src/oci_image/images.d.ts +1 -1
  18. package/dist/src/oci_image/images.js +15 -1
  19. package/dist/src/oci_image/utils.d.ts +5 -5
  20. package/dist/src/package_version.d.ts +8 -0
  21. package/dist/src/package_version.js +31 -0
  22. package/dist/src/provider.d.ts +4 -3
  23. package/dist/src/provider.js +6 -2
  24. package/dist/src/providers/base_java.d.ts +67 -3
  25. package/dist/src/providers/base_java.js +80 -23
  26. package/dist/src/providers/base_javascript.d.ts +74 -9
  27. package/dist/src/providers/base_javascript.js +116 -7
  28. package/dist/src/providers/base_pyproject.d.ts +10 -5
  29. package/dist/src/providers/base_pyproject.js +7 -0
  30. package/dist/src/providers/containerfile_parser.d.ts +5 -0
  31. package/dist/src/providers/containerfile_parser.js +20 -0
  32. package/dist/src/providers/golang_gomodules.d.ts +2 -1
  33. package/dist/src/providers/golang_gomodules.js +50 -8
  34. package/dist/src/providers/java_gradle.d.ts +48 -0
  35. package/dist/src/providers/java_gradle.js +206 -22
  36. package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
  37. package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
  38. package/dist/src/providers/java_maven.d.ts +29 -9
  39. package/dist/src/providers/java_maven.js +176 -10
  40. package/dist/src/providers/javascript_bun.d.ts +22 -0
  41. package/dist/src/providers/javascript_bun.js +141 -0
  42. package/dist/src/providers/javascript_npm.d.ts +19 -1
  43. package/dist/src/providers/javascript_npm.js +40 -1
  44. package/dist/src/providers/javascript_pnpm.d.ts +13 -0
  45. package/dist/src/providers/javascript_pnpm.js +47 -1
  46. package/dist/src/providers/javascript_yarn.d.ts +12 -0
  47. package/dist/src/providers/javascript_yarn.js +75 -2
  48. package/dist/src/providers/manifest.js +6 -3
  49. package/dist/src/providers/oci_dockerfile.d.ts +51 -0
  50. package/dist/src/providers/oci_dockerfile.js +177 -0
  51. package/dist/src/providers/processors/yarn_berry_processor.d.ts +6 -2
  52. package/dist/src/providers/processors/yarn_berry_processor.js +3 -2
  53. package/dist/src/providers/processors/yarn_classic_processor.d.ts +6 -2
  54. package/dist/src/providers/processors/yarn_classic_processor.js +8 -6
  55. package/dist/src/providers/python_controller.js +61 -4
  56. package/dist/src/providers/python_pip.d.ts +1 -0
  57. package/dist/src/providers/python_pip.js +1 -1
  58. package/dist/src/providers/python_pip_pyproject.js +3 -1
  59. package/dist/src/providers/python_poetry.d.ts +31 -8
  60. package/dist/src/providers/python_poetry.js +62 -14
  61. package/dist/src/providers/python_uv.d.ts +5 -14
  62. package/dist/src/providers/python_uv.js +50 -2
  63. package/dist/src/providers/requirements_parser.js +1 -1
  64. package/dist/src/providers/rust_cargo.d.ts +6 -2
  65. package/dist/src/providers/rust_cargo.js +101 -33
  66. package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
  67. package/dist/src/remediate.d.ts +107 -0
  68. package/dist/src/remediate.js +244 -0
  69. package/dist/src/remediation.d.ts +81 -0
  70. package/dist/src/remediation.js +475 -0
  71. package/dist/src/remediation_report.d.ts +23 -0
  72. package/dist/src/remediation_report.js +180 -0
  73. package/dist/src/sbom.d.ts +11 -0
  74. package/dist/src/sbom.js +10 -0
  75. package/dist/src/tools.d.ts +11 -13
  76. package/dist/src/tools.js +34 -13
  77. package/dist/src/updaters/maven_updater.d.ts +58 -0
  78. package/dist/src/updaters/maven_updater.js +345 -0
  79. package/dist/src/updaters/toml_updater.d.ts +62 -0
  80. package/dist/src/updaters/toml_updater.js +276 -0
  81. package/dist/src/workspace.d.ts +2 -1
  82. package/dist/src/workspace.js +2 -1
  83. package/package.json +31 -27
@@ -0,0 +1,475 @@
1
+ import { PackageURL } from 'packageurl-js';
2
+ /**
3
+ * A single per-CVE vulnerability carried by a remediation: one CVE with its own severity and the
4
+ * advisories attributed to it.
5
+ * @typedef {{id: string, severity: string, advisories: Array<{id: string, url: string}>}} Vulnerability
6
+ */
7
+ /**
8
+ * A single applicable remediation as produced by {@link extractRemediations}: a dependency, the
9
+ * version that fixes it, and the per-CVE `vulnerabilities` it resolves.
10
+ * @typedef {{
11
+ * purl: string,
12
+ * groupId: string,
13
+ * artifactId: string,
14
+ * currentVersion: string,
15
+ * fixedInVersion: string,
16
+ * fixedInPurl: string,
17
+ * provider: string,
18
+ * source: string,
19
+ * vulnerabilities: Vulnerability[]
20
+ * }} Remediation
21
+ */
22
+ /**
23
+ * Extracts the major version segment from a version string.
24
+ * @param {string} version
25
+ * @returns {string} the first dot-separated segment
26
+ */
27
+ function getMajorVersion(version) {
28
+ return version.split('.')[0] || '';
29
+ }
30
+ /**
31
+ * @typedef {{selectVersion: (fixedInVersions: string[], currentVersion: string) => string, resolveConflict: (existing: ConflictCandidate, candidate: ConflictCandidate) => 'existing'|'candidate'}} VersionStrategy
32
+ */
33
+ /**
34
+ * Version selection strategy that prefers the closest compatible version
35
+ * within the same major version stream. Falls back to the lowest cross-major
36
+ * version when no same-major option exists.
37
+ * @type {VersionStrategy}
38
+ */
39
+ export const closestCoverageStrategy = {
40
+ selectVersion(fixedInVersions, currentVersion) {
41
+ const currentMajor = getMajorVersion(currentVersion);
42
+ const sameMajor = fixedInVersions.filter(v => getMajorVersion(v) === currentMajor);
43
+ if (sameMajor.length > 0) {
44
+ return sameMajor.sort((a, b) => compareVersions(b, a))[0];
45
+ }
46
+ return fixedInVersions.sort((a, b) => compareVersions(a, b))[0];
47
+ },
48
+ resolveConflict(existing, candidate) {
49
+ if (existing._fromTrustedContent && !candidate._fromTrustedContent) {
50
+ return 'existing';
51
+ }
52
+ if (!existing._fromTrustedContent && candidate._fromTrustedContent) {
53
+ return 'candidate';
54
+ }
55
+ const currentMajor = getMajorVersion(existing.currentVersion);
56
+ const existingSameMajor = getMajorVersion(existing.fixedInVersion) === currentMajor;
57
+ const candidateSameMajor = getMajorVersion(candidate.fixedInVersion) === currentMajor;
58
+ if (existingSameMajor && !candidateSameMajor) {
59
+ return 'existing';
60
+ }
61
+ if (!existingSameMajor && candidateSameMajor) {
62
+ return 'candidate';
63
+ }
64
+ return compareVersions(candidate.fixedInVersion, existing.fixedInVersion) > 0
65
+ ? 'candidate'
66
+ : 'existing';
67
+ },
68
+ };
69
+ /**
70
+ * Version selection strategy that always picks the highest version regardless
71
+ * of major version distance. Guarantees maximum CVE coverage but may produce
72
+ * large version jumps. This is the original behavior before pluggable strategies.
73
+ * @type {VersionStrategy}
74
+ */
75
+ export const highestStrategy = {
76
+ selectVersion(fixedInVersions) {
77
+ return fixedInVersions.sort((a, b) => compareVersions(b, a))[0];
78
+ },
79
+ resolveConflict(existing, candidate) {
80
+ if (existing._fromTrustedContent && !candidate._fromTrustedContent) {
81
+ return 'existing';
82
+ }
83
+ if (!existing._fromTrustedContent && candidate._fromTrustedContent) {
84
+ return 'candidate';
85
+ }
86
+ return compareVersions(candidate.fixedInVersion, existing.fixedInVersion) > 0
87
+ ? 'candidate'
88
+ : 'existing';
89
+ },
90
+ };
91
+ /**
92
+ * Extracts actionable remediation instructions from a DA AnalysisReport response.
93
+ *
94
+ * Walks the provider/source/dependency/issue tree, collects fixedIn and trustedContent
95
+ * remediation data, applies provider priority resolution, and uses the configured
96
+ * version selection strategy to resolve conflicts. Dependencies with no remediation
97
+ * data are skipped.
98
+ *
99
+ * @param {import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport.ts').AnalysisReport} analysisReport - raw DA AnalysisReport JSON response
100
+ * @param {object} [options] - extraction options
101
+ * @param {string[]} [options.providerPriority] - provider names in descending priority order.
102
+ * The first entry has the highest priority. Providers not listed share the lowest priority.
103
+ * When omitted or empty, all providers are treated equally and the highest fix version wins.
104
+ * @param {VersionStrategy} [options.versionStrategy] - version selection strategy with selectVersion
105
+ * and resolveConflict methods. Defaults to closestCoverageStrategy.
106
+ * @returns {Remediation[]} `vulnerabilities` is the sole source of vulnerability data — each entry
107
+ * holds one CVE with its own severity and advisories. Use {@link maxSeverity} to derive a
108
+ * dependency-level severity.
109
+ */
110
+ export function extractRemediations(analysisReport, options = {}) {
111
+ if (!analysisReport || !analysisReport.providers) {
112
+ return [];
113
+ }
114
+ const priorityMap = buildPriorityMap(options.providerPriority);
115
+ const strategy = options.versionStrategy || closestCoverageStrategy;
116
+ /** @type {Map<string, Remediation & { _fromTrustedContent?: boolean}>} */
117
+ const remediationsByDep = new Map();
118
+ const rankByDep = new Map();
119
+ for (const [providerName, providerReport] of Object.entries(analysisReport.providers)) {
120
+ const providerRank = priorityMap.get(providerName) ?? 0;
121
+ extractFromSources(providerReport, providerName, providerRank, remediationsByDep, rankByDep, strategy);
122
+ extractFromRecommendations(providerReport, providerName, providerRank, remediationsByDep, rankByDep);
123
+ }
124
+ return Array.from(remediationsByDep.values())
125
+ .map((entry) => {
126
+ delete entry._fromTrustedContent;
127
+ return entry;
128
+ })
129
+ .sort((a, b) => a.purl.localeCompare(b.purl));
130
+ }
131
+ /**
132
+ * Builds a priority lookup map from a provider priority array.
133
+ * First entry gets the highest numeric priority.
134
+ * @param {string[]} [providerPriority]
135
+ * @returns {Map<string, number>}
136
+ */
137
+ function buildPriorityMap(providerPriority) {
138
+ const map = new Map();
139
+ if (!providerPriority || providerPriority.length === 0) {
140
+ return map;
141
+ }
142
+ for (let i = 0; i < providerPriority.length; i++) {
143
+ map.set(providerPriority[i], providerPriority.length - i);
144
+ }
145
+ return map;
146
+ }
147
+ /**
148
+ * Extracts remediations from the sources/dependencies/issues tree of a provider report.
149
+ * @param {import('@trustify-da/trustify-da-api-model/model/v5/ProviderReport.js').ProviderReport} providerReport
150
+ * @param {string} providerName
151
+ * @param {number} providerRank - numeric priority rank for this provider
152
+ * @param {Map<string, Remediation & { _fromTrustedContent?: boolean }>} remediationsByDep - accumulator keyed by dependency PURL
153
+ * @param {Map<string, number>} rankByDep - tracks current winning rank per dependency
154
+ * @param {VersionStrategy} strategy - version selection strategy
155
+ */
156
+ function extractFromSources(providerReport, providerName, providerRank, remediationsByDep, rankByDep, strategy) {
157
+ if (!providerReport.sources) {
158
+ return;
159
+ }
160
+ for (const [sourceName, sourceReport] of Object.entries(providerReport.sources)) {
161
+ if (!sourceReport.dependencies) {
162
+ continue;
163
+ }
164
+ for (const dep of sourceReport.dependencies) {
165
+ if (!dep.issues) {
166
+ continue;
167
+ }
168
+ for (const issue of dep.issues) {
169
+ processIssueRemediation(issue, dep, providerName, sourceName, providerRank, remediationsByDep, rankByDep, strategy);
170
+ }
171
+ }
172
+ }
173
+ }
174
+ /**
175
+ * Processes a single issue's remediation data and merges it into the accumulator.
176
+ * @param {import('@trustify-da/trustify-da-api-model/model/v5/Issue.js').Issue} issue - issue object containing remediation and CVE data
177
+ * @param {import('@trustify-da/trustify-da-api-model/model/v5/DependencyReport.js').DependencyReport} dep - dependency object containing the ref PURL
178
+ * @param {string} providerName
179
+ * @param {string} sourceName
180
+ * @param {number} providerRank
181
+ * @param {Map<string, Remediation & { _fromTrustedContent?: boolean }>} remediationsByDep
182
+ * @param {Map<string, number>} rankByDep
183
+ * @param {VersionStrategy} strategy - version selection strategy
184
+ */
185
+ function processIssueRemediation(issue, dep, providerName, sourceName, providerRank, remediationsByDep, rankByDep, strategy) {
186
+ const depPurl = dep.ref;
187
+ if (!depPurl) {
188
+ return;
189
+ }
190
+ let parsedDep;
191
+ try {
192
+ parsedDep = PackageURL.fromString(depPurl);
193
+ }
194
+ catch {
195
+ return;
196
+ }
197
+ const currentVersion = parsedDep.version || '';
198
+ const fixedInPurl = getFixedInPurl(issue, depPurl, strategy, currentVersion);
199
+ if (!fixedInPurl) {
200
+ return;
201
+ }
202
+ const isTrustedContent = !!(issue.remediation && issue.remediation.trustedContent && issue.remediation.trustedContent.ref);
203
+ let parsedFix;
204
+ try {
205
+ parsedFix = PackageURL.fromString(fixedInPurl);
206
+ }
207
+ catch {
208
+ return;
209
+ }
210
+ const fixedInVersion = parsedFix.version;
211
+ if (!fixedInVersion) {
212
+ return;
213
+ }
214
+ const cveId = issue.id;
215
+ const severity = issue.severity || 'UNKNOWN';
216
+ const advisories = extractAdvisories(issue);
217
+ const existing = remediationsByDep.get(depPurl);
218
+ if (!existing) {
219
+ const entry = {
220
+ purl: depPurl,
221
+ groupId: parsedDep.namespace || '',
222
+ artifactId: parsedDep.name,
223
+ currentVersion,
224
+ fixedInVersion,
225
+ fixedInPurl,
226
+ provider: providerName,
227
+ source: sourceName,
228
+ vulnerabilities: [],
229
+ _fromTrustedContent: isTrustedContent,
230
+ };
231
+ addVulnerability(entry, cveId, severity, advisories);
232
+ remediationsByDep.set(depPurl, entry);
233
+ rankByDep.set(depPurl, providerRank);
234
+ return;
235
+ }
236
+ addVulnerability(existing, cveId, severity, advisories);
237
+ const existingRank = rankByDep.get(depPurl);
238
+ if (providerRank > existingRank) {
239
+ existing.fixedInVersion = fixedInVersion;
240
+ existing.fixedInPurl = fixedInPurl;
241
+ existing.provider = providerName;
242
+ existing.source = sourceName;
243
+ existing._fromTrustedContent = isTrustedContent;
244
+ rankByDep.set(depPurl, providerRank);
245
+ }
246
+ else if (providerRank === existingRank) {
247
+ const winner = strategy.resolveConflict({ fixedInVersion: existing.fixedInVersion, _fromTrustedContent: existing._fromTrustedContent, currentVersion }, { fixedInVersion, _fromTrustedContent: isTrustedContent, currentVersion });
248
+ if (winner === 'candidate') {
249
+ existing.fixedInVersion = fixedInVersion;
250
+ existing.fixedInPurl = fixedInPurl;
251
+ existing.provider = providerName;
252
+ existing.source = sourceName;
253
+ existing._fromTrustedContent = isTrustedContent;
254
+ }
255
+ }
256
+ }
257
+ /**
258
+ * Extracts remediations from the recommendations section of a provider report.
259
+ * Merges CVEs and advisories into existing entries when present.
260
+ * @param {import('@trustify-da/trustify-da-api-model/model/v5/ProviderReport.js').ProviderReport} providerReport
261
+ * @param {string} providerName
262
+ * @param {number} providerRank
263
+ * @param {Map<string, Remediation & { _fromTrustedContent?: boolean }>} remediationsByDep
264
+ * @param {Map<string, number>} rankByDep
265
+ */
266
+ function extractFromRecommendations(providerReport, providerName, providerRank, remediationsByDep, rankByDep) {
267
+ if (!providerReport.recommendations || !providerReport.recommendations.dependencies) {
268
+ return;
269
+ }
270
+ for (const dep of providerReport.recommendations.dependencies) {
271
+ if (!dep.recommendation || !dep.ref) {
272
+ continue;
273
+ }
274
+ const recommendedPurl = dep.recommendation.ref || dep.recommendation;
275
+ if (typeof recommendedPurl !== 'string') {
276
+ continue;
277
+ }
278
+ let parsedDep;
279
+ let parsedRec;
280
+ try {
281
+ parsedDep = PackageURL.fromString(dep.ref);
282
+ parsedRec = PackageURL.fromString(recommendedPurl);
283
+ }
284
+ catch {
285
+ continue;
286
+ }
287
+ const fixedInVersion = parsedRec.version;
288
+ if (!fixedInVersion) {
289
+ continue;
290
+ }
291
+ const depPurl = dep.ref;
292
+ const existing = remediationsByDep.get(depPurl);
293
+ if (!existing) {
294
+ remediationsByDep.set(depPurl, {
295
+ purl: depPurl,
296
+ groupId: parsedDep.namespace || '',
297
+ artifactId: parsedDep.name,
298
+ currentVersion: parsedDep.version || '',
299
+ fixedInVersion,
300
+ fixedInPurl: recommendedPurl,
301
+ provider: providerName,
302
+ source: 'recommendation',
303
+ vulnerabilities: [],
304
+ });
305
+ rankByDep.set(depPurl, providerRank);
306
+ continue;
307
+ }
308
+ const existingRank = rankByDep.get(depPurl);
309
+ if (providerRank > existingRank) {
310
+ existing.fixedInVersion = fixedInVersion;
311
+ existing.fixedInPurl = recommendedPurl;
312
+ existing.provider = providerName;
313
+ existing.source = 'recommendation';
314
+ rankByDep.set(depPurl, providerRank);
315
+ }
316
+ else if (providerRank === existingRank) {
317
+ if (compareVersions(fixedInVersion, existing.fixedInVersion) > 0) {
318
+ existing.fixedInVersion = fixedInVersion;
319
+ existing.fixedInPurl = recommendedPurl;
320
+ existing.provider = providerName;
321
+ existing.source = 'recommendation';
322
+ }
323
+ }
324
+ }
325
+ }
326
+ /**
327
+ * Gets the fixedIn PURL from an issue's remediation, preferring trustedContent.
328
+ * When fixedIn is an array of version strings (not PURLs), uses the strategy's
329
+ * selectVersion to pick the best candidate and constructs a PURL from the dependency ref.
330
+ * @param {import('@trustify-da/trustify-da-api-model/model/v5/Issue.js').Issue} issue
331
+ * @param {string} depPurl - the dependency PURL, used to construct fixedIn PURLs from version strings
332
+ * @param {VersionStrategy} strategy - version selection strategy
333
+ * @param {string} currentVersion - the dependency's current version
334
+ * @returns {string|undefined}
335
+ */
336
+ function getFixedInPurl(issue, depPurl, strategy, currentVersion) {
337
+ if (!issue.remediation) {
338
+ return undefined;
339
+ }
340
+ if (issue.remediation.trustedContent?.ref) {
341
+ return issue.remediation.trustedContent.ref;
342
+ }
343
+ const fixedIn = issue.remediation.fixedIn;
344
+ if (fixedIn?.length > 0) {
345
+ const version = fixedIn.length > 1
346
+ ? strategy.selectVersion(fixedIn, currentVersion)
347
+ : fixedIn[0];
348
+ if (typeof version === 'string' && version.startsWith('pkg:')) {
349
+ return version;
350
+ }
351
+ if (typeof version === 'string' && depPurl) {
352
+ try {
353
+ const parsed = PackageURL.fromString(depPurl);
354
+ parsed.version = version;
355
+ return parsed.toString();
356
+ }
357
+ catch {
358
+ return undefined;
359
+ }
360
+ }
361
+ }
362
+ return undefined;
363
+ }
364
+ /**
365
+ * Adds a per-CVE vulnerability entry to a remediation, deduplicating by CVE id. When the
366
+ * CVE is already present, the higher severity is kept and its advisories are merged.
367
+ * Issues without a CVE id contribute no vulnerability entry.
368
+ * @param {object} entry - remediation accumulator entry with a `vulnerabilities` array
369
+ * @param {string|undefined} cveId - the CVE identifier for this issue
370
+ * @param {string} severity - the issue's severity
371
+ * @param {Array<{id: string, url: string}>} advisories - advisories attributed to this issue
372
+ */
373
+ function addVulnerability(entry, cveId, severity, advisories) {
374
+ if (!cveId) {
375
+ return;
376
+ }
377
+ const normalizedSeverity = (severity || 'UNKNOWN').toUpperCase();
378
+ const existingVuln = entry.vulnerabilities.find(v => v.id === cveId);
379
+ if (existingVuln) {
380
+ existingVuln.severity = higherSeverity(existingVuln.severity, normalizedSeverity);
381
+ mergeAdvisories(existingVuln.advisories, advisories);
382
+ return;
383
+ }
384
+ entry.vulnerabilities.push({
385
+ id: cveId,
386
+ severity: normalizedSeverity,
387
+ advisories: [...advisories],
388
+ });
389
+ }
390
+ /**
391
+ * Extracts advisory objects from an issue.
392
+ * @param {import('@trustify-da/trustify-da-api-model/model/v5/Issue.js').Issue} issue
393
+ * @returns {Array<{id: string, url: string}>}
394
+ */
395
+ function extractAdvisories(issue) {
396
+ const advisories = [];
397
+ for (const advisory of issue.remediation?.advisories ?? []) {
398
+ if (advisory.advisory?.id) {
399
+ advisories.push({
400
+ id: advisory.advisory.id,
401
+ url: advisory.advisory.url || '',
402
+ });
403
+ }
404
+ }
405
+ return advisories;
406
+ }
407
+ /**
408
+ * Merges new advisories into existing list, avoiding duplicates by id.
409
+ * @param {Array<{id: string, url: string}>} existing
410
+ * @param {Array<{id: string, url: string}>} incoming
411
+ */
412
+ function mergeAdvisories(existing, incoming) {
413
+ const seen = new Set(existing.map(a => a.id));
414
+ for (const adv of incoming) {
415
+ if (!seen.has(adv.id)) {
416
+ existing.push(adv);
417
+ seen.add(adv.id);
418
+ }
419
+ }
420
+ }
421
+ /**
422
+ * Compares two version strings segment by segment.
423
+ * Returns positive if a > b, negative if a < b, zero if equal.
424
+ * Falls back to lexicographic comparison for non-numeric segments.
425
+ * @param {string} a
426
+ * @param {string} b
427
+ * @returns {number}
428
+ */
429
+ function compareVersions(a, b) {
430
+ const partsA = a.split('.');
431
+ const partsB = b.split('.');
432
+ const len = Math.max(partsA.length, partsB.length);
433
+ for (let i = 0; i < len; i++) {
434
+ const segA = partsA[i] || '';
435
+ const segB = partsB[i] || '';
436
+ const numA = Number(segA);
437
+ const numB = Number(segB);
438
+ if (Number.isFinite(numA) && Number.isFinite(numB)) {
439
+ const diff = numA - numB;
440
+ if (diff !== 0) {
441
+ return diff;
442
+ }
443
+ }
444
+ else {
445
+ const cmp = segA.localeCompare(segB);
446
+ if (cmp !== 0) {
447
+ return cmp;
448
+ }
449
+ }
450
+ }
451
+ return 0;
452
+ }
453
+ export const SEVERITY_ORDER = ['UNKNOWN', 'LOW', 'MEDIUM', 'HIGH', 'CRITICAL'];
454
+ /**
455
+ * Returns the higher of two severity strings, normalized to uppercase.
456
+ * @param {string} a
457
+ * @param {string} b
458
+ * @returns {string}
459
+ */
460
+ function higherSeverity(a, b) {
461
+ const upperA = a.toUpperCase();
462
+ const upperB = b.toUpperCase();
463
+ const indexA = SEVERITY_ORDER.indexOf(upperA);
464
+ const indexB = SEVERITY_ORDER.indexOf(upperB);
465
+ return indexA >= indexB ? upperA : upperB;
466
+ }
467
+ /**
468
+ * Derives a dependency-level severity as the max across a list of vulnerabilities.
469
+ * Returns 'UNKNOWN' for an empty or missing list.
470
+ * @param {Array<{severity: string}>} [vulnerabilities]
471
+ * @returns {string}
472
+ */
473
+ export function maxSeverity(vulnerabilities) {
474
+ return (vulnerabilities || []).reduce((acc, v) => higherSeverity(acc, v.severity), 'UNKNOWN');
475
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Generates a formatted report from an array of remediation entries.
3
+ *
4
+ * @param {import('./remediation.js').Remediation[]} remediations
5
+ * @param {object} [options]
6
+ * @param {'dependency'|'bundle'} [options.groupBy='dependency'] - grouping strategy
7
+ * @param {'markdown'|'json'} [options.format='markdown'] - output format
8
+ * @param {boolean} [options.dryRun=false] - when true, produces tabular summary
9
+ * @returns {string}
10
+ */
11
+ export function generateReport(remediations: import("./remediation.js").Remediation[], options?: {
12
+ groupBy?: "bundle" | "dependency" | undefined;
13
+ format?: "markdown" | "json" | undefined;
14
+ dryRun?: boolean | undefined;
15
+ }): string;
16
+ /**
17
+ * Generates a deterministic deduplication key for a remediation entry.
18
+ * Suitable for checking existing PR titles or branch names.
19
+ *
20
+ * @param {object} remediation - a single remediation entry
21
+ * @returns {string} stable key in the form `groupId:artifactId:newVersion`
22
+ */
23
+ export function generateDeduplicationKey(remediation: object): string;
@@ -0,0 +1,180 @@
1
+ /**
2
+ * Report generator that transforms remediation extractor output into structured
3
+ * markdown for PR bodies, CLI dry-run output, and JSON.
4
+ */
5
+ import { SEVERITY_ORDER, maxSeverity } from './remediation.js';
6
+ /**
7
+ * Generates a formatted report from an array of remediation entries.
8
+ *
9
+ * @param {import('./remediation.js').Remediation[]} remediations
10
+ * @param {object} [options]
11
+ * @param {'dependency'|'bundle'} [options.groupBy='dependency'] - grouping strategy
12
+ * @param {'markdown'|'json'} [options.format='markdown'] - output format
13
+ * @param {boolean} [options.dryRun=false] - when true, produces tabular summary
14
+ * @returns {string}
15
+ */
16
+ export function generateReport(remediations, options = {}) {
17
+ const { groupBy = 'dependency', format = 'markdown', dryRun = false } = options;
18
+ if (!remediations || remediations.length === 0) {
19
+ return format === 'json' ? '[]' : 'No remediations found.';
20
+ }
21
+ if (format === 'json') {
22
+ return JSON.stringify(remediations, null, '\t');
23
+ }
24
+ if (dryRun) {
25
+ return generateDryRunReport(remediations);
26
+ }
27
+ if (groupBy === 'bundle') {
28
+ return generateBundledReport(remediations);
29
+ }
30
+ return generatePerDependencyReport(remediations);
31
+ }
32
+ /**
33
+ * Generates a per-dependency markdown report with one section per remediation entry.
34
+ *
35
+ * Each vulnerability row is rendered from its own per-CVE severity and advisories
36
+ * (from `rem.vulnerabilities`), so a Moderate CVE is no longer inflated to the
37
+ * dependency's max severity.
38
+ * @param {import('./remediation.js').Remediation[]} remediations
39
+ * @returns {string}
40
+ */
41
+ function generatePerDependencyReport(remediations) {
42
+ const sections = remediations.map(rem => {
43
+ const depName = rem.groupId
44
+ ? `${rem.groupId}:${rem.artifactId}`
45
+ : rem.artifactId;
46
+ const lines = [
47
+ `## Security Update: ${depName} ${rem.currentVersion} → ${rem.fixedInVersion}`,
48
+ '',
49
+ `**Provider:** ${rem.provider} | **Source:** ${rem.source}`,
50
+ '',
51
+ ];
52
+ const vulnerabilities = rem.vulnerabilities || [];
53
+ if (vulnerabilities.length > 0) {
54
+ lines.push('### Vulnerabilities resolved');
55
+ lines.push('');
56
+ lines.push('| CVE | Severity | Advisory |');
57
+ lines.push('| --- | --- | --- |');
58
+ for (const v of vulnerabilities) {
59
+ lines.push(`| ${v.id} | ${v.severity} | ${formatAdvisoryLinks(v.advisories)} |`);
60
+ }
61
+ }
62
+ return lines.join('\n');
63
+ });
64
+ return sections.join('\n\n');
65
+ }
66
+ /**
67
+ * Generates a bundled markdown report grouping all remediations by severity.
68
+ * @param {import('./remediation.js').Remediation[]} remediations
69
+ * @returns {string}
70
+ */
71
+ function generateBundledReport(remediations) {
72
+ const lines = ['# Security Update Summary', ''];
73
+ const bySeverity = groupBySeverity(remediations);
74
+ for (const severity of [...SEVERITY_ORDER].reverse()) {
75
+ const group = bySeverity.get(severity);
76
+ if (!group || group.length === 0) {
77
+ continue;
78
+ }
79
+ lines.push(`## ${severity}`);
80
+ lines.push('');
81
+ lines.push('| Dependency | Current | Fixed | Provider | CVEs | Advisory |');
82
+ lines.push('| --- | --- | --- | --- | --- | --- |');
83
+ for (const rem of group) {
84
+ const depName = rem.groupId
85
+ ? `${rem.groupId}:${rem.artifactId}`
86
+ : rem.artifactId;
87
+ const vulnerabilities = rem.vulnerabilities || [];
88
+ const cves = vulnerabilities.map(v => v.id).join(', ');
89
+ const advisoryLinks = formatAdvisoryLinks(collectAdvisories(vulnerabilities));
90
+ lines.push(`| ${depName} | ${rem.currentVersion} | ${rem.fixedInVersion}`
91
+ + ` | ${rem.provider} | ${cves} | ${advisoryLinks} |`);
92
+ }
93
+ lines.push('');
94
+ }
95
+ return lines.join('\n').trimEnd();
96
+ }
97
+ /**
98
+ * Generates a tabular dry-run summary of proposed changes.
99
+ * @param {import('./remediation.js').Remediation[]} remediations
100
+ * @returns {string}
101
+ */
102
+ function generateDryRunReport(remediations) {
103
+ const lines = [
104
+ 'Proposed dependency updates:',
105
+ '',
106
+ '| Dependency | Current | Fixed | Severity | Provider |',
107
+ '| --- | --- | --- | --- | --- |',
108
+ ];
109
+ for (const rem of remediations) {
110
+ const depName = rem.groupId
111
+ ? `${rem.groupId}:${rem.artifactId}`
112
+ : rem.artifactId;
113
+ lines.push(`| ${depName} | ${rem.currentVersion} | ${rem.fixedInVersion}`
114
+ + ` | ${maxSeverity(rem.vulnerabilities)} | ${rem.provider} |`);
115
+ }
116
+ return lines.join('\n');
117
+ }
118
+ /**
119
+ * Groups remediations by their severity.
120
+ * @param {import('./remediation.js').Remediation[]} remediations
121
+ * @returns {Map<string, Array<object>>}
122
+ */
123
+ function groupBySeverity(remediations) {
124
+ const map = new Map();
125
+ for (const severity of SEVERITY_ORDER) {
126
+ map.set(severity, []);
127
+ }
128
+ for (const rem of remediations) {
129
+ const sev = maxSeverity(rem.vulnerabilities);
130
+ if (!map.has(sev)) {
131
+ map.set(sev, []);
132
+ }
133
+ map.get(sev).push(rem);
134
+ }
135
+ return map;
136
+ }
137
+ /**
138
+ * Collects the de-duplicated union of advisories across a list of vulnerabilities.
139
+ * @param {Array<{advisories: Array<{id: string, url: string}>}>} vulnerabilities
140
+ * @returns {Array<{id: string, url: string}>}
141
+ */
142
+ function collectAdvisories(vulnerabilities) {
143
+ const merged = [];
144
+ const seen = new Set();
145
+ for (const v of vulnerabilities) {
146
+ for (const adv of v.advisories || []) {
147
+ if (!seen.has(adv.id)) {
148
+ seen.add(adv.id);
149
+ merged.push(adv);
150
+ }
151
+ }
152
+ }
153
+ return merged;
154
+ }
155
+ /**
156
+ * Formats advisory entries into markdown links or plain text.
157
+ * @param {Array<{id: string, url: string}>} advisories
158
+ * @returns {string}
159
+ */
160
+ function formatAdvisoryLinks(advisories) {
161
+ if (!advisories || advisories.length === 0) {
162
+ return '-';
163
+ }
164
+ return advisories
165
+ .map(adv => adv.url ? `[${adv.id}](${adv.url})` : adv.id)
166
+ .join(', ');
167
+ }
168
+ /**
169
+ * Generates a deterministic deduplication key for a remediation entry.
170
+ * Suitable for checking existing PR titles or branch names.
171
+ *
172
+ * @param {object} remediation - a single remediation entry
173
+ * @returns {string} stable key in the form `groupId:artifactId:newVersion`
174
+ */
175
+ export function generateDeduplicationKey(remediation) {
176
+ const group = remediation.groupId || '';
177
+ const artifact = remediation.artifactId || '';
178
+ const version = remediation.fixedInVersion || '';
179
+ return `${group}:${artifact}:${version}`;
180
+ }
@@ -33,6 +33,17 @@ export default class Sbom {
33
33
  alg: string;
34
34
  content: string;
35
35
  }>): CycloneDxSbom;
36
+ /**
37
+ * Attach hashes to existing components by matching their PURL. Post-processing
38
+ * step used by ecosystem-specific providers (e.g. Maven) to enrich the SBOM
39
+ * with artifact hashes without leaking their concern into the shared parser.
40
+ * @param {Map<string, Array<{alg: string, content: string}>>} hashMap - PURL→hashes map
41
+ * @return {Sbom}
42
+ */
43
+ attachHashes(hashMap: Map<string, Array<{
44
+ alg: string;
45
+ content: string;
46
+ }>>): Sbom;
36
47
  /**
37
48
  * @return String sbom json in a string format
38
49
  */