@trustify-da/trustify-da-javascript-client 0.3.0-ea.608d6fa → 0.3.0-ea.61444d4
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 +304 -13
- package/dist/package.json +31 -23
- package/dist/src/analysis.d.ts +28 -5
- package/dist/src/analysis.js +78 -13
- package/dist/src/batch_opts.d.ts +24 -0
- package/dist/src/batch_opts.js +35 -0
- package/dist/src/cli.js +276 -5
- package/dist/src/cyclone_dx_sbom.d.ts +14 -1
- package/dist/src/cyclone_dx_sbom.js +45 -9
- package/dist/src/index.d.ts +142 -7
- package/dist/src/index.js +355 -6
- 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/oci_image/utils.js +11 -2
- package/dist/src/provider.d.ts +8 -4
- package/dist/src/provider.js +16 -5
- package/dist/src/providers/base_java.d.ts +14 -12
- package/dist/src/providers/base_java.js +18 -44
- package/dist/src/providers/base_javascript.d.ts +37 -10
- package/dist/src/providers/base_javascript.js +108 -20
- package/dist/src/providers/base_pyproject.d.ts +158 -0
- package/dist/src/providers/base_pyproject.js +322 -0
- 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 +23 -13
- package/dist/src/providers/golang_gomodules.js +207 -120
- package/dist/src/providers/gomod_parser.d.ts +4 -0
- package/dist/src/providers/gomod_parser.js +16 -0
- package/dist/src/providers/java_gradle.d.ts +19 -0
- package/dist/src/providers/java_gradle.js +116 -2
- 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 +10 -2
- package/dist/src/providers/java_maven.js +165 -1
- package/dist/src/providers/javascript_bun.d.ts +10 -0
- package/dist/src/providers/javascript_bun.js +100 -0
- package/dist/src/providers/javascript_npm.d.ts +6 -0
- package/dist/src/providers/javascript_npm.js +21 -0
- package/dist/src/providers/javascript_pnpm.d.ts +1 -1
- package/dist/src/providers/javascript_pnpm.js +8 -4
- package/dist/src/providers/manifest.d.ts +2 -0
- package/dist/src/providers/manifest.js +22 -4
- 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 +2 -2
- package/dist/src/providers/processors/yarn_berry_processor.js +88 -5
- package/dist/src/providers/processors/yarn_classic_processor.d.ts +2 -2
- 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 +6 -6
- 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 +98 -0
- package/dist/src/providers/python_poetry.js +286 -0
- package/dist/src/providers/python_uv.d.ts +46 -0
- package/dist/src/providers/python_uv.js +269 -0
- package/dist/src/providers/rust_cargo.d.ts +12 -3
- package/dist/src/providers/rust_cargo.js +115 -21
- package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
- package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
- package/dist/src/remediate.d.ts +21 -0
- package/dist/src/remediate.js +149 -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 +14 -1
- package/dist/src/sbom.js +13 -2
- package/dist/src/tools.d.ts +28 -2
- package/dist/src/tools.js +58 -0
- package/dist/src/updaters/maven_updater.d.ts +26 -0
- package/dist/src/updaters/maven_updater.js +322 -0
- package/dist/src/updaters/toml_updater.d.ts +33 -0
- package/dist/src/updaters/toml_updater.js +244 -0
- package/dist/src/workspace.d.ts +70 -0
- package/dist/src/workspace.js +256 -0
- package/package.json +32 -24
|
@@ -4,8 +4,8 @@ import { PackageURL } from 'packageurl-js';
|
|
|
4
4
|
import { parse as parseToml } from 'smol-toml';
|
|
5
5
|
import { getLicense } from '../license/license_utils.js';
|
|
6
6
|
import Sbom from '../sbom.js';
|
|
7
|
-
import { getCustomPath, invokeCommand } from '../tools.js';
|
|
8
|
-
export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
|
|
7
|
+
import { getCustom, getCustomPath, invokeCommand } from '../tools.js';
|
|
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
|
}
|
|
@@ -88,6 +95,26 @@ function readLicenseFromManifest(manifestPath) {
|
|
|
88
95
|
}
|
|
89
96
|
return getLicense(fromManifest, manifestPath);
|
|
90
97
|
}
|
|
98
|
+
/**
|
|
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
|
+
}
|
|
91
118
|
/**
|
|
92
119
|
* Validates that Cargo.lock exists in the manifest directory or in a parent
|
|
93
120
|
* workspace root directory. In Cargo workspaces the lock file always lives at
|
|
@@ -95,10 +122,18 @@ function readLicenseFromManifest(manifestPath) {
|
|
|
95
122
|
* up the directory tree looking for Cargo.lock (stopping when we find a
|
|
96
123
|
* Cargo.toml that contains a [workspace] section, or when we reach the
|
|
97
124
|
* filesystem root).
|
|
125
|
+
* When TRUSTIFY_DA_WORKSPACE_DIR is provided (via env var or opts),
|
|
126
|
+
* checks only that directory for Cargo.lock — no walk-up.
|
|
98
127
|
* @param {string} manifestDir - the directory where the manifest lies
|
|
128
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
|
|
99
129
|
* @returns {boolean} true if Cargo.lock is found
|
|
100
130
|
*/
|
|
101
|
-
function validateLockFile(manifestDir) {
|
|
131
|
+
function validateLockFile(manifestDir, opts = {}) {
|
|
132
|
+
const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
|
|
133
|
+
if (workspaceDir) {
|
|
134
|
+
const dir = path.resolve(workspaceDir);
|
|
135
|
+
return fs.existsSync(path.join(dir, 'Cargo.lock'));
|
|
136
|
+
}
|
|
102
137
|
let dir = path.resolve(manifestDir);
|
|
103
138
|
let parent = dir;
|
|
104
139
|
do {
|
|
@@ -165,13 +200,14 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
|
|
|
165
200
|
let metadata = executeCargoMetadata(cargoBin, manifestDir);
|
|
166
201
|
let ignoredDeps = getIgnoredDeps(manifest, metadata);
|
|
167
202
|
let crateType = detectCrateType(metadata);
|
|
168
|
-
let license = readLicenseFromManifest(manifest);
|
|
203
|
+
let license = readLicenseFromManifest(manifest, metadata);
|
|
204
|
+
let hashMap = parseCargoLockHashes(manifestDir);
|
|
169
205
|
let sbom;
|
|
170
206
|
if (crateType === CrateType.WORKSPACE_VIRTUAL) {
|
|
171
|
-
sbom = handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license);
|
|
207
|
+
sbom = handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license, hashMap);
|
|
172
208
|
}
|
|
173
209
|
else {
|
|
174
|
-
sbom = handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license);
|
|
210
|
+
sbom = handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license, hashMap);
|
|
175
211
|
}
|
|
176
212
|
return sbom;
|
|
177
213
|
}
|
|
@@ -209,6 +245,63 @@ function executeCargoMetadata(cargoBin, manifestDir) {
|
|
|
209
245
|
throw new Error('failed to execute cargo metadata', { cause: error });
|
|
210
246
|
}
|
|
211
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* Parses Cargo.lock to extract SHA-256 checksums for each package.
|
|
250
|
+
* @param {string} manifestDir - directory containing Cargo.toml (Cargo.lock is searched upward)
|
|
251
|
+
* @returns {Map<string, Array<{alg: string, content: string}>>} map of "name@version" to CycloneDX hashes
|
|
252
|
+
* @private
|
|
253
|
+
*/
|
|
254
|
+
function parseCargoLockHashes(manifestDir) {
|
|
255
|
+
let hashMap = new Map();
|
|
256
|
+
let lockPath = findCargoLock(manifestDir);
|
|
257
|
+
if (!lockPath) {
|
|
258
|
+
return hashMap;
|
|
259
|
+
}
|
|
260
|
+
try {
|
|
261
|
+
let content = fs.readFileSync(lockPath, 'utf-8');
|
|
262
|
+
let parsed = parseToml(content);
|
|
263
|
+
for (let pkg of (parsed.package || [])) {
|
|
264
|
+
if (pkg.name && pkg.version && pkg.checksum) {
|
|
265
|
+
hashMap.set(`${pkg.name}@${pkg.version}`, [{ alg: 'SHA-256', content: pkg.checksum }]);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
console.warn('Failed to parse Cargo.lock for hashes, SBOM will be generated without hashes');
|
|
271
|
+
}
|
|
272
|
+
return hashMap;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Searches upward from the manifest directory to find the nearest Cargo.lock.
|
|
276
|
+
* @param {string} manifestDir - starting directory
|
|
277
|
+
* @returns {string|null} absolute path to Cargo.lock or null if not found
|
|
278
|
+
* @private
|
|
279
|
+
*/
|
|
280
|
+
function findCargoLock(manifestDir) {
|
|
281
|
+
let dir = path.resolve(manifestDir);
|
|
282
|
+
let parent = dir;
|
|
283
|
+
do {
|
|
284
|
+
dir = parent;
|
|
285
|
+
let lockFile = path.join(dir, 'Cargo.lock');
|
|
286
|
+
if (fs.existsSync(lockFile)) {
|
|
287
|
+
return lockFile;
|
|
288
|
+
}
|
|
289
|
+
let cargoToml = path.join(dir, 'Cargo.toml');
|
|
290
|
+
if (fs.existsSync(cargoToml)) {
|
|
291
|
+
try {
|
|
292
|
+
let content = fs.readFileSync(cargoToml, 'utf-8');
|
|
293
|
+
if (/\[workspace\]/.test(content)) {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
catch (_) {
|
|
298
|
+
// ignore read errors, keep searching
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
parent = path.dirname(dir);
|
|
302
|
+
} while (parent !== dir);
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
212
305
|
/**
|
|
213
306
|
* Detects the type of Cargo project from metadata.
|
|
214
307
|
* @param {object} metadata - parsed cargo metadata
|
|
@@ -240,7 +333,7 @@ function detectCrateType(metadata) {
|
|
|
240
333
|
* @returns {string} SBOM json string
|
|
241
334
|
* @private
|
|
242
335
|
*/
|
|
243
|
-
function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license) {
|
|
336
|
+
function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license, hashMap) {
|
|
244
337
|
let rootPackageId = metadata.resolve.root;
|
|
245
338
|
let rootPackage = findPackageById(metadata, rootPackageId);
|
|
246
339
|
let rootPurl = toPurl(rootPackage.name, rootPackage.version);
|
|
@@ -251,10 +344,10 @@ function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, licen
|
|
|
251
344
|
return sbom.getAsJsonString(opts);
|
|
252
345
|
}
|
|
253
346
|
if (includeTransitive) {
|
|
254
|
-
addTransitiveDeps(sbom, metadata, rootPackageId, ignoredDeps, new Set(), rootPurl);
|
|
347
|
+
addTransitiveDeps(sbom, metadata, rootPackageId, ignoredDeps, new Set(), hashMap, rootPurl);
|
|
255
348
|
}
|
|
256
349
|
else {
|
|
257
|
-
addDirectDeps(sbom, metadata, rootPackageId, rootPurl, ignoredDeps);
|
|
350
|
+
addDirectDeps(sbom, metadata, rootPackageId, rootPurl, ignoredDeps, hashMap);
|
|
258
351
|
}
|
|
259
352
|
return sbom.getAsJsonString(opts);
|
|
260
353
|
}
|
|
@@ -280,7 +373,7 @@ function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, licen
|
|
|
280
373
|
* @returns {string} SBOM json string
|
|
281
374
|
* @private
|
|
282
375
|
*/
|
|
283
|
-
function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license) {
|
|
376
|
+
function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license, hashMap) {
|
|
284
377
|
let workspaceRoot = metadata.workspace_root;
|
|
285
378
|
let rootName = path.basename(workspaceRoot);
|
|
286
379
|
let workspaceVersion = getWorkspaceVersion(metadata);
|
|
@@ -288,7 +381,6 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
288
381
|
let sbom = new Sbom();
|
|
289
382
|
sbom.addRoot(rootPurl, license);
|
|
290
383
|
if (includeTransitive) {
|
|
291
|
-
// Stack analysis: walk all members and their full dependency trees
|
|
292
384
|
let workspaceMembers = metadata.workspace_members || [];
|
|
293
385
|
for (let memberId of workspaceMembers) {
|
|
294
386
|
let memberPackage = findPackageById(metadata, memberId);
|
|
@@ -299,11 +391,10 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
299
391
|
? toPathDepPurl(memberPackage.name, memberPackage.version)
|
|
300
392
|
: toPurl(memberPackage.name, memberPackage.version);
|
|
301
393
|
sbom.addDependency(rootPurl, memberPurl);
|
|
302
|
-
addTransitiveDeps(sbom, metadata, memberId, ignoredDeps, new Set(), memberPurl);
|
|
394
|
+
addTransitiveDeps(sbom, metadata, memberId, ignoredDeps, new Set(), hashMap, memberPurl);
|
|
303
395
|
}
|
|
304
396
|
}
|
|
305
397
|
else {
|
|
306
|
-
// Component analysis: only [workspace.dependencies] from root Cargo.toml
|
|
307
398
|
let workspaceDeps = getWorkspaceDepsFromManifest(manifest);
|
|
308
399
|
for (let depName of workspaceDeps) {
|
|
309
400
|
if (isDepIgnored(depName, ignoredDeps)) {
|
|
@@ -320,7 +411,8 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
320
411
|
let depPurl = pkg.source == null
|
|
321
412
|
? toPathDepPurl(pkg.name, pkg.version)
|
|
322
413
|
: toPurl(pkg.name, pkg.version);
|
|
323
|
-
|
|
414
|
+
let hashes = hashMap.get(`${pkg.name}@${pkg.version}`);
|
|
415
|
+
sbom.addDependency(rootPurl, depPurl, undefined, hashes);
|
|
324
416
|
}
|
|
325
417
|
}
|
|
326
418
|
return sbom.getAsJsonString(opts);
|
|
@@ -339,7 +431,7 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
339
431
|
* so callers can ensure it matches the purl already added to the SBOM
|
|
340
432
|
* @private
|
|
341
433
|
*/
|
|
342
|
-
function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, startingPurl) {
|
|
434
|
+
function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, hashMap, startingPurl) {
|
|
343
435
|
if (visited.has(packageId)) {
|
|
344
436
|
return;
|
|
345
437
|
}
|
|
@@ -367,8 +459,9 @@ function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, star
|
|
|
367
459
|
let depPurl = depPackage.source == null
|
|
368
460
|
? toPathDepPurl(depPackage.name, depPackage.version)
|
|
369
461
|
: toPurl(depPackage.name, depPackage.version);
|
|
370
|
-
|
|
371
|
-
|
|
462
|
+
let hashes = hashMap ? hashMap.get(`${depPackage.name}@${depPackage.version}`) : undefined;
|
|
463
|
+
sbom.addDependency(sourcePurl, depPurl, undefined, hashes);
|
|
464
|
+
addTransitiveDeps(sbom, metadata, depId, ignoredDeps, visited, hashMap);
|
|
372
465
|
}
|
|
373
466
|
}
|
|
374
467
|
/**
|
|
@@ -381,7 +474,7 @@ function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, star
|
|
|
381
474
|
* @param {Set<string>} ignoredDeps - set of ignored dependency names
|
|
382
475
|
* @private
|
|
383
476
|
*/
|
|
384
|
-
function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps) {
|
|
477
|
+
function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps, hashMap) {
|
|
385
478
|
let resolveNode = findResolveNode(metadata, packageId);
|
|
386
479
|
if (!resolveNode) {
|
|
387
480
|
return;
|
|
@@ -398,7 +491,8 @@ function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps) {
|
|
|
398
491
|
let depPurl = depPackage.source == null
|
|
399
492
|
? toPathDepPurl(depPackage.name, depPackage.version)
|
|
400
493
|
: toPurl(depPackage.name, depPackage.version);
|
|
401
|
-
|
|
494
|
+
let hashes = hashMap ? hashMap.get(`${depPackage.name}@${depPackage.version}`) : undefined;
|
|
495
|
+
sbom.addDependency(parentPurl, depPurl, undefined, hashes);
|
|
402
496
|
}
|
|
403
497
|
}
|
|
404
498
|
/**
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orchestrates the full remediation pipeline for a single manifest or directory:
|
|
3
|
+
* discover manifests → scan via DA backend → extract remediations → apply or preview.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} targetPath - path to a manifest file or directory
|
|
6
|
+
* @param {object} [options]
|
|
7
|
+
* @param {boolean} [options.dryRun=false] - preview changes without modifying files (applies by default)
|
|
8
|
+
* @param {string} [options.providers] - comma-separated provider list
|
|
9
|
+
* @param {string} [options.sources] - comma-separated source list
|
|
10
|
+
* @param {'dependency'|'bundle'} [options.groupBy='dependency'] - report grouping strategy
|
|
11
|
+
* @returns {Promise<{exitCode: number, output: string}>}
|
|
12
|
+
*/
|
|
13
|
+
export function runRemediation(targetPath: string, options?: {
|
|
14
|
+
dryRun?: boolean | undefined;
|
|
15
|
+
providers?: string | undefined;
|
|
16
|
+
sources?: string | undefined;
|
|
17
|
+
groupBy?: "dependency" | "bundle" | undefined;
|
|
18
|
+
}): Promise<{
|
|
19
|
+
exitCode: number;
|
|
20
|
+
output: string;
|
|
21
|
+
}>;
|
|
@@ -0,0 +1,149 @@
|
|
|
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 { generateReport } from './remediation_report.js';
|
|
7
|
+
import { updateMavenVersions } from './updaters/maven_updater.js';
|
|
8
|
+
import { updateTomlVersions } from './updaters/toml_updater.js';
|
|
9
|
+
import { selectTrustifyDABackend } from './index.js';
|
|
10
|
+
// Mirrors DEFAULT_WORKSPACE_DISCOVERY_IGNORE in workspace.js
|
|
11
|
+
const SKIP_DIRS = new Set(['node_modules', '.git']);
|
|
12
|
+
const MANIFEST_TYPES = [
|
|
13
|
+
{
|
|
14
|
+
test: (basename) => basename === 'pom.xml',
|
|
15
|
+
updater: updateMavenVersions,
|
|
16
|
+
label: 'maven',
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
test: (basename) => basename.endsWith('.versions.toml') || basename === 'libs.versions.toml',
|
|
20
|
+
updater: updateTomlVersions,
|
|
21
|
+
label: 'toml',
|
|
22
|
+
},
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* Discovers supported manifest files recursively within a directory.
|
|
26
|
+
* @param {string} dirPath - absolute path to the directory
|
|
27
|
+
* @returns {string[]} array of absolute paths to supported manifest files
|
|
28
|
+
*/
|
|
29
|
+
function discoverManifests(dirPath) {
|
|
30
|
+
const manifests = [];
|
|
31
|
+
function walk(dir) {
|
|
32
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
33
|
+
for (const entry of entries) {
|
|
34
|
+
if (SKIP_DIRS.has(entry.name)) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const fullPath = path.join(dir, entry.name);
|
|
38
|
+
if (entry.isDirectory()) {
|
|
39
|
+
walk(fullPath);
|
|
40
|
+
}
|
|
41
|
+
else if (getManifestType(entry.name)) {
|
|
42
|
+
manifests.push(fullPath);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
walk(dirPath);
|
|
47
|
+
return manifests;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Returns the manifest type descriptor for a given filename, or null if unsupported.
|
|
51
|
+
* @param {string} basename - the file name to check
|
|
52
|
+
* @returns {{test: function, updater: function, label: string}|null}
|
|
53
|
+
*/
|
|
54
|
+
function getManifestType(basename) {
|
|
55
|
+
return MANIFEST_TYPES.find(t => t.test(basename)) || null;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Orchestrates the full remediation pipeline for a single manifest or directory:
|
|
59
|
+
* discover manifests → scan via DA backend → extract remediations → apply or preview.
|
|
60
|
+
*
|
|
61
|
+
* @param {string} targetPath - path to a manifest file or directory
|
|
62
|
+
* @param {object} [options]
|
|
63
|
+
* @param {boolean} [options.dryRun=false] - preview changes without modifying files (applies by default)
|
|
64
|
+
* @param {string} [options.providers] - comma-separated provider list
|
|
65
|
+
* @param {string} [options.sources] - comma-separated source list
|
|
66
|
+
* @param {'dependency'|'bundle'} [options.groupBy='dependency'] - report grouping strategy
|
|
67
|
+
* @returns {Promise<{exitCode: number, output: string}>}
|
|
68
|
+
*/
|
|
69
|
+
export async function runRemediation(targetPath, options = {}) {
|
|
70
|
+
const { dryRun = false, providers, sources, groupBy = 'dependency' } = options;
|
|
71
|
+
const resolvedPath = path.resolve(targetPath);
|
|
72
|
+
let manifestPaths;
|
|
73
|
+
let stat;
|
|
74
|
+
try {
|
|
75
|
+
stat = fs.statSync(resolvedPath);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
throw new Error(`Path not found: ${resolvedPath}`);
|
|
79
|
+
}
|
|
80
|
+
if (stat.isDirectory()) {
|
|
81
|
+
manifestPaths = discoverManifests(resolvedPath);
|
|
82
|
+
if (manifestPaths.length === 0) {
|
|
83
|
+
return { exitCode: 0, output: 'No supported manifest files found.' };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const basename = path.basename(resolvedPath);
|
|
88
|
+
if (!getManifestType(basename)) {
|
|
89
|
+
throw new Error(`Unsupported manifest type: ${basename}`);
|
|
90
|
+
}
|
|
91
|
+
manifestPaths = [resolvedPath];
|
|
92
|
+
}
|
|
93
|
+
const opts = {};
|
|
94
|
+
if (providers) {
|
|
95
|
+
opts.TRUSTIFY_DA_PROVIDERS = providers;
|
|
96
|
+
}
|
|
97
|
+
if (sources) {
|
|
98
|
+
opts.TRUSTIFY_DA_SOURCES = sources;
|
|
99
|
+
}
|
|
100
|
+
const url = selectTrustifyDABackend(opts);
|
|
101
|
+
const allRemediations = [];
|
|
102
|
+
const appliedFiles = [];
|
|
103
|
+
for (const manifestPath of manifestPaths) {
|
|
104
|
+
const basename = path.basename(manifestPath);
|
|
105
|
+
const manifestType = getManifestType(basename);
|
|
106
|
+
if (!manifestType) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
let provider;
|
|
110
|
+
try {
|
|
111
|
+
provider = match(manifestPath, availableProviders, opts);
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const analysisReport = await analysis.requestStack(provider, manifestPath, url, false, opts);
|
|
117
|
+
const remediations = extractRemediations(analysisReport, {
|
|
118
|
+
providerPriority: providers ? providers.split(',').map(p => p.trim()).filter(Boolean) : undefined,
|
|
119
|
+
});
|
|
120
|
+
if (remediations.length === 0) {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
allRemediations.push(...remediations);
|
|
124
|
+
if (!dryRun) {
|
|
125
|
+
const content = fs.readFileSync(manifestPath, 'utf-8');
|
|
126
|
+
const versionChanges = remediations.map(r => ({
|
|
127
|
+
groupId: r.groupId,
|
|
128
|
+
artifactId: r.artifactId,
|
|
129
|
+
newVersion: r.fixedInVersion,
|
|
130
|
+
}));
|
|
131
|
+
const result = manifestType.updater(content, versionChanges);
|
|
132
|
+
if (result.applied.length > 0) {
|
|
133
|
+
fs.writeFileSync(manifestPath, result.content, 'utf-8');
|
|
134
|
+
appliedFiles.push(manifestPath);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (allRemediations.length === 0) {
|
|
139
|
+
return { exitCode: 0, output: 'No remediations found.' };
|
|
140
|
+
}
|
|
141
|
+
const report = generateReport(allRemediations, { groupBy });
|
|
142
|
+
if (dryRun) {
|
|
143
|
+
return { exitCode: 2, output: report };
|
|
144
|
+
}
|
|
145
|
+
const summary = appliedFiles.length > 0
|
|
146
|
+
? `Updated ${appliedFiles.length} file(s):\n${appliedFiles.map(f => ` ${f}`).join('\n')}\n\n${report}`
|
|
147
|
+
: report;
|
|
148
|
+
return { exitCode: 0, output: summary };
|
|
149
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
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 uses the configured
|
|
6
|
+
* version selection strategy to resolve conflicts. Dependencies with no remediation
|
|
7
|
+
* 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
|
+
* @param {object} [options.versionStrategy] - version selection strategy with selectVersion
|
|
15
|
+
* and resolveConflict methods. Defaults to closestCoverageStrategy.
|
|
16
|
+
* @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[]}>}
|
|
17
|
+
*/
|
|
18
|
+
export function extractRemediations(analysisReport: object, options?: {
|
|
19
|
+
providerPriority?: string[] | undefined;
|
|
20
|
+
versionStrategy?: object | undefined;
|
|
21
|
+
}): Array<{
|
|
22
|
+
purl: string;
|
|
23
|
+
groupId: string;
|
|
24
|
+
artifactId: string;
|
|
25
|
+
currentVersion: string;
|
|
26
|
+
fixedInVersion: string;
|
|
27
|
+
fixedInPurl: string;
|
|
28
|
+
provider: string;
|
|
29
|
+
source: string;
|
|
30
|
+
advisories: Array<{
|
|
31
|
+
id: string;
|
|
32
|
+
url: string;
|
|
33
|
+
}>;
|
|
34
|
+
severity: string;
|
|
35
|
+
cves: string[];
|
|
36
|
+
}>;
|
|
37
|
+
/**
|
|
38
|
+
* Version selection strategy that prefers the closest compatible version
|
|
39
|
+
* within the same major version stream. Falls back to the lowest cross-major
|
|
40
|
+
* version when no same-major option exists.
|
|
41
|
+
* @type {{selectVersion: function(string[], string): string, resolveConflict: function(object, object): string}}
|
|
42
|
+
*/
|
|
43
|
+
export const closestCoverageStrategy: {
|
|
44
|
+
selectVersion: (arg0: string[], arg1: string) => string;
|
|
45
|
+
resolveConflict: (arg0: object, arg1: object) => string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Version selection strategy that always picks the highest version regardless
|
|
49
|
+
* of major version distance. Guarantees maximum CVE coverage but may produce
|
|
50
|
+
* large version jumps. This is the original behavior before pluggable strategies.
|
|
51
|
+
* @type {{selectVersion: function(string[], string): string, resolveConflict: function(object, object): string}}
|
|
52
|
+
*/
|
|
53
|
+
export const highestStrategy: {
|
|
54
|
+
selectVersion: (arg0: string[], arg1: string) => string;
|
|
55
|
+
resolveConflict: (arg0: object, arg1: object) => string;
|
|
56
|
+
};
|
|
57
|
+
export const SEVERITY_ORDER: string[];
|