@trustify-da/trustify-da-javascript-client 0.3.0-ea.608d6fa → 0.3.0-ea.62b88e5
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 +326 -21
- package/dist/package.json +36 -27
- package/dist/src/analysis.d.ts +58 -14
- package/dist/src/analysis.js +118 -17
- package/dist/src/batch_opts.d.ts +24 -0
- package/dist/src/batch_opts.js +35 -0
- package/dist/src/cli.js +357 -8
- package/dist/src/config.d.ts +120 -0
- package/dist/src/config.js +260 -0
- package/dist/src/cyclone_dx_sbom.d.ts +27 -1
- package/dist/src/cyclone_dx_sbom.js +84 -10
- package/dist/src/index.d.ts +151 -16
- package/dist/src/index.js +369 -36
- package/dist/src/license/index.d.ts +4 -4
- package/dist/src/license/index.js +13 -7
- package/dist/src/license/license_utils.js +4 -1
- package/dist/src/license/licenses_api.d.ts +3 -3
- package/dist/src/license/licenses_api.js +1 -1
- 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/package_version.d.ts +8 -0
- package/dist/src/package_version.js +31 -0
- package/dist/src/provider.d.ts +8 -4
- package/dist/src/provider.js +16 -5
- 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 +91 -10
- package/dist/src/providers/base_javascript.js +214 -24
- 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 +208 -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 +67 -0
- package/dist/src/providers/java_gradle.js +320 -22
- 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 +14 -1
- package/dist/src/providers/javascript_pnpm.js +55 -5
- package/dist/src/providers/javascript_yarn.d.ts +12 -0
- package/dist/src/providers/javascript_yarn.js +75 -2
- package/dist/src/providers/manifest.d.ts +2 -0
- package/dist/src/providers/manifest.js +28 -7
- 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 +91 -7
- 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 +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 +275 -0
- package/dist/src/providers/requirements_parser.js +1 -1
- package/dist/src/providers/rust_cargo.d.ts +12 -3
- package/dist/src/providers/rust_cargo.js +107 -31
- 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 +107 -0
- package/dist/src/remediate.js +244 -0
- package/dist/src/remediation.d.ts +81 -0
- package/dist/src/remediation.js +475 -0
- package/dist/src/remediation_report.d.ts +23 -0
- package/dist/src/remediation_report.js +180 -0
- package/dist/src/sbom.d.ts +25 -1
- package/dist/src/sbom.js +23 -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 +71 -0
- package/dist/src/workspace.js +257 -0
- package/package.json +37 -28
|
@@ -0,0 +1,275 @@
|
|
|
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';
|
|
5
|
+
import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
|
|
6
|
+
import { filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore, toManifestGlobPatterns } from '../workspace.js';
|
|
7
|
+
import Base_pyproject from './base_pyproject.js';
|
|
8
|
+
import { evaluateMarker } from './marker_evaluator.js';
|
|
9
|
+
import { getParser, getPinnedVersionQuery } from './requirements_parser.js';
|
|
10
|
+
export default class Python_uv extends Base_pyproject {
|
|
11
|
+
/** @returns {string} */
|
|
12
|
+
_lockFileName() {
|
|
13
|
+
return 'uv.lock';
|
|
14
|
+
}
|
|
15
|
+
/** @returns {string} */
|
|
16
|
+
_cmdName() {
|
|
17
|
+
return 'uv';
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} manifestDir - directory containing the target pyproject.toml
|
|
21
|
+
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
|
|
22
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
23
|
+
* @param {Object} opts
|
|
24
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, import('./base_pyproject.js').GraphEntry>}>}
|
|
25
|
+
*/
|
|
26
|
+
async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
|
|
27
|
+
let projectName = this._getProjectName(parsed);
|
|
28
|
+
let uvOutput = this._getUvExportOutput(manifestDir, opts);
|
|
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
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get the uv export output, either from env var or by running the command.
|
|
75
|
+
* @param {string} manifestDir
|
|
76
|
+
* @param {Object} opts
|
|
77
|
+
* @returns {string}
|
|
78
|
+
*/
|
|
79
|
+
_getUvExportOutput(manifestDir, opts) {
|
|
80
|
+
if (environmentVariableIsPopulated('TRUSTIFY_DA_UV_EXPORT')) {
|
|
81
|
+
return Buffer.from(process.env['TRUSTIFY_DA_UV_EXPORT'], 'base64').toString('ascii');
|
|
82
|
+
}
|
|
83
|
+
let uvBin = getCustomPath('uv', opts);
|
|
84
|
+
return invokeCommand(uvBin, ['export', '--format', 'requirements.txt', '--frozen', '--no-hashes', '--no-dev', '--no-emit-project'], { cwd: manifestDir }).toString();
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Parse uv export output into a dependency graph using tree-sitter-requirements
|
|
88
|
+
* for package/version extraction and string parsing for "# via" comments.
|
|
89
|
+
*
|
|
90
|
+
* @param {string} output
|
|
91
|
+
* @param {string} projectName - canonical project name to identify direct deps
|
|
92
|
+
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
|
|
93
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
94
|
+
*/
|
|
95
|
+
async _parseUvExport(output, projectName, workspaceDir) {
|
|
96
|
+
let [parser, pinnedVersionQuery] = await Promise.all([
|
|
97
|
+
getParser(), getPinnedVersionQuery()
|
|
98
|
+
]);
|
|
99
|
+
let tree = parser.parse(output);
|
|
100
|
+
let root = tree.rootNode;
|
|
101
|
+
let canonProjectName = this._canonicalize(projectName);
|
|
102
|
+
let packages = new Map(); // canonical name -> {name, version, parents: Set}
|
|
103
|
+
let currentPkg = null;
|
|
104
|
+
let collectingVia = false;
|
|
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
|
+
}
|
|
132
|
+
if (child.type === 'requirement') {
|
|
133
|
+
let nameNode = child.children.find(c => c.type === 'package');
|
|
134
|
+
if (!nameNode) {
|
|
135
|
+
continue;
|
|
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
|
+
}
|
|
147
|
+
// PEP 440 direct references (name @ url) — skip, no pinned version available
|
|
148
|
+
if (child.children.find(c => c.type === 'url_spec')) {
|
|
149
|
+
currentPkg = null;
|
|
150
|
+
collectingVia = false;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
let name = nameNode.text;
|
|
154
|
+
let version = null;
|
|
155
|
+
let versionMatches = pinnedVersionQuery.matches(child);
|
|
156
|
+
if (versionMatches.length > 0) {
|
|
157
|
+
version = versionMatches[0].captures.find(c => c.name === 'version').node.text;
|
|
158
|
+
}
|
|
159
|
+
if (!version) {
|
|
160
|
+
throw new Error(`uv export: package '${name}' has no pinned version`);
|
|
161
|
+
}
|
|
162
|
+
let key = this._canonicalize(name);
|
|
163
|
+
currentPkg = { name, version, parents: new Set() };
|
|
164
|
+
packages.set(key, currentPkg);
|
|
165
|
+
collectingVia = false;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (child.type === 'comment' && currentPkg) {
|
|
169
|
+
let text = child.text.trim();
|
|
170
|
+
let viaSingle = text.match(/^# via ([A-Za-z0-9][A-Za-z0-9._-]*)$/);
|
|
171
|
+
if (viaSingle) {
|
|
172
|
+
currentPkg.parents.add(this._canonicalize(viaSingle[1]));
|
|
173
|
+
collectingVia = false;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (text === '# via') {
|
|
177
|
+
collectingVia = true;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (collectingVia) {
|
|
181
|
+
let parentMatch = text.match(/^#\s+([A-Za-z0-9][A-Za-z0-9._-]*)$/);
|
|
182
|
+
if (parentMatch) {
|
|
183
|
+
currentPkg.parents.add(this._canonicalize(parentMatch[1]));
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
collectingVia = false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// Build forward dependency map and extract direct deps in one pass
|
|
191
|
+
let graph = new Map();
|
|
192
|
+
let directDeps = [];
|
|
193
|
+
for (let [key, pkg] of packages) {
|
|
194
|
+
graph.set(key, { name: pkg.name, version: pkg.version, children: [] });
|
|
195
|
+
}
|
|
196
|
+
for (let [childKey, pkg] of packages) {
|
|
197
|
+
for (let parentKey of pkg.parents) {
|
|
198
|
+
if (parentKey === canonProjectName) {
|
|
199
|
+
directDeps.push(childKey);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
let parentEntry = graph.get(parentKey);
|
|
203
|
+
if (parentEntry) {
|
|
204
|
+
parentEntry.children.push(childKey);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return { directDeps, graph };
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const DEFAULT_UV_DISCOVERY_IGNORE = [
|
|
212
|
+
'**/__pycache__/**',
|
|
213
|
+
'**/.venv/**',
|
|
214
|
+
];
|
|
215
|
+
/**
|
|
216
|
+
* Discover all pyproject.toml manifest paths in a uv workspace.
|
|
217
|
+
* Parses `[tool.uv.workspace]` from root pyproject.toml and glob-expands member patterns.
|
|
218
|
+
*
|
|
219
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain pyproject.toml and uv.lock)
|
|
220
|
+
* @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}]
|
|
221
|
+
* @returns {Promise<string[]>} Paths to pyproject.toml files (absolute)
|
|
222
|
+
*/
|
|
223
|
+
export async function discoverUvWorkspaceMembers(workspaceRoot, opts = {}) {
|
|
224
|
+
const root = path.resolve(workspaceRoot);
|
|
225
|
+
const rootPyproject = path.join(root, 'pyproject.toml');
|
|
226
|
+
const uvLock = path.join(root, 'uv.lock');
|
|
227
|
+
if (!fs.existsSync(rootPyproject) || !fs.existsSync(uvLock)) {
|
|
228
|
+
return [];
|
|
229
|
+
}
|
|
230
|
+
let parsed;
|
|
231
|
+
try {
|
|
232
|
+
parsed = parseToml(fs.readFileSync(rootPyproject, 'utf-8'));
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
return [];
|
|
236
|
+
}
|
|
237
|
+
const workspaceConfig = parsed?.tool?.uv?.workspace;
|
|
238
|
+
if (!workspaceConfig) {
|
|
239
|
+
return [];
|
|
240
|
+
}
|
|
241
|
+
const memberPatterns = workspaceConfig.members;
|
|
242
|
+
if (!Array.isArray(memberPatterns) || memberPatterns.length === 0) {
|
|
243
|
+
return [];
|
|
244
|
+
}
|
|
245
|
+
const excludePatterns = Array.isArray(workspaceConfig.exclude) ? workspaceConfig.exclude : [];
|
|
246
|
+
const excludeGlobs = excludePatterns
|
|
247
|
+
.filter(p => typeof p === 'string' && p.trim())
|
|
248
|
+
.map(p => `${p.trim()}/pyproject.toml`);
|
|
249
|
+
const ignorePatterns = [...resolveWorkspaceDiscoveryIgnore(opts), ...DEFAULT_UV_DISCOVERY_IGNORE];
|
|
250
|
+
const globOpts = {
|
|
251
|
+
cwd: root,
|
|
252
|
+
absolute: true,
|
|
253
|
+
onlyFiles: true,
|
|
254
|
+
ignore: [...ignorePatterns, ...excludeGlobs],
|
|
255
|
+
followSymbolicLinks: false,
|
|
256
|
+
};
|
|
257
|
+
const patterns = toManifestGlobPatterns(memberPatterns.filter(p => typeof p === 'string'), 'pyproject.toml');
|
|
258
|
+
const manifestPaths = await fg(patterns, globOpts);
|
|
259
|
+
if (!manifestPaths.includes(rootPyproject) && hasProjectMetadata(parsed)) {
|
|
260
|
+
manifestPaths.unshift(rootPyproject);
|
|
261
|
+
}
|
|
262
|
+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* @param {import('smol-toml').TomlTable} parsedPyProject
|
|
266
|
+
* @returns {boolean}
|
|
267
|
+
*/
|
|
268
|
+
function hasProjectMetadata(parsedPyProject) {
|
|
269
|
+
try {
|
|
270
|
+
return typeof parsedPyProject?.project?.name === 'string' && parsedPyProject.project.name.trim() !== '';
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
@@ -16,7 +16,7 @@ export async function getRequirementQuery() {
|
|
|
16
16
|
}
|
|
17
17
|
export async function getIgnoreQuery() {
|
|
18
18
|
const language = await init();
|
|
19
|
-
return new Query(language, '((requirement (package) @name) @req . (comment) @comment (#match? @comment "^#[\\t ]*exhortignore"))');
|
|
19
|
+
return new Query(language, '((requirement (package) @name) @req . (comment) @comment (#match? @comment "^#[\\t ]*(exhortignore|trustify-da-ignore)(?![\\w-])"))');
|
|
20
20
|
}
|
|
21
21
|
export async function getPinnedVersionQuery() {
|
|
22
22
|
const language = await init();
|
|
@@ -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
|
|
@@ -19,10 +20,15 @@ declare function isSupported(manifestName: string): boolean;
|
|
|
19
20
|
* up the directory tree looking for Cargo.lock (stopping when we find a
|
|
20
21
|
* Cargo.toml that contains a [workspace] section, or when we reach the
|
|
21
22
|
* filesystem root).
|
|
23
|
+
* When TRUSTIFY_DA_WORKSPACE_DIR is provided (via env var or opts),
|
|
24
|
+
* checks only that directory for Cargo.lock — no walk-up.
|
|
22
25
|
* @param {string} manifestDir - the directory where the manifest lies
|
|
26
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
|
|
23
27
|
* @returns {boolean} true if Cargo.lock is found
|
|
24
28
|
*/
|
|
25
|
-
declare function validateLockFile(manifestDir: string
|
|
29
|
+
declare function validateLockFile(manifestDir: string, opts?: {
|
|
30
|
+
TRUSTIFY_DA_WORKSPACE_DIR?: string;
|
|
31
|
+
}): boolean;
|
|
26
32
|
/**
|
|
27
33
|
* Provide content and content type for Cargo component analysis.
|
|
28
34
|
* @param {string} manifest - path to Cargo.toml for component report
|
|
@@ -41,7 +47,10 @@ declare function provideStack(manifest: string, opts?: {}): Provided;
|
|
|
41
47
|
* Read project license from Cargo.toml, with fallback to LICENSE file.
|
|
42
48
|
* Supports the `license` field under `[package]` (single crate / workspace
|
|
43
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.
|
|
44
52
|
* @param {string} manifestPath - path to Cargo.toml
|
|
53
|
+
* @param {object} [metadata] - cargo metadata output (needed for workspace license resolution)
|
|
45
54
|
* @returns {string|null} SPDX identifier or null
|
|
46
55
|
*/
|
|
47
|
-
declare function readLicenseFromManifest(manifestPath: string): string | null;
|
|
56
|
+
declare function readLicenseFromManifest(manifestPath: string, metadata?: object): string | null;
|
|
@@ -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
|
}
|
|
@@ -89,22 +96,45 @@ function readLicenseFromManifest(manifestPath) {
|
|
|
89
96
|
return getLicense(fromManifest, manifestPath);
|
|
90
97
|
}
|
|
91
98
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
|
|
97
|
-
|
|
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.
|
|
98
121
|
* @param {string} manifestDir - the directory where the manifest lies
|
|
99
|
-
* @
|
|
122
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
|
|
123
|
+
* @returns {string|null} the directory path containing Cargo.lock, or null if not found
|
|
124
|
+
* @private
|
|
100
125
|
*/
|
|
101
|
-
function
|
|
126
|
+
function _findCargoLockDir(manifestDir, opts = {}) {
|
|
127
|
+
const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
|
|
128
|
+
if (workspaceDir) {
|
|
129
|
+
const dir = path.resolve(workspaceDir);
|
|
130
|
+
return fs.existsSync(path.join(dir, 'Cargo.lock')) ? dir : null;
|
|
131
|
+
}
|
|
102
132
|
let dir = path.resolve(manifestDir);
|
|
103
133
|
let parent = dir;
|
|
104
134
|
do {
|
|
105
135
|
dir = parent;
|
|
106
136
|
if (fs.existsSync(path.join(dir, 'Cargo.lock'))) {
|
|
107
|
-
return
|
|
137
|
+
return dir;
|
|
108
138
|
}
|
|
109
139
|
// If this directory has a Cargo.toml with [workspace], the lock file
|
|
110
140
|
// should have been here — stop searching.
|
|
@@ -113,7 +143,7 @@ function validateLockFile(manifestDir) {
|
|
|
113
143
|
try {
|
|
114
144
|
let content = fs.readFileSync(cargoToml, 'utf-8');
|
|
115
145
|
if (/\[workspace\]/.test(content)) {
|
|
116
|
-
return
|
|
146
|
+
return null;
|
|
117
147
|
}
|
|
118
148
|
}
|
|
119
149
|
catch (_) {
|
|
@@ -122,7 +152,23 @@ function validateLockFile(manifestDir) {
|
|
|
122
152
|
}
|
|
123
153
|
parent = path.dirname(dir);
|
|
124
154
|
} while (parent !== dir);
|
|
125
|
-
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;
|
|
126
172
|
}
|
|
127
173
|
/**
|
|
128
174
|
* Provide content and content type for Cargo stack analysis.
|
|
@@ -165,13 +211,14 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
|
|
|
165
211
|
let metadata = executeCargoMetadata(cargoBin, manifestDir);
|
|
166
212
|
let ignoredDeps = getIgnoredDeps(manifest, metadata);
|
|
167
213
|
let crateType = detectCrateType(metadata);
|
|
168
|
-
let license = readLicenseFromManifest(manifest);
|
|
214
|
+
let license = readLicenseFromManifest(manifest, metadata);
|
|
215
|
+
let hashMap = parseCargoLockHashes(manifestDir, opts);
|
|
169
216
|
let sbom;
|
|
170
217
|
if (crateType === CrateType.WORKSPACE_VIRTUAL) {
|
|
171
|
-
sbom = handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license);
|
|
218
|
+
sbom = handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license, hashMap);
|
|
172
219
|
}
|
|
173
220
|
else {
|
|
174
|
-
sbom = handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license);
|
|
221
|
+
sbom = handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license, hashMap);
|
|
175
222
|
}
|
|
176
223
|
return sbom;
|
|
177
224
|
}
|
|
@@ -209,6 +256,34 @@ function executeCargoMetadata(cargoBin, manifestDir) {
|
|
|
209
256
|
throw new Error('failed to execute cargo metadata', { cause: error });
|
|
210
257
|
}
|
|
211
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
|
+
}
|
|
212
287
|
/**
|
|
213
288
|
* Detects the type of Cargo project from metadata.
|
|
214
289
|
* @param {object} metadata - parsed cargo metadata
|
|
@@ -240,7 +315,7 @@ function detectCrateType(metadata) {
|
|
|
240
315
|
* @returns {string} SBOM json string
|
|
241
316
|
* @private
|
|
242
317
|
*/
|
|
243
|
-
function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license) {
|
|
318
|
+
function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license, hashMap) {
|
|
244
319
|
let rootPackageId = metadata.resolve.root;
|
|
245
320
|
let rootPackage = findPackageById(metadata, rootPackageId);
|
|
246
321
|
let rootPurl = toPurl(rootPackage.name, rootPackage.version);
|
|
@@ -251,10 +326,10 @@ function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, licen
|
|
|
251
326
|
return sbom.getAsJsonString(opts);
|
|
252
327
|
}
|
|
253
328
|
if (includeTransitive) {
|
|
254
|
-
addTransitiveDeps(sbom, metadata, rootPackageId, ignoredDeps, new Set(), rootPurl);
|
|
329
|
+
addTransitiveDeps(sbom, metadata, rootPackageId, ignoredDeps, new Set(), hashMap, rootPurl);
|
|
255
330
|
}
|
|
256
331
|
else {
|
|
257
|
-
addDirectDeps(sbom, metadata, rootPackageId, rootPurl, ignoredDeps);
|
|
332
|
+
addDirectDeps(sbom, metadata, rootPackageId, rootPurl, ignoredDeps, hashMap);
|
|
258
333
|
}
|
|
259
334
|
return sbom.getAsJsonString(opts);
|
|
260
335
|
}
|
|
@@ -280,7 +355,7 @@ function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, licen
|
|
|
280
355
|
* @returns {string} SBOM json string
|
|
281
356
|
* @private
|
|
282
357
|
*/
|
|
283
|
-
function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license) {
|
|
358
|
+
function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license, hashMap) {
|
|
284
359
|
let workspaceRoot = metadata.workspace_root;
|
|
285
360
|
let rootName = path.basename(workspaceRoot);
|
|
286
361
|
let workspaceVersion = getWorkspaceVersion(metadata);
|
|
@@ -288,7 +363,6 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
288
363
|
let sbom = new Sbom();
|
|
289
364
|
sbom.addRoot(rootPurl, license);
|
|
290
365
|
if (includeTransitive) {
|
|
291
|
-
// Stack analysis: walk all members and their full dependency trees
|
|
292
366
|
let workspaceMembers = metadata.workspace_members || [];
|
|
293
367
|
for (let memberId of workspaceMembers) {
|
|
294
368
|
let memberPackage = findPackageById(metadata, memberId);
|
|
@@ -299,11 +373,10 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
299
373
|
? toPathDepPurl(memberPackage.name, memberPackage.version)
|
|
300
374
|
: toPurl(memberPackage.name, memberPackage.version);
|
|
301
375
|
sbom.addDependency(rootPurl, memberPurl);
|
|
302
|
-
addTransitiveDeps(sbom, metadata, memberId, ignoredDeps, new Set(), memberPurl);
|
|
376
|
+
addTransitiveDeps(sbom, metadata, memberId, ignoredDeps, new Set(), hashMap, memberPurl);
|
|
303
377
|
}
|
|
304
378
|
}
|
|
305
379
|
else {
|
|
306
|
-
// Component analysis: only [workspace.dependencies] from root Cargo.toml
|
|
307
380
|
let workspaceDeps = getWorkspaceDepsFromManifest(manifest);
|
|
308
381
|
for (let depName of workspaceDeps) {
|
|
309
382
|
if (isDepIgnored(depName, ignoredDeps)) {
|
|
@@ -320,7 +393,8 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
320
393
|
let depPurl = pkg.source == null
|
|
321
394
|
? toPathDepPurl(pkg.name, pkg.version)
|
|
322
395
|
: toPurl(pkg.name, pkg.version);
|
|
323
|
-
|
|
396
|
+
let hashes = hashMap.get(`${pkg.name}@${pkg.version}`);
|
|
397
|
+
sbom.addDependency(rootPurl, depPurl, undefined, hashes);
|
|
324
398
|
}
|
|
325
399
|
}
|
|
326
400
|
return sbom.getAsJsonString(opts);
|
|
@@ -339,7 +413,7 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
339
413
|
* so callers can ensure it matches the purl already added to the SBOM
|
|
340
414
|
* @private
|
|
341
415
|
*/
|
|
342
|
-
function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, startingPurl) {
|
|
416
|
+
function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, hashMap, startingPurl) {
|
|
343
417
|
if (visited.has(packageId)) {
|
|
344
418
|
return;
|
|
345
419
|
}
|
|
@@ -367,8 +441,9 @@ function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, star
|
|
|
367
441
|
let depPurl = depPackage.source == null
|
|
368
442
|
? toPathDepPurl(depPackage.name, depPackage.version)
|
|
369
443
|
: toPurl(depPackage.name, depPackage.version);
|
|
370
|
-
|
|
371
|
-
|
|
444
|
+
let hashes = hashMap.get(`${depPackage.name}@${depPackage.version}`);
|
|
445
|
+
sbom.addDependency(sourcePurl, depPurl, undefined, hashes);
|
|
446
|
+
addTransitiveDeps(sbom, metadata, depId, ignoredDeps, visited, hashMap);
|
|
372
447
|
}
|
|
373
448
|
}
|
|
374
449
|
/**
|
|
@@ -381,7 +456,7 @@ function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, star
|
|
|
381
456
|
* @param {Set<string>} ignoredDeps - set of ignored dependency names
|
|
382
457
|
* @private
|
|
383
458
|
*/
|
|
384
|
-
function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps) {
|
|
459
|
+
function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps, hashMap) {
|
|
385
460
|
let resolveNode = findResolveNode(metadata, packageId);
|
|
386
461
|
if (!resolveNode) {
|
|
387
462
|
return;
|
|
@@ -398,7 +473,8 @@ function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps) {
|
|
|
398
473
|
let depPurl = depPackage.source == null
|
|
399
474
|
? toPathDepPurl(depPackage.name, depPackage.version)
|
|
400
475
|
: toPurl(depPackage.name, depPackage.version);
|
|
401
|
-
|
|
476
|
+
let hashes = hashMap.get(`${depPackage.name}@${depPackage.version}`);
|
|
477
|
+
sbom.addDependency(parentPurl, depPurl, undefined, hashes);
|
|
402
478
|
}
|
|
403
479
|
}
|
|
404
480
|
/**
|
|
Binary file
|
|
Binary file
|