@trustify-da/trustify-da-javascript-client 0.3.0-ea.7ed8d8c → 0.3.0-ea.7ee4047

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.
Files changed (98) hide show
  1. package/README.md +388 -20
  2. package/dist/package.json +39 -26
  3. package/dist/src/analysis.d.ts +58 -20
  4. package/dist/src/analysis.js +134 -78
  5. package/dist/src/batch_opts.d.ts +24 -0
  6. package/dist/src/batch_opts.js +35 -0
  7. package/dist/src/cli.js +427 -12
  8. package/dist/src/config.d.ts +120 -0
  9. package/dist/src/config.js +260 -0
  10. package/dist/src/cyclone_dx_sbom.d.ts +30 -2
  11. package/dist/src/cyclone_dx_sbom.js +95 -9
  12. package/dist/src/index.d.ts +154 -16
  13. package/dist/src/index.js +372 -36
  14. package/dist/src/license/index.d.ts +28 -0
  15. package/dist/src/license/index.js +106 -0
  16. package/dist/src/license/license_utils.d.ts +40 -0
  17. package/dist/src/license/license_utils.js +137 -0
  18. package/dist/src/license/licenses_api.d.ts +34 -0
  19. package/dist/src/license/licenses_api.js +98 -0
  20. package/dist/src/license/project_license.d.ts +20 -0
  21. package/dist/src/license/project_license.js +62 -0
  22. package/dist/src/oci_image/images.d.ts +1 -1
  23. package/dist/src/oci_image/images.js +15 -1
  24. package/dist/src/oci_image/utils.d.ts +5 -5
  25. package/dist/src/oci_image/utils.js +11 -2
  26. package/dist/src/package_version.d.ts +8 -0
  27. package/dist/src/package_version.js +31 -0
  28. package/dist/src/provider.d.ts +16 -3
  29. package/dist/src/provider.js +33 -5
  30. package/dist/src/providers/base_java.d.ts +67 -12
  31. package/dist/src/providers/base_java.js +82 -61
  32. package/dist/src/providers/base_javascript.d.ts +99 -12
  33. package/dist/src/providers/base_javascript.js +244 -28
  34. package/dist/src/providers/base_pyproject.d.ts +158 -0
  35. package/dist/src/providers/base_pyproject.js +322 -0
  36. package/dist/src/providers/containerfile_parser.d.ts +5 -0
  37. package/dist/src/providers/containerfile_parser.js +20 -0
  38. package/dist/src/providers/golang_gomodules.d.ts +30 -13
  39. package/dist/src/providers/golang_gomodules.js +219 -122
  40. package/dist/src/providers/gomod_parser.d.ts +4 -0
  41. package/dist/src/providers/gomod_parser.js +16 -0
  42. package/dist/src/providers/java_gradle.d.ts +73 -0
  43. package/dist/src/providers/java_gradle.js +331 -23
  44. package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
  45. package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
  46. package/dist/src/providers/java_maven.d.ts +44 -9
  47. package/dist/src/providers/java_maven.js +299 -13
  48. package/dist/src/providers/javascript_bun.d.ts +22 -0
  49. package/dist/src/providers/javascript_bun.js +141 -0
  50. package/dist/src/providers/javascript_npm.d.ts +19 -0
  51. package/dist/src/providers/javascript_npm.js +61 -1
  52. package/dist/src/providers/javascript_pnpm.d.ts +14 -1
  53. package/dist/src/providers/javascript_pnpm.js +55 -5
  54. package/dist/src/providers/javascript_yarn.d.ts +12 -0
  55. package/dist/src/providers/javascript_yarn.js +75 -2
  56. package/dist/src/providers/manifest.d.ts +2 -0
  57. package/dist/src/providers/manifest.js +28 -7
  58. package/dist/src/providers/marker_evaluator.d.ts +14 -0
  59. package/dist/src/providers/marker_evaluator.js +191 -0
  60. package/dist/src/providers/oci_dockerfile.d.ts +51 -0
  61. package/dist/src/providers/oci_dockerfile.js +177 -0
  62. package/dist/src/providers/processors/yarn_berry_processor.d.ts +6 -2
  63. package/dist/src/providers/processors/yarn_berry_processor.js +91 -7
  64. package/dist/src/providers/processors/yarn_classic_processor.d.ts +6 -2
  65. package/dist/src/providers/processors/yarn_classic_processor.js +8 -6
  66. package/dist/src/providers/python_controller.d.ts +5 -1
  67. package/dist/src/providers/python_controller.js +69 -8
  68. package/dist/src/providers/python_pip.d.ts +12 -0
  69. package/dist/src/providers/python_pip.js +18 -8
  70. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  71. package/dist/src/providers/python_pip_pyproject.js +146 -0
  72. package/dist/src/providers/python_poetry.d.ts +98 -0
  73. package/dist/src/providers/python_poetry.js +286 -0
  74. package/dist/src/providers/python_uv.d.ts +46 -0
  75. package/dist/src/providers/python_uv.js +275 -0
  76. package/dist/src/providers/requirements_parser.js +6 -9
  77. package/dist/src/providers/rust_cargo.d.ts +56 -0
  78. package/dist/src/providers/rust_cargo.js +682 -0
  79. package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
  80. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  81. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  82. package/dist/src/remediate.d.ts +107 -0
  83. package/dist/src/remediate.js +244 -0
  84. package/dist/src/remediation.d.ts +81 -0
  85. package/dist/src/remediation.js +475 -0
  86. package/dist/src/remediation_report.d.ts +23 -0
  87. package/dist/src/remediation_report.js +180 -0
  88. package/dist/src/sbom.d.ts +28 -2
  89. package/dist/src/sbom.js +26 -4
  90. package/dist/src/tools.d.ts +53 -11
  91. package/dist/src/tools.js +147 -13
  92. package/dist/src/updaters/maven_updater.d.ts +58 -0
  93. package/dist/src/updaters/maven_updater.js +345 -0
  94. package/dist/src/updaters/toml_updater.d.ts +62 -0
  95. package/dist/src/updaters/toml_updater.js +276 -0
  96. package/dist/src/workspace.d.ts +71 -0
  97. package/dist/src/workspace.js +257 -0
  98. package/package.json +40 -27
@@ -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, packageManagerName() { return 'pip'; } };
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
- sbom.addRoot(rootPurl);
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
- sbom.addRoot(rootPurl);
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
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Python provider for pyproject.toml files using PEP 621 format without a lock file.
3
+ * Uses `pip install --dry-run --ignore-installed --report` to resolve the full dependency tree.
4
+ * Acts as the fallback provider when no lock file (uv.lock/poetry.lock) is found.
5
+ */
6
+ export default class Python_pip_pyproject extends Base_pyproject {
7
+ /**
8
+ * Always returns true — pip provider is the fallback when no lock file is found.
9
+ * @param {string} manifestDir
10
+ * @param {{}} [opts={}]
11
+ * @returns {boolean}
12
+ */
13
+ validateLockFile(manifestDir: string, opts?: {}): boolean;
14
+ /**
15
+ * Get pip report output from env var override or by running pip.
16
+ * @param {string} manifestDir - directory containing pyproject.toml
17
+ * @param {{}} [opts={}]
18
+ * @returns {string} pip report JSON string
19
+ */
20
+ _getPipReportOutput(manifestDir: string, opts?: {}): string;
21
+ /**
22
+ * Parse pip report JSON and build dependency graph.
23
+ * @param {string} reportJson - pip report JSON string
24
+ * @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
25
+ */
26
+ _parsePipReport(reportJson: string): {
27
+ directDeps: string[];
28
+ graph: Map<string, {
29
+ name: string;
30
+ version: string;
31
+ children: string[];
32
+ }>;
33
+ };
34
+ /**
35
+ * Check if a requires_dist entry is an extras-only dependency.
36
+ * @param {string} req - e.g. "PySocks!=1.5.7,>=1.5.6; extra == \"socks\""
37
+ * @returns {boolean}
38
+ */
39
+ _hasExtraMarker(req: string): boolean;
40
+ /**
41
+ * Extract package name from a requires_dist entry.
42
+ * @param {string} req - e.g. "charset_normalizer<4,>=2"
43
+ * @returns {string|null}
44
+ */
45
+ _extractDepName(req: string): string | null;
46
+ /**
47
+ * Resolve dependencies using pip install --dry-run --report.
48
+ * @param {string} manifestDir
49
+ * @param {string} _workspaceDir - unused (pip resolves from manifest directory)
50
+ * @param {object} parsed - parsed pyproject.toml
51
+ * @param {{}} [opts={}]
52
+ * @returns {Promise<{directDeps: string[], graph: Map}>}
53
+ */
54
+ _getDependencyData(manifestDir: string, _workspaceDir: string, parsed: object, opts?: {}): Promise<{
55
+ directDeps: string[];
56
+ graph: Map<any, any>;
57
+ }>;
58
+ _findEggInfoDirs(dir: any): string[];
59
+ _cleanupEggInfo(dir: any, existing: any): void;
60
+ }
61
+ import Base_pyproject from './base_pyproject.js';
@@ -0,0 +1,146 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
4
+ import Base_pyproject from './base_pyproject.js';
5
+ /**
6
+ * Python provider for pyproject.toml files using PEP 621 format without a lock file.
7
+ * Uses `pip install --dry-run --ignore-installed --report` to resolve the full dependency tree.
8
+ * Acts as the fallback provider when no lock file (uv.lock/poetry.lock) is found.
9
+ */
10
+ export default class Python_pip_pyproject extends Base_pyproject {
11
+ /** @returns {string} */
12
+ _lockFileName() {
13
+ return '.pip-lock-nonexistent';
14
+ }
15
+ /** @returns {string} */
16
+ _cmdName() {
17
+ return 'pip';
18
+ }
19
+ /**
20
+ * Always returns true — pip provider is the fallback when no lock file is found.
21
+ * @param {string} manifestDir
22
+ * @param {{}} [opts={}]
23
+ * @returns {boolean}
24
+ */
25
+ // eslint-disable-next-line no-unused-vars
26
+ validateLockFile(manifestDir, opts = {}) {
27
+ return true;
28
+ }
29
+ /**
30
+ * Get pip report output from env var override or by running pip.
31
+ * @param {string} manifestDir - directory containing pyproject.toml
32
+ * @param {{}} [opts={}]
33
+ * @returns {string} pip report JSON string
34
+ */
35
+ _getPipReportOutput(manifestDir, opts) {
36
+ if (environmentVariableIsPopulated('TRUSTIFY_DA_PIP_REPORT')) {
37
+ return Buffer.from(process.env['TRUSTIFY_DA_PIP_REPORT'], 'base64').toString('ascii');
38
+ }
39
+ let pipBin = getCustomPath('pip3', opts);
40
+ try {
41
+ invokeCommand(pipBin, ['--version']);
42
+ }
43
+ catch {
44
+ pipBin = getCustomPath('pip', opts);
45
+ }
46
+ let eggInfoDirs = this._findEggInfoDirs(manifestDir);
47
+ let result = invokeCommand(pipBin, [
48
+ 'install', '--dry-run', '--ignore-installed', '--quiet', '--report', '-', '.'
49
+ ], { cwd: manifestDir }).toString();
50
+ this._cleanupEggInfo(manifestDir, eggInfoDirs);
51
+ return result;
52
+ }
53
+ /**
54
+ * Parse pip report JSON and build dependency graph.
55
+ * @param {string} reportJson - pip report JSON string
56
+ * @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
57
+ */
58
+ _parsePipReport(reportJson) {
59
+ let report = JSON.parse(reportJson);
60
+ let packages = report.install || [];
61
+ let rootEntry = packages.find(p => p.download_info?.dir_info !== undefined);
62
+ let rootRequires = rootEntry?.metadata?.requires_dist || [];
63
+ let directDepNames = new Set();
64
+ for (let req of rootRequires) {
65
+ if (this._hasExtraMarker(req)) {
66
+ continue;
67
+ }
68
+ let name = this._extractDepName(req);
69
+ if (name) {
70
+ directDepNames.add(this._canonicalize(name));
71
+ }
72
+ }
73
+ let graph = new Map();
74
+ let nonRootPackages = packages.filter(p => p !== rootEntry);
75
+ for (let pkg of nonRootPackages) {
76
+ let name = pkg.metadata.name;
77
+ let version = pkg.metadata.version;
78
+ let key = this._canonicalize(name);
79
+ let sha256 = pkg.download_info?.archive_info?.hashes?.sha256;
80
+ let hashes = sha256 ? [{ alg: "SHA-256", content: sha256 }] : undefined;
81
+ graph.set(key, { name, version, children: [], hashes });
82
+ }
83
+ for (let pkg of nonRootPackages) {
84
+ let key = this._canonicalize(pkg.metadata.name);
85
+ let entry = graph.get(key);
86
+ let requires = pkg.metadata.requires_dist || [];
87
+ for (let req of requires) {
88
+ let depName = this._extractDepName(req);
89
+ if (!depName) {
90
+ continue;
91
+ }
92
+ let depKey = this._canonicalize(depName);
93
+ if (graph.has(depKey)) {
94
+ entry.children.push(depKey);
95
+ }
96
+ }
97
+ }
98
+ let directDeps = [...directDepNames].filter(key => graph.has(key));
99
+ return { directDeps, graph };
100
+ }
101
+ /**
102
+ * Check if a requires_dist entry is an extras-only dependency.
103
+ * @param {string} req - e.g. "PySocks!=1.5.7,>=1.5.6; extra == \"socks\""
104
+ * @returns {boolean}
105
+ */
106
+ _hasExtraMarker(req) {
107
+ return /;\s*.*extra\s*==/.test(req);
108
+ }
109
+ /**
110
+ * Extract package name from a requires_dist entry.
111
+ * @param {string} req - e.g. "charset_normalizer<4,>=2"
112
+ * @returns {string|null}
113
+ */
114
+ _extractDepName(req) {
115
+ let match = req.match(/^([A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?)/);
116
+ return match ? match[1] : null;
117
+ }
118
+ /**
119
+ * Resolve dependencies using pip install --dry-run --report.
120
+ * @param {string} manifestDir
121
+ * @param {string} _workspaceDir - unused (pip resolves from manifest directory)
122
+ * @param {object} parsed - parsed pyproject.toml
123
+ * @param {{}} [opts={}]
124
+ * @returns {Promise<{directDeps: string[], graph: Map}>}
125
+ */
126
+ // eslint-disable-next-line no-unused-vars
127
+ async _getDependencyData(manifestDir, _workspaceDir, parsed, opts) {
128
+ let reportOutput = this._getPipReportOutput(manifestDir, opts);
129
+ return this._parsePipReport(reportOutput);
130
+ }
131
+ _findEggInfoDirs(dir) {
132
+ try {
133
+ return fs.readdirSync(dir).filter(f => f.endsWith('.egg-info'));
134
+ }
135
+ catch {
136
+ return [];
137
+ }
138
+ }
139
+ _cleanupEggInfo(dir, existing) {
140
+ for (let entry of this._findEggInfoDirs(dir)) {
141
+ if (!existing.includes(entry)) {
142
+ fs.rmSync(path.join(dir, entry), { recursive: true, force: true });
143
+ }
144
+ }
145
+ }
146
+ }
@@ -0,0 +1,98 @@
1
+ export default class Python_poetry extends Base_pyproject {
2
+ _verifyPoetryAccessible(poetryBin: any): void;
3
+ /**
4
+ * @param {string} manifestDir
5
+ * @param {string} _workspaceDir - unused (poetry has no workspace support)
6
+ * @param {object} parsed - parsed pyproject.toml
7
+ * @param {Object} opts
8
+ * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
9
+ */
10
+ _getDependencyData(manifestDir: string, _workspaceDir: string, parsed: object, opts: Object): Promise<{
11
+ directDeps: string[];
12
+ graph: Map<string, {
13
+ name: string;
14
+ version: string;
15
+ children: string[];
16
+ }>;
17
+ }>;
18
+ /**
19
+ * Get poetry show --tree output.
20
+ * @param {string} manifestDir
21
+ * @param {boolean} hasDevGroup
22
+ * @param {string} poetryBin
23
+ * @returns {string}
24
+ */
25
+ _getPoetryShowTreeOutput(manifestDir: string, hasDevGroup: boolean, poetryBin: string): string;
26
+ /**
27
+ * Get poetry show --all output (flat list with resolved versions).
28
+ * @param {string} manifestDir
29
+ * @param {string} poetryBin
30
+ * @returns {string}
31
+ */
32
+ _getPoetryShowAllOutput(manifestDir: string, poetryBin: string): string;
33
+ /**
34
+ * Parse poetry show --all output into a version map.
35
+ * Lines look like: "name (!) 1.2.3 Description text..."
36
+ * or: "name 1.2.3 Description text..."
37
+ * @param {string} output
38
+ * @returns {Map<string, string>} canonical name -> version
39
+ */
40
+ _parsePoetryShowAll(output: string): Map<string, string>;
41
+ /**
42
+ * Collects PEP 508 marker expressions for direct and transitive deps.
43
+ * Direct markers come from pyproject.toml dependency strings, e.g.:
44
+ * "pywin32>=311 ; sys_platform == 'win32'" → directMarkers['pywin32'] = "sys_platform == 'win32'"
45
+ * Transitive markers come from poetry.lock [package.dependencies] entries, e.g.:
46
+ * colorama = {version = "*", markers = "sys_platform == 'win32'"}
47
+ * → transitiveMarkers['click']['colorama'] = "sys_platform == 'win32'"
48
+ * @param {string|null} lockDir
49
+ * @param {object} parsed - parsed pyproject.toml
50
+ * @returns {{directMarkers: Map<string, string>, transitiveMarkers: Map<string, Map<string, string>>, hashMap: Map<string, Array<{alg: string, content: string}>>}}
51
+ */
52
+ _extractMarkerData(lockDir: string | null, parsed: object): {
53
+ directMarkers: Map<string, string>;
54
+ transitiveMarkers: Map<string, Map<string, string>>;
55
+ hashMap: Map<string, Array<{
56
+ alg: string;
57
+ content: string;
58
+ }>>;
59
+ };
60
+ /**
61
+ * Extract a SHA-256 hex digest from poetry.lock files array.
62
+ * Prefers the sdist (.tar.gz) hash; falls back to the first wheel hash.
63
+ * @param {Array<{file: string, hash: string}>} [files]
64
+ * @returns {string|null}
65
+ */
66
+ _extractSha256FromFiles(files?: Array<{
67
+ file: string;
68
+ hash: string;
69
+ }>): string | null;
70
+ /**
71
+ * Parse poetry show --tree output into a dependency graph structure.
72
+ *
73
+ * @param {string} treeOutput
74
+ * @param {Map<string, string>} versionMap - canonical name -> resolved version
75
+ * @param {{directMarkers: Map<string, string>, transitiveMarkers: Map<string, Map<string, string>>, hashMap: Map<string, Array<{alg: string, content: string}>>}} markerData
76
+ * @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[], hashes?: Array<{alg: string, content: string}>}>}}
77
+ */
78
+ _parsePoetryTree(treeOutput: string, versionMap: Map<string, string>, markerData: {
79
+ directMarkers: Map<string, string>;
80
+ transitiveMarkers: Map<string, Map<string, string>>;
81
+ hashMap: Map<string, Array<{
82
+ alg: string;
83
+ content: string;
84
+ }>>;
85
+ }): {
86
+ directDeps: string[];
87
+ graph: Map<string, {
88
+ name: string;
89
+ version: string;
90
+ children: string[];
91
+ hashes?: Array<{
92
+ alg: string;
93
+ content: string;
94
+ }>;
95
+ }>;
96
+ };
97
+ }
98
+ import Base_pyproject from './base_pyproject.js';
@@ -0,0 +1,286 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { parse as parseToml } from 'smol-toml';
4
+ import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand } from '../tools.js';
5
+ import Base_pyproject from './base_pyproject.js';
6
+ import { evaluateMarker } from './marker_evaluator.js';
7
+ export default class Python_poetry extends Base_pyproject {
8
+ /**
9
+ * Poetry has no native workspace/monorepo support (python-poetry/poetry#2270).
10
+ * Each poetry project is treated independently — no lock file walk-up.
11
+ * Running `poetry show` from a parent directory returns the parent's deps, not
12
+ * the sub-package's, so walk-up would produce incorrect SBOMs.
13
+ * @param {string} manifestDir
14
+ * @param {Object} [opts={}]
15
+ * @returns {string|null}
16
+ * @protected
17
+ */
18
+ _findLockFileDir(manifestDir, opts = {}) {
19
+ const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
20
+ if (workspaceDir) {
21
+ const dir = path.resolve(workspaceDir);
22
+ return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
23
+ }
24
+ const dir = path.resolve(manifestDir);
25
+ return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
26
+ }
27
+ /** @returns {string} */
28
+ _lockFileName() {
29
+ return 'poetry.lock';
30
+ }
31
+ /** @returns {string} */
32
+ _cmdName() {
33
+ return 'poetry';
34
+ }
35
+ _verifyPoetryAccessible(poetryBin) {
36
+ try {
37
+ invokeCommand(poetryBin, ['--version']);
38
+ }
39
+ catch (error) {
40
+ if (error.code === 'ENOENT') {
41
+ throw new Error(`poetry is not accessible at "${poetryBin}"`, { cause: error });
42
+ }
43
+ throw new Error('failed to check for poetry binary', { cause: error });
44
+ }
45
+ }
46
+ /**
47
+ * @param {string} manifestDir
48
+ * @param {string} _workspaceDir - unused (poetry has no workspace support)
49
+ * @param {object} parsed - parsed pyproject.toml
50
+ * @param {Object} opts
51
+ * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
52
+ */
53
+ // eslint-disable-next-line no-unused-vars
54
+ async _getDependencyData(manifestDir, _workspaceDir, parsed, opts) {
55
+ let poetryBin = getCustomPath('poetry', opts);
56
+ let envBypass = environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_TREE')
57
+ && environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_ALL');
58
+ if (!envBypass) {
59
+ this._verifyPoetryAccessible(poetryBin);
60
+ }
61
+ let hasDevGroup = !!(parsed.tool?.poetry?.group?.dev || parsed.tool?.poetry?.['dev-dependencies']);
62
+ let treeOutput = this._getPoetryShowTreeOutput(manifestDir, hasDevGroup, poetryBin);
63
+ let showAllOutput = this._getPoetryShowAllOutput(manifestDir, poetryBin);
64
+ let versionMap = this._parsePoetryShowAll(showAllOutput);
65
+ let lockDir = this._findLockFileDir(manifestDir, opts);
66
+ let markerData = this._extractMarkerData(lockDir, parsed);
67
+ return this._parsePoetryTree(treeOutput, versionMap, markerData);
68
+ }
69
+ /**
70
+ * Get poetry show --tree output.
71
+ * @param {string} manifestDir
72
+ * @param {boolean} hasDevGroup
73
+ * @param {string} poetryBin
74
+ * @returns {string}
75
+ */
76
+ _getPoetryShowTreeOutput(manifestDir, hasDevGroup, poetryBin) {
77
+ if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_TREE')) {
78
+ return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_TREE'], 'base64').toString('utf-8');
79
+ }
80
+ let args = ['show', '--tree', '--no-ansi'];
81
+ if (hasDevGroup) {
82
+ args.push('--without', 'dev');
83
+ }
84
+ return invokeCommand(poetryBin, args, { cwd: manifestDir }).toString();
85
+ }
86
+ /**
87
+ * Get poetry show --all output (flat list with resolved versions).
88
+ * @param {string} manifestDir
89
+ * @param {string} poetryBin
90
+ * @returns {string}
91
+ */
92
+ _getPoetryShowAllOutput(manifestDir, poetryBin) {
93
+ if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_ALL')) {
94
+ return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_ALL'], 'base64').toString('utf-8');
95
+ }
96
+ return invokeCommand(poetryBin, ['show', '--no-ansi', '--all'], { cwd: manifestDir }).toString();
97
+ }
98
+ /**
99
+ * Parse poetry show --all output into a version map.
100
+ * Lines look like: "name (!) 1.2.3 Description text..."
101
+ * or: "name 1.2.3 Description text..."
102
+ * @param {string} output
103
+ * @returns {Map<string, string>} canonical name -> version
104
+ */
105
+ _parsePoetryShowAll(output) {
106
+ let versions = new Map();
107
+ let lines = output.split(/\r?\n/);
108
+ for (let line of lines) {
109
+ let trimmed = line.trim();
110
+ if (!trimmed) {
111
+ continue;
112
+ }
113
+ let match = trimmed.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(?:\(!\)\s+)?(\S+)/);
114
+ if (match) {
115
+ versions.set(this._canonicalize(match[1]), match[2]);
116
+ }
117
+ }
118
+ return versions;
119
+ }
120
+ /**
121
+ * Collects PEP 508 marker expressions for direct and transitive deps.
122
+ * Direct markers come from pyproject.toml dependency strings, e.g.:
123
+ * "pywin32>=311 ; sys_platform == 'win32'" → directMarkers['pywin32'] = "sys_platform == 'win32'"
124
+ * Transitive markers come from poetry.lock [package.dependencies] entries, e.g.:
125
+ * colorama = {version = "*", markers = "sys_platform == 'win32'"}
126
+ * → transitiveMarkers['click']['colorama'] = "sys_platform == 'win32'"
127
+ * @param {string|null} lockDir
128
+ * @param {object} parsed - parsed pyproject.toml
129
+ * @returns {{directMarkers: Map<string, string>, transitiveMarkers: Map<string, Map<string, string>>, hashMap: Map<string, Array<{alg: string, content: string}>>}}
130
+ */
131
+ _extractMarkerData(lockDir, parsed) {
132
+ let directMarkers = new Map();
133
+ let transitiveMarkers = new Map();
134
+ let hashMap = new Map();
135
+ // Extract markers from PEP 621 dependency strings: "name[extras]>=ver ; marker"
136
+ let deps = parsed.project?.dependencies || [];
137
+ for (let dep of deps) {
138
+ let m = dep.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s*[^;]*;\s*(.+)$/);
139
+ if (m) {
140
+ directMarkers.set(this._canonicalize(m[1]), m[2].trim());
141
+ }
142
+ }
143
+ if (lockDir) {
144
+ let lockPath = path.join(lockDir, this._lockFileName());
145
+ if (fs.existsSync(lockPath)) {
146
+ let lockContent = fs.readFileSync(lockPath, 'utf-8');
147
+ let lock = parseToml(lockContent);
148
+ let packages = lock.package || [];
149
+ for (let pkg of packages) {
150
+ let pkgKey = this._canonicalize(pkg.name);
151
+ let pkgDeps = pkg.dependencies || {};
152
+ for (let [depName, depSpec] of Object.entries(pkgDeps)) {
153
+ let markers = typeof depSpec === 'object' && depSpec != null ? depSpec.markers : null;
154
+ if (markers) {
155
+ if (!transitiveMarkers.has(pkgKey)) {
156
+ transitiveMarkers.set(pkgKey, new Map());
157
+ }
158
+ transitiveMarkers.get(pkgKey).set(this._canonicalize(depName), markers);
159
+ }
160
+ }
161
+ let sha256Hex = this._extractSha256FromFiles(pkg.files);
162
+ if (sha256Hex) {
163
+ hashMap.set(pkgKey, [{ alg: "SHA-256", content: sha256Hex }]);
164
+ }
165
+ }
166
+ }
167
+ }
168
+ return { directMarkers, transitiveMarkers, hashMap };
169
+ }
170
+ /**
171
+ * Extract a SHA-256 hex digest from poetry.lock files array.
172
+ * Prefers the sdist (.tar.gz) hash; falls back to the first wheel hash.
173
+ * @param {Array<{file: string, hash: string}>} [files]
174
+ * @returns {string|null}
175
+ */
176
+ _extractSha256FromFiles(files) {
177
+ if (!Array.isArray(files) || files.length === 0) {
178
+ return null;
179
+ }
180
+ let sdist = files.find(f => f.file.endsWith('.tar.gz'));
181
+ let entry = sdist || files[0];
182
+ let hash = entry.hash;
183
+ if (hash && hash.startsWith('sha256:')) {
184
+ return hash.slice(7);
185
+ }
186
+ return null;
187
+ }
188
+ /**
189
+ * Parse poetry show --tree output into a dependency graph structure.
190
+ *
191
+ * @param {string} treeOutput
192
+ * @param {Map<string, string>} versionMap - canonical name -> resolved version
193
+ * @param {{directMarkers: Map<string, string>, transitiveMarkers: Map<string, Map<string, string>>, hashMap: Map<string, Array<{alg: string, content: string}>>}} markerData
194
+ * @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[], hashes?: Array<{alg: string, content: string}>}>}}
195
+ */
196
+ _parsePoetryTree(treeOutput, versionMap, markerData) {
197
+ let lines = treeOutput.split(/\r?\n/);
198
+ let graph = new Map();
199
+ let directDeps = [];
200
+ let stack = []; // [{key, depth}]
201
+ let currentDirectDep = null;
202
+ for (let line of lines) {
203
+ if (!line.trim()) {
204
+ continue;
205
+ }
206
+ // top-level line: "name version description..."
207
+ let topMatch = line.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(\S+)(?:\s|$)/);
208
+ if (topMatch) {
209
+ let name = topMatch[1];
210
+ let version = topMatch[2];
211
+ let key = this._canonicalize(name);
212
+ let marker = markerData.directMarkers.get(key);
213
+ if (marker && !evaluateMarker(marker)) {
214
+ currentDirectDep = null;
215
+ stack = [];
216
+ continue;
217
+ }
218
+ directDeps.push(key);
219
+ if (!graph.has(key)) {
220
+ let entry = { name, version, children: [] };
221
+ let hashes = markerData.hashMap.get(key);
222
+ if (hashes) {
223
+ entry.hashes = hashes;
224
+ }
225
+ graph.set(key, entry);
226
+ }
227
+ currentDirectDep = key;
228
+ stack = [{ key, depth: -1 }];
229
+ continue;
230
+ }
231
+ if (!currentDirectDep) {
232
+ continue;
233
+ }
234
+ // indented line with tree chars (UTF-8 box-drawing: ├── └── │)
235
+ let nameStart = line.search(/[A-Za-z0-9]/);
236
+ if (nameStart < 0) {
237
+ continue;
238
+ }
239
+ let rest = line.substring(nameStart);
240
+ let depMatch = rest.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)/);
241
+ if (!depMatch) {
242
+ continue;
243
+ }
244
+ let depName = depMatch[1];
245
+ let depKey = this._canonicalize(depName);
246
+ // determine depth by counting tree-drawing groups in the prefix
247
+ let prefix = line.substring(0, nameStart);
248
+ let depth = (prefix.match(/(?:[├└│ ][\s─]{2} ?)/g) || []).length;
249
+ // pop stack back to find the parent at depth-1
250
+ while (stack.length > 0 && stack[stack.length - 1].depth >= depth) {
251
+ stack.pop();
252
+ }
253
+ let parentKey = stack.length > 0 ? stack[stack.length - 1].key : null;
254
+ if (parentKey) {
255
+ let parentMarkers = markerData.transitiveMarkers.get(parentKey);
256
+ if (parentMarkers) {
257
+ let marker = parentMarkers.get(depKey);
258
+ if (marker && !evaluateMarker(marker)) {
259
+ continue;
260
+ }
261
+ }
262
+ }
263
+ // resolve version from the version map
264
+ let version = versionMap.get(depKey) || null;
265
+ if (!version) {
266
+ throw new Error(`poetry: package '${depName}' has no resolved version`);
267
+ }
268
+ if (!graph.has(depKey)) {
269
+ let entry = { name: depName, version, children: [] };
270
+ let hashes = markerData.hashMap.get(depKey);
271
+ if (hashes) {
272
+ entry.hashes = hashes;
273
+ }
274
+ graph.set(depKey, entry);
275
+ }
276
+ if (parentKey) {
277
+ let parentEntry = graph.get(parentKey);
278
+ if (parentEntry && !parentEntry.children.includes(depKey)) {
279
+ parentEntry.children.push(depKey);
280
+ }
281
+ }
282
+ stack.push({ key: depKey, depth });
283
+ }
284
+ return { directDeps, graph };
285
+ }
286
+ }