@trustify-da/trustify-da-javascript-client 0.3.0-ea.c2f6c64 → 0.3.0-ea.c300d9f
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 +199 -11
- package/dist/package.json +30 -26
- package/dist/src/analysis.d.ts +14 -7
- package/dist/src/analysis.js +32 -14
- package/dist/src/cli.js +188 -6
- package/dist/src/config.d.ts +120 -0
- package/dist/src/config.js +260 -0
- package/dist/src/cyclone_dx_sbom.d.ts +20 -1
- package/dist/src/cyclone_dx_sbom.js +68 -9
- package/dist/src/index.d.ts +79 -15
- package/dist/src/index.js +90 -34
- package/dist/src/license/index.d.ts +3 -3
- package/dist/src/license/index.js +4 -1
- package/dist/src/license/license_utils.js +4 -1
- package/dist/src/license/licenses_api.d.ts +2 -2
- package/dist/src/oci_image/images.d.ts +1 -1
- package/dist/src/oci_image/images.js +15 -1
- package/dist/src/oci_image/utils.d.ts +5 -5
- package/dist/src/package_version.d.ts +8 -0
- package/dist/src/package_version.js +31 -0
- package/dist/src/provider.d.ts +4 -3
- package/dist/src/provider.js +8 -2
- package/dist/src/providers/base_java.d.ts +67 -12
- package/dist/src/providers/base_java.js +82 -61
- package/dist/src/providers/base_javascript.d.ts +74 -9
- package/dist/src/providers/base_javascript.js +116 -7
- package/dist/src/providers/base_pyproject.d.ts +42 -31
- package/dist/src/providers/base_pyproject.js +121 -78
- package/dist/src/providers/containerfile_parser.d.ts +5 -0
- package/dist/src/providers/containerfile_parser.js +20 -0
- package/dist/src/providers/golang_gomodules.d.ts +11 -1
- package/dist/src/providers/golang_gomodules.js +114 -15
- package/dist/src/providers/java_gradle.d.ts +67 -0
- package/dist/src/providers/java_gradle.js +312 -20
- package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
- package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
- package/dist/src/providers/java_maven.d.ts +37 -9
- package/dist/src/providers/java_maven.js +269 -11
- package/dist/src/providers/javascript_bun.d.ts +22 -0
- package/dist/src/providers/javascript_bun.js +141 -0
- package/dist/src/providers/javascript_npm.d.ts +19 -0
- package/dist/src/providers/javascript_npm.js +61 -1
- package/dist/src/providers/javascript_pnpm.d.ts +13 -0
- package/dist/src/providers/javascript_pnpm.js +53 -3
- package/dist/src/providers/javascript_yarn.d.ts +12 -0
- package/dist/src/providers/javascript_yarn.js +75 -2
- package/dist/src/providers/manifest.js +6 -3
- package/dist/src/providers/marker_evaluator.d.ts +14 -0
- package/dist/src/providers/marker_evaluator.js +191 -0
- package/dist/src/providers/oci_dockerfile.d.ts +51 -0
- package/dist/src/providers/oci_dockerfile.js +177 -0
- package/dist/src/providers/processors/yarn_berry_processor.d.ts +6 -2
- package/dist/src/providers/processors/yarn_berry_processor.js +9 -4
- package/dist/src/providers/processors/yarn_classic_processor.d.ts +6 -2
- package/dist/src/providers/processors/yarn_classic_processor.js +8 -6
- package/dist/src/providers/python_controller.d.ts +5 -1
- package/dist/src/providers/python_controller.js +69 -8
- package/dist/src/providers/python_pip.d.ts +5 -0
- package/dist/src/providers/python_pip.js +5 -5
- package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
- package/dist/src/providers/python_pip_pyproject.js +146 -0
- package/dist/src/providers/python_poetry.d.ts +64 -8
- package/dist/src/providers/python_poetry.js +165 -25
- package/dist/src/providers/python_uv.d.ts +22 -2
- package/dist/src/providers/python_uv.js +157 -6
- package/dist/src/providers/requirements_parser.js +1 -1
- package/dist/src/providers/rust_cargo.d.ts +6 -2
- package/dist/src/providers/rust_cargo.js +101 -33
- package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
- package/dist/src/remediate.d.ts +120 -0
- package/dist/src/remediate.js +253 -0
- package/dist/src/remediation.d.ts +57 -0
- package/dist/src/remediation.js +436 -0
- package/dist/src/remediation_report.d.ts +40 -0
- package/dist/src/remediation_report.js +159 -0
- package/dist/src/sbom.d.ts +18 -1
- package/dist/src/sbom.js +14 -2
- package/dist/src/tools.d.ts +37 -13
- package/dist/src/tools.js +92 -13
- package/dist/src/updaters/maven_updater.d.ts +58 -0
- package/dist/src/updaters/maven_updater.js +345 -0
- package/dist/src/updaters/toml_updater.d.ts +62 -0
- package/dist/src/updaters/toml_updater.js +276 -0
- package/dist/src/workspace.d.ts +11 -1
- package/dist/src/workspace.js +3 -2
- package/package.json +31 -27
|
@@ -5,7 +5,7 @@ import { parse as parseToml } from 'smol-toml';
|
|
|
5
5
|
import { getLicense } from '../license/license_utils.js';
|
|
6
6
|
import Sbom from '../sbom.js';
|
|
7
7
|
import { getCustom, getCustomPath, invokeCommand } from '../tools.js';
|
|
8
|
-
export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
|
|
8
|
+
export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest, packageManagerName() { return 'cargo'; } };
|
|
9
9
|
/** @typedef {import('../provider').Provider} */
|
|
10
10
|
/** @typedef {import('../provider').Provided} Provided */
|
|
11
11
|
/**
|
|
@@ -71,15 +71,22 @@ function isSupported(manifestName) {
|
|
|
71
71
|
* Read project license from Cargo.toml, with fallback to LICENSE file.
|
|
72
72
|
* Supports the `license` field under `[package]` (single crate / workspace
|
|
73
73
|
* with root) and under `[workspace.package]` (virtual workspaces).
|
|
74
|
+
* When a member crate uses `license = { workspace = true }`, the actual
|
|
75
|
+
* license is resolved from the workspace root Cargo.toml.
|
|
74
76
|
* @param {string} manifestPath - path to Cargo.toml
|
|
77
|
+
* @param {object} [metadata] - cargo metadata output (needed for workspace license resolution)
|
|
75
78
|
* @returns {string|null} SPDX identifier or null
|
|
76
79
|
*/
|
|
77
|
-
function readLicenseFromManifest(manifestPath) {
|
|
80
|
+
function readLicenseFromManifest(manifestPath, metadata) {
|
|
78
81
|
let fromManifest = null;
|
|
79
82
|
try {
|
|
80
83
|
let content = fs.readFileSync(manifestPath, 'utf-8');
|
|
81
84
|
let parsed = parseToml(content);
|
|
82
|
-
|
|
85
|
+
let packageLicense = parsed.package?.license;
|
|
86
|
+
if (typeof packageLicense === 'object' && packageLicense?.workspace === true) {
|
|
87
|
+
packageLicense = resolveWorkspaceLicense(parsed, metadata);
|
|
88
|
+
}
|
|
89
|
+
fromManifest = packageLicense
|
|
83
90
|
|| parsed.workspace?.package?.license
|
|
84
91
|
|| null;
|
|
85
92
|
}
|
|
@@ -89,30 +96,45 @@ function readLicenseFromManifest(manifestPath) {
|
|
|
89
96
|
return getLicense(fromManifest, manifestPath);
|
|
90
97
|
}
|
|
91
98
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
99
|
+
* Resolve a workspace-inherited license from the workspace root Cargo.toml.
|
|
100
|
+
* @param {object} parsed - the parsed member crate Cargo.toml
|
|
101
|
+
* @param {object} [metadata] - cargo metadata output with workspace_root
|
|
102
|
+
* @returns {string|null} resolved license string or null
|
|
103
|
+
*/
|
|
104
|
+
function resolveWorkspaceLicense(parsed, metadata) {
|
|
105
|
+
if (metadata?.workspace_root) {
|
|
106
|
+
let cargoTomlPath = path.join(metadata.workspace_root, 'Cargo.toml');
|
|
107
|
+
try {
|
|
108
|
+
let content = fs.readFileSync(cargoTomlPath, 'utf-8');
|
|
109
|
+
let workspaceParsed = parseToml(content);
|
|
110
|
+
return workspaceParsed.workspace?.package?.license || null;
|
|
111
|
+
}
|
|
112
|
+
catch (_) {
|
|
113
|
+
// fall through
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return parsed.workspace?.package?.license || null;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Finds the directory containing Cargo.lock, honoring TRUSTIFY_DA_WORKSPACE_DIR.
|
|
120
|
+
* Walks up from manifestDir looking for Cargo.lock, stopping at workspace root or filesystem root.
|
|
100
121
|
* @param {string} manifestDir - the directory where the manifest lies
|
|
101
122
|
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
|
|
102
|
-
* @returns {
|
|
123
|
+
* @returns {string|null} the directory path containing Cargo.lock, or null if not found
|
|
124
|
+
* @private
|
|
103
125
|
*/
|
|
104
|
-
function
|
|
126
|
+
function _findCargoLockDir(manifestDir, opts = {}) {
|
|
105
127
|
const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
|
|
106
128
|
if (workspaceDir) {
|
|
107
129
|
const dir = path.resolve(workspaceDir);
|
|
108
|
-
return fs.existsSync(path.join(dir, 'Cargo.lock'));
|
|
130
|
+
return fs.existsSync(path.join(dir, 'Cargo.lock')) ? dir : null;
|
|
109
131
|
}
|
|
110
132
|
let dir = path.resolve(manifestDir);
|
|
111
133
|
let parent = dir;
|
|
112
134
|
do {
|
|
113
135
|
dir = parent;
|
|
114
136
|
if (fs.existsSync(path.join(dir, 'Cargo.lock'))) {
|
|
115
|
-
return
|
|
137
|
+
return dir;
|
|
116
138
|
}
|
|
117
139
|
// If this directory has a Cargo.toml with [workspace], the lock file
|
|
118
140
|
// should have been here — stop searching.
|
|
@@ -121,7 +143,7 @@ function validateLockFile(manifestDir, opts = {}) {
|
|
|
121
143
|
try {
|
|
122
144
|
let content = fs.readFileSync(cargoToml, 'utf-8');
|
|
123
145
|
if (/\[workspace\]/.test(content)) {
|
|
124
|
-
return
|
|
146
|
+
return null;
|
|
125
147
|
}
|
|
126
148
|
}
|
|
127
149
|
catch (_) {
|
|
@@ -130,7 +152,23 @@ function validateLockFile(manifestDir, opts = {}) {
|
|
|
130
152
|
}
|
|
131
153
|
parent = path.dirname(dir);
|
|
132
154
|
} while (parent !== dir);
|
|
133
|
-
return
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Validates that Cargo.lock exists in the manifest directory or in a parent
|
|
159
|
+
* workspace root directory. In Cargo workspaces the lock file always lives at
|
|
160
|
+
* the workspace root, so when a member crate's Cargo.toml is provided we walk
|
|
161
|
+
* up the directory tree looking for Cargo.lock (stopping when we find a
|
|
162
|
+
* Cargo.toml that contains a [workspace] section, or when we reach the
|
|
163
|
+
* filesystem root).
|
|
164
|
+
* When TRUSTIFY_DA_WORKSPACE_DIR is provided (via env var or opts),
|
|
165
|
+
* checks only that directory for Cargo.lock — no walk-up.
|
|
166
|
+
* @param {string} manifestDir - the directory where the manifest lies
|
|
167
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
|
|
168
|
+
* @returns {boolean} true if Cargo.lock is found
|
|
169
|
+
*/
|
|
170
|
+
function validateLockFile(manifestDir, opts = {}) {
|
|
171
|
+
return _findCargoLockDir(manifestDir, opts) !== null;
|
|
134
172
|
}
|
|
135
173
|
/**
|
|
136
174
|
* Provide content and content type for Cargo stack analysis.
|
|
@@ -173,13 +211,14 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
|
|
|
173
211
|
let metadata = executeCargoMetadata(cargoBin, manifestDir);
|
|
174
212
|
let ignoredDeps = getIgnoredDeps(manifest, metadata);
|
|
175
213
|
let crateType = detectCrateType(metadata);
|
|
176
|
-
let license = readLicenseFromManifest(manifest);
|
|
214
|
+
let license = readLicenseFromManifest(manifest, metadata);
|
|
215
|
+
let hashMap = parseCargoLockHashes(manifestDir, opts);
|
|
177
216
|
let sbom;
|
|
178
217
|
if (crateType === CrateType.WORKSPACE_VIRTUAL) {
|
|
179
|
-
sbom = handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license);
|
|
218
|
+
sbom = handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license, hashMap);
|
|
180
219
|
}
|
|
181
220
|
else {
|
|
182
|
-
sbom = handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license);
|
|
221
|
+
sbom = handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license, hashMap);
|
|
183
222
|
}
|
|
184
223
|
return sbom;
|
|
185
224
|
}
|
|
@@ -217,6 +256,34 @@ function executeCargoMetadata(cargoBin, manifestDir) {
|
|
|
217
256
|
throw new Error('failed to execute cargo metadata', { cause: error });
|
|
218
257
|
}
|
|
219
258
|
}
|
|
259
|
+
/**
|
|
260
|
+
* Parses Cargo.lock to extract SHA-256 checksums for each package.
|
|
261
|
+
* @param {string} manifestDir - directory containing Cargo.toml (Cargo.lock is searched upward)
|
|
262
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
|
|
263
|
+
* @returns {Map<string, Array<{alg: string, content: string}>>} map of "name@version" to CycloneDX hashes
|
|
264
|
+
* @private
|
|
265
|
+
*/
|
|
266
|
+
function parseCargoLockHashes(manifestDir, opts = {}) {
|
|
267
|
+
let hashMap = new Map();
|
|
268
|
+
let lockDir = _findCargoLockDir(manifestDir, opts);
|
|
269
|
+
if (!lockDir) {
|
|
270
|
+
return hashMap;
|
|
271
|
+
}
|
|
272
|
+
let lockPath = path.join(lockDir, 'Cargo.lock');
|
|
273
|
+
try {
|
|
274
|
+
let content = fs.readFileSync(lockPath, 'utf-8');
|
|
275
|
+
let parsed = parseToml(content);
|
|
276
|
+
for (let pkg of (parsed.package || [])) {
|
|
277
|
+
if (pkg.name && pkg.version && pkg.checksum) {
|
|
278
|
+
hashMap.set(`${pkg.name}@${pkg.version}`, [{ alg: 'SHA-256', content: pkg.checksum }]);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
console.warn(`Failed to parse Cargo.lock at ${lockPath} for hashes: ${error.message}. SBOM will be generated without hashes.`);
|
|
284
|
+
}
|
|
285
|
+
return hashMap;
|
|
286
|
+
}
|
|
220
287
|
/**
|
|
221
288
|
* Detects the type of Cargo project from metadata.
|
|
222
289
|
* @param {object} metadata - parsed cargo metadata
|
|
@@ -248,7 +315,7 @@ function detectCrateType(metadata) {
|
|
|
248
315
|
* @returns {string} SBOM json string
|
|
249
316
|
* @private
|
|
250
317
|
*/
|
|
251
|
-
function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license) {
|
|
318
|
+
function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license, hashMap) {
|
|
252
319
|
let rootPackageId = metadata.resolve.root;
|
|
253
320
|
let rootPackage = findPackageById(metadata, rootPackageId);
|
|
254
321
|
let rootPurl = toPurl(rootPackage.name, rootPackage.version);
|
|
@@ -259,10 +326,10 @@ function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, licen
|
|
|
259
326
|
return sbom.getAsJsonString(opts);
|
|
260
327
|
}
|
|
261
328
|
if (includeTransitive) {
|
|
262
|
-
addTransitiveDeps(sbom, metadata, rootPackageId, ignoredDeps, new Set(), rootPurl);
|
|
329
|
+
addTransitiveDeps(sbom, metadata, rootPackageId, ignoredDeps, new Set(), hashMap, rootPurl);
|
|
263
330
|
}
|
|
264
331
|
else {
|
|
265
|
-
addDirectDeps(sbom, metadata, rootPackageId, rootPurl, ignoredDeps);
|
|
332
|
+
addDirectDeps(sbom, metadata, rootPackageId, rootPurl, ignoredDeps, hashMap);
|
|
266
333
|
}
|
|
267
334
|
return sbom.getAsJsonString(opts);
|
|
268
335
|
}
|
|
@@ -288,7 +355,7 @@ function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, licen
|
|
|
288
355
|
* @returns {string} SBOM json string
|
|
289
356
|
* @private
|
|
290
357
|
*/
|
|
291
|
-
function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license) {
|
|
358
|
+
function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license, hashMap) {
|
|
292
359
|
let workspaceRoot = metadata.workspace_root;
|
|
293
360
|
let rootName = path.basename(workspaceRoot);
|
|
294
361
|
let workspaceVersion = getWorkspaceVersion(metadata);
|
|
@@ -296,7 +363,6 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
296
363
|
let sbom = new Sbom();
|
|
297
364
|
sbom.addRoot(rootPurl, license);
|
|
298
365
|
if (includeTransitive) {
|
|
299
|
-
// Stack analysis: walk all members and their full dependency trees
|
|
300
366
|
let workspaceMembers = metadata.workspace_members || [];
|
|
301
367
|
for (let memberId of workspaceMembers) {
|
|
302
368
|
let memberPackage = findPackageById(metadata, memberId);
|
|
@@ -307,11 +373,10 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
307
373
|
? toPathDepPurl(memberPackage.name, memberPackage.version)
|
|
308
374
|
: toPurl(memberPackage.name, memberPackage.version);
|
|
309
375
|
sbom.addDependency(rootPurl, memberPurl);
|
|
310
|
-
addTransitiveDeps(sbom, metadata, memberId, ignoredDeps, new Set(), memberPurl);
|
|
376
|
+
addTransitiveDeps(sbom, metadata, memberId, ignoredDeps, new Set(), hashMap, memberPurl);
|
|
311
377
|
}
|
|
312
378
|
}
|
|
313
379
|
else {
|
|
314
|
-
// Component analysis: only [workspace.dependencies] from root Cargo.toml
|
|
315
380
|
let workspaceDeps = getWorkspaceDepsFromManifest(manifest);
|
|
316
381
|
for (let depName of workspaceDeps) {
|
|
317
382
|
if (isDepIgnored(depName, ignoredDeps)) {
|
|
@@ -328,7 +393,8 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
328
393
|
let depPurl = pkg.source == null
|
|
329
394
|
? toPathDepPurl(pkg.name, pkg.version)
|
|
330
395
|
: toPurl(pkg.name, pkg.version);
|
|
331
|
-
|
|
396
|
+
let hashes = hashMap.get(`${pkg.name}@${pkg.version}`);
|
|
397
|
+
sbom.addDependency(rootPurl, depPurl, undefined, hashes);
|
|
332
398
|
}
|
|
333
399
|
}
|
|
334
400
|
return sbom.getAsJsonString(opts);
|
|
@@ -347,7 +413,7 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
347
413
|
* so callers can ensure it matches the purl already added to the SBOM
|
|
348
414
|
* @private
|
|
349
415
|
*/
|
|
350
|
-
function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, startingPurl) {
|
|
416
|
+
function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, hashMap, startingPurl) {
|
|
351
417
|
if (visited.has(packageId)) {
|
|
352
418
|
return;
|
|
353
419
|
}
|
|
@@ -375,8 +441,9 @@ function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, star
|
|
|
375
441
|
let depPurl = depPackage.source == null
|
|
376
442
|
? toPathDepPurl(depPackage.name, depPackage.version)
|
|
377
443
|
: toPurl(depPackage.name, depPackage.version);
|
|
378
|
-
|
|
379
|
-
|
|
444
|
+
let hashes = hashMap.get(`${depPackage.name}@${depPackage.version}`);
|
|
445
|
+
sbom.addDependency(sourcePurl, depPurl, undefined, hashes);
|
|
446
|
+
addTransitiveDeps(sbom, metadata, depId, ignoredDeps, visited, hashMap);
|
|
380
447
|
}
|
|
381
448
|
}
|
|
382
449
|
/**
|
|
@@ -389,7 +456,7 @@ function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, star
|
|
|
389
456
|
* @param {Set<string>} ignoredDeps - set of ignored dependency names
|
|
390
457
|
* @private
|
|
391
458
|
*/
|
|
392
|
-
function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps) {
|
|
459
|
+
function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps, hashMap) {
|
|
393
460
|
let resolveNode = findResolveNode(metadata, packageId);
|
|
394
461
|
if (!resolveNode) {
|
|
395
462
|
return;
|
|
@@ -406,7 +473,8 @@ function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps) {
|
|
|
406
473
|
let depPurl = depPackage.source == null
|
|
407
474
|
? toPathDepPurl(depPackage.name, depPackage.version)
|
|
408
475
|
: toPurl(depPackage.name, depPackage.version);
|
|
409
|
-
|
|
476
|
+
let hashes = hashMap.get(`${depPackage.name}@${depPackage.version}`);
|
|
477
|
+
sbom.addDependency(parentPurl, depPurl, undefined, hashes);
|
|
410
478
|
}
|
|
411
479
|
}
|
|
412
480
|
/**
|
|
Binary file
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves a target path to the supported manifest files it contains: the single file
|
|
3
|
+
* if `targetPath` is a supported manifest, or every supported manifest discovered
|
|
4
|
+
* recursively if it's a directory. This is the single source of truth for what
|
|
5
|
+
* `runRemediation` will scan, so callers (e.g. the CLI) can reuse it to tell
|
|
6
|
+
* "no manifests here" apart from "manifests, but nothing to fix".
|
|
7
|
+
* @param {string} targetPath - path to a manifest file or directory
|
|
8
|
+
* @returns {string[]} absolute paths to supported manifests (empty for an empty directory)
|
|
9
|
+
* @throws if the path does not exist, or is a file of an unsupported manifest type
|
|
10
|
+
*/
|
|
11
|
+
export function findManifests(targetPath: string): string[];
|
|
12
|
+
/**
|
|
13
|
+
* Orchestrates the full remediation pipeline for a single manifest or directory:
|
|
14
|
+
* discover manifests → scan via DA backend → extract remediations → apply or preview.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} targetPath - path to a manifest file or directory
|
|
17
|
+
* @param {object} [options]
|
|
18
|
+
* @param {boolean} [options.dryRun=false] - preview changes without modifying files (applies by default)
|
|
19
|
+
* @param {string} [options.providers] - comma-separated provider list
|
|
20
|
+
* @param {string} [options.sources] - comma-separated source list
|
|
21
|
+
* @param {string} [options.backendUrl] - Trustify DA backend URL
|
|
22
|
+
* @param {boolean} [options.perDependencyChanges=false] - when true, each remediation is populated with
|
|
23
|
+
* a `changes` array describing the isolated, single-dependency edit (see {@link DependencyFix}). This lets
|
|
24
|
+
* callers create one commit/PR per dependency without attributing diff hunks themselves.
|
|
25
|
+
* @returns {Promise<{exitCode: number, output: string, remediations: Remediation[], manifests: string[], appliedFiles: string[]}>}
|
|
26
|
+
* exitCode is 2 for a dry-run that found remediations (nothing written), 0 otherwise. `remediations`
|
|
27
|
+
* is the structured, per-manifest list of applicable updates — each entry carries the originating
|
|
28
|
+
* manifest path(s) in `files` so callers can group and create per-dependency changes. `appliedFiles`
|
|
29
|
+
* lists only the manifests actually written to disk (empty on a dry-run), so callers can report a
|
|
30
|
+
* truthful "updated N files" count without conflating "had remediations" with "was written".
|
|
31
|
+
*/
|
|
32
|
+
export function runRemediation(targetPath: string, options?: {
|
|
33
|
+
dryRun?: boolean | undefined;
|
|
34
|
+
providers?: string | undefined;
|
|
35
|
+
sources?: string | undefined;
|
|
36
|
+
backendUrl?: string | undefined;
|
|
37
|
+
perDependencyChanges?: boolean | undefined;
|
|
38
|
+
}): Promise<{
|
|
39
|
+
exitCode: number;
|
|
40
|
+
output: string;
|
|
41
|
+
remediations: Remediation[];
|
|
42
|
+
manifests: string[];
|
|
43
|
+
appliedFiles: string[];
|
|
44
|
+
}>;
|
|
45
|
+
/**
|
|
46
|
+
* A single version change requested from an updater: bump `groupId:artifactId` to `newVersion`.
|
|
47
|
+
* The input side of every updater; each updater's output side (its `applied` entries) is its own
|
|
48
|
+
* type — see {@link AppliedChange}.
|
|
49
|
+
*/
|
|
50
|
+
export type VersionChangeRequest = {
|
|
51
|
+
groupId: string;
|
|
52
|
+
artifactId: string;
|
|
53
|
+
newVersion: string;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* One entry from an updater's `applied` list, describing where and how a version was changed.
|
|
57
|
+
* Owned by the updater layer: the union of each updater's applied-entry shape. `changeKey`
|
|
58
|
+
* (per {@link ManifestType}) turns one of these into a stable edit-site identifier.
|
|
59
|
+
*/
|
|
60
|
+
export type AppliedChange = import("./updaters/maven_updater.js").MavenAppliedChange | import("./updaters/toml_updater.js").TomlAppliedChange;
|
|
61
|
+
/**
|
|
62
|
+
* The result of running an updater over a manifest's raw content.
|
|
63
|
+
*/
|
|
64
|
+
export type UpdaterResult = {
|
|
65
|
+
content: string;
|
|
66
|
+
applied: AppliedChange[];
|
|
67
|
+
skipped: Array<{
|
|
68
|
+
groupId: string;
|
|
69
|
+
artifactId: string;
|
|
70
|
+
newVersion: string;
|
|
71
|
+
reason: string;
|
|
72
|
+
}>;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* A supported manifest type and the operations that act on it.
|
|
76
|
+
* `changeKey` builds a stable edit-site key from a single `applied` entry, so callers can detect
|
|
77
|
+
* inseparable remediations (same key => same commit/PR).
|
|
78
|
+
*/
|
|
79
|
+
export type ManifestType = {
|
|
80
|
+
test: (basename: string) => boolean;
|
|
81
|
+
updater: (content: string, versionChanges: VersionChangeRequest[]) => UpdaterResult;
|
|
82
|
+
label: ("maven" | "toml");
|
|
83
|
+
changeKey: (manifestPath: string, applied: AppliedChange) => string;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* An isolated, single-dependency edit to one manifest file.
|
|
87
|
+
* - `after` is the *original* manifest content with only this dependency's fix applied, so a caller
|
|
88
|
+
* can create an isolated commit by writing `after` to `path` on a branch cut from the base.
|
|
89
|
+
* - `changeKey` is a stable identifier for the underlying edit site. Two remediations that share a
|
|
90
|
+
* `changeKey` are inseparable (e.g. two Maven deps whose versions resolve to the same `${property}`,
|
|
91
|
+
* or two Gradle libraries sharing one `version.ref`) and MUST land in the same commit/PR — the
|
|
92
|
+
* caller should union their CVEs/advisories.
|
|
93
|
+
*/
|
|
94
|
+
export type DependencyFix = {
|
|
95
|
+
path: string;
|
|
96
|
+
after: string;
|
|
97
|
+
changeKey: string;
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* A single applicable remediation, as produced by `extractRemediations` and enriched by
|
|
101
|
+
* `runRemediation` with the originating manifest path(s) and (optionally) per-dependency changes.
|
|
102
|
+
*/
|
|
103
|
+
export type Remediation = {
|
|
104
|
+
purl: string;
|
|
105
|
+
groupId: string;
|
|
106
|
+
artifactId: string;
|
|
107
|
+
currentVersion: string;
|
|
108
|
+
fixedInVersion: string;
|
|
109
|
+
fixedInPurl: string;
|
|
110
|
+
provider: string;
|
|
111
|
+
source: string;
|
|
112
|
+
advisories: Array<{
|
|
113
|
+
id: string;
|
|
114
|
+
url: string;
|
|
115
|
+
}>;
|
|
116
|
+
severity: string;
|
|
117
|
+
cves: string[];
|
|
118
|
+
files: string[];
|
|
119
|
+
changes?: DependencyFix[];
|
|
120
|
+
};
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import analysis from './analysis.js';
|
|
4
|
+
import { availableProviders, match } from './provider.js';
|
|
5
|
+
import { extractRemediations } from './remediation.js';
|
|
6
|
+
import { mavenChangeKey, updateMavenVersions } from './updaters/maven_updater.js';
|
|
7
|
+
import { tomlChangeKey, updateTomlVersions } from './updaters/toml_updater.js';
|
|
8
|
+
import { selectTrustifyDABackend } from './index.js';
|
|
9
|
+
// Mirrors DEFAULT_WORKSPACE_DISCOVERY_IGNORE in workspace.js
|
|
10
|
+
const SKIP_DIRS = new Set(['node_modules', '.git']);
|
|
11
|
+
/**
|
|
12
|
+
* A single version change requested from an updater: bump `groupId:artifactId` to `newVersion`.
|
|
13
|
+
* The input side of every updater; each updater's output side (its `applied` entries) is its own
|
|
14
|
+
* type — see {@link AppliedChange}.
|
|
15
|
+
* @typedef {{groupId: string, artifactId: string, newVersion: string}} VersionChangeRequest
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* One entry from an updater's `applied` list, describing where and how a version was changed.
|
|
19
|
+
* Owned by the updater layer: the union of each updater's applied-entry shape. `changeKey`
|
|
20
|
+
* (per {@link ManifestType}) turns one of these into a stable edit-site identifier.
|
|
21
|
+
* @typedef {import('./updaters/maven_updater.js').MavenAppliedChange
|
|
22
|
+
* | import('./updaters/toml_updater.js').TomlAppliedChange} AppliedChange
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* The result of running an updater over a manifest's raw content.
|
|
26
|
+
* @typedef {{content: string, applied: AppliedChange[], skipped: Array<{groupId: string, artifactId: string, newVersion: string, reason: string}>}} UpdaterResult
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* A supported manifest type and the operations that act on it.
|
|
30
|
+
* `changeKey` builds a stable edit-site key from a single `applied` entry, so callers can detect
|
|
31
|
+
* inseparable remediations (same key => same commit/PR).
|
|
32
|
+
* @typedef {{
|
|
33
|
+
* test: (basename: string) => boolean,
|
|
34
|
+
* updater: (content: string, versionChanges: VersionChangeRequest[]) => UpdaterResult,
|
|
35
|
+
* label: ('maven'|'toml'),
|
|
36
|
+
* changeKey: (manifestPath: string, applied: AppliedChange) => string
|
|
37
|
+
* }} ManifestType
|
|
38
|
+
*/
|
|
39
|
+
/**
|
|
40
|
+
* An isolated, single-dependency edit to one manifest file.
|
|
41
|
+
* - `after` is the *original* manifest content with only this dependency's fix applied, so a caller
|
|
42
|
+
* can create an isolated commit by writing `after` to `path` on a branch cut from the base.
|
|
43
|
+
* - `changeKey` is a stable identifier for the underlying edit site. Two remediations that share a
|
|
44
|
+
* `changeKey` are inseparable (e.g. two Maven deps whose versions resolve to the same `${property}`,
|
|
45
|
+
* or two Gradle libraries sharing one `version.ref`) and MUST land in the same commit/PR — the
|
|
46
|
+
* caller should union their CVEs/advisories.
|
|
47
|
+
* @typedef {{ path: string, after: string, changeKey: string }} DependencyFix
|
|
48
|
+
*/
|
|
49
|
+
/**
|
|
50
|
+
* A single applicable remediation, as produced by `extractRemediations` and enriched by
|
|
51
|
+
* `runRemediation` with the originating manifest path(s) and (optionally) per-dependency changes.
|
|
52
|
+
* @typedef {{
|
|
53
|
+
* purl: string,
|
|
54
|
+
* groupId: string,
|
|
55
|
+
* artifactId: string,
|
|
56
|
+
* currentVersion: string,
|
|
57
|
+
* fixedInVersion: string,
|
|
58
|
+
* fixedInPurl: string,
|
|
59
|
+
* provider: string,
|
|
60
|
+
* source: string,
|
|
61
|
+
* advisories: Array<{id: string, url: string}>,
|
|
62
|
+
* severity: string,
|
|
63
|
+
* cves: string[],
|
|
64
|
+
* files: string[],
|
|
65
|
+
* changes?: DependencyFix[]
|
|
66
|
+
* }} Remediation
|
|
67
|
+
*/
|
|
68
|
+
/** @type {ManifestType[]} */
|
|
69
|
+
const MANIFEST_TYPES = [
|
|
70
|
+
{
|
|
71
|
+
test: (basename) => basename === 'pom.xml',
|
|
72
|
+
updater: updateMavenVersions,
|
|
73
|
+
label: 'maven',
|
|
74
|
+
changeKey: mavenChangeKey
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
test: (basename) => basename.endsWith('.versions.toml') || basename === 'libs.versions.toml',
|
|
78
|
+
updater: updateTomlVersions,
|
|
79
|
+
label: 'toml',
|
|
80
|
+
changeKey: tomlChangeKey
|
|
81
|
+
},
|
|
82
|
+
];
|
|
83
|
+
/**
|
|
84
|
+
* Returns the manifest type descriptor for a given filename, or null if unsupported.
|
|
85
|
+
* @param {string} basename - the file name to check
|
|
86
|
+
* @returns {ManifestType|null}
|
|
87
|
+
*/
|
|
88
|
+
function getManifestType(basename) {
|
|
89
|
+
return MANIFEST_TYPES.find(t => t.test(basename)) || null;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Resolves a target path to the supported manifest files it contains: the single file
|
|
93
|
+
* if `targetPath` is a supported manifest, or every supported manifest discovered
|
|
94
|
+
* recursively if it's a directory. This is the single source of truth for what
|
|
95
|
+
* `runRemediation` will scan, so callers (e.g. the CLI) can reuse it to tell
|
|
96
|
+
* "no manifests here" apart from "manifests, but nothing to fix".
|
|
97
|
+
* @param {string} targetPath - path to a manifest file or directory
|
|
98
|
+
* @returns {string[]} absolute paths to supported manifests (empty for an empty directory)
|
|
99
|
+
* @throws if the path does not exist, or is a file of an unsupported manifest type
|
|
100
|
+
*/
|
|
101
|
+
export function findManifests(targetPath) {
|
|
102
|
+
const resolvedPath = path.resolve(targetPath);
|
|
103
|
+
let stat;
|
|
104
|
+
try {
|
|
105
|
+
stat = fs.statSync(resolvedPath);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
throw new Error(`Path not found: ${resolvedPath}`);
|
|
109
|
+
}
|
|
110
|
+
// A single file must itself be a supported manifest.
|
|
111
|
+
if (!stat.isDirectory()) {
|
|
112
|
+
const basename = path.basename(resolvedPath);
|
|
113
|
+
if (!getManifestType(basename)) {
|
|
114
|
+
throw new Error(`Unsupported manifest type: ${basename}`);
|
|
115
|
+
}
|
|
116
|
+
return [resolvedPath];
|
|
117
|
+
}
|
|
118
|
+
// A directory yields every supported manifest found recursively beneath it.
|
|
119
|
+
const manifests = [];
|
|
120
|
+
function walk(dir) {
|
|
121
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
122
|
+
if (SKIP_DIRS.has(entry.name)) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const fullPath = path.join(dir, entry.name);
|
|
126
|
+
if (entry.isDirectory()) {
|
|
127
|
+
walk(fullPath);
|
|
128
|
+
}
|
|
129
|
+
else if (getManifestType(entry.name)) {
|
|
130
|
+
manifests.push(fullPath);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
walk(resolvedPath);
|
|
135
|
+
return manifests;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Orchestrates the full remediation pipeline for a single manifest or directory:
|
|
139
|
+
* discover manifests → scan via DA backend → extract remediations → apply or preview.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} targetPath - path to a manifest file or directory
|
|
142
|
+
* @param {object} [options]
|
|
143
|
+
* @param {boolean} [options.dryRun=false] - preview changes without modifying files (applies by default)
|
|
144
|
+
* @param {string} [options.providers] - comma-separated provider list
|
|
145
|
+
* @param {string} [options.sources] - comma-separated source list
|
|
146
|
+
* @param {string} [options.backendUrl] - Trustify DA backend URL
|
|
147
|
+
* @param {boolean} [options.perDependencyChanges=false] - when true, each remediation is populated with
|
|
148
|
+
* a `changes` array describing the isolated, single-dependency edit (see {@link DependencyFix}). This lets
|
|
149
|
+
* callers create one commit/PR per dependency without attributing diff hunks themselves.
|
|
150
|
+
* @returns {Promise<{exitCode: number, output: string, remediations: Remediation[], manifests: string[], appliedFiles: string[]}>}
|
|
151
|
+
* exitCode is 2 for a dry-run that found remediations (nothing written), 0 otherwise. `remediations`
|
|
152
|
+
* is the structured, per-manifest list of applicable updates — each entry carries the originating
|
|
153
|
+
* manifest path(s) in `files` so callers can group and create per-dependency changes. `appliedFiles`
|
|
154
|
+
* lists only the manifests actually written to disk (empty on a dry-run), so callers can report a
|
|
155
|
+
* truthful "updated N files" count without conflating "had remediations" with "was written".
|
|
156
|
+
*/
|
|
157
|
+
export async function runRemediation(targetPath, options = {}) {
|
|
158
|
+
const { dryRun = false, providers, sources, perDependencyChanges = false, backendUrl } = options;
|
|
159
|
+
const manifestPaths = findManifests(targetPath);
|
|
160
|
+
if (manifestPaths.length === 0) {
|
|
161
|
+
return { exitCode: 0, remediations: [], manifests: manifestPaths, appliedFiles: [] };
|
|
162
|
+
}
|
|
163
|
+
const opts = {};
|
|
164
|
+
if (backendUrl !== undefined) {
|
|
165
|
+
opts.TRUSTIFY_DA_BACKEND_URL = backendUrl;
|
|
166
|
+
}
|
|
167
|
+
if (providers !== undefined) {
|
|
168
|
+
opts.TRUSTIFY_DA_PROVIDERS = providers;
|
|
169
|
+
}
|
|
170
|
+
if (sources !== undefined) {
|
|
171
|
+
opts.TRUSTIFY_DA_SOURCES = sources;
|
|
172
|
+
}
|
|
173
|
+
const url = selectTrustifyDABackend(opts);
|
|
174
|
+
const allRemediations = [];
|
|
175
|
+
const appliedFiles = [];
|
|
176
|
+
for (const manifestPath of manifestPaths) {
|
|
177
|
+
const basename = path.basename(manifestPath);
|
|
178
|
+
const manifestType = getManifestType(basename);
|
|
179
|
+
if (!manifestType) {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
let provider;
|
|
183
|
+
try {
|
|
184
|
+
provider = match(manifestPath, availableProviders, opts);
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const analysisReport = await analysis.requestStack(provider, manifestPath, url, false, opts);
|
|
190
|
+
const remediations = extractRemediations(analysisReport, {
|
|
191
|
+
providerPriority: providers ? providers.split(',').map(p => p.trim()).filter(Boolean) : undefined,
|
|
192
|
+
});
|
|
193
|
+
if (remediations.length === 0) {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
// Tag each remediation with the manifest it came from so callers can group
|
|
197
|
+
// changes per dependency across a multi-manifest workspace.
|
|
198
|
+
for (const remediation of remediations) {
|
|
199
|
+
remediation.files = [manifestPath];
|
|
200
|
+
}
|
|
201
|
+
// Read the pristine manifest once. Both the atomic apply and the per-dependency
|
|
202
|
+
// change computation must diff against the *original* content.
|
|
203
|
+
const needsContent = perDependencyChanges || !dryRun;
|
|
204
|
+
const originalContent = needsContent ? fs.readFileSync(manifestPath, 'utf-8') : null;
|
|
205
|
+
if (perDependencyChanges) {
|
|
206
|
+
// With per-dependency changes every returned remediation must carry an isolated
|
|
207
|
+
// edit. A dependency the updater cannot locate in this manifest — e.g. a vulnerable
|
|
208
|
+
// *transitive* dependency surfaced by analysis but not declared here — yields no
|
|
209
|
+
// applied change, so it is dropped rather than returned with an absent `changes`
|
|
210
|
+
// array (which would crash callers that iterate `remediation.changes` to build PRs).
|
|
211
|
+
const applicable = [];
|
|
212
|
+
for (const remediation of remediations) {
|
|
213
|
+
const isolated = manifestType.updater(originalContent, [{
|
|
214
|
+
groupId: remediation.groupId,
|
|
215
|
+
artifactId: remediation.artifactId,
|
|
216
|
+
newVersion: remediation.fixedInVersion,
|
|
217
|
+
}]);
|
|
218
|
+
if (isolated.applied.length === 0) {
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
remediation.changes = [{
|
|
222
|
+
path: manifestPath,
|
|
223
|
+
after: isolated.content,
|
|
224
|
+
changeKey: manifestType.changeKey(manifestPath, isolated.applied[0])
|
|
225
|
+
}];
|
|
226
|
+
applicable.push(remediation);
|
|
227
|
+
}
|
|
228
|
+
allRemediations.push(...applicable);
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
allRemediations.push(...remediations);
|
|
232
|
+
}
|
|
233
|
+
if (!dryRun) {
|
|
234
|
+
// Disk receives the union of every dependency's fix. When
|
|
235
|
+
// perDependencyChanges is also set, each remediation's `changes[].after`
|
|
236
|
+
// deliberately stays isolated (single-dep) for branch-per-dep workflows.
|
|
237
|
+
const result = manifestType.updater(originalContent, remediations.map(r => ({
|
|
238
|
+
groupId: r.groupId,
|
|
239
|
+
artifactId: r.artifactId,
|
|
240
|
+
newVersion: r.fixedInVersion,
|
|
241
|
+
})));
|
|
242
|
+
if (result.applied.length > 0) {
|
|
243
|
+
fs.writeFileSync(manifestPath, result.content, 'utf-8');
|
|
244
|
+
appliedFiles.push(manifestPath);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (allRemediations.length === 0) {
|
|
249
|
+
return { exitCode: 0, remediations: [], manifests: manifestPaths, appliedFiles };
|
|
250
|
+
}
|
|
251
|
+
// Dry-run signals "changes available but not written" via exit code 2.
|
|
252
|
+
return { exitCode: dryRun ? 2 : 0, remediations: allRemediations, manifests: manifestPaths, appliedFiles };
|
|
253
|
+
}
|