@trustify-da/trustify-da-javascript-client 0.3.0-ea.477151f → 0.3.0-ea.608d6fa

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 (38) hide show
  1. package/README.md +65 -2
  2. package/dist/package.json +6 -2
  3. package/dist/src/analysis.d.ts +0 -6
  4. package/dist/src/analysis.js +19 -64
  5. package/dist/src/cli.js +72 -6
  6. package/dist/src/cyclone_dx_sbom.d.ts +3 -1
  7. package/dist/src/cyclone_dx_sbom.js +16 -4
  8. package/dist/src/index.d.ts +3 -0
  9. package/dist/src/index.js +3 -0
  10. package/dist/src/license/index.d.ts +28 -0
  11. package/dist/src/license/index.js +100 -0
  12. package/dist/src/license/license_utils.d.ts +40 -0
  13. package/dist/src/license/license_utils.js +134 -0
  14. package/dist/src/license/licenses_api.d.ts +34 -0
  15. package/dist/src/license/licenses_api.js +98 -0
  16. package/dist/src/license/project_license.d.ts +20 -0
  17. package/dist/src/license/project_license.js +62 -0
  18. package/dist/src/provider.d.ts +10 -1
  19. package/dist/src/provider.js +19 -2
  20. package/dist/src/providers/base_javascript.d.ts +10 -4
  21. package/dist/src/providers/base_javascript.js +30 -4
  22. package/dist/src/providers/golang_gomodules.d.ts +8 -1
  23. package/dist/src/providers/golang_gomodules.js +13 -4
  24. package/dist/src/providers/java_gradle.d.ts +6 -0
  25. package/dist/src/providers/java_gradle.js +12 -2
  26. package/dist/src/providers/java_maven.d.ts +8 -1
  27. package/dist/src/providers/java_maven.js +32 -4
  28. package/dist/src/providers/python_pip.d.ts +7 -0
  29. package/dist/src/providers/python_pip.js +13 -3
  30. package/dist/src/providers/requirements_parser.js +5 -8
  31. package/dist/src/providers/rust_cargo.d.ts +47 -0
  32. package/dist/src/providers/rust_cargo.js +606 -0
  33. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  34. package/dist/src/sbom.d.ts +3 -1
  35. package/dist/src/sbom.js +3 -2
  36. package/dist/src/tools.d.ts +18 -0
  37. package/dist/src/tools.js +55 -0
  38. package/package.json +7 -3
@@ -3,6 +3,7 @@ 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 Provided = import("../provider").Provided;
@@ -20,7 +21,7 @@ export type Dependency = {
20
21
  /**
21
22
  * @param {string} manifestName - the subject manifest name-type
22
23
  * @returns {boolean} - return true if `pom.xml` is the manifest name-type
23
- */
24
+ */
24
25
  declare function isSupported(manifestName: string): boolean;
25
26
  /**
26
27
  * @param {string} manifestDir - the directory where the manifest lies
@@ -40,3 +41,9 @@ declare function provideComponent(manifest: string, opts?: {}): Provided;
40
41
  * @returns {Provided}
41
42
  */
42
43
  declare function provideStack(manifest: string, opts?: {}): Provided;
44
+ /**
45
+ * Go modules have no standard license field in go.mod
46
+ * @param {string} manifestPath - path to go.mod
47
+ * @returns {string|null}
48
+ */
49
+ declare function readLicenseFromManifest(manifestPath: string): string | null;
@@ -2,9 +2,10 @@ import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { EOL } from "os";
4
4
  import { PackageURL } from 'packageurl-js';
5
+ import { readLicenseFile } from '../license/license_utils.js';
5
6
  import Sbom from '../sbom.js';
6
7
  import { getCustom, getCustomPath, invokeCommand } from "../tools.js";
7
- export default { isSupported, validateLockFile, provideComponent, provideStack };
8
+ export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
8
9
  /** @typedef {import('../provider').Provider} */
9
10
  /** @typedef {import('../provider').Provided} Provided */
10
11
  /** @typedef {{name: string, version: string}} Package */
@@ -12,16 +13,23 @@ export default { isSupported, validateLockFile, provideComponent, provideStack }
12
13
  /**
13
14
  * @type {string} ecosystem for npm-npm is 'maven'
14
15
  * @private
15
- */
16
+ */
16
17
  const ecosystem = 'golang';
17
18
  const defaultMainModuleVersion = "v0.0.0";
18
19
  /**
19
20
  * @param {string} manifestName - the subject manifest name-type
20
21
  * @returns {boolean} - return true if `pom.xml` is the manifest name-type
21
- */
22
+ */
22
23
  function isSupported(manifestName) {
23
24
  return 'go.mod' === manifestName;
24
25
  }
26
+ /**
27
+ * Go modules have no standard license field in go.mod
28
+ * @param {string} manifestPath - path to go.mod
29
+ * @returns {string|null}
30
+ */
31
+ // eslint-disable-next-line no-unused-vars
32
+ function readLicenseFromManifest(manifestPath) { return readLicenseFile(manifestPath); }
25
33
  /**
26
34
  * @param {string} manifestDir - the directory where the manifest lies
27
35
  */
@@ -250,7 +258,8 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
250
258
  performManifestVersionsCheck(root, rows, manifest);
251
259
  }
252
260
  const mainModule = toPurl(root, "@");
253
- sbom.addRoot(mainModule);
261
+ const license = readLicenseFromManifest(manifest);
262
+ sbom.addRoot(mainModule, license);
254
263
  const exhortGoMvsLogicEnabled = getCustom("TRUSTIFY_DA_GO_MVS_LOGIC_ENABLED", "true", opts);
255
264
  if (includeTransitive && exhortGoMvsLogicEnabled === "true") {
256
265
  rows = getFinalPackagesVersionsForModule(rows, manifest, goBin);
@@ -15,6 +15,12 @@ export default class Java_gradle extends Base_java {
15
15
  * @param {string} manifestDir - the directory where the manifest lies
16
16
  */
17
17
  validateLockFile(): boolean;
18
+ /**
19
+ * Gradle manifests (build.gradle, build.gradle.kts) have no standard license field.
20
+ * @param {string} manifestPath - path to manifest
21
+ * @returns {null}
22
+ */
23
+ readLicenseFromManifest(manifestPath: string): null;
18
24
  /**
19
25
  * Provide content and content type for stack analysis.
20
26
  * @param {string} manifest - the manifest path or name
@@ -2,6 +2,7 @@ import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { EOL } from 'os';
4
4
  import TOML from 'fast-toml';
5
+ import { readLicenseFile } from '../license/license_utils.js';
5
6
  import Sbom from '../sbom.js';
6
7
  import Base_java, { ecosystem_gradle } from "./base_java.js";
7
8
  /** @typedef {import('../provider.js').Provider} */
@@ -49,6 +50,13 @@ export default class Java_gradle extends Base_java {
49
50
  * @param {string} manifestDir - the directory where the manifest lies
50
51
  */
51
52
  validateLockFile() { return true; }
53
+ /**
54
+ * Gradle manifests (build.gradle, build.gradle.kts) have no standard license field.
55
+ * @param {string} manifestPath - path to manifest
56
+ * @returns {null}
57
+ */
58
+ // eslint-disable-next-line no-unused-vars
59
+ readLicenseFromManifest(manifestPath) { return readLicenseFile(manifestPath); }
52
60
  /**
53
61
  * Provide content and content type for stack analysis.
54
62
  * @param {string} manifest - the manifest path or name
@@ -158,7 +166,8 @@ export default class Java_gradle extends Base_java {
158
166
  let sbom = new Sbom();
159
167
  let root = `${properties.group}:${properties[ROOT_PROJECT_KEY_NAME].match(/Root project '(.+)'/)[1]}:jar:${properties.version}`;
160
168
  let rootPurl = this.parseDep(root);
161
- sbom.addRoot(rootPurl);
169
+ const license = this.readLicenseFromManifest(manifestPath);
170
+ sbom.addRoot(rootPurl, license);
162
171
  let ignoredDeps = this.#getIgnoredDeps(manifestPath);
163
172
  const [runtimeConfig, compileConfig] = this.#extractConfigurations(content);
164
173
  const processedDeps = new Set();
@@ -298,7 +307,8 @@ export default class Java_gradle extends Base_java {
298
307
  let sbom = new Sbom();
299
308
  let root = `${properties.group}:${properties[ROOT_PROJECT_KEY_NAME].match(/Root project '(.+)'/)[1]}:jar:${properties.version}`;
300
309
  let rootPurl = this.parseDep(root);
301
- sbom.addRoot(rootPurl);
310
+ const license = this.readLicenseFromManifest(manifestPath);
311
+ sbom.addRoot(rootPurl, license);
302
312
  let ignoredDeps = this.#getIgnoredDeps(manifestPath);
303
313
  const [runtimeConfig, compileConfig] = this.#extractConfigurations(content);
304
314
  let directDependencies = new Map();
@@ -27,13 +27,20 @@ export default class Java_maven extends Base_java {
27
27
  * @returns {Provided}
28
28
  */
29
29
  provideComponent(manifest: string, opts?: {}): Provided;
30
+ /**
31
+ * Read license from pom.xml manifest, with fallback to LICENSE file
32
+ * @param {string} manifestPath - path to pom.xml
33
+ * @returns {string|null}
34
+ */
35
+ readLicenseFromManifest(manifestPath: string): string | null;
30
36
  /**
31
37
  *
32
38
  * @param {String} textGraphList Text graph String of the manifest
33
39
  * @param {[String]} ignoredDeps List of ignored dependencies to be omitted from sbom
40
+ * @param {String} manifestPath Path to the pom.xml manifest
34
41
  * @return {String} formatted sbom Json String with all dependencies
35
42
  */
36
- createSbomFileFromTextFormat(textGraphList: string, ignoredDeps: [string], opts: any): string;
43
+ createSbomFileFromTextFormat(textGraphList: string, ignoredDeps: [string], opts: any, manifestPath: string): string;
37
44
  #private;
38
45
  }
39
46
  export type Java_maven = import("../provider").Provider;
@@ -3,6 +3,7 @@ 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
8
  import { getCustom } from '../tools.js';
8
9
  import Base_java, { ecosystem_maven } from "./base_java.js";
@@ -51,6 +52,30 @@ export default class Java_maven extends Base_java {
51
52
  contentType: 'application/vnd.cyclonedx+json'
52
53
  };
53
54
  }
55
+ /**
56
+ * Read license from pom.xml manifest, with fallback to LICENSE file
57
+ * @param {string} manifestPath - path to pom.xml
58
+ * @returns {string|null}
59
+ */
60
+ readLicenseFromManifest(manifestPath) {
61
+ let fromPom = null;
62
+ try {
63
+ const xml = fs.readFileSync(manifestPath, 'utf-8');
64
+ const parser = new XMLParser({ ignoreAttributes: false });
65
+ const obj = parser.parse(xml);
66
+ const project = obj?.project;
67
+ if (project?.licenses?.license) {
68
+ const license = Array.isArray(project.licenses.license)
69
+ ? project.licenses.license[0]
70
+ : project.licenses.license;
71
+ fromPom = (license?.name && license.name.trim()) || null;
72
+ }
73
+ }
74
+ catch {
75
+ // leave fromPom as null
76
+ }
77
+ return getLicense(fromPom, manifestPath);
78
+ }
54
79
  /**
55
80
  * Create a Dot Graph dependency tree for a manifest path.
56
81
  * @param {string} manifest - path for pom.xml
@@ -105,7 +130,7 @@ export default class Java_maven extends Base_java {
105
130
  if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
106
131
  console.error("Dependency tree that will be used as input for creating the BOM =>" + EOL + EOL + content.toString());
107
132
  }
108
- let sbom = this.createSbomFileFromTextFormat(content.toString(), ignoredDeps, opts);
133
+ let sbom = this.createSbomFileFromTextFormat(content.toString(), ignoredDeps, opts, manifest);
109
134
  // delete temp file and directory
110
135
  fs.rmSync(tmpDir, { recursive: true, force: true });
111
136
  // return dependency graph as string
@@ -115,15 +140,17 @@ export default class Java_maven extends Base_java {
115
140
  *
116
141
  * @param {String} textGraphList Text graph String of the manifest
117
142
  * @param {[String]} ignoredDeps List of ignored dependencies to be omitted from sbom
143
+ * @param {String} manifestPath Path to the pom.xml manifest
118
144
  * @return {String} formatted sbom Json String with all dependencies
119
145
  */
120
- createSbomFileFromTextFormat(textGraphList, ignoredDeps, opts) {
146
+ createSbomFileFromTextFormat(textGraphList, ignoredDeps, opts, manifestPath) {
121
147
  let lines = textGraphList.split(EOL);
122
148
  // get root component
123
149
  let root = lines[0];
124
150
  let rootPurl = this.parseDep(root);
151
+ const license = this.readLicenseFromManifest(manifestPath);
125
152
  let sbom = new Sbom();
126
- sbom.addRoot(rootPurl);
153
+ sbom.addRoot(rootPurl, license);
127
154
  this.parseDependencyTree(root, 0, lines.slice(1), sbom);
128
155
  return sbom.filterIgnoredDeps(ignoredDeps).getAsJsonString(opts);
129
156
  }
@@ -156,7 +183,8 @@ export default class Java_maven extends Base_java {
156
183
  let sbom = new Sbom();
157
184
  let rootDependency = this.#getRootFromPom(tmpEffectivePom, manifestPath);
158
185
  let purlRoot = this.toPurl(rootDependency.groupId, rootDependency.artifactId, rootDependency.version);
159
- sbom.addRoot(purlRoot);
186
+ const license = this.readLicenseFromManifest(manifestPath);
187
+ sbom.addRoot(purlRoot, license);
160
188
  dependencies.forEach(dep => {
161
189
  let currentPurl = this.toPurl(dep.groupId, dep.artifactId, dep.version);
162
190
  sbom.addDependency(purlRoot, currentPurl);
@@ -3,6 +3,7 @@ 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 = {
@@ -33,3 +34,9 @@ declare function provideComponent(manifest: string, opts?: {}): Promise<Provided
33
34
  * @returns {Promise<Provided>}
34
35
  */
35
36
  declare function provideStack(manifest: string, opts?: {}): Promise<Provided>;
37
+ /**
38
+ * Python requirements.txt has no standard license field
39
+ * @param {string} manifestPath - path to requirements.txt
40
+ * @returns {string|null}
41
+ */
42
+ declare function readLicenseFromManifest(manifestPath: string): string | null;
@@ -1,10 +1,11 @@
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
+ export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
8
9
  /** @typedef {{name: string, version: string, dependencies: DependencyEntry[]}} DependencyEntry */
9
10
  /**
10
11
  * @type {string} ecosystem for python-pip is 'pip'
@@ -18,6 +19,13 @@ const ecosystem = 'pip';
18
19
  function isSupported(manifestName) {
19
20
  return 'requirements.txt' === manifestName;
20
21
  }
22
+ /**
23
+ * Python requirements.txt has no standard license field
24
+ * @param {string} manifestPath - path to requirements.txt
25
+ * @returns {string|null}
26
+ */
27
+ // eslint-disable-next-line no-unused-vars
28
+ function readLicenseFromManifest(manifestPath) { return readLicenseFile(manifestPath); }
21
29
  /**
22
30
  * @param {string} manifestDir - the directory where the manifest lies
23
31
  */
@@ -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,7 +199,8 @@ 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
205
  sbom.addDependency(rootPurl, toPurl(dep.name, dep.version));
196
206
  });
@@ -1,13 +1,10 @@
1
- import { createRequire } from 'node:module';
1
+ import { readFile } from 'node:fs/promises';
2
2
  import { Language, Parser, Query } from 'web-tree-sitter';
3
- const require = createRequire(import.meta.url);
3
+ const wasmUrl = new URL('./tree-sitter-requirements.wasm', import.meta.url);
4
4
  async function init() {
5
- await Parser.init({
6
- locateFile() {
7
- return require.resolve('web-tree-sitter/web-tree-sitter.wasm');
8
- }
9
- });
10
- return await Language.load(require.resolve('tree-sitter-requirements/tree-sitter-requirements.wasm'));
5
+ await Parser.init();
6
+ const wasmBytes = new Uint8Array(await readFile(wasmUrl));
7
+ return await Language.load(wasmBytes);
11
8
  }
12
9
  export async function getParser() {
13
10
  const language = await init();
@@ -0,0 +1,47 @@
1
+ declare namespace _default {
2
+ export { isSupported };
3
+ export { validateLockFile };
4
+ export { provideComponent };
5
+ export { provideStack };
6
+ export { readLicenseFromManifest };
7
+ }
8
+ export default _default;
9
+ export type Provided = import("../provider").Provided;
10
+ /**
11
+ * @param {string} manifestName - the subject manifest name-type
12
+ * @returns {boolean} - return true if `Cargo.toml` is the manifest name-type
13
+ */
14
+ declare function isSupported(manifestName: string): boolean;
15
+ /**
16
+ * Validates that Cargo.lock exists in the manifest directory or in a parent
17
+ * workspace root directory. In Cargo workspaces the lock file always lives at
18
+ * the workspace root, so when a member crate's Cargo.toml is provided we walk
19
+ * up the directory tree looking for Cargo.lock (stopping when we find a
20
+ * Cargo.toml that contains a [workspace] section, or when we reach the
21
+ * filesystem root).
22
+ * @param {string} manifestDir - the directory where the manifest lies
23
+ * @returns {boolean} true if Cargo.lock is found
24
+ */
25
+ declare function validateLockFile(manifestDir: string): boolean;
26
+ /**
27
+ * Provide content and content type for Cargo component analysis.
28
+ * @param {string} manifest - path to Cargo.toml for component report
29
+ * @param {{}} [opts={}] - optional various options to pass along the application
30
+ * @returns {Provided}
31
+ */
32
+ declare function provideComponent(manifest: string, opts?: {}): Provided;
33
+ /**
34
+ * Provide content and content type for Cargo stack analysis.
35
+ * @param {string} manifest - the manifest path
36
+ * @param {{}} [opts={}] - optional various options to pass along the application
37
+ * @returns {Provided}
38
+ */
39
+ declare function provideStack(manifest: string, opts?: {}): Provided;
40
+ /**
41
+ * Read project license from Cargo.toml, with fallback to LICENSE file.
42
+ * Supports the `license` field under `[package]` (single crate / workspace
43
+ * with root) and under `[workspace.package]` (virtual workspaces).
44
+ * @param {string} manifestPath - path to Cargo.toml
45
+ * @returns {string|null} SPDX identifier or null
46
+ */
47
+ declare function readLicenseFromManifest(manifestPath: string): string | null;