@trustify-da/trustify-da-javascript-client 0.3.0-ea.e660b02 → 0.3.0-ea.efdc87d

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