@trustify-da/trustify-da-javascript-client 0.3.0-ea.b68592e → 0.3.0-ea.b6d9c3f

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.
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Extracts actionable remediation instructions from a DA AnalysisReport response.
3
+ *
4
+ * Walks the provider/source/dependency/issue tree, collects fixedIn and trustedContent
5
+ * remediation data, applies provider priority resolution, and for the same dependency
6
+ * affected by multiple CVEs selects the highest fixedIn version from the highest-priority
7
+ * provider. Dependencies with no remediation data are skipped.
8
+ *
9
+ * @param {object} analysisReport - raw DA AnalysisReport JSON response
10
+ * @param {object} [options] - extraction options
11
+ * @param {string[]} [options.providerPriority] - provider names in descending priority order.
12
+ * The first entry has the highest priority. Providers not listed share the lowest priority.
13
+ * When omitted or empty, all providers are treated equally and the highest fix version wins.
14
+ * @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[]}>}
15
+ */
16
+ export function extractRemediations(analysisReport: object, options?: {
17
+ providerPriority?: string[] | undefined;
18
+ }): Array<{
19
+ purl: string;
20
+ groupId: string;
21
+ artifactId: string;
22
+ currentVersion: string;
23
+ fixedInVersion: string;
24
+ fixedInPurl: string;
25
+ provider: string;
26
+ source: string;
27
+ advisories: Array<{
28
+ id: string;
29
+ url: string;
30
+ }>;
31
+ severity: string;
32
+ cves: string[];
33
+ }>;
@@ -0,0 +1,321 @@
1
+ import { PackageURL } from 'packageurl-js';
2
+ /**
3
+ * Extracts actionable remediation instructions from a DA AnalysisReport response.
4
+ *
5
+ * Walks the provider/source/dependency/issue tree, collects fixedIn and trustedContent
6
+ * remediation data, applies provider priority resolution, and for the same dependency
7
+ * affected by multiple CVEs selects the highest fixedIn version from the highest-priority
8
+ * provider. Dependencies with no remediation data are skipped.
9
+ *
10
+ * @param {object} analysisReport - raw DA AnalysisReport JSON response
11
+ * @param {object} [options] - extraction options
12
+ * @param {string[]} [options.providerPriority] - provider names in descending priority order.
13
+ * The first entry has the highest priority. Providers not listed share the lowest priority.
14
+ * When omitted or empty, all providers are treated equally and the highest fix version wins.
15
+ * @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[]}>}
16
+ */
17
+ export function extractRemediations(analysisReport, options = {}) {
18
+ if (!analysisReport || !analysisReport.providers) {
19
+ return [];
20
+ }
21
+ const priorityMap = buildPriorityMap(options.providerPriority);
22
+ const remediationsByDep = new Map();
23
+ const rankByDep = new Map();
24
+ for (const [providerName, providerReport] of Object.entries(analysisReport.providers)) {
25
+ const providerRank = priorityMap.get(providerName) ?? 0;
26
+ extractFromSources(providerReport, providerName, providerRank, remediationsByDep, rankByDep);
27
+ extractFromRecommendations(providerReport, providerName, providerRank, remediationsByDep, rankByDep);
28
+ }
29
+ return Array.from(remediationsByDep.values())
30
+ .sort((a, b) => a.purl.localeCompare(b.purl));
31
+ }
32
+ /**
33
+ * Builds a priority lookup map from a provider priority array.
34
+ * First entry gets the highest numeric priority.
35
+ * @param {string[]} [providerPriority]
36
+ * @returns {Map<string, number>}
37
+ */
38
+ function buildPriorityMap(providerPriority) {
39
+ const map = new Map();
40
+ if (!providerPriority || providerPriority.length === 0) {
41
+ return map;
42
+ }
43
+ for (let i = 0; i < providerPriority.length; i++) {
44
+ map.set(providerPriority[i], providerPriority.length - i);
45
+ }
46
+ return map;
47
+ }
48
+ /**
49
+ * Extracts remediations from the sources/dependencies/issues tree of a provider report.
50
+ * @param {object} providerReport
51
+ * @param {string} providerName
52
+ * @param {number} providerRank - numeric priority rank for this provider
53
+ * @param {Map<string, object>} remediationsByDep - accumulator keyed by dependency PURL
54
+ * @param {Map<string, number>} rankByDep - tracks current winning rank per dependency
55
+ */
56
+ function extractFromSources(providerReport, providerName, providerRank, remediationsByDep, rankByDep) {
57
+ if (!providerReport.sources) {
58
+ return;
59
+ }
60
+ for (const [sourceName, sourceReport] of Object.entries(providerReport.sources)) {
61
+ if (!sourceReport.dependencies) {
62
+ continue;
63
+ }
64
+ for (const dep of sourceReport.dependencies) {
65
+ if (!dep.issues) {
66
+ continue;
67
+ }
68
+ for (const issue of dep.issues) {
69
+ processIssueRemediation(issue, dep, providerName, sourceName, providerRank, remediationsByDep, rankByDep);
70
+ }
71
+ }
72
+ }
73
+ }
74
+ /**
75
+ * Processes a single issue's remediation data and merges it into the accumulator.
76
+ * @param {object} issue - issue object containing remediation and CVE data
77
+ * @param {object} dep - dependency object containing the ref PURL
78
+ * @param {string} providerName
79
+ * @param {string} sourceName
80
+ * @param {number} providerRank
81
+ * @param {Map<string, object>} remediationsByDep
82
+ * @param {Map<string, number>} rankByDep
83
+ */
84
+ function processIssueRemediation(issue, dep, providerName, sourceName, providerRank, remediationsByDep, rankByDep) {
85
+ const fixedInPurl = getFixedInPurl(issue);
86
+ if (!fixedInPurl) {
87
+ return;
88
+ }
89
+ const depPurl = dep.ref;
90
+ if (!depPurl) {
91
+ return;
92
+ }
93
+ let parsedDep;
94
+ let parsedFix;
95
+ try {
96
+ parsedDep = PackageURL.fromString(depPurl);
97
+ parsedFix = PackageURL.fromString(fixedInPurl);
98
+ }
99
+ catch {
100
+ return;
101
+ }
102
+ const fixedInVersion = parsedFix.version;
103
+ if (!fixedInVersion) {
104
+ return;
105
+ }
106
+ const cveId = issue.id || issue.cve;
107
+ const severity = issue.severity || 'UNKNOWN';
108
+ const advisories = extractAdvisories(issue);
109
+ const existing = remediationsByDep.get(depPurl);
110
+ if (!existing) {
111
+ remediationsByDep.set(depPurl, {
112
+ purl: depPurl,
113
+ groupId: parsedDep.namespace || '',
114
+ artifactId: parsedDep.name,
115
+ currentVersion: parsedDep.version || '',
116
+ fixedInVersion,
117
+ fixedInPurl,
118
+ provider: providerName,
119
+ source: sourceName,
120
+ advisories,
121
+ severity: severity.toUpperCase(),
122
+ cves: cveId ? [cveId] : [],
123
+ });
124
+ rankByDep.set(depPurl, providerRank);
125
+ return;
126
+ }
127
+ if (cveId && !existing.cves.includes(cveId)) {
128
+ existing.cves.push(cveId);
129
+ }
130
+ mergeAdvisories(existing.advisories, advisories);
131
+ const existingRank = rankByDep.get(depPurl);
132
+ if (providerRank > existingRank) {
133
+ existing.fixedInVersion = fixedInVersion;
134
+ existing.fixedInPurl = fixedInPurl;
135
+ existing.provider = providerName;
136
+ existing.source = sourceName;
137
+ existing.severity = higherSeverity(existing.severity, severity);
138
+ rankByDep.set(depPurl, providerRank);
139
+ }
140
+ else if (providerRank === existingRank) {
141
+ if (compareVersions(fixedInVersion, existing.fixedInVersion) > 0) {
142
+ existing.fixedInVersion = fixedInVersion;
143
+ existing.fixedInPurl = fixedInPurl;
144
+ existing.provider = providerName;
145
+ existing.source = sourceName;
146
+ }
147
+ existing.severity = higherSeverity(existing.severity, severity);
148
+ }
149
+ }
150
+ /**
151
+ * Extracts remediations from the recommendations section of a provider report.
152
+ * Merges CVEs and advisories into existing entries when present.
153
+ * @param {object} providerReport
154
+ * @param {string} providerName
155
+ * @param {number} providerRank
156
+ * @param {Map<string, object>} remediationsByDep
157
+ * @param {Map<string, number>} rankByDep
158
+ */
159
+ function extractFromRecommendations(providerReport, providerName, providerRank, remediationsByDep, rankByDep) {
160
+ if (!providerReport.recommendations || !providerReport.recommendations.dependencies) {
161
+ return;
162
+ }
163
+ for (const dep of providerReport.recommendations.dependencies) {
164
+ if (!dep.recommendation || !dep.ref) {
165
+ continue;
166
+ }
167
+ const recommendedPurl = dep.recommendation.ref || dep.recommendation;
168
+ if (typeof recommendedPurl !== 'string') {
169
+ continue;
170
+ }
171
+ let parsedDep;
172
+ let parsedRec;
173
+ try {
174
+ parsedDep = PackageURL.fromString(dep.ref);
175
+ parsedRec = PackageURL.fromString(recommendedPurl);
176
+ }
177
+ catch {
178
+ continue;
179
+ }
180
+ const fixedInVersion = parsedRec.version;
181
+ if (!fixedInVersion) {
182
+ continue;
183
+ }
184
+ const depPurl = dep.ref;
185
+ const existing = remediationsByDep.get(depPurl);
186
+ if (!existing) {
187
+ remediationsByDep.set(depPurl, {
188
+ purl: depPurl,
189
+ groupId: parsedDep.namespace || '',
190
+ artifactId: parsedDep.name,
191
+ currentVersion: parsedDep.version || '',
192
+ fixedInVersion,
193
+ fixedInPurl: recommendedPurl,
194
+ provider: providerName,
195
+ source: 'recommendation',
196
+ advisories: [],
197
+ severity: 'UNKNOWN',
198
+ cves: [],
199
+ });
200
+ rankByDep.set(depPurl, providerRank);
201
+ continue;
202
+ }
203
+ const existingRank = rankByDep.get(depPurl);
204
+ if (providerRank > existingRank) {
205
+ existing.fixedInVersion = fixedInVersion;
206
+ existing.fixedInPurl = recommendedPurl;
207
+ existing.provider = providerName;
208
+ existing.source = 'recommendation';
209
+ rankByDep.set(depPurl, providerRank);
210
+ }
211
+ else if (providerRank === existingRank) {
212
+ if (compareVersions(fixedInVersion, existing.fixedInVersion) > 0) {
213
+ existing.fixedInVersion = fixedInVersion;
214
+ existing.fixedInPurl = recommendedPurl;
215
+ existing.provider = providerName;
216
+ existing.source = 'recommendation';
217
+ }
218
+ }
219
+ }
220
+ }
221
+ /**
222
+ * Gets the fixedIn PURL from an issue's remediation, preferring trustedContent.
223
+ * @param {object} issue
224
+ * @returns {string|undefined}
225
+ */
226
+ function getFixedInPurl(issue) {
227
+ if (!issue.remediation) {
228
+ return undefined;
229
+ }
230
+ if (issue.remediation.trustedContent && issue.remediation.trustedContent.ref) {
231
+ return issue.remediation.trustedContent.ref;
232
+ }
233
+ if (issue.remediation.fixedIn) {
234
+ return issue.remediation.fixedIn;
235
+ }
236
+ return undefined;
237
+ }
238
+ /**
239
+ * Extracts advisory objects from an issue.
240
+ * @param {object} issue
241
+ * @returns {Array<{id: string, url: string}>}
242
+ */
243
+ function extractAdvisories(issue) {
244
+ const advisories = [];
245
+ if (issue.remediation && issue.remediation.trustedContent) {
246
+ const tc = issue.remediation.trustedContent;
247
+ if (tc.advisory) {
248
+ advisories.push({
249
+ id: tc.advisory.id || tc.advisory,
250
+ url: tc.advisory.url || '',
251
+ });
252
+ }
253
+ }
254
+ if (issue.advisories) {
255
+ for (const adv of issue.advisories) {
256
+ advisories.push({
257
+ id: adv.id || adv,
258
+ url: adv.url || '',
259
+ });
260
+ }
261
+ }
262
+ return advisories;
263
+ }
264
+ /**
265
+ * Merges new advisories into existing list, avoiding duplicates by id.
266
+ * @param {Array<{id: string, url: string}>} existing
267
+ * @param {Array<{id: string, url: string}>} incoming
268
+ */
269
+ function mergeAdvisories(existing, incoming) {
270
+ const seen = new Set(existing.map(a => a.id));
271
+ for (const adv of incoming) {
272
+ if (!seen.has(adv.id)) {
273
+ existing.push(adv);
274
+ seen.add(adv.id);
275
+ }
276
+ }
277
+ }
278
+ /**
279
+ * Compares two version strings segment by segment.
280
+ * Returns positive if a > b, negative if a < b, zero if equal.
281
+ * Falls back to lexicographic comparison for non-numeric segments.
282
+ * @param {string} a
283
+ * @param {string} b
284
+ * @returns {number}
285
+ */
286
+ function compareVersions(a, b) {
287
+ const partsA = a.split('.');
288
+ const partsB = b.split('.');
289
+ const len = Math.max(partsA.length, partsB.length);
290
+ for (let i = 0; i < len; i++) {
291
+ const segA = partsA[i] || '';
292
+ const segB = partsB[i] || '';
293
+ const numA = Number(segA);
294
+ const numB = Number(segB);
295
+ if (Number.isFinite(numA) && Number.isFinite(numB)) {
296
+ const diff = numA - numB;
297
+ if (diff !== 0)
298
+ return diff;
299
+ }
300
+ else {
301
+ const cmp = segA.localeCompare(segB);
302
+ if (cmp !== 0)
303
+ return cmp;
304
+ }
305
+ }
306
+ return 0;
307
+ }
308
+ const SEVERITY_ORDER = ['UNKNOWN', 'LOW', 'MEDIUM', 'HIGH', 'CRITICAL'];
309
+ /**
310
+ * Returns the higher of two severity strings, normalized to uppercase.
311
+ * @param {string} a
312
+ * @param {string} b
313
+ * @returns {string}
314
+ */
315
+ function higherSeverity(a, b) {
316
+ const upperA = a.toUpperCase();
317
+ const upperB = b.toUpperCase();
318
+ const indexA = SEVERITY_ORDER.indexOf(upperA);
319
+ const indexB = SEVERITY_ORDER.indexOf(upperB);
320
+ return indexA >= indexB ? upperA : upperB;
321
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Updates dependency versions in a Maven pom.xml while preserving file formatting.
3
+ *
4
+ * Uses `fast-xml-parser` to understand the XML structure (dependencies and properties),
5
+ * then performs position-based string splice to replace version strings without altering
6
+ * any other formatting, comments, whitespace, or indentation.
7
+ *
8
+ * Supports `${property}` indirection: when a dependency's `<version>` is a property
9
+ * reference, the updater resolves the property chain (including recursive references)
10
+ * and updates the terminal property value in `<properties>` instead.
11
+ *
12
+ * @param {string} pomContent - the raw pom.xml file content
13
+ * @param {Array<{groupId: string, artifactId: string, newVersion: string}>} versionChanges -
14
+ * list of version changes to apply
15
+ * @returns {{content: string, applied: Array, skipped: Array}} the updated content and
16
+ * lists of applied and skipped changes
17
+ */
18
+ export function updateMavenVersions(pomContent: string, versionChanges: Array<{
19
+ groupId: string;
20
+ artifactId: string;
21
+ newVersion: string;
22
+ }>): {
23
+ content: string;
24
+ applied: any[];
25
+ skipped: any[];
26
+ };