@trustify-da/trustify-da-javascript-client 0.3.0-ea.f2d5d72 → 0.3.0-ea.fa16f06
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 +2 -2
- package/dist/package.json +26 -23
- package/dist/src/analysis.d.ts +14 -7
- package/dist/src/analysis.js +32 -15
- package/dist/src/cli.js +156 -3
- package/dist/src/cyclone_dx_sbom.d.ts +7 -1
- package/dist/src/cyclone_dx_sbom.js +29 -8
- package/dist/src/index.d.ts +84 -11
- package/dist/src/index.js +95 -8
- 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 +4 -3
- package/dist/src/provider.js +8 -2
- 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 +20 -9
- package/dist/src/providers/base_javascript.js +10 -3
- 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 +65 -8
- 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.js +6 -2
- 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 +6 -2
- 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 +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/rust_cargo.d.ts +6 -2
- package/dist/src/providers/rust_cargo.js +105 -19
- package/dist/src/providers/tree-sitter-containerfile.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 +7 -1
- package/dist/src/sbom.js +4 -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 +9 -0
- package/dist/src/workspace.js +1 -1
- package/package.json +27 -24
|
@@ -1,5 +1,11 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import fg from 'fast-glob';
|
|
4
|
+
import { parse as parseToml } from 'smol-toml';
|
|
1
5
|
import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
|
|
6
|
+
import { filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore, toManifestGlobPatterns } from '../workspace.js';
|
|
2
7
|
import Base_pyproject from './base_pyproject.js';
|
|
8
|
+
import { evaluateMarker } from './marker_evaluator.js';
|
|
3
9
|
import { getParser, getPinnedVersionQuery } from './requirements_parser.js';
|
|
4
10
|
export default class Python_uv extends Base_pyproject {
|
|
5
11
|
/** @returns {string} */
|
|
@@ -11,15 +17,58 @@ export default class Python_uv extends Base_pyproject {
|
|
|
11
17
|
return 'uv';
|
|
12
18
|
}
|
|
13
19
|
/**
|
|
14
|
-
* @param {string} manifestDir
|
|
20
|
+
* @param {string} manifestDir - directory containing the target pyproject.toml
|
|
21
|
+
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
|
|
15
22
|
* @param {object} parsed - parsed pyproject.toml
|
|
16
23
|
* @param {Object} opts
|
|
17
|
-
* @returns {Promise<{directDeps: string[], graph: Map<string,
|
|
24
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, import('./base_pyproject.js').GraphEntry>}>}
|
|
18
25
|
*/
|
|
19
|
-
async _getDependencyData(manifestDir, parsed, opts) {
|
|
26
|
+
async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
|
|
20
27
|
let projectName = this._getProjectName(parsed);
|
|
21
28
|
let uvOutput = this._getUvExportOutput(manifestDir, opts);
|
|
22
|
-
|
|
29
|
+
let { directDeps, graph } = await this._parseUvExport(uvOutput, projectName, workspaceDir);
|
|
30
|
+
await this._attachHashesFromLockFile(path.join(workspaceDir, 'uv.lock'), graph);
|
|
31
|
+
return { directDeps, graph };
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Parse uv.lock and attach SHA-256 hashes to graph entries.
|
|
35
|
+
* @param {string} lockFilePath - path to uv.lock
|
|
36
|
+
* @param {Map<string, import('./base_pyproject.js').GraphEntry>} graph
|
|
37
|
+
*/
|
|
38
|
+
async _attachHashesFromLockFile(lockFilePath, graph) {
|
|
39
|
+
let lockContent;
|
|
40
|
+
try {
|
|
41
|
+
lockContent = await fs.promises.readFile(lockFilePath, 'utf-8');
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
let parsed;
|
|
47
|
+
try {
|
|
48
|
+
parsed = parseToml(lockContent);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
let packages = parsed.package;
|
|
54
|
+
if (!Array.isArray(packages)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
for (let pkg of packages) {
|
|
58
|
+
if (!pkg.name) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
let hashStr = pkg.sdist?.hash || pkg.wheels?.[0]?.hash;
|
|
62
|
+
if (!hashStr || !hashStr.startsWith('sha256:')) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
let key = this._canonicalize(pkg.name);
|
|
66
|
+
let entry = graph.get(key);
|
|
67
|
+
if (!entry || entry.hashes) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
entry.hashes = [{ alg: "SHA-256", content: hashStr.slice(7) }];
|
|
71
|
+
}
|
|
23
72
|
}
|
|
24
73
|
/**
|
|
25
74
|
* Get the uv export output, either from env var or by running the command.
|
|
@@ -32,7 +81,7 @@ export default class Python_uv extends Base_pyproject {
|
|
|
32
81
|
return Buffer.from(process.env['TRUSTIFY_DA_UV_EXPORT'], 'base64').toString('ascii');
|
|
33
82
|
}
|
|
34
83
|
let uvBin = getCustomPath('uv', opts);
|
|
35
|
-
return invokeCommand(uvBin, ['export', '--format', 'requirements.txt', '--frozen', '--no-hashes'], { cwd: manifestDir }).toString();
|
|
84
|
+
return invokeCommand(uvBin, ['export', '--format', 'requirements.txt', '--frozen', '--no-hashes', '--no-dev', '--no-emit-project'], { cwd: manifestDir }).toString();
|
|
36
85
|
}
|
|
37
86
|
/**
|
|
38
87
|
* Parse uv export output into a dependency graph using tree-sitter-requirements
|
|
@@ -40,9 +89,10 @@ export default class Python_uv extends Base_pyproject {
|
|
|
40
89
|
*
|
|
41
90
|
* @param {string} output
|
|
42
91
|
* @param {string} projectName - canonical project name to identify direct deps
|
|
92
|
+
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
|
|
43
93
|
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
44
94
|
*/
|
|
45
|
-
async _parseUvExport(output, projectName) {
|
|
95
|
+
async _parseUvExport(output, projectName, workspaceDir) {
|
|
46
96
|
let [parser, pinnedVersionQuery] = await Promise.all([
|
|
47
97
|
getParser(), getPinnedVersionQuery()
|
|
48
98
|
]);
|
|
@@ -53,11 +103,47 @@ export default class Python_uv extends Base_pyproject {
|
|
|
53
103
|
let currentPkg = null;
|
|
54
104
|
let collectingVia = false;
|
|
55
105
|
for (let child of root.children) {
|
|
106
|
+
if (child.type === 'global_opt') {
|
|
107
|
+
let optNode = child.children.find(c => c.type === 'option');
|
|
108
|
+
let pathNode = child.children.find(c => c.type === 'path');
|
|
109
|
+
if (optNode?.text === '-e' && pathNode && workspaceDir) {
|
|
110
|
+
let memberDir = path.resolve(workspaceDir, pathNode.text);
|
|
111
|
+
let memberManifest = path.join(memberDir, 'pyproject.toml');
|
|
112
|
+
if (fs.existsSync(memberManifest)) {
|
|
113
|
+
let memberParsed = parseToml(fs.readFileSync(memberManifest, 'utf-8'));
|
|
114
|
+
let name = memberParsed.project?.name || memberParsed.tool?.poetry?.name;
|
|
115
|
+
let version = memberParsed.project?.version || memberParsed.tool?.poetry?.version;
|
|
116
|
+
if (name && version) {
|
|
117
|
+
let key = this._canonicalize(name);
|
|
118
|
+
if (key === canonProjectName) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
currentPkg = { name, version, parents: new Set() };
|
|
122
|
+
packages.set(key, currentPkg);
|
|
123
|
+
collectingVia = false;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
currentPkg = null;
|
|
129
|
+
collectingVia = false;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
56
132
|
if (child.type === 'requirement') {
|
|
57
133
|
let nameNode = child.children.find(c => c.type === 'package');
|
|
58
134
|
if (!nameNode) {
|
|
59
135
|
continue;
|
|
60
136
|
}
|
|
137
|
+
// Skip packages with non-matching PEP 508 markers, e.g. "pywin32==311 ; sys_platform == 'win32'"
|
|
138
|
+
let markerNode = child.children.find(c => c.type === 'marker_spec');
|
|
139
|
+
if (markerNode) {
|
|
140
|
+
let markerText = markerNode.text.replace(/^\s*;\s*/, '');
|
|
141
|
+
if (!evaluateMarker(markerText)) {
|
|
142
|
+
currentPkg = null;
|
|
143
|
+
collectingVia = false;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
61
147
|
let name = nameNode.text;
|
|
62
148
|
let version = null;
|
|
63
149
|
let versionMatches = pinnedVersionQuery.matches(child);
|
|
@@ -116,3 +202,68 @@ export default class Python_uv extends Base_pyproject {
|
|
|
116
202
|
return { directDeps, graph };
|
|
117
203
|
}
|
|
118
204
|
}
|
|
205
|
+
const DEFAULT_UV_DISCOVERY_IGNORE = [
|
|
206
|
+
'**/__pycache__/**',
|
|
207
|
+
'**/.venv/**',
|
|
208
|
+
];
|
|
209
|
+
/**
|
|
210
|
+
* Discover all pyproject.toml manifest paths in a uv workspace.
|
|
211
|
+
* Parses `[tool.uv.workspace]` from root pyproject.toml and glob-expands member patterns.
|
|
212
|
+
*
|
|
213
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain pyproject.toml and uv.lock)
|
|
214
|
+
* @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}]
|
|
215
|
+
* @returns {Promise<string[]>} Paths to pyproject.toml files (absolute)
|
|
216
|
+
*/
|
|
217
|
+
export async function discoverUvWorkspaceMembers(workspaceRoot, opts = {}) {
|
|
218
|
+
const root = path.resolve(workspaceRoot);
|
|
219
|
+
const rootPyproject = path.join(root, 'pyproject.toml');
|
|
220
|
+
const uvLock = path.join(root, 'uv.lock');
|
|
221
|
+
if (!fs.existsSync(rootPyproject) || !fs.existsSync(uvLock)) {
|
|
222
|
+
return [];
|
|
223
|
+
}
|
|
224
|
+
let parsed;
|
|
225
|
+
try {
|
|
226
|
+
parsed = parseToml(fs.readFileSync(rootPyproject, 'utf-8'));
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
const workspaceConfig = parsed?.tool?.uv?.workspace;
|
|
232
|
+
if (!workspaceConfig) {
|
|
233
|
+
return [];
|
|
234
|
+
}
|
|
235
|
+
const memberPatterns = workspaceConfig.members;
|
|
236
|
+
if (!Array.isArray(memberPatterns) || memberPatterns.length === 0) {
|
|
237
|
+
return [];
|
|
238
|
+
}
|
|
239
|
+
const excludePatterns = Array.isArray(workspaceConfig.exclude) ? workspaceConfig.exclude : [];
|
|
240
|
+
const excludeGlobs = excludePatterns
|
|
241
|
+
.filter(p => typeof p === 'string' && p.trim())
|
|
242
|
+
.map(p => `${p.trim()}/pyproject.toml`);
|
|
243
|
+
const ignorePatterns = [...resolveWorkspaceDiscoveryIgnore(opts), ...DEFAULT_UV_DISCOVERY_IGNORE];
|
|
244
|
+
const globOpts = {
|
|
245
|
+
cwd: root,
|
|
246
|
+
absolute: true,
|
|
247
|
+
onlyFiles: true,
|
|
248
|
+
ignore: [...ignorePatterns, ...excludeGlobs],
|
|
249
|
+
followSymbolicLinks: false,
|
|
250
|
+
};
|
|
251
|
+
const patterns = toManifestGlobPatterns(memberPatterns.filter(p => typeof p === 'string'), 'pyproject.toml');
|
|
252
|
+
const manifestPaths = await fg(patterns, globOpts);
|
|
253
|
+
if (!manifestPaths.includes(rootPyproject) && hasProjectMetadata(parsed)) {
|
|
254
|
+
manifestPaths.unshift(rootPyproject);
|
|
255
|
+
}
|
|
256
|
+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* @param {import('smol-toml').TomlTable} parsedPyProject
|
|
260
|
+
* @returns {boolean}
|
|
261
|
+
*/
|
|
262
|
+
function hasProjectMetadata(parsedPyProject) {
|
|
263
|
+
try {
|
|
264
|
+
return typeof parsedPyProject?.project?.name === 'string' && parsedPyProject.project.name.trim() !== '';
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
@@ -4,9 +4,10 @@ declare namespace _default {
|
|
|
4
4
|
export { provideComponent };
|
|
5
5
|
export { provideStack };
|
|
6
6
|
export { readLicenseFromManifest };
|
|
7
|
+
export function packageManagerName(): string;
|
|
7
8
|
}
|
|
8
9
|
export default _default;
|
|
9
|
-
export type Provided =
|
|
10
|
+
export type Provided = any;
|
|
10
11
|
/**
|
|
11
12
|
* @param {string} manifestName - the subject manifest name-type
|
|
12
13
|
* @returns {boolean} - return true if `Cargo.toml` is the manifest name-type
|
|
@@ -46,7 +47,10 @@ declare function provideStack(manifest: string, opts?: {}): Provided;
|
|
|
46
47
|
* Read project license from Cargo.toml, with fallback to LICENSE file.
|
|
47
48
|
* Supports the `license` field under `[package]` (single crate / workspace
|
|
48
49
|
* with root) and under `[workspace.package]` (virtual workspaces).
|
|
50
|
+
* When a member crate uses `license = { workspace = true }`, the actual
|
|
51
|
+
* license is resolved from the workspace root Cargo.toml.
|
|
49
52
|
* @param {string} manifestPath - path to Cargo.toml
|
|
53
|
+
* @param {object} [metadata] - cargo metadata output (needed for workspace license resolution)
|
|
50
54
|
* @returns {string|null} SPDX identifier or null
|
|
51
55
|
*/
|
|
52
|
-
declare function readLicenseFromManifest(manifestPath: string): string | null;
|
|
56
|
+
declare function readLicenseFromManifest(manifestPath: string, metadata?: object): string | null;
|
|
@@ -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
|
}
|
|
@@ -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
|
|
@@ -173,13 +200,14 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
|
|
|
173
200
|
let metadata = executeCargoMetadata(cargoBin, manifestDir);
|
|
174
201
|
let ignoredDeps = getIgnoredDeps(manifest, metadata);
|
|
175
202
|
let crateType = detectCrateType(metadata);
|
|
176
|
-
let license = readLicenseFromManifest(manifest);
|
|
203
|
+
let license = readLicenseFromManifest(manifest, metadata);
|
|
204
|
+
let hashMap = parseCargoLockHashes(manifestDir);
|
|
177
205
|
let sbom;
|
|
178
206
|
if (crateType === CrateType.WORKSPACE_VIRTUAL) {
|
|
179
|
-
sbom = handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license);
|
|
207
|
+
sbom = handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license, hashMap);
|
|
180
208
|
}
|
|
181
209
|
else {
|
|
182
|
-
sbom = handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license);
|
|
210
|
+
sbom = handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license, hashMap);
|
|
183
211
|
}
|
|
184
212
|
return sbom;
|
|
185
213
|
}
|
|
@@ -217,6 +245,63 @@ function executeCargoMetadata(cargoBin, manifestDir) {
|
|
|
217
245
|
throw new Error('failed to execute cargo metadata', { cause: error });
|
|
218
246
|
}
|
|
219
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
|
+
}
|
|
220
305
|
/**
|
|
221
306
|
* Detects the type of Cargo project from metadata.
|
|
222
307
|
* @param {object} metadata - parsed cargo metadata
|
|
@@ -248,7 +333,7 @@ function detectCrateType(metadata) {
|
|
|
248
333
|
* @returns {string} SBOM json string
|
|
249
334
|
* @private
|
|
250
335
|
*/
|
|
251
|
-
function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license) {
|
|
336
|
+
function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license, hashMap) {
|
|
252
337
|
let rootPackageId = metadata.resolve.root;
|
|
253
338
|
let rootPackage = findPackageById(metadata, rootPackageId);
|
|
254
339
|
let rootPurl = toPurl(rootPackage.name, rootPackage.version);
|
|
@@ -259,10 +344,10 @@ function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, licen
|
|
|
259
344
|
return sbom.getAsJsonString(opts);
|
|
260
345
|
}
|
|
261
346
|
if (includeTransitive) {
|
|
262
|
-
addTransitiveDeps(sbom, metadata, rootPackageId, ignoredDeps, new Set(), rootPurl);
|
|
347
|
+
addTransitiveDeps(sbom, metadata, rootPackageId, ignoredDeps, new Set(), hashMap, rootPurl);
|
|
263
348
|
}
|
|
264
349
|
else {
|
|
265
|
-
addDirectDeps(sbom, metadata, rootPackageId, rootPurl, ignoredDeps);
|
|
350
|
+
addDirectDeps(sbom, metadata, rootPackageId, rootPurl, ignoredDeps, hashMap);
|
|
266
351
|
}
|
|
267
352
|
return sbom.getAsJsonString(opts);
|
|
268
353
|
}
|
|
@@ -288,7 +373,7 @@ function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, licen
|
|
|
288
373
|
* @returns {string} SBOM json string
|
|
289
374
|
* @private
|
|
290
375
|
*/
|
|
291
|
-
function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license) {
|
|
376
|
+
function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license, hashMap) {
|
|
292
377
|
let workspaceRoot = metadata.workspace_root;
|
|
293
378
|
let rootName = path.basename(workspaceRoot);
|
|
294
379
|
let workspaceVersion = getWorkspaceVersion(metadata);
|
|
@@ -296,7 +381,6 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
296
381
|
let sbom = new Sbom();
|
|
297
382
|
sbom.addRoot(rootPurl, license);
|
|
298
383
|
if (includeTransitive) {
|
|
299
|
-
// Stack analysis: walk all members and their full dependency trees
|
|
300
384
|
let workspaceMembers = metadata.workspace_members || [];
|
|
301
385
|
for (let memberId of workspaceMembers) {
|
|
302
386
|
let memberPackage = findPackageById(metadata, memberId);
|
|
@@ -307,11 +391,10 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
307
391
|
? toPathDepPurl(memberPackage.name, memberPackage.version)
|
|
308
392
|
: toPurl(memberPackage.name, memberPackage.version);
|
|
309
393
|
sbom.addDependency(rootPurl, memberPurl);
|
|
310
|
-
addTransitiveDeps(sbom, metadata, memberId, ignoredDeps, new Set(), memberPurl);
|
|
394
|
+
addTransitiveDeps(sbom, metadata, memberId, ignoredDeps, new Set(), hashMap, memberPurl);
|
|
311
395
|
}
|
|
312
396
|
}
|
|
313
397
|
else {
|
|
314
|
-
// Component analysis: only [workspace.dependencies] from root Cargo.toml
|
|
315
398
|
let workspaceDeps = getWorkspaceDepsFromManifest(manifest);
|
|
316
399
|
for (let depName of workspaceDeps) {
|
|
317
400
|
if (isDepIgnored(depName, ignoredDeps)) {
|
|
@@ -328,7 +411,8 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
328
411
|
let depPurl = pkg.source == null
|
|
329
412
|
? toPathDepPurl(pkg.name, pkg.version)
|
|
330
413
|
: toPurl(pkg.name, pkg.version);
|
|
331
|
-
|
|
414
|
+
let hashes = hashMap.get(`${pkg.name}@${pkg.version}`);
|
|
415
|
+
sbom.addDependency(rootPurl, depPurl, undefined, hashes);
|
|
332
416
|
}
|
|
333
417
|
}
|
|
334
418
|
return sbom.getAsJsonString(opts);
|
|
@@ -347,7 +431,7 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
347
431
|
* so callers can ensure it matches the purl already added to the SBOM
|
|
348
432
|
* @private
|
|
349
433
|
*/
|
|
350
|
-
function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, startingPurl) {
|
|
434
|
+
function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, hashMap, startingPurl) {
|
|
351
435
|
if (visited.has(packageId)) {
|
|
352
436
|
return;
|
|
353
437
|
}
|
|
@@ -375,8 +459,9 @@ function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, star
|
|
|
375
459
|
let depPurl = depPackage.source == null
|
|
376
460
|
? toPathDepPurl(depPackage.name, depPackage.version)
|
|
377
461
|
: toPurl(depPackage.name, depPackage.version);
|
|
378
|
-
|
|
379
|
-
|
|
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);
|
|
380
465
|
}
|
|
381
466
|
}
|
|
382
467
|
/**
|
|
@@ -389,7 +474,7 @@ function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, star
|
|
|
389
474
|
* @param {Set<string>} ignoredDeps - set of ignored dependency names
|
|
390
475
|
* @private
|
|
391
476
|
*/
|
|
392
|
-
function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps) {
|
|
477
|
+
function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps, hashMap) {
|
|
393
478
|
let resolveNode = findResolveNode(metadata, packageId);
|
|
394
479
|
if (!resolveNode) {
|
|
395
480
|
return;
|
|
@@ -406,7 +491,8 @@ function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps) {
|
|
|
406
491
|
let depPurl = depPackage.source == null
|
|
407
492
|
? toPathDepPurl(depPackage.name, depPackage.version)
|
|
408
493
|
: toPurl(depPackage.name, depPackage.version);
|
|
409
|
-
|
|
494
|
+
let hashes = hashMap ? hashMap.get(`${depPackage.name}@${depPackage.version}`) : undefined;
|
|
495
|
+
sbom.addDependency(parentPurl, depPurl, undefined, hashes);
|
|
410
496
|
}
|
|
411
497
|
}
|
|
412
498
|
/**
|
|
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
|
+
}
|