@trustify-da/trustify-da-javascript-client 0.3.0-ea.63ae5c2 → 0.3.0-ea.7144952
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 +119 -4
- package/dist/package.json +10 -2
- package/dist/src/analysis.d.ts +16 -6
- package/dist/src/analysis.js +69 -66
- package/dist/src/batch_opts.d.ts +24 -0
- package/dist/src/batch_opts.js +35 -0
- package/dist/src/cli.js +192 -8
- package/dist/src/cyclone_dx_sbom.d.ts +3 -1
- package/dist/src/cyclone_dx_sbom.js +18 -5
- package/dist/src/index.d.ts +64 -1
- package/dist/src/index.js +267 -4
- package/dist/src/license/index.d.ts +28 -0
- package/dist/src/license/index.js +100 -0
- package/dist/src/license/license_utils.d.ts +40 -0
- package/dist/src/license/license_utils.js +134 -0
- package/dist/src/license/licenses_api.d.ts +34 -0
- package/dist/src/license/licenses_api.js +98 -0
- package/dist/src/license/project_license.d.ts +20 -0
- package/dist/src/license/project_license.js +62 -0
- package/dist/src/provider.d.ts +15 -3
- package/dist/src/provider.js +23 -5
- package/dist/src/providers/base_javascript.d.ts +19 -7
- package/dist/src/providers/base_javascript.js +48 -14
- package/dist/src/providers/golang_gomodules.d.ts +8 -1
- package/dist/src/providers/golang_gomodules.js +13 -4
- package/dist/src/providers/java_gradle.d.ts +6 -0
- package/dist/src/providers/java_gradle.js +12 -2
- package/dist/src/providers/java_maven.d.ts +8 -1
- package/dist/src/providers/java_maven.js +32 -4
- package/dist/src/providers/javascript_pnpm.d.ts +1 -1
- package/dist/src/providers/javascript_pnpm.js +2 -2
- package/dist/src/providers/python_pip.d.ts +7 -0
- package/dist/src/providers/python_pip.js +13 -3
- package/dist/src/providers/requirements_parser.js +5 -8
- package/dist/src/providers/rust_cargo.d.ts +52 -0
- package/dist/src/providers/rust_cargo.js +614 -0
- package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
- package/dist/src/sbom.d.ts +3 -1
- package/dist/src/sbom.js +3 -2
- package/dist/src/tools.d.ts +18 -0
- package/dist/src/tools.js +55 -0
- package/dist/src/workspace.d.ts +61 -0
- package/dist/src/workspace.js +256 -0
- package/package.json +11 -3
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { PackageURL } from 'packageurl-js';
|
|
4
|
+
import { parse as parseToml } from 'smol-toml';
|
|
5
|
+
import { getLicense } from '../license/license_utils.js';
|
|
6
|
+
import Sbom from '../sbom.js';
|
|
7
|
+
import { getCustom, getCustomPath, invokeCommand } from '../tools.js';
|
|
8
|
+
export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
|
|
9
|
+
/** @typedef {import('../provider').Provider} */
|
|
10
|
+
/** @typedef {import('../provider').Provided} Provided */
|
|
11
|
+
/**
|
|
12
|
+
* @type {string} ecosystem identifier for cargo/crates packages
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
const ecosystem = 'cargo';
|
|
16
|
+
/**
|
|
17
|
+
* Ignore markers recognised in Cargo.toml comments.
|
|
18
|
+
* Supports both the legacy `exhortignore` marker and the new `trustify-da-ignore` marker.
|
|
19
|
+
* @type {string[]}
|
|
20
|
+
* @private
|
|
21
|
+
*/
|
|
22
|
+
const IGNORE_MARKERS = ['exhortignore', 'trustify-da-ignore'];
|
|
23
|
+
/**
|
|
24
|
+
* Checks whether a line contains any of the recognised ignore markers.
|
|
25
|
+
* @param {string} line - the line to check
|
|
26
|
+
* @returns {boolean} true if the line contains at least one ignore marker
|
|
27
|
+
* @private
|
|
28
|
+
*/
|
|
29
|
+
function hasIgnoreMarker(line) {
|
|
30
|
+
return IGNORE_MARKERS.some(marker => line.includes(marker));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Checks whether a dependency name is in the ignored set, accounting for
|
|
34
|
+
* Cargo's underscore/hyphen equivalence
|
|
35
|
+
* @param {string} name - the dependency name to check
|
|
36
|
+
* @param {Set<string>} ignoredDeps - set of ignored dependency names
|
|
37
|
+
* @returns {boolean} true if the dependency should be ignored
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
40
|
+
function isDepIgnored(name, ignoredDeps) {
|
|
41
|
+
if (ignoredDeps.has(name)) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
let normalized = name.replace(/_/g, '-');
|
|
45
|
+
if (normalized !== name && ignoredDeps.has(normalized)) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
normalized = name.replace(/-/g, '_');
|
|
49
|
+
if (normalized !== name && ignoredDeps.has(normalized)) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Enum-like constants for Cargo project types.
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
const CrateType = {
|
|
59
|
+
SINGLE_CRATE: 'SINGLE_CRATE',
|
|
60
|
+
WORKSPACE_VIRTUAL: 'WORKSPACE_VIRTUAL',
|
|
61
|
+
WORKSPACE_WITH_ROOT_CRATE: 'WORKSPACE_WITH_ROOT_CRATE'
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* @param {string} manifestName - the subject manifest name-type
|
|
65
|
+
* @returns {boolean} - return true if `Cargo.toml` is the manifest name-type
|
|
66
|
+
*/
|
|
67
|
+
function isSupported(manifestName) {
|
|
68
|
+
return 'Cargo.toml' === manifestName;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Read project license from Cargo.toml, with fallback to LICENSE file.
|
|
72
|
+
* Supports the `license` field under `[package]` (single crate / workspace
|
|
73
|
+
* with root) and under `[workspace.package]` (virtual workspaces).
|
|
74
|
+
* @param {string} manifestPath - path to Cargo.toml
|
|
75
|
+
* @returns {string|null} SPDX identifier or null
|
|
76
|
+
*/
|
|
77
|
+
function readLicenseFromManifest(manifestPath) {
|
|
78
|
+
let fromManifest = null;
|
|
79
|
+
try {
|
|
80
|
+
let content = fs.readFileSync(manifestPath, 'utf-8');
|
|
81
|
+
let parsed = parseToml(content);
|
|
82
|
+
fromManifest = parsed.package?.license
|
|
83
|
+
|| parsed.workspace?.package?.license
|
|
84
|
+
|| null;
|
|
85
|
+
}
|
|
86
|
+
catch (_) {
|
|
87
|
+
// leave fromManifest as null
|
|
88
|
+
}
|
|
89
|
+
return getLicense(fromManifest, manifestPath);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Validates that Cargo.lock exists in the manifest directory or in a parent
|
|
93
|
+
* workspace root directory. In Cargo workspaces the lock file always lives at
|
|
94
|
+
* the workspace root, so when a member crate's Cargo.toml is provided we walk
|
|
95
|
+
* up the directory tree looking for Cargo.lock (stopping when we find a
|
|
96
|
+
* Cargo.toml that contains a [workspace] section, or when we reach the
|
|
97
|
+
* filesystem root).
|
|
98
|
+
* When TRUSTIFY_DA_WORKSPACE_DIR is provided (via env var or opts),
|
|
99
|
+
* checks only that directory for Cargo.lock — no walk-up.
|
|
100
|
+
* @param {string} manifestDir - the directory where the manifest lies
|
|
101
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
|
|
102
|
+
* @returns {boolean} true if Cargo.lock is found
|
|
103
|
+
*/
|
|
104
|
+
function validateLockFile(manifestDir, opts = {}) {
|
|
105
|
+
const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
|
|
106
|
+
if (workspaceDir) {
|
|
107
|
+
const dir = path.resolve(workspaceDir);
|
|
108
|
+
return fs.existsSync(path.join(dir, 'Cargo.lock'));
|
|
109
|
+
}
|
|
110
|
+
let dir = path.resolve(manifestDir);
|
|
111
|
+
let parent = dir;
|
|
112
|
+
do {
|
|
113
|
+
dir = parent;
|
|
114
|
+
if (fs.existsSync(path.join(dir, 'Cargo.lock'))) {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
// If this directory has a Cargo.toml with [workspace], the lock file
|
|
118
|
+
// should have been here — stop searching.
|
|
119
|
+
let cargoToml = path.join(dir, 'Cargo.toml');
|
|
120
|
+
if (fs.existsSync(cargoToml)) {
|
|
121
|
+
try {
|
|
122
|
+
let content = fs.readFileSync(cargoToml, 'utf-8');
|
|
123
|
+
if (/\[workspace\]/.test(content)) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
catch (_) {
|
|
128
|
+
// ignore read errors, keep searching
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
parent = path.dirname(dir);
|
|
132
|
+
} while (parent !== dir);
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Provide content and content type for Cargo stack analysis.
|
|
137
|
+
* @param {string} manifest - the manifest path
|
|
138
|
+
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
139
|
+
* @returns {Provided}
|
|
140
|
+
*/
|
|
141
|
+
function provideStack(manifest, opts = {}) {
|
|
142
|
+
return {
|
|
143
|
+
ecosystem,
|
|
144
|
+
content: getSBOM(manifest, opts, true),
|
|
145
|
+
contentType: 'application/vnd.cyclonedx+json'
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Provide content and content type for Cargo component analysis.
|
|
150
|
+
* @param {string} manifest - path to Cargo.toml for component report
|
|
151
|
+
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
152
|
+
* @returns {Provided}
|
|
153
|
+
*/
|
|
154
|
+
function provideComponent(manifest, opts = {}) {
|
|
155
|
+
return {
|
|
156
|
+
ecosystem,
|
|
157
|
+
content: getSBOM(manifest, opts, false),
|
|
158
|
+
contentType: 'application/vnd.cyclonedx+json'
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Create SBOM json string for a Cargo project.
|
|
163
|
+
* @param {string} manifest - path to Cargo.toml
|
|
164
|
+
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
165
|
+
* @param {boolean} includeTransitive - whether the sbom should contain transitive dependencies
|
|
166
|
+
* @returns {string} the SBOM json content
|
|
167
|
+
* @private
|
|
168
|
+
*/
|
|
169
|
+
function getSBOM(manifest, opts = {}, includeTransitive) {
|
|
170
|
+
let cargoBin = getCustomPath('cargo', opts);
|
|
171
|
+
verifyCargoAccessible(cargoBin);
|
|
172
|
+
let manifestDir = path.dirname(manifest);
|
|
173
|
+
let metadata = executeCargoMetadata(cargoBin, manifestDir);
|
|
174
|
+
let ignoredDeps = getIgnoredDeps(manifest, metadata);
|
|
175
|
+
let crateType = detectCrateType(metadata);
|
|
176
|
+
let license = readLicenseFromManifest(manifest);
|
|
177
|
+
let sbom;
|
|
178
|
+
if (crateType === CrateType.WORKSPACE_VIRTUAL) {
|
|
179
|
+
sbom = handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
sbom = handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license);
|
|
183
|
+
}
|
|
184
|
+
return sbom;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Verifies that the cargo binary is accessible.
|
|
188
|
+
* @param {string} cargoBin - path to cargo binary
|
|
189
|
+
* @throws {Error} if cargo is not accessible
|
|
190
|
+
* @private
|
|
191
|
+
*/
|
|
192
|
+
function verifyCargoAccessible(cargoBin) {
|
|
193
|
+
try {
|
|
194
|
+
invokeCommand(cargoBin, ['--version']);
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
if (error.code === 'ENOENT') {
|
|
198
|
+
throw new Error(`cargo binary is not accessible at "${cargoBin}"`);
|
|
199
|
+
}
|
|
200
|
+
throw new Error('failed to check for cargo binary', { cause: error });
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Executes `cargo metadata` and returns the parsed JSON.
|
|
205
|
+
* @param {string} cargoBin - path to cargo binary
|
|
206
|
+
* @param {string} manifestDir - directory containing Cargo.toml
|
|
207
|
+
* @returns {object} parsed cargo metadata JSON
|
|
208
|
+
* @throws {Error} if cargo metadata fails
|
|
209
|
+
* @private
|
|
210
|
+
*/
|
|
211
|
+
function executeCargoMetadata(cargoBin, manifestDir) {
|
|
212
|
+
try {
|
|
213
|
+
let output = invokeCommand(cargoBin, ['metadata', '--format-version', '1'], { cwd: manifestDir });
|
|
214
|
+
return JSON.parse(output.toString().trim());
|
|
215
|
+
}
|
|
216
|
+
catch (error) {
|
|
217
|
+
throw new Error('failed to execute cargo metadata', { cause: error });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Detects the type of Cargo project from metadata.
|
|
222
|
+
* @param {object} metadata - parsed cargo metadata
|
|
223
|
+
* @returns {string} one of CrateType values
|
|
224
|
+
* @private
|
|
225
|
+
*/
|
|
226
|
+
function detectCrateType(metadata) {
|
|
227
|
+
let rootPackageId = metadata.resolve?.root;
|
|
228
|
+
let workspaceMembers = metadata.workspace_members || [];
|
|
229
|
+
if (!rootPackageId && workspaceMembers.length > 0) {
|
|
230
|
+
return CrateType.WORKSPACE_VIRTUAL;
|
|
231
|
+
}
|
|
232
|
+
if (rootPackageId && workspaceMembers.length > 1) {
|
|
233
|
+
return CrateType.WORKSPACE_WITH_ROOT_CRATE;
|
|
234
|
+
}
|
|
235
|
+
return CrateType.SINGLE_CRATE;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Handles SBOM generation for single crate and workspace-with-root-crate projects.
|
|
239
|
+
* For workspace-with-root-crate, workspace members are only included if they
|
|
240
|
+
* appear in the root crate's dependency graph from cargo metadata. We don't
|
|
241
|
+
* automatically add all members as dependencies since most workspace members
|
|
242
|
+
* (examples, tools, benchmarks) depend ON the root crate, not the other way around.
|
|
243
|
+
* @param {object} metadata - parsed cargo metadata
|
|
244
|
+
* @param {Set<string>} ignoredDeps - set of ignored dependency names
|
|
245
|
+
* @param {boolean} includeTransitive - whether to include transitive dependencies
|
|
246
|
+
* @param {{}} opts - options
|
|
247
|
+
* @param {string|null} license - SPDX license identifier for the root component
|
|
248
|
+
* @returns {string} SBOM json string
|
|
249
|
+
* @private
|
|
250
|
+
*/
|
|
251
|
+
function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license) {
|
|
252
|
+
let rootPackageId = metadata.resolve.root;
|
|
253
|
+
let rootPackage = findPackageById(metadata, rootPackageId);
|
|
254
|
+
let rootPurl = toPurl(rootPackage.name, rootPackage.version);
|
|
255
|
+
let sbom = new Sbom();
|
|
256
|
+
sbom.addRoot(rootPurl, license);
|
|
257
|
+
let resolveNode = findResolveNode(metadata, rootPackageId);
|
|
258
|
+
if (!resolveNode) {
|
|
259
|
+
return sbom.getAsJsonString(opts);
|
|
260
|
+
}
|
|
261
|
+
if (includeTransitive) {
|
|
262
|
+
addTransitiveDeps(sbom, metadata, rootPackageId, ignoredDeps, new Set(), rootPurl);
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
addDirectDeps(sbom, metadata, rootPackageId, rootPurl, ignoredDeps);
|
|
266
|
+
}
|
|
267
|
+
return sbom.getAsJsonString(opts);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Handles SBOM generation for virtual workspace projects.
|
|
271
|
+
*
|
|
272
|
+
* For stack analysis (includeTransitive=true):
|
|
273
|
+
* Iterates all workspace members and walks their full dependency trees.
|
|
274
|
+
*
|
|
275
|
+
* For component analysis (includeTransitive=false):
|
|
276
|
+
* Only includes dependencies explicitly listed in [workspace.dependencies]
|
|
277
|
+
* of the root Cargo.toml. If that section is absent the SBOM contains only
|
|
278
|
+
* the synthetic workspace root with no dependencies — this matches the Java
|
|
279
|
+
* client behaviour where CA "just analyzes direct dependencies defined in
|
|
280
|
+
* Cargo.toml".
|
|
281
|
+
*
|
|
282
|
+
* @param {string} manifest - path to the root Cargo.toml
|
|
283
|
+
* @param {object} metadata - parsed cargo metadata
|
|
284
|
+
* @param {Set<string>} ignoredDeps - set of ignored dependency names
|
|
285
|
+
* @param {boolean} includeTransitive - whether to include transitive dependencies
|
|
286
|
+
* @param {{}} opts - options
|
|
287
|
+
* @param {string|null} license - SPDX license identifier for the root component
|
|
288
|
+
* @returns {string} SBOM json string
|
|
289
|
+
* @private
|
|
290
|
+
*/
|
|
291
|
+
function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license) {
|
|
292
|
+
let workspaceRoot = metadata.workspace_root;
|
|
293
|
+
let rootName = path.basename(workspaceRoot);
|
|
294
|
+
let workspaceVersion = getWorkspaceVersion(metadata);
|
|
295
|
+
let rootPurl = toPurl(rootName, workspaceVersion);
|
|
296
|
+
let sbom = new Sbom();
|
|
297
|
+
sbom.addRoot(rootPurl, license);
|
|
298
|
+
if (includeTransitive) {
|
|
299
|
+
// Stack analysis: walk all members and their full dependency trees
|
|
300
|
+
let workspaceMembers = metadata.workspace_members || [];
|
|
301
|
+
for (let memberId of workspaceMembers) {
|
|
302
|
+
let memberPackage = findPackageById(metadata, memberId);
|
|
303
|
+
if (!memberPackage) {
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
let memberPurl = memberPackage.source == null
|
|
307
|
+
? toPathDepPurl(memberPackage.name, memberPackage.version)
|
|
308
|
+
: toPurl(memberPackage.name, memberPackage.version);
|
|
309
|
+
sbom.addDependency(rootPurl, memberPurl);
|
|
310
|
+
addTransitiveDeps(sbom, metadata, memberId, ignoredDeps, new Set(), memberPurl);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
// Component analysis: only [workspace.dependencies] from root Cargo.toml
|
|
315
|
+
let workspaceDeps = getWorkspaceDepsFromManifest(manifest);
|
|
316
|
+
for (let depName of workspaceDeps) {
|
|
317
|
+
if (isDepIgnored(depName, ignoredDeps)) {
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
let pkg = metadata.packages.find(p => p.name === depName);
|
|
321
|
+
if (!pkg) {
|
|
322
|
+
let altName = depName.replace(/-/g, '_');
|
|
323
|
+
pkg = metadata.packages.find(p => p.name === altName);
|
|
324
|
+
}
|
|
325
|
+
if (!pkg) {
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
let depPurl = pkg.source == null
|
|
329
|
+
? toPathDepPurl(pkg.name, pkg.version)
|
|
330
|
+
: toPurl(pkg.name, pkg.version);
|
|
331
|
+
sbom.addDependency(rootPurl, depPurl);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return sbom.getAsJsonString(opts);
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Recursively adds transitive dependencies to the SBOM.
|
|
338
|
+
* Path dependencies (source == null) are included with a
|
|
339
|
+
* {@code repository_url=local} qualifier so the backend can skip
|
|
340
|
+
* vulnerability checks while still showing them in the dependency tree.
|
|
341
|
+
* @param {Sbom} sbom - the SBOM to add dependencies to
|
|
342
|
+
* @param {object} metadata - parsed cargo metadata
|
|
343
|
+
* @param {string} packageId - the package ID to resolve dependencies for
|
|
344
|
+
* @param {Set<string>} ignoredDeps - set of ignored dependency names
|
|
345
|
+
* @param {Set<string>} visited - set of already-visited package IDs to prevent cycles
|
|
346
|
+
* @param {PackageURL} [startingPurl] - purl to use for the starting package,
|
|
347
|
+
* so callers can ensure it matches the purl already added to the SBOM
|
|
348
|
+
* @private
|
|
349
|
+
*/
|
|
350
|
+
function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, startingPurl) {
|
|
351
|
+
if (visited.has(packageId)) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
visited.add(packageId);
|
|
355
|
+
let resolveNode = findResolveNode(metadata, packageId);
|
|
356
|
+
if (!resolveNode) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
let sourcePackage = findPackageById(metadata, packageId);
|
|
360
|
+
if (!sourcePackage) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
let sourcePurl = startingPurl || (sourcePackage.source == null
|
|
364
|
+
? toPathDepPurl(sourcePackage.name, sourcePackage.version)
|
|
365
|
+
: toPurl(sourcePackage.name, sourcePackage.version));
|
|
366
|
+
let runtimeDeps = filterRuntimeDeps(resolveNode);
|
|
367
|
+
for (let depId of runtimeDeps) {
|
|
368
|
+
let depPackage = findPackageById(metadata, depId);
|
|
369
|
+
if (!depPackage) {
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
if (isDepIgnored(depPackage.name, ignoredDeps)) {
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
let depPurl = depPackage.source == null
|
|
376
|
+
? toPathDepPurl(depPackage.name, depPackage.version)
|
|
377
|
+
: toPurl(depPackage.name, depPackage.version);
|
|
378
|
+
sbom.addDependency(sourcePurl, depPurl);
|
|
379
|
+
addTransitiveDeps(sbom, metadata, depId, ignoredDeps, visited);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Adds only direct (non-transitive) dependencies to the SBOM.
|
|
384
|
+
* Path dependencies are included with a {@code repository_url=local} qualifier.
|
|
385
|
+
* @param {Sbom} sbom - the SBOM to add dependencies to
|
|
386
|
+
* @param {object} metadata - parsed cargo metadata
|
|
387
|
+
* @param {string} packageId - the package ID to resolve dependencies for
|
|
388
|
+
* @param {PackageURL} parentPurl - the parent purl to attach dependencies to
|
|
389
|
+
* @param {Set<string>} ignoredDeps - set of ignored dependency names
|
|
390
|
+
* @private
|
|
391
|
+
*/
|
|
392
|
+
function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps) {
|
|
393
|
+
let resolveNode = findResolveNode(metadata, packageId);
|
|
394
|
+
if (!resolveNode) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
let runtimeDeps = filterRuntimeDeps(resolveNode);
|
|
398
|
+
for (let depId of runtimeDeps) {
|
|
399
|
+
let depPackage = findPackageById(metadata, depId);
|
|
400
|
+
if (!depPackage) {
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
if (isDepIgnored(depPackage.name, ignoredDeps)) {
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
let depPurl = depPackage.source == null
|
|
407
|
+
? toPathDepPurl(depPackage.name, depPackage.version)
|
|
408
|
+
: toPurl(depPackage.name, depPackage.version);
|
|
409
|
+
sbom.addDependency(parentPurl, depPurl);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Filters the deps of a resolve node to only include runtime (normal) dependencies.
|
|
414
|
+
* @param {object} resolveNode - a node from metadata.resolve.nodes
|
|
415
|
+
* @returns {string[]} array of package IDs for runtime dependencies
|
|
416
|
+
* @private
|
|
417
|
+
*/
|
|
418
|
+
function filterRuntimeDeps(resolveNode) {
|
|
419
|
+
if (!resolveNode.deps) {
|
|
420
|
+
return [];
|
|
421
|
+
}
|
|
422
|
+
return resolveNode.deps
|
|
423
|
+
.filter(dep => {
|
|
424
|
+
if (!dep.dep_kinds || dep.dep_kinds.length === 0) {
|
|
425
|
+
return true;
|
|
426
|
+
}
|
|
427
|
+
return dep.dep_kinds.some(dk => dk.kind == null || dk.kind === 'normal');
|
|
428
|
+
})
|
|
429
|
+
.map(dep => dep.pkg);
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Finds a package in the metadata by its ID.
|
|
433
|
+
* @param {object} metadata - parsed cargo metadata
|
|
434
|
+
* @param {string} packageId - the package ID to find
|
|
435
|
+
* @returns {object|undefined} the found package or undefined
|
|
436
|
+
* @private
|
|
437
|
+
*/
|
|
438
|
+
function findPackageById(metadata, packageId) {
|
|
439
|
+
return metadata.packages.find(pkg => pkg.id === packageId);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Finds a resolve node by package ID.
|
|
443
|
+
* @param {object} metadata - parsed cargo metadata
|
|
444
|
+
* @param {string} packageId - the package ID to find
|
|
445
|
+
* @returns {object|undefined} the found resolve node or undefined
|
|
446
|
+
* @private
|
|
447
|
+
*/
|
|
448
|
+
function findResolveNode(metadata, packageId) {
|
|
449
|
+
return metadata.resolve.nodes.find(node => node.id === packageId);
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Parses the root Cargo.toml and returns the dependency names listed in the
|
|
453
|
+
* [workspace.dependencies] section. Returns an empty array when the section
|
|
454
|
+
* does not exist.
|
|
455
|
+
* @param {string} manifest - path to the root Cargo.toml
|
|
456
|
+
* @returns {string[]} list of dependency names
|
|
457
|
+
* @private
|
|
458
|
+
*/
|
|
459
|
+
function getWorkspaceDepsFromManifest(manifest) {
|
|
460
|
+
let content = fs.readFileSync(manifest, 'utf-8');
|
|
461
|
+
let parsed = parseToml(content);
|
|
462
|
+
return Object.keys(parsed.workspace?.dependencies || {});
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Extracts the workspace version from metadata if defined.
|
|
466
|
+
* @param {object} metadata - parsed cargo metadata
|
|
467
|
+
* @returns {string} the workspace version or a placeholder
|
|
468
|
+
* @private
|
|
469
|
+
*/
|
|
470
|
+
function getWorkspaceVersion(metadata) {
|
|
471
|
+
let workspaceRoot = metadata.workspace_root;
|
|
472
|
+
let cargoTomlPath = path.join(workspaceRoot, 'Cargo.toml');
|
|
473
|
+
try {
|
|
474
|
+
let content = fs.readFileSync(cargoTomlPath, 'utf-8');
|
|
475
|
+
let parsed = parseToml(content);
|
|
476
|
+
return parsed.workspace?.package?.version || '0.0.0';
|
|
477
|
+
}
|
|
478
|
+
catch (_) {
|
|
479
|
+
return '0.0.0';
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Parses Cargo.toml for dependencies annotated with an ignore marker.
|
|
484
|
+
* Supports both `exhortignore` and `trustify-da-ignore` markers.
|
|
485
|
+
* Supports inline deps, table-based deps, and workspace-level dependency sections.
|
|
486
|
+
* Uses cargo metadata to discover workspace members (which already handles glob
|
|
487
|
+
* expansion and exclude filtering) instead of parsing workspace member paths ourselves.
|
|
488
|
+
* @param {string} manifest - path to Cargo.toml
|
|
489
|
+
* @param {object} metadata - parsed cargo metadata
|
|
490
|
+
* @returns {Set<string>} set of dependency names to ignore
|
|
491
|
+
* @private
|
|
492
|
+
*/
|
|
493
|
+
function getIgnoredDeps(manifest, metadata) {
|
|
494
|
+
let ignored = new Set();
|
|
495
|
+
scanManifestForIgnored(manifest, ignored);
|
|
496
|
+
// Scan workspace member manifests using metadata
|
|
497
|
+
let manifestDir = path.dirname(manifest);
|
|
498
|
+
let workspaceRoot = metadata.workspace_root;
|
|
499
|
+
let workspaceMembers = metadata.workspace_members || [];
|
|
500
|
+
for (let memberId of workspaceMembers) {
|
|
501
|
+
let memberRelDir = getMemberRelativeDir(memberId, workspaceRoot);
|
|
502
|
+
if (memberRelDir == null) {
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
let memberManifest = path.join(manifestDir, memberRelDir, 'Cargo.toml');
|
|
506
|
+
if (path.resolve(memberManifest) === path.resolve(manifest)) {
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
if (fs.existsSync(memberManifest)) {
|
|
510
|
+
scanManifestForIgnored(memberManifest, ignored);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return ignored;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Extracts a workspace member's directory path relative to the workspace root
|
|
517
|
+
* from its cargo package ID. Workspace member IDs use the `path+file://` scheme,
|
|
518
|
+
* e.g. `path+file:///workspace/root/crates/member#0.1.0`.
|
|
519
|
+
* @param {string} packageId - the cargo package ID
|
|
520
|
+
* @param {string} workspaceRoot - the workspace root path from cargo metadata
|
|
521
|
+
* @returns {string|null} relative directory path, or null if it cannot be determined
|
|
522
|
+
* @private
|
|
523
|
+
*/
|
|
524
|
+
function getMemberRelativeDir(packageId, workspaceRoot) {
|
|
525
|
+
let pathMatch = packageId.match(/path\+file:\/\/(.+?)#/);
|
|
526
|
+
if (!pathMatch) {
|
|
527
|
+
return null;
|
|
528
|
+
}
|
|
529
|
+
let pkgPath = pathMatch[1];
|
|
530
|
+
if (!pkgPath.startsWith(workspaceRoot)) {
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
let relPath = pkgPath.substring(workspaceRoot.length);
|
|
534
|
+
if (relPath.startsWith('/')) {
|
|
535
|
+
relPath = relPath.substring(1);
|
|
536
|
+
}
|
|
537
|
+
return relPath || null;
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Scans a single Cargo.toml for ignored dependencies and adds them to the set.
|
|
541
|
+
* @param {string} manifest - path to Cargo.toml
|
|
542
|
+
* @param {Set<string>} ignored - the set to add ignored dependency names to
|
|
543
|
+
* @private
|
|
544
|
+
*/
|
|
545
|
+
function scanManifestForIgnored(manifest, ignored) {
|
|
546
|
+
let content = fs.readFileSync(manifest, 'utf-8');
|
|
547
|
+
let lines = content.split(/\r?\n/);
|
|
548
|
+
let currentSection = '';
|
|
549
|
+
let currentDepName = null;
|
|
550
|
+
for (let line of lines) {
|
|
551
|
+
let trimmed = line.trim();
|
|
552
|
+
let sectionMatch = trimmed.match(/^\[([^\]]+)\]\s*(?:#.*)?$/);
|
|
553
|
+
if (sectionMatch) {
|
|
554
|
+
currentSection = sectionMatch[1];
|
|
555
|
+
currentDepName = null;
|
|
556
|
+
let tableDep = currentSection.match(/^(?:dependencies|dev-dependencies|build-dependencies|workspace\.dependencies)\.(.+)$/);
|
|
557
|
+
if (tableDep) {
|
|
558
|
+
currentDepName = tableDep[1];
|
|
559
|
+
// If the section header line itself carries an ignore marker,
|
|
560
|
+
// immediately add the dep
|
|
561
|
+
if (hasIgnoreMarker(line)) {
|
|
562
|
+
ignored.add(currentDepName);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
let isIgnored = hasIgnoreMarker(line);
|
|
568
|
+
if (isIgnored && isInDependencySection(currentSection)) {
|
|
569
|
+
let inlineMatch = trimmed.match(/^([a-zA-Z0-9_-]+)\s*=/);
|
|
570
|
+
if (inlineMatch) {
|
|
571
|
+
ignored.add(inlineMatch[1]);
|
|
572
|
+
continue;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
if (isIgnored && currentDepName) {
|
|
576
|
+
ignored.add(currentDepName);
|
|
577
|
+
continue;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Checks if a section name is a dependency section.
|
|
583
|
+
* @param {string} section - the TOML section name
|
|
584
|
+
* @returns {boolean}
|
|
585
|
+
* @private
|
|
586
|
+
*/
|
|
587
|
+
function isInDependencySection(section) {
|
|
588
|
+
return section === 'dependencies' ||
|
|
589
|
+
section === 'dev-dependencies' ||
|
|
590
|
+
section === 'build-dependencies' ||
|
|
591
|
+
section === 'workspace.dependencies';
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Creates a PackageURL for a Cargo/crates.io package.
|
|
595
|
+
* @param {string} name - the crate name
|
|
596
|
+
* @param {string} version - the crate version
|
|
597
|
+
* @returns {PackageURL} the package URL
|
|
598
|
+
* @private
|
|
599
|
+
*/
|
|
600
|
+
function toPurl(name, version) {
|
|
601
|
+
return new PackageURL(ecosystem, undefined, name, version, undefined, undefined);
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Creates a PackageURL for a local path dependency, marked with a
|
|
605
|
+
* {@code repository_url=local} qualifier so the backend can distinguish
|
|
606
|
+
* it from registry packages and skip vulnerability checks.
|
|
607
|
+
* @param {string} name - the crate name
|
|
608
|
+
* @param {string} version - the crate version
|
|
609
|
+
* @returns {PackageURL} the package URL with local qualifier
|
|
610
|
+
* @private
|
|
611
|
+
*/
|
|
612
|
+
function toPathDepPurl(name, version) {
|
|
613
|
+
return new PackageURL(ecosystem, undefined, name, version, { repository_url: 'local' }, undefined);
|
|
614
|
+
}
|
|
Binary file
|
package/dist/src/sbom.d.ts
CHANGED
|
@@ -2,9 +2,10 @@ export default class Sbom {
|
|
|
2
2
|
sbomModel: CycloneDxSbom;
|
|
3
3
|
/**
|
|
4
4
|
* @param {PackageURL} root - add main/root component for sbom
|
|
5
|
+
* @param {string|Array} [licenses] - optional license(s) for the root component
|
|
5
6
|
* @return Sbom
|
|
6
7
|
*/
|
|
7
|
-
addRoot(root: PackageURL): CycloneDxSbom;
|
|
8
|
+
addRoot(root: PackageURL, licenses?: string | any[]): CycloneDxSbom;
|
|
8
9
|
/**
|
|
9
10
|
* @return {{{"bom-ref": string, name, purl: string, type, version}}} root component of sbom.
|
|
10
11
|
*/
|
|
@@ -43,6 +44,7 @@ export default class Sbom {
|
|
|
43
44
|
type: any;
|
|
44
45
|
version: any;
|
|
45
46
|
scope: any;
|
|
47
|
+
licenses?: any;
|
|
46
48
|
};
|
|
47
49
|
/** This method gets a component object, and a string name, and checks if the name is a substring of the component' purl.
|
|
48
50
|
* @param {} component to search in its dependencies
|
package/dist/src/sbom.js
CHANGED
|
@@ -12,10 +12,11 @@ export default class Sbom {
|
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* @param {PackageURL} root - add main/root component for sbom
|
|
15
|
+
* @param {string|Array} [licenses] - optional license(s) for the root component
|
|
15
16
|
* @return Sbom
|
|
16
17
|
*/
|
|
17
|
-
addRoot(root) {
|
|
18
|
-
return this.sbomModel.addRoot(root);
|
|
18
|
+
addRoot(root, licenses) {
|
|
19
|
+
return this.sbomModel.addRoot(root, licenses);
|
|
19
20
|
}
|
|
20
21
|
/**
|
|
21
22
|
* @return {{{"bom-ref": string, name, purl: string, type, version}}} root component of sbom.
|
package/dist/src/tools.d.ts
CHANGED
|
@@ -68,5 +68,23 @@ export function getGitRootDir(cwd: string): string | undefined;
|
|
|
68
68
|
* @returns {string}
|
|
69
69
|
*/
|
|
70
70
|
export function invokeCommand(bin: string, args: Array<string>, opts?: import("child_process").ExecFileOptionsWithStringEncoding): string;
|
|
71
|
+
/**
|
|
72
|
+
* Adds proxy agent configuration to fetch options if a proxy URL is specified
|
|
73
|
+
* @param {RequestInit} options - The base fetch options
|
|
74
|
+
* @param {import("index.js").Options} opts - The trustify DA options that may contain proxy configuration
|
|
75
|
+
* @returns {RequestInit} The fetch options with proxy agent if applicable
|
|
76
|
+
*/
|
|
77
|
+
export function addProxyAgent(options: RequestInit, opts: import("index.js").Options): RequestInit;
|
|
78
|
+
/**
|
|
79
|
+
* Utility function for fetching vendor tokens
|
|
80
|
+
* @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
|
|
81
|
+
* @returns {{}}
|
|
82
|
+
*/
|
|
83
|
+
export function getTokenHeaders(opts?: import("index.js").Options): {};
|
|
71
84
|
export const RegexNotToBeLogged: RegExp;
|
|
85
|
+
export const TRUSTIFY_DA_TOKEN_HEADER: "trust-da-token";
|
|
86
|
+
export const TRUSTIFY_DA_TELEMETRY_ID_HEADER: "telemetry-anonymous-id";
|
|
87
|
+
export const TRUSTIFY_DA_SOURCE_HEADER: "trust-da-source";
|
|
88
|
+
export const TRUSTIFY_DA_OPERATION_TYPE_HEADER: "trust-da-operation-type";
|
|
89
|
+
export const TRUSTIFY_DA_PACKAGE_MANAGER_HEADER: "trust-da-pkg-manager";
|
|
72
90
|
import { PackageURL } from "packageurl-js";
|