@trustify-da/trustify-da-javascript-client 0.3.0-ea.7ed8d8c → 0.3.0-ea.848421d
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 +191 -11
- package/dist/package.json +13 -4
- package/dist/src/analysis.d.ts +16 -6
- package/dist/src/analysis.js +72 -68
- package/dist/src/batch_opts.d.ts +24 -0
- package/dist/src/batch_opts.js +35 -0
- package/dist/src/cli.js +241 -8
- package/dist/src/cyclone_dx_sbom.d.ts +17 -2
- package/dist/src/cyclone_dx_sbom.js +48 -8
- package/dist/src/index.d.ts +134 -1
- package/dist/src/index.js +342 -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/oci_image/utils.js +11 -2
- package/dist/src/provider.d.ts +15 -3
- package/dist/src/provider.js +29 -5
- package/dist/src/providers/base_java.d.ts +0 -9
- package/dist/src/providers/base_java.js +2 -38
- package/dist/src/providers/base_javascript.d.ts +29 -7
- package/dist/src/providers/base_javascript.js +129 -22
- package/dist/src/providers/base_pyproject.d.ts +153 -0
- package/dist/src/providers/base_pyproject.js +315 -0
- package/dist/src/providers/golang_gomodules.d.ts +28 -12
- package/dist/src/providers/golang_gomodules.js +161 -114
- 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 +25 -0
- package/dist/src/providers/java_gradle.js +126 -2
- package/dist/src/providers/java_maven.d.ts +16 -1
- package/dist/src/providers/java_maven.js +125 -5
- package/dist/src/providers/javascript_npm.d.ts +1 -0
- package/dist/src/providers/javascript_npm.js +21 -0
- package/dist/src/providers/javascript_pnpm.d.ts +1 -1
- package/dist/src/providers/javascript_pnpm.js +8 -4
- package/dist/src/providers/manifest.d.ts +2 -0
- package/dist/src/providers/manifest.js +22 -4
- package/dist/src/providers/processors/yarn_berry_processor.js +88 -5
- package/dist/src/providers/python_controller.d.ts +5 -1
- package/dist/src/providers/python_controller.js +8 -4
- package/dist/src/providers/python_pip.d.ts +11 -0
- package/dist/src/providers/python_pip.js +18 -8
- package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
- package/dist/src/providers/python_pip_pyproject.js +144 -0
- package/dist/src/providers/python_poetry.d.ts +58 -0
- package/dist/src/providers/python_poetry.js +175 -0
- package/dist/src/providers/python_uv.d.ts +42 -0
- package/dist/src/providers/python_uv.js +149 -0
- 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-gomod.wasm +0 -0
- package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
- package/dist/src/sbom.d.ts +17 -2
- package/dist/src/sbom.js +16 -4
- package/dist/src/tools.d.ts +44 -0
- package/dist/src/tools.js +113 -0
- package/dist/src/workspace.d.ts +61 -0
- package/dist/src/workspace.js +256 -0
- package/package.json +14 -5
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discover all pom.xml manifest paths in a Maven multi-module project.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain pom.xml)
|
|
5
|
+
* @param {object} [opts={}]
|
|
6
|
+
* @returns {Promise<string[]>} Paths to pom.xml files (absolute)
|
|
7
|
+
*/
|
|
8
|
+
export function discoverMavenModules(workspaceRoot: string, opts?: object): Promise<string[]>;
|
|
1
9
|
/** @typedef {import('../provider').Provider} */
|
|
2
10
|
/** @typedef {import('../provider').Provided} Provided */
|
|
3
11
|
/** @typedef {{name: string, version: string}} Package */
|
|
@@ -27,13 +35,20 @@ export default class Java_maven extends Base_java {
|
|
|
27
35
|
* @returns {Provided}
|
|
28
36
|
*/
|
|
29
37
|
provideComponent(manifest: string, opts?: {}): Provided;
|
|
38
|
+
/**
|
|
39
|
+
* Read license from pom.xml manifest, with fallback to LICENSE file
|
|
40
|
+
* @param {string} manifestPath - path to pom.xml
|
|
41
|
+
* @returns {string|null}
|
|
42
|
+
*/
|
|
43
|
+
readLicenseFromManifest(manifestPath: string): string | null;
|
|
30
44
|
/**
|
|
31
45
|
*
|
|
32
46
|
* @param {String} textGraphList Text graph String of the manifest
|
|
33
47
|
* @param {[String]} ignoredDeps List of ignored dependencies to be omitted from sbom
|
|
48
|
+
* @param {String} manifestPath Path to the pom.xml manifest
|
|
34
49
|
* @return {String} formatted sbom Json String with all dependencies
|
|
35
50
|
*/
|
|
36
|
-
createSbomFileFromTextFormat(textGraphList: string, ignoredDeps: [string], opts: any): string;
|
|
51
|
+
createSbomFileFromTextFormat(textGraphList: string, ignoredDeps: [string], opts: any, manifestPath: string): string;
|
|
37
52
|
#private;
|
|
38
53
|
}
|
|
39
54
|
export type Java_maven = import("../provider").Provider;
|
|
@@ -3,8 +3,10 @@ import os from 'node:os';
|
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { EOL } from 'os';
|
|
5
5
|
import { XMLParser } from 'fast-xml-parser';
|
|
6
|
+
import { getLicense } from '../license/license_utils.js';
|
|
6
7
|
import Sbom from '../sbom.js';
|
|
7
|
-
import { getCustom } from '../tools.js';
|
|
8
|
+
import { getCustom, invokeCommand } from '../tools.js';
|
|
9
|
+
import { filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore } from '../workspace.js';
|
|
8
10
|
import Base_java, { ecosystem_maven } from "./base_java.js";
|
|
9
11
|
/** @typedef {import('../provider').Provider} */
|
|
10
12
|
/** @typedef {import('../provider').Provided} Provided */
|
|
@@ -51,6 +53,30 @@ export default class Java_maven extends Base_java {
|
|
|
51
53
|
contentType: 'application/vnd.cyclonedx+json'
|
|
52
54
|
};
|
|
53
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Read license from pom.xml manifest, with fallback to LICENSE file
|
|
58
|
+
* @param {string} manifestPath - path to pom.xml
|
|
59
|
+
* @returns {string|null}
|
|
60
|
+
*/
|
|
61
|
+
readLicenseFromManifest(manifestPath) {
|
|
62
|
+
let fromPom = null;
|
|
63
|
+
try {
|
|
64
|
+
const xml = fs.readFileSync(manifestPath, 'utf-8');
|
|
65
|
+
const parser = new XMLParser({ ignoreAttributes: false });
|
|
66
|
+
const obj = parser.parse(xml);
|
|
67
|
+
const project = obj?.project;
|
|
68
|
+
if (project?.licenses?.license) {
|
|
69
|
+
const license = Array.isArray(project.licenses.license)
|
|
70
|
+
? project.licenses.license[0]
|
|
71
|
+
: project.licenses.license;
|
|
72
|
+
fromPom = (license?.name && license.name.trim()) || null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// leave fromPom as null
|
|
77
|
+
}
|
|
78
|
+
return getLicense(fromPom, manifestPath);
|
|
79
|
+
}
|
|
54
80
|
/**
|
|
55
81
|
* Create a Dot Graph dependency tree for a manifest path.
|
|
56
82
|
* @param {string} manifest - path for pom.xml
|
|
@@ -105,7 +131,7 @@ export default class Java_maven extends Base_java {
|
|
|
105
131
|
if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
|
|
106
132
|
console.error("Dependency tree that will be used as input for creating the BOM =>" + EOL + EOL + content.toString());
|
|
107
133
|
}
|
|
108
|
-
let sbom = this.createSbomFileFromTextFormat(content.toString(), ignoredDeps, opts);
|
|
134
|
+
let sbom = this.createSbomFileFromTextFormat(content.toString(), ignoredDeps, opts, manifest);
|
|
109
135
|
// delete temp file and directory
|
|
110
136
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
111
137
|
// return dependency graph as string
|
|
@@ -115,15 +141,17 @@ export default class Java_maven extends Base_java {
|
|
|
115
141
|
*
|
|
116
142
|
* @param {String} textGraphList Text graph String of the manifest
|
|
117
143
|
* @param {[String]} ignoredDeps List of ignored dependencies to be omitted from sbom
|
|
144
|
+
* @param {String} manifestPath Path to the pom.xml manifest
|
|
118
145
|
* @return {String} formatted sbom Json String with all dependencies
|
|
119
146
|
*/
|
|
120
|
-
createSbomFileFromTextFormat(textGraphList, ignoredDeps, opts) {
|
|
147
|
+
createSbomFileFromTextFormat(textGraphList, ignoredDeps, opts, manifestPath) {
|
|
121
148
|
let lines = textGraphList.split(EOL);
|
|
122
149
|
// get root component
|
|
123
150
|
let root = lines[0];
|
|
124
151
|
let rootPurl = this.parseDep(root);
|
|
152
|
+
const license = this.readLicenseFromManifest(manifestPath);
|
|
125
153
|
let sbom = new Sbom();
|
|
126
|
-
sbom.addRoot(rootPurl);
|
|
154
|
+
sbom.addRoot(rootPurl, license);
|
|
127
155
|
this.parseDependencyTree(root, 0, lines.slice(1), sbom);
|
|
128
156
|
return sbom.filterIgnoredDeps(ignoredDeps).getAsJsonString(opts);
|
|
129
157
|
}
|
|
@@ -156,7 +184,8 @@ export default class Java_maven extends Base_java {
|
|
|
156
184
|
let sbom = new Sbom();
|
|
157
185
|
let rootDependency = this.#getRootFromPom(tmpEffectivePom, manifestPath);
|
|
158
186
|
let purlRoot = this.toPurl(rootDependency.groupId, rootDependency.artifactId, rootDependency.version);
|
|
159
|
-
|
|
187
|
+
const license = this.readLicenseFromManifest(manifestPath);
|
|
188
|
+
sbom.addRoot(purlRoot, license);
|
|
160
189
|
dependencies.forEach(dep => {
|
|
161
190
|
let currentPurl = this.toPurl(dep.groupId, dep.artifactId, dep.version);
|
|
162
191
|
sbom.addDependency(purlRoot, currentPurl);
|
|
@@ -261,3 +290,94 @@ export default class Java_maven extends Base_java {
|
|
|
261
290
|
return deps.filter(d => dep.artifactId === d.artifactId && dep.groupId === d.groupId && dep.scope === d.scope).length > 0;
|
|
262
291
|
}
|
|
263
292
|
}
|
|
293
|
+
const DEFAULT_MAVEN_DISCOVERY_IGNORE = [
|
|
294
|
+
'**/target/**',
|
|
295
|
+
];
|
|
296
|
+
/**
|
|
297
|
+
* Discover all pom.xml manifest paths in a Maven multi-module project.
|
|
298
|
+
*
|
|
299
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain pom.xml)
|
|
300
|
+
* @param {object} [opts={}]
|
|
301
|
+
* @returns {Promise<string[]>} Paths to pom.xml files (absolute)
|
|
302
|
+
*/
|
|
303
|
+
export async function discoverMavenModules(workspaceRoot, opts = {}) {
|
|
304
|
+
const root = path.resolve(workspaceRoot);
|
|
305
|
+
const rootPom = path.join(root, 'pom.xml');
|
|
306
|
+
if (!fs.existsSync(rootPom)) {
|
|
307
|
+
return [];
|
|
308
|
+
}
|
|
309
|
+
let mvnBin;
|
|
310
|
+
try {
|
|
311
|
+
mvnBin = new Java_maven().selectToolBinary(rootPom, opts);
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
return [rootPom];
|
|
315
|
+
}
|
|
316
|
+
const visited = new Set();
|
|
317
|
+
const manifestPaths = [rootPom];
|
|
318
|
+
collectMavenModules(root, mvnBin, visited, manifestPaths);
|
|
319
|
+
const ignorePatterns = [...resolveWorkspaceDiscoveryIgnore(opts), ...DEFAULT_MAVEN_DISCOVERY_IGNORE];
|
|
320
|
+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* @param {string} dir - Absolute path to directory containing pom.xml
|
|
324
|
+
* @param {string} mvnBin - Maven binary path
|
|
325
|
+
* @param {Set<string>} visited - Already-visited directories (cycle guard)
|
|
326
|
+
* @param {string[]} manifestPaths - Accumulator for discovered pom.xml paths
|
|
327
|
+
*/
|
|
328
|
+
function collectMavenModules(dir, mvnBin, visited, manifestPaths) {
|
|
329
|
+
const resolvedDir = path.resolve(dir);
|
|
330
|
+
if (visited.has(resolvedDir)) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
visited.add(resolvedDir);
|
|
334
|
+
const modules = listMavenModules(resolvedDir, mvnBin);
|
|
335
|
+
for (const mod of modules) {
|
|
336
|
+
const moduleDir = path.resolve(resolvedDir, mod);
|
|
337
|
+
const modulePom = path.join(moduleDir, 'pom.xml');
|
|
338
|
+
if (fs.existsSync(modulePom)) {
|
|
339
|
+
manifestPaths.push(modulePom);
|
|
340
|
+
collectMavenModules(moduleDir, mvnBin, visited, manifestPaths);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* @param {string} dir - Directory containing pom.xml
|
|
346
|
+
* @param {string} mvnBin - Maven binary path
|
|
347
|
+
* @returns {string[]} Module directory names (relative to `dir`)
|
|
348
|
+
*/
|
|
349
|
+
function listMavenModules(dir, mvnBin) {
|
|
350
|
+
let output;
|
|
351
|
+
try {
|
|
352
|
+
output = invokeCommand(mvnBin, [
|
|
353
|
+
'help:evaluate',
|
|
354
|
+
'-Dexpression=project.modules',
|
|
355
|
+
'-q',
|
|
356
|
+
'-DforceStdout',
|
|
357
|
+
'-f', path.join(dir, 'pom.xml'),
|
|
358
|
+
'--batch-mode',
|
|
359
|
+
], { cwd: dir });
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
return [];
|
|
363
|
+
}
|
|
364
|
+
const raw = output.toString().trim();
|
|
365
|
+
if (!raw || raw.startsWith('<modules')) {
|
|
366
|
+
return [];
|
|
367
|
+
}
|
|
368
|
+
return parseMavenModuleList(raw);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* @param {string} raw - Raw stdout from mvn help:evaluate -DforceStdout
|
|
372
|
+
* @returns {string[]}
|
|
373
|
+
*/
|
|
374
|
+
function parseMavenModuleList(raw) {
|
|
375
|
+
const parser = new XMLParser();
|
|
376
|
+
const parsed = parser.parse(raw);
|
|
377
|
+
const entries = parsed?.strings?.string;
|
|
378
|
+
if (!entries) {
|
|
379
|
+
return [];
|
|
380
|
+
}
|
|
381
|
+
const list = Array.isArray(entries) ? entries : [entries];
|
|
382
|
+
return list.map(s => String(s).trim()).filter(Boolean);
|
|
383
|
+
}
|
|
@@ -12,4 +12,25 @@ export default class Javascript_npm extends Base_javascript {
|
|
|
12
12
|
_updateLockFileCmdArgs() {
|
|
13
13
|
return ['install', '--package-lock-only'];
|
|
14
14
|
}
|
|
15
|
+
_buildDependencyTree(includeTransitive, opts = {}) {
|
|
16
|
+
// npm ls --json returns a single tree rooted at the workspace root.
|
|
17
|
+
// When analyzing a workspace member, its deps are nested under the
|
|
18
|
+
// root's dependencies keyed by the member name — extract that subtree
|
|
19
|
+
// so downstream analysis sees only the member's dependencies.
|
|
20
|
+
const tree = super._buildDependencyTree(includeTransitive, opts);
|
|
21
|
+
const memberName = this._getManifest().name;
|
|
22
|
+
if (tree.name === memberName) {
|
|
23
|
+
return tree;
|
|
24
|
+
}
|
|
25
|
+
const memberEntry = tree.dependencies?.[memberName];
|
|
26
|
+
if (memberEntry) {
|
|
27
|
+
return {
|
|
28
|
+
name: memberName,
|
|
29
|
+
version: memberEntry.version || this._getManifest().version,
|
|
30
|
+
dependencies: memberEntry.dependencies,
|
|
31
|
+
optionalDependencies: memberEntry.optionalDependencies,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return tree;
|
|
35
|
+
}
|
|
15
36
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export default class Javascript_pnpm extends Base_javascript {
|
|
2
2
|
_listCmdArgs(includeTransitive: any): string[];
|
|
3
|
-
_buildDependencyTree(includeTransitive: any,
|
|
3
|
+
_buildDependencyTree(includeTransitive: any, opts?: {}): any;
|
|
4
4
|
}
|
|
5
5
|
import Base_javascript from './base_javascript.js';
|
|
@@ -7,15 +7,19 @@ export default class Javascript_pnpm extends Base_javascript {
|
|
|
7
7
|
return "pnpm";
|
|
8
8
|
}
|
|
9
9
|
_listCmdArgs(includeTransitive) {
|
|
10
|
-
return ['ls', includeTransitive ? '--depth=Infinity' : '--depth=0', '--prod', '--json'];
|
|
10
|
+
return ['ls', includeTransitive ? '--depth=Infinity' : '--depth=0', '--prod', '--json', '-r'];
|
|
11
11
|
}
|
|
12
12
|
_updateLockFileCmdArgs() {
|
|
13
13
|
return ['install', '--frozen-lockfile'];
|
|
14
14
|
}
|
|
15
|
-
_buildDependencyTree(includeTransitive,
|
|
16
|
-
|
|
15
|
+
_buildDependencyTree(includeTransitive, opts = {}) {
|
|
16
|
+
// pnpm ls --json returns an array with one entry per workspace package.
|
|
17
|
+
// When analyzing a workspace member, find its entry by name instead of
|
|
18
|
+
// blindly taking the first element (which is the workspace root).
|
|
19
|
+
const tree = super._buildDependencyTree(includeTransitive, opts);
|
|
17
20
|
if (Array.isArray(tree) && tree.length > 0) {
|
|
18
|
-
|
|
21
|
+
const memberName = this._getManifest().name;
|
|
22
|
+
return tree.find(pkg => pkg.name === memberName) || tree[0];
|
|
19
23
|
}
|
|
20
24
|
return {};
|
|
21
25
|
}
|
|
@@ -9,6 +9,8 @@ export default class Manifest {
|
|
|
9
9
|
this.manifestPath = manifestPath;
|
|
10
10
|
const content = this.loadManifest();
|
|
11
11
|
this.dependencies = this.loadDependencies(content);
|
|
12
|
+
this.peerDependencies = content.peerDependencies || {};
|
|
13
|
+
this.optionalDependencies = content.optionalDependencies || {};
|
|
12
14
|
this.name = content.name;
|
|
13
15
|
this.version = content.version || DEFAULT_VERSION;
|
|
14
16
|
this.ignored = this.loadIgnored(content);
|
|
@@ -27,11 +29,27 @@ export default class Manifest {
|
|
|
27
29
|
}
|
|
28
30
|
loadDependencies(content) {
|
|
29
31
|
let deps = [];
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
const depSources = [
|
|
33
|
+
content.dependencies,
|
|
34
|
+
content.peerDependencies,
|
|
35
|
+
content.optionalDependencies,
|
|
36
|
+
];
|
|
37
|
+
for (const source of depSources) {
|
|
38
|
+
if (source) {
|
|
39
|
+
for (let dep in source) {
|
|
40
|
+
if (!deps.includes(dep)) {
|
|
41
|
+
deps.push(dep);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
32
45
|
}
|
|
33
|
-
|
|
34
|
-
|
|
46
|
+
// bundledDependencies is an array of package names (subset of dependencies)
|
|
47
|
+
if (Array.isArray(content.bundledDependencies)) {
|
|
48
|
+
for (const dep of content.bundledDependencies) {
|
|
49
|
+
if (!deps.includes(dep)) {
|
|
50
|
+
deps.push(dep);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
35
53
|
}
|
|
36
54
|
return deps;
|
|
37
55
|
}
|
|
@@ -15,7 +15,10 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
15
15
|
* @returns {string[]} Command arguments for listing dependencies
|
|
16
16
|
*/
|
|
17
17
|
listCmdArgs(includeTransitive) {
|
|
18
|
-
|
|
18
|
+
// --all is needed to include workspace members in the output
|
|
19
|
+
return includeTransitive
|
|
20
|
+
? ['info', '--recursive', '--all', '--json']
|
|
21
|
+
: ['info', '--all', '--json'];
|
|
19
22
|
}
|
|
20
23
|
/**
|
|
21
24
|
* Returns the command arguments for updating the lock file
|
|
@@ -48,13 +51,15 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
48
51
|
if (!depTree) {
|
|
49
52
|
return new Map();
|
|
50
53
|
}
|
|
51
|
-
return new Map(depTree.filter(dep => !this.#isRoot(dep.value))
|
|
54
|
+
return new Map(depTree.filter(dep => !this.#isRoot(dep.value))
|
|
55
|
+
.map(dep => {
|
|
52
56
|
const depName = dep.value;
|
|
53
57
|
const idx = depName.lastIndexOf('@');
|
|
54
58
|
const name = depName.substring(0, idx);
|
|
55
59
|
const version = dep.children.Version;
|
|
56
60
|
return [name, toPurl(purlType, name, version)];
|
|
57
|
-
})
|
|
61
|
+
})
|
|
62
|
+
.filter(([name]) => this._manifest.dependencies.includes(name)));
|
|
58
63
|
}
|
|
59
64
|
/**
|
|
60
65
|
* Checks if a dependency is the root package
|
|
@@ -66,7 +71,8 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
66
71
|
if (!name) {
|
|
67
72
|
return false;
|
|
68
73
|
}
|
|
69
|
-
|
|
74
|
+
// Workspace members use paths like "member-a@workspace:packages/member-a", not just "@workspace:."
|
|
75
|
+
return name.startsWith(`${this._manifest.name}@workspace:`);
|
|
70
76
|
}
|
|
71
77
|
/**
|
|
72
78
|
* Adds dependencies to the SBOM
|
|
@@ -77,14 +83,58 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
77
83
|
if (!depTree) {
|
|
78
84
|
return;
|
|
79
85
|
}
|
|
86
|
+
// Build index of nodes by their value for quick lookup
|
|
87
|
+
const nodeIndex = new Map();
|
|
88
|
+
depTree.forEach(n => nodeIndex.set(n.value, n));
|
|
89
|
+
// Determine the set of node values reachable from root via production deps
|
|
90
|
+
const prodDeps = new Set(this._manifest.dependencies);
|
|
91
|
+
const reachable = new Set();
|
|
92
|
+
const queue = [];
|
|
93
|
+
// Seed with root's production dependencies
|
|
94
|
+
const rootNode = depTree.find(n => this.#isRoot(n.value));
|
|
95
|
+
if (rootNode?.children?.Dependencies) {
|
|
96
|
+
for (const d of rootNode.children.Dependencies) {
|
|
97
|
+
const to = this.#purlFromLocator(d.locator);
|
|
98
|
+
if (to) {
|
|
99
|
+
const fullName = to.namespace ? `${to.namespace}/${to.name}` : to.name;
|
|
100
|
+
if (prodDeps.has(fullName)) {
|
|
101
|
+
queue.push(d.locator);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// BFS to find all transitively reachable packages
|
|
107
|
+
while (queue.length > 0) {
|
|
108
|
+
const locator = queue.shift();
|
|
109
|
+
if (reachable.has(locator)) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
reachable.add(locator);
|
|
113
|
+
const node = nodeIndex.get(this.#nodeValueFromLocator(locator));
|
|
114
|
+
if (node?.children?.Dependencies) {
|
|
115
|
+
for (const d of node.children.Dependencies) {
|
|
116
|
+
if (!reachable.has(d.locator)) {
|
|
117
|
+
queue.push(d.locator);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// Only emit edges for root and reachable nodes
|
|
80
123
|
depTree.forEach(n => {
|
|
81
124
|
const depName = n.value;
|
|
82
|
-
const
|
|
125
|
+
const isRoot = this.#isRoot(depName);
|
|
126
|
+
if (!isRoot && !this.#isReachableNode(depName, reachable)) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const from = isRoot ? toPurlFromString(sbom.getRoot().purl) : this.#purlFromNode(depName, n);
|
|
83
130
|
const deps = n.children?.Dependencies;
|
|
84
131
|
if (!deps) {
|
|
85
132
|
return;
|
|
86
133
|
}
|
|
87
134
|
deps.forEach(d => {
|
|
135
|
+
if (!reachable.has(d.locator)) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
88
138
|
const to = this.#purlFromLocator(d.locator);
|
|
89
139
|
if (to) {
|
|
90
140
|
sbom.addDependency(from, to);
|
|
@@ -92,6 +142,39 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
92
142
|
});
|
|
93
143
|
});
|
|
94
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* Converts a locator to the node value format used in yarn info output
|
|
147
|
+
* @param {string} locator - e.g. "express@npm:4.17.1"
|
|
148
|
+
* @returns {string} The node value, same as locator for non-virtual
|
|
149
|
+
* @private
|
|
150
|
+
*/
|
|
151
|
+
#nodeValueFromLocator(locator) {
|
|
152
|
+
// Virtual locators: "@scope/name@virtual:hash#npm:version" → "@scope/name@npm:version"
|
|
153
|
+
const virtualMatch = Yarn_berry_processor.VIRTUAL_LOCATOR_PATTERN.exec(locator);
|
|
154
|
+
if (virtualMatch) {
|
|
155
|
+
return `${virtualMatch[1]}@npm:${virtualMatch[2]}`;
|
|
156
|
+
}
|
|
157
|
+
return locator;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Checks if a node is in the reachable set by matching its value against reachable locators
|
|
161
|
+
* @param {string} depName - The node value (e.g. "express@npm:4.17.1")
|
|
162
|
+
* @param {Set<string>} reachable - Set of reachable locators
|
|
163
|
+
* @returns {boolean}
|
|
164
|
+
* @private
|
|
165
|
+
*/
|
|
166
|
+
#isReachableNode(depName, reachable) {
|
|
167
|
+
if (reachable.has(depName)) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
// Check if any reachable locator resolves to this node value
|
|
171
|
+
for (const locator of reachable) {
|
|
172
|
+
if (this.#nodeValueFromLocator(locator) === depName) {
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
95
178
|
/**
|
|
96
179
|
* Creates a PackageURL from a dependency locator
|
|
97
180
|
* @param {string} locator - The dependency locator
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[]}} DependencyEntry */
|
|
1
|
+
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[], hashes?: Array<{alg: string, content: string}>}} DependencyEntry */
|
|
2
2
|
export default class Python_controller {
|
|
3
3
|
/**
|
|
4
4
|
* Constructor to create new python controller instance to interact with pip package manager
|
|
@@ -31,4 +31,8 @@ export type DependencyEntry = {
|
|
|
31
31
|
name: string;
|
|
32
32
|
version: string;
|
|
33
33
|
dependencies: DependencyEntry[];
|
|
34
|
+
hashes?: Array<{
|
|
35
|
+
alg: string;
|
|
36
|
+
content: string;
|
|
37
|
+
}>;
|
|
34
38
|
};
|
|
@@ -19,7 +19,7 @@ function getPipShowOutput(depNames) {
|
|
|
19
19
|
throw new Error('fail invoking \'pip show\' to fetch metadata for all installed packages in environment', { cause: error });
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
-
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[]}} DependencyEntry */
|
|
22
|
+
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[], hashes?: Array<{alg: string, content: string}>}} DependencyEntry */
|
|
23
23
|
export default class Python_controller {
|
|
24
24
|
pythonEnvDir;
|
|
25
25
|
pathToPipBin;
|
|
@@ -95,7 +95,7 @@ export default class Python_controller {
|
|
|
95
95
|
}
|
|
96
96
|
/**
|
|
97
97
|
* Parse the requirements.txt file using tree-sitter and return structured requirement data.
|
|
98
|
-
* @return {Promise<{name: string, version: string|null}[]>}
|
|
98
|
+
* @return {Promise<{name: string, version: string|null, hasMarker: boolean}[]>}
|
|
99
99
|
*/
|
|
100
100
|
async #parseRequirements() {
|
|
101
101
|
const content = fs.readFileSync(this.pathToRequirements).toString();
|
|
@@ -107,7 +107,8 @@ export default class Python_controller {
|
|
|
107
107
|
const version = versionMatches.length > 0
|
|
108
108
|
? versionMatches[0].captures.find(c => c.name === 'version').node.text
|
|
109
109
|
: null;
|
|
110
|
-
|
|
110
|
+
const hasMarker = reqNode.children.some(c => c.type === 'marker_spec');
|
|
111
|
+
return { name, version, hasMarker };
|
|
111
112
|
}));
|
|
112
113
|
}
|
|
113
114
|
#decideIfWindowsOrLinuxPath(fileName) {
|
|
@@ -224,7 +225,10 @@ export default class Python_controller {
|
|
|
224
225
|
CachedEnvironmentDeps[packageName.replace("_", "-")] = pipDepTreeEntryForCache;
|
|
225
226
|
});
|
|
226
227
|
}
|
|
227
|
-
parsedRequirements.forEach(({ name: depName, version: manifestVersion }) => {
|
|
228
|
+
parsedRequirements.forEach(({ name: depName, version: manifestVersion, hasMarker }) => {
|
|
229
|
+
if (hasMarker && CachedEnvironmentDeps[depName.toLowerCase()] === undefined) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
228
232
|
if (matchManifestVersions === "true" && manifestVersion != null) {
|
|
229
233
|
let installedVersion;
|
|
230
234
|
if (CachedEnvironmentDeps[depName.toLowerCase()] !== undefined) {
|
|
@@ -3,12 +3,17 @@ declare namespace _default {
|
|
|
3
3
|
export { validateLockFile };
|
|
4
4
|
export { provideComponent };
|
|
5
5
|
export { provideStack };
|
|
6
|
+
export { readLicenseFromManifest };
|
|
6
7
|
}
|
|
7
8
|
export default _default;
|
|
8
9
|
export type DependencyEntry = {
|
|
9
10
|
name: string;
|
|
10
11
|
version: string;
|
|
11
12
|
dependencies: DependencyEntry[];
|
|
13
|
+
hashes?: Array<{
|
|
14
|
+
alg: string;
|
|
15
|
+
content: string;
|
|
16
|
+
}>;
|
|
12
17
|
};
|
|
13
18
|
/**
|
|
14
19
|
* @param {string} manifestName - the subject manifest name-type
|
|
@@ -33,3 +38,9 @@ declare function provideComponent(manifest: string, opts?: {}): Promise<Provided
|
|
|
33
38
|
* @returns {Promise<Provided>}
|
|
34
39
|
*/
|
|
35
40
|
declare function provideStack(manifest: string, opts?: {}): Promise<Provided>;
|
|
41
|
+
/**
|
|
42
|
+
* Python requirements.txt has no standard license field
|
|
43
|
+
* @param {string} manifestPath - path to requirements.txt
|
|
44
|
+
* @returns {string|null}
|
|
45
|
+
*/
|
|
46
|
+
declare function readLicenseFromManifest(manifestPath: string): string | null;
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import { PackageURL } from 'packageurl-js';
|
|
3
|
+
import { readLicenseFile } from '../license/license_utils.js';
|
|
3
4
|
import Sbom from '../sbom.js';
|
|
4
5
|
import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand } from "../tools.js";
|
|
5
6
|
import Python_controller from './python_controller.js';
|
|
6
7
|
import { getParser, getIgnoreQuery, getPinnedVersionQuery } from './requirements_parser.js';
|
|
7
|
-
export default { isSupported, validateLockFile, provideComponent, provideStack };
|
|
8
|
-
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[]}} DependencyEntry */
|
|
8
|
+
export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
|
|
9
|
+
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[], hashes?: Array<{alg: string, content: string}>}} DependencyEntry */
|
|
9
10
|
/**
|
|
10
11
|
* @type {string} ecosystem for python-pip is 'pip'
|
|
11
12
|
* @private
|
|
12
13
|
*/
|
|
13
14
|
const ecosystem = 'pip';
|
|
15
|
+
const NO_SCOPE = undefined;
|
|
14
16
|
/**
|
|
15
17
|
* @param {string} manifestName - the subject manifest name-type
|
|
16
18
|
* @returns {boolean} - return true if `requirements.txt` is the manifest name-type
|
|
@@ -18,6 +20,13 @@ const ecosystem = 'pip';
|
|
|
18
20
|
function isSupported(manifestName) {
|
|
19
21
|
return 'requirements.txt' === manifestName;
|
|
20
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Python requirements.txt has no standard license field
|
|
25
|
+
* @param {string} manifestPath - path to requirements.txt
|
|
26
|
+
* @returns {string|null}
|
|
27
|
+
*/
|
|
28
|
+
// eslint-disable-next-line no-unused-vars
|
|
29
|
+
function readLicenseFromManifest(manifestPath) { return readLicenseFile(manifestPath); }
|
|
21
30
|
/**
|
|
22
31
|
* @param {string} manifestDir - the directory where the manifest lies
|
|
23
32
|
*/
|
|
@@ -48,7 +57,6 @@ async function provideComponent(manifest, opts = {}) {
|
|
|
48
57
|
contentType: 'application/vnd.cyclonedx+json'
|
|
49
58
|
};
|
|
50
59
|
}
|
|
51
|
-
/** @typedef {{name: string, , version: string, dependencies: DependencyEntry[]}} DependencyEntry */
|
|
52
60
|
/**
|
|
53
61
|
*
|
|
54
62
|
* @param {PackageURL}source
|
|
@@ -58,7 +66,7 @@ async function provideComponent(manifest, opts = {}) {
|
|
|
58
66
|
*/
|
|
59
67
|
function addAllDependencies(source, dep, sbom) {
|
|
60
68
|
let targetPurl = toPurl(dep["name"], dep["version"]);
|
|
61
|
-
sbom.addDependency(source, targetPurl);
|
|
69
|
+
sbom.addDependency(source, targetPurl, NO_SCOPE, dep["hashes"]);
|
|
62
70
|
let directDeps = dep["dependencies"];
|
|
63
71
|
if (directDeps !== undefined && directDeps.length > 0) {
|
|
64
72
|
directDeps.forEach((dependency) => { addAllDependencies(toPurl(dep["name"], dep["version"]), dependency, sbom); });
|
|
@@ -67,7 +75,7 @@ function addAllDependencies(source, dep, sbom) {
|
|
|
67
75
|
/**
|
|
68
76
|
*
|
|
69
77
|
* @param {string} manifest - path to requirements.txt
|
|
70
|
-
* @return {PackageURL
|
|
78
|
+
* @return {Promise<PackageURL[]>}
|
|
71
79
|
*/
|
|
72
80
|
async function getIgnoredDependencies(manifest) {
|
|
73
81
|
const [parser, ignoreQuery, pinnedVersionQuery] = await Promise.all([
|
|
@@ -167,7 +175,8 @@ async function createSbomStackAnalysis(manifest, opts = {}) {
|
|
|
167
175
|
let dependencies = await pythonController.getDependencies(true);
|
|
168
176
|
let sbom = new Sbom();
|
|
169
177
|
const rootPurl = toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION);
|
|
170
|
-
|
|
178
|
+
const license = readLicenseFromManifest(manifest);
|
|
179
|
+
sbom.addRoot(rootPurl, license);
|
|
171
180
|
dependencies.forEach(dep => {
|
|
172
181
|
addAllDependencies(rootPurl, dep, sbom);
|
|
173
182
|
});
|
|
@@ -190,9 +199,10 @@ async function getSbomForComponentAnalysis(manifest, opts = {}) {
|
|
|
190
199
|
let dependencies = await pythonController.getDependencies(false);
|
|
191
200
|
let sbom = new Sbom();
|
|
192
201
|
const rootPurl = toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION);
|
|
193
|
-
|
|
202
|
+
const license = readLicenseFromManifest(manifest);
|
|
203
|
+
sbom.addRoot(rootPurl, license);
|
|
194
204
|
dependencies.forEach(dep => {
|
|
195
|
-
sbom.addDependency(rootPurl, toPurl(dep.name, dep.version));
|
|
205
|
+
sbom.addDependency(rootPurl, toPurl(dep.name, dep.version), NO_SCOPE, dep.hashes);
|
|
196
206
|
});
|
|
197
207
|
await handleIgnoredDependencies(manifest, sbom, opts);
|
|
198
208
|
// In python there is no root component, then we must remove the dummy root we added, so the sbom json will be accepted by the DA backend
|