@trustify-da/trustify-da-javascript-client 0.3.0-ea.2ea1d77 → 0.3.0-ea.329b3cf

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 (80) hide show
  1. package/README.md +75 -10
  2. package/dist/package.json +28 -24
  3. package/dist/src/analysis.d.ts +16 -7
  4. package/dist/src/analysis.js +36 -17
  5. package/dist/src/cli.js +109 -3
  6. package/dist/src/cyclone_dx_sbom.d.ts +14 -1
  7. package/dist/src/cyclone_dx_sbom.js +43 -8
  8. package/dist/src/index.d.ts +86 -11
  9. package/dist/src/index.js +97 -8
  10. package/dist/src/license/index.d.ts +3 -3
  11. package/dist/src/license/index.js +4 -1
  12. package/dist/src/license/license_utils.js +4 -1
  13. package/dist/src/license/licenses_api.d.ts +2 -2
  14. package/dist/src/oci_image/images.d.ts +1 -1
  15. package/dist/src/oci_image/images.js +15 -1
  16. package/dist/src/oci_image/utils.d.ts +5 -5
  17. package/dist/src/oci_image/utils.js +11 -2
  18. package/dist/src/provider.d.ts +4 -3
  19. package/dist/src/provider.js +12 -2
  20. package/dist/src/providers/base_java.d.ts +14 -12
  21. package/dist/src/providers/base_java.js +18 -44
  22. package/dist/src/providers/base_javascript.d.ts +36 -15
  23. package/dist/src/providers/base_javascript.js +100 -20
  24. package/dist/src/providers/base_pyproject.d.ts +158 -0
  25. package/dist/src/providers/base_pyproject.js +322 -0
  26. package/dist/src/providers/containerfile_parser.d.ts +5 -0
  27. package/dist/src/providers/containerfile_parser.js +20 -0
  28. package/dist/src/providers/golang_gomodules.d.ts +23 -13
  29. package/dist/src/providers/golang_gomodules.js +165 -119
  30. package/dist/src/providers/gomod_parser.d.ts +4 -0
  31. package/dist/src/providers/gomod_parser.js +16 -0
  32. package/dist/src/providers/java_gradle.d.ts +19 -0
  33. package/dist/src/providers/java_gradle.js +116 -2
  34. package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
  35. package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
  36. package/dist/src/providers/java_maven.d.ts +10 -2
  37. package/dist/src/providers/java_maven.js +165 -1
  38. package/dist/src/providers/javascript_bun.d.ts +10 -0
  39. package/dist/src/providers/javascript_bun.js +100 -0
  40. package/dist/src/providers/javascript_npm.d.ts +6 -0
  41. package/dist/src/providers/javascript_npm.js +21 -0
  42. package/dist/src/providers/javascript_pnpm.js +6 -2
  43. package/dist/src/providers/manifest.d.ts +2 -0
  44. package/dist/src/providers/manifest.js +22 -4
  45. package/dist/src/providers/marker_evaluator.d.ts +14 -0
  46. package/dist/src/providers/marker_evaluator.js +191 -0
  47. package/dist/src/providers/oci_dockerfile.d.ts +51 -0
  48. package/dist/src/providers/oci_dockerfile.js +177 -0
  49. package/dist/src/providers/processors/yarn_berry_processor.d.ts +2 -2
  50. package/dist/src/providers/processors/yarn_berry_processor.js +88 -5
  51. package/dist/src/providers/processors/yarn_classic_processor.d.ts +2 -2
  52. package/dist/src/providers/python_controller.d.ts +5 -1
  53. package/dist/src/providers/python_controller.js +69 -8
  54. package/dist/src/providers/python_pip.d.ts +5 -0
  55. package/dist/src/providers/python_pip.js +6 -6
  56. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  57. package/dist/src/providers/python_pip_pyproject.js +146 -0
  58. package/dist/src/providers/python_poetry.d.ts +98 -0
  59. package/dist/src/providers/python_poetry.js +286 -0
  60. package/dist/src/providers/python_uv.d.ts +46 -0
  61. package/dist/src/providers/python_uv.js +269 -0
  62. package/dist/src/providers/rust_cargo.d.ts +6 -2
  63. package/dist/src/providers/rust_cargo.js +31 -4
  64. package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
  65. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  66. package/dist/src/remediation.d.ts +34 -0
  67. package/dist/src/remediation.js +321 -0
  68. package/dist/src/remediation_report.d.ts +40 -0
  69. package/dist/src/remediation_report.js +159 -0
  70. package/dist/src/sbom.d.ts +14 -1
  71. package/dist/src/sbom.js +13 -2
  72. package/dist/src/tools.d.ts +28 -2
  73. package/dist/src/tools.js +58 -0
  74. package/dist/src/updaters/maven_updater.d.ts +26 -0
  75. package/dist/src/updaters/maven_updater.js +322 -0
  76. package/dist/src/updaters/toml_updater.d.ts +33 -0
  77. package/dist/src/updaters/toml_updater.js +244 -0
  78. package/dist/src/workspace.d.ts +9 -0
  79. package/dist/src/workspace.js +1 -1
  80. package/package.json +29 -25
@@ -0,0 +1,269 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import fg from 'fast-glob';
4
+ import { parse as parseToml } from 'smol-toml';
5
+ import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
6
+ import { filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore, toManifestGlobPatterns } from '../workspace.js';
7
+ import Base_pyproject from './base_pyproject.js';
8
+ import { evaluateMarker } from './marker_evaluator.js';
9
+ import { getParser, getPinnedVersionQuery } from './requirements_parser.js';
10
+ export default class Python_uv extends Base_pyproject {
11
+ /** @returns {string} */
12
+ _lockFileName() {
13
+ return 'uv.lock';
14
+ }
15
+ /** @returns {string} */
16
+ _cmdName() {
17
+ return 'uv';
18
+ }
19
+ /**
20
+ * @param {string} manifestDir - directory containing the target pyproject.toml
21
+ * @param {string} workspaceDir - workspace root (for resolving editable install paths)
22
+ * @param {object} parsed - parsed pyproject.toml
23
+ * @param {Object} opts
24
+ * @returns {Promise<{directDeps: string[], graph: Map<string, import('./base_pyproject.js').GraphEntry>}>}
25
+ */
26
+ async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
27
+ let projectName = this._getProjectName(parsed);
28
+ let uvOutput = this._getUvExportOutput(manifestDir, opts);
29
+ let { directDeps, graph } = await this._parseUvExport(uvOutput, projectName, workspaceDir);
30
+ await this._attachHashesFromLockFile(path.join(workspaceDir, 'uv.lock'), graph);
31
+ return { directDeps, graph };
32
+ }
33
+ /**
34
+ * Parse uv.lock and attach SHA-256 hashes to graph entries.
35
+ * @param {string} lockFilePath - path to uv.lock
36
+ * @param {Map<string, import('./base_pyproject.js').GraphEntry>} graph
37
+ */
38
+ async _attachHashesFromLockFile(lockFilePath, graph) {
39
+ let lockContent;
40
+ try {
41
+ lockContent = await fs.promises.readFile(lockFilePath, 'utf-8');
42
+ }
43
+ catch {
44
+ return;
45
+ }
46
+ let parsed;
47
+ try {
48
+ parsed = parseToml(lockContent);
49
+ }
50
+ catch {
51
+ return;
52
+ }
53
+ let packages = parsed.package;
54
+ if (!Array.isArray(packages)) {
55
+ return;
56
+ }
57
+ for (let pkg of packages) {
58
+ if (!pkg.name) {
59
+ continue;
60
+ }
61
+ let hashStr = pkg.sdist?.hash || pkg.wheels?.[0]?.hash;
62
+ if (!hashStr || !hashStr.startsWith('sha256:')) {
63
+ continue;
64
+ }
65
+ let key = this._canonicalize(pkg.name);
66
+ let entry = graph.get(key);
67
+ if (!entry || entry.hashes) {
68
+ continue;
69
+ }
70
+ entry.hashes = [{ alg: "SHA-256", content: hashStr.slice(7) }];
71
+ }
72
+ }
73
+ /**
74
+ * Get the uv export output, either from env var or by running the command.
75
+ * @param {string} manifestDir
76
+ * @param {Object} opts
77
+ * @returns {string}
78
+ */
79
+ _getUvExportOutput(manifestDir, opts) {
80
+ if (environmentVariableIsPopulated('TRUSTIFY_DA_UV_EXPORT')) {
81
+ return Buffer.from(process.env['TRUSTIFY_DA_UV_EXPORT'], 'base64').toString('ascii');
82
+ }
83
+ let uvBin = getCustomPath('uv', opts);
84
+ return invokeCommand(uvBin, ['export', '--format', 'requirements.txt', '--frozen', '--no-hashes', '--no-dev', '--no-emit-project'], { cwd: manifestDir }).toString();
85
+ }
86
+ /**
87
+ * Parse uv export output into a dependency graph using tree-sitter-requirements
88
+ * for package/version extraction and string parsing for "# via" comments.
89
+ *
90
+ * @param {string} output
91
+ * @param {string} projectName - canonical project name to identify direct deps
92
+ * @param {string} workspaceDir - workspace root (for resolving editable install paths)
93
+ * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
94
+ */
95
+ async _parseUvExport(output, projectName, workspaceDir) {
96
+ let [parser, pinnedVersionQuery] = await Promise.all([
97
+ getParser(), getPinnedVersionQuery()
98
+ ]);
99
+ let tree = parser.parse(output);
100
+ let root = tree.rootNode;
101
+ let canonProjectName = this._canonicalize(projectName);
102
+ let packages = new Map(); // canonical name -> {name, version, parents: Set}
103
+ let currentPkg = null;
104
+ let collectingVia = false;
105
+ for (let child of root.children) {
106
+ if (child.type === 'global_opt') {
107
+ let optNode = child.children.find(c => c.type === 'option');
108
+ let pathNode = child.children.find(c => c.type === 'path');
109
+ if (optNode?.text === '-e' && pathNode && workspaceDir) {
110
+ let memberDir = path.resolve(workspaceDir, pathNode.text);
111
+ let memberManifest = path.join(memberDir, 'pyproject.toml');
112
+ if (fs.existsSync(memberManifest)) {
113
+ let memberParsed = parseToml(fs.readFileSync(memberManifest, 'utf-8'));
114
+ let name = memberParsed.project?.name || memberParsed.tool?.poetry?.name;
115
+ let version = memberParsed.project?.version || memberParsed.tool?.poetry?.version;
116
+ if (name && version) {
117
+ let key = this._canonicalize(name);
118
+ if (key === canonProjectName) {
119
+ continue;
120
+ }
121
+ currentPkg = { name, version, parents: new Set() };
122
+ packages.set(key, currentPkg);
123
+ collectingVia = false;
124
+ continue;
125
+ }
126
+ }
127
+ }
128
+ currentPkg = null;
129
+ collectingVia = false;
130
+ continue;
131
+ }
132
+ if (child.type === 'requirement') {
133
+ let nameNode = child.children.find(c => c.type === 'package');
134
+ if (!nameNode) {
135
+ continue;
136
+ }
137
+ // Skip packages with non-matching PEP 508 markers, e.g. "pywin32==311 ; sys_platform == 'win32'"
138
+ let markerNode = child.children.find(c => c.type === 'marker_spec');
139
+ if (markerNode) {
140
+ let markerText = markerNode.text.replace(/^\s*;\s*/, '');
141
+ if (!evaluateMarker(markerText)) {
142
+ currentPkg = null;
143
+ collectingVia = false;
144
+ continue;
145
+ }
146
+ }
147
+ let name = nameNode.text;
148
+ let version = null;
149
+ let versionMatches = pinnedVersionQuery.matches(child);
150
+ if (versionMatches.length > 0) {
151
+ version = versionMatches[0].captures.find(c => c.name === 'version').node.text;
152
+ }
153
+ if (!version) {
154
+ throw new Error(`uv export: package '${name}' has no pinned version`);
155
+ }
156
+ let key = this._canonicalize(name);
157
+ currentPkg = { name, version, parents: new Set() };
158
+ packages.set(key, currentPkg);
159
+ collectingVia = false;
160
+ continue;
161
+ }
162
+ if (child.type === 'comment' && currentPkg) {
163
+ let text = child.text.trim();
164
+ let viaSingle = text.match(/^# via ([A-Za-z0-9][A-Za-z0-9._-]*)$/);
165
+ if (viaSingle) {
166
+ currentPkg.parents.add(this._canonicalize(viaSingle[1]));
167
+ collectingVia = false;
168
+ continue;
169
+ }
170
+ if (text === '# via') {
171
+ collectingVia = true;
172
+ continue;
173
+ }
174
+ if (collectingVia) {
175
+ let parentMatch = text.match(/^#\s+([A-Za-z0-9][A-Za-z0-9._-]*)$/);
176
+ if (parentMatch) {
177
+ currentPkg.parents.add(this._canonicalize(parentMatch[1]));
178
+ continue;
179
+ }
180
+ collectingVia = false;
181
+ }
182
+ }
183
+ }
184
+ // Build forward dependency map and extract direct deps in one pass
185
+ let graph = new Map();
186
+ let directDeps = [];
187
+ for (let [key, pkg] of packages) {
188
+ graph.set(key, { name: pkg.name, version: pkg.version, children: [] });
189
+ }
190
+ for (let [childKey, pkg] of packages) {
191
+ for (let parentKey of pkg.parents) {
192
+ if (parentKey === canonProjectName) {
193
+ directDeps.push(childKey);
194
+ continue;
195
+ }
196
+ let parentEntry = graph.get(parentKey);
197
+ if (parentEntry) {
198
+ parentEntry.children.push(childKey);
199
+ }
200
+ }
201
+ }
202
+ return { directDeps, graph };
203
+ }
204
+ }
205
+ const DEFAULT_UV_DISCOVERY_IGNORE = [
206
+ '**/__pycache__/**',
207
+ '**/.venv/**',
208
+ ];
209
+ /**
210
+ * Discover all pyproject.toml manifest paths in a uv workspace.
211
+ * Parses `[tool.uv.workspace]` from root pyproject.toml and glob-expands member patterns.
212
+ *
213
+ * @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain pyproject.toml and uv.lock)
214
+ * @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}]
215
+ * @returns {Promise<string[]>} Paths to pyproject.toml files (absolute)
216
+ */
217
+ export async function discoverUvWorkspaceMembers(workspaceRoot, opts = {}) {
218
+ const root = path.resolve(workspaceRoot);
219
+ const rootPyproject = path.join(root, 'pyproject.toml');
220
+ const uvLock = path.join(root, 'uv.lock');
221
+ if (!fs.existsSync(rootPyproject) || !fs.existsSync(uvLock)) {
222
+ return [];
223
+ }
224
+ let parsed;
225
+ try {
226
+ parsed = parseToml(fs.readFileSync(rootPyproject, 'utf-8'));
227
+ }
228
+ catch {
229
+ return [];
230
+ }
231
+ const workspaceConfig = parsed?.tool?.uv?.workspace;
232
+ if (!workspaceConfig) {
233
+ return [];
234
+ }
235
+ const memberPatterns = workspaceConfig.members;
236
+ if (!Array.isArray(memberPatterns) || memberPatterns.length === 0) {
237
+ return [];
238
+ }
239
+ const excludePatterns = Array.isArray(workspaceConfig.exclude) ? workspaceConfig.exclude : [];
240
+ const excludeGlobs = excludePatterns
241
+ .filter(p => typeof p === 'string' && p.trim())
242
+ .map(p => `${p.trim()}/pyproject.toml`);
243
+ const ignorePatterns = [...resolveWorkspaceDiscoveryIgnore(opts), ...DEFAULT_UV_DISCOVERY_IGNORE];
244
+ const globOpts = {
245
+ cwd: root,
246
+ absolute: true,
247
+ onlyFiles: true,
248
+ ignore: [...ignorePatterns, ...excludeGlobs],
249
+ followSymbolicLinks: false,
250
+ };
251
+ const patterns = toManifestGlobPatterns(memberPatterns.filter(p => typeof p === 'string'), 'pyproject.toml');
252
+ const manifestPaths = await fg(patterns, globOpts);
253
+ if (!manifestPaths.includes(rootPyproject) && hasProjectMetadata(parsed)) {
254
+ manifestPaths.unshift(rootPyproject);
255
+ }
256
+ return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
257
+ }
258
+ /**
259
+ * @param {import('smol-toml').TomlTable} parsedPyProject
260
+ * @returns {boolean}
261
+ */
262
+ function hasProjectMetadata(parsedPyProject) {
263
+ try {
264
+ return typeof parsedPyProject?.project?.name === 'string' && parsedPyProject.project.name.trim() !== '';
265
+ }
266
+ catch {
267
+ return false;
268
+ }
269
+ }
@@ -4,9 +4,10 @@ declare namespace _default {
4
4
  export { provideComponent };
5
5
  export { provideStack };
6
6
  export { readLicenseFromManifest };
7
+ export function packageManagerName(): string;
7
8
  }
8
9
  export default _default;
9
- export type Provided = import("../provider").Provided;
10
+ export type Provided = any;
10
11
  /**
11
12
  * @param {string} manifestName - the subject manifest name-type
12
13
  * @returns {boolean} - return true if `Cargo.toml` is the manifest name-type
@@ -46,7 +47,10 @@ declare function provideStack(manifest: string, opts?: {}): Provided;
46
47
  * Read project license from Cargo.toml, with fallback to LICENSE file.
47
48
  * Supports the `license` field under `[package]` (single crate / workspace
48
49
  * with root) and under `[workspace.package]` (virtual workspaces).
50
+ * When a member crate uses `license = { workspace = true }`, the actual
51
+ * license is resolved from the workspace root Cargo.toml.
49
52
  * @param {string} manifestPath - path to Cargo.toml
53
+ * @param {object} [metadata] - cargo metadata output (needed for workspace license resolution)
50
54
  * @returns {string|null} SPDX identifier or null
51
55
  */
52
- declare function readLicenseFromManifest(manifestPath: string): string | null;
56
+ declare function readLicenseFromManifest(manifestPath: string, metadata?: object): string | null;
@@ -5,7 +5,7 @@ import { parse as parseToml } from 'smol-toml';
5
5
  import { getLicense } from '../license/license_utils.js';
6
6
  import Sbom from '../sbom.js';
7
7
  import { getCustom, getCustomPath, invokeCommand } from '../tools.js';
8
- export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
8
+ export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest, packageManagerName() { return 'cargo'; } };
9
9
  /** @typedef {import('../provider').Provider} */
10
10
  /** @typedef {import('../provider').Provided} Provided */
11
11
  /**
@@ -71,15 +71,22 @@ function isSupported(manifestName) {
71
71
  * Read project license from Cargo.toml, with fallback to LICENSE file.
72
72
  * Supports the `license` field under `[package]` (single crate / workspace
73
73
  * with root) and under `[workspace.package]` (virtual workspaces).
74
+ * When a member crate uses `license = { workspace = true }`, the actual
75
+ * license is resolved from the workspace root Cargo.toml.
74
76
  * @param {string} manifestPath - path to Cargo.toml
77
+ * @param {object} [metadata] - cargo metadata output (needed for workspace license resolution)
75
78
  * @returns {string|null} SPDX identifier or null
76
79
  */
77
- function readLicenseFromManifest(manifestPath) {
80
+ function readLicenseFromManifest(manifestPath, metadata) {
78
81
  let fromManifest = null;
79
82
  try {
80
83
  let content = fs.readFileSync(manifestPath, 'utf-8');
81
84
  let parsed = parseToml(content);
82
- fromManifest = parsed.package?.license
85
+ let packageLicense = parsed.package?.license;
86
+ if (typeof packageLicense === 'object' && packageLicense?.workspace === true) {
87
+ packageLicense = resolveWorkspaceLicense(parsed, metadata);
88
+ }
89
+ fromManifest = packageLicense
83
90
  || parsed.workspace?.package?.license
84
91
  || null;
85
92
  }
@@ -88,6 +95,26 @@ function readLicenseFromManifest(manifestPath) {
88
95
  }
89
96
  return getLicense(fromManifest, manifestPath);
90
97
  }
98
+ /**
99
+ * Resolve a workspace-inherited license from the workspace root Cargo.toml.
100
+ * @param {object} parsed - the parsed member crate Cargo.toml
101
+ * @param {object} [metadata] - cargo metadata output with workspace_root
102
+ * @returns {string|null} resolved license string or null
103
+ */
104
+ function resolveWorkspaceLicense(parsed, metadata) {
105
+ if (metadata?.workspace_root) {
106
+ let cargoTomlPath = path.join(metadata.workspace_root, 'Cargo.toml');
107
+ try {
108
+ let content = fs.readFileSync(cargoTomlPath, 'utf-8');
109
+ let workspaceParsed = parseToml(content);
110
+ return workspaceParsed.workspace?.package?.license || null;
111
+ }
112
+ catch (_) {
113
+ // fall through
114
+ }
115
+ }
116
+ return parsed.workspace?.package?.license || null;
117
+ }
91
118
  /**
92
119
  * Validates that Cargo.lock exists in the manifest directory or in a parent
93
120
  * workspace root directory. In Cargo workspaces the lock file always lives at
@@ -173,7 +200,7 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
173
200
  let metadata = executeCargoMetadata(cargoBin, manifestDir);
174
201
  let ignoredDeps = getIgnoredDeps(manifest, metadata);
175
202
  let crateType = detectCrateType(metadata);
176
- let license = readLicenseFromManifest(manifest);
203
+ let license = readLicenseFromManifest(manifest, metadata);
177
204
  let sbom;
178
205
  if (crateType === CrateType.WORKSPACE_VIRTUAL) {
179
206
  sbom = handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license);
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Extracts actionable remediation instructions from a DA AnalysisReport response.
3
+ *
4
+ * Walks the provider/source/dependency/issue tree, collects fixedIn and trustedContent
5
+ * remediation data, applies provider priority resolution, and for the same dependency
6
+ * affected by multiple CVEs selects the highest fixedIn version from the highest-priority
7
+ * provider. Dependencies with no remediation data are skipped.
8
+ *
9
+ * @param {object} analysisReport - raw DA AnalysisReport JSON response
10
+ * @param {object} [options] - extraction options
11
+ * @param {string[]} [options.providerPriority] - provider names in descending priority order.
12
+ * The first entry has the highest priority. Providers not listed share the lowest priority.
13
+ * When omitted or empty, all providers are treated equally and the highest fix version wins.
14
+ * @returns {Array<{purl: string, groupId: string, artifactId: string, currentVersion: string, fixedInVersion: string, fixedInPurl: string, provider: string, source: string, advisories: Array<{id: string, url: string}>, severity: string, cves: string[]}>}
15
+ */
16
+ export function extractRemediations(analysisReport: object, options?: {
17
+ providerPriority?: string[] | undefined;
18
+ }): Array<{
19
+ purl: string;
20
+ groupId: string;
21
+ artifactId: string;
22
+ currentVersion: string;
23
+ fixedInVersion: string;
24
+ fixedInPurl: string;
25
+ provider: string;
26
+ source: string;
27
+ advisories: Array<{
28
+ id: string;
29
+ url: string;
30
+ }>;
31
+ severity: string;
32
+ cves: string[];
33
+ }>;
34
+ export const SEVERITY_ORDER: string[];