@trustify-da/trustify-da-javascript-client 0.3.0-ea.8adb67b → 0.3.0-ea.9988076

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 (40) hide show
  1. package/README.md +65 -2
  2. package/dist/package.json +12 -4
  3. package/dist/src/analysis.js +21 -76
  4. package/dist/src/cli.js +72 -6
  5. package/dist/src/cyclone_dx_sbom.d.ts +3 -1
  6. package/dist/src/cyclone_dx_sbom.js +18 -5
  7. package/dist/src/index.d.ts +6 -3
  8. package/dist/src/index.js +6 -3
  9. package/dist/src/license/index.d.ts +28 -0
  10. package/dist/src/license/index.js +100 -0
  11. package/dist/src/license/license_utils.d.ts +40 -0
  12. package/dist/src/license/license_utils.js +134 -0
  13. package/dist/src/license/licenses_api.d.ts +34 -0
  14. package/dist/src/license/licenses_api.js +98 -0
  15. package/dist/src/license/project_license.d.ts +20 -0
  16. package/dist/src/license/project_license.js +62 -0
  17. package/dist/src/provider.d.ts +12 -3
  18. package/dist/src/provider.js +19 -2
  19. package/dist/src/providers/base_javascript.d.ts +10 -4
  20. package/dist/src/providers/base_javascript.js +30 -4
  21. package/dist/src/providers/golang_gomodules.d.ts +8 -1
  22. package/dist/src/providers/golang_gomodules.js +13 -4
  23. package/dist/src/providers/java_gradle.d.ts +6 -0
  24. package/dist/src/providers/java_gradle.js +12 -2
  25. package/dist/src/providers/java_maven.d.ts +8 -1
  26. package/dist/src/providers/java_maven.js +32 -4
  27. package/dist/src/providers/python_controller.d.ts +5 -2
  28. package/dist/src/providers/python_controller.js +56 -58
  29. package/dist/src/providers/python_pip.d.ts +11 -4
  30. package/dist/src/providers/python_pip.js +46 -53
  31. package/dist/src/providers/requirements_parser.d.ts +6 -0
  32. package/dist/src/providers/requirements_parser.js +24 -0
  33. package/dist/src/providers/rust_cargo.d.ts +47 -0
  34. package/dist/src/providers/rust_cargo.js +606 -0
  35. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  36. package/dist/src/sbom.d.ts +3 -1
  37. package/dist/src/sbom.js +3 -2
  38. package/dist/src/tools.d.ts +18 -0
  39. package/dist/src/tools.js +56 -1
  40. package/package.json +13 -5
@@ -1,10 +1,11 @@
1
1
  import fs from 'node:fs';
2
- import { EOL } from 'os';
3
2
  import { PackageURL } from 'packageurl-js';
3
+ import { readLicenseFile } from '../license/license_utils.js';
4
4
  import Sbom from '../sbom.js';
5
5
  import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand } from "../tools.js";
6
6
  import Python_controller from './python_controller.js';
7
- export default { isSupported, validateLockFile, provideComponent, provideStack };
7
+ import { getParser, getIgnoreQuery, getPinnedVersionQuery } from './requirements_parser.js';
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
  */
@@ -26,12 +34,12 @@ function validateLockFile() { return true; }
26
34
  * Provide content and content type for python-pip stack analysis.
27
35
  * @param {string} manifest - the manifest path or name
28
36
  * @param {{}} [opts={}] - optional various options to pass along the application
29
- * @returns {Provided}
37
+ * @returns {Promise<Provided>}
30
38
  */
31
- function provideStack(manifest, opts = {}) {
39
+ async function provideStack(manifest, opts = {}) {
32
40
  return {
33
41
  ecosystem,
34
- content: createSbomStackAnalysis(manifest, opts),
42
+ content: await createSbomStackAnalysis(manifest, opts),
35
43
  contentType: 'application/vnd.cyclonedx+json'
36
44
  };
37
45
  }
@@ -39,12 +47,12 @@ function provideStack(manifest, opts = {}) {
39
47
  * Provide content and content type for python-pip component analysis.
40
48
  * @param {string} manifest - path to requirements.txt for component report
41
49
  * @param {{}} [opts={}] - optional various options to pass along the application
42
- * @returns {Provided}
50
+ * @returns {Promise<Provided>}
43
51
  */
44
- function provideComponent(manifest, opts = {}) {
52
+ async function provideComponent(manifest, opts = {}) {
45
53
  return {
46
54
  ecosystem,
47
- content: getSbomForComponentAnalysis(manifest, opts),
55
+ content: await getSbomForComponentAnalysis(manifest, opts),
48
56
  contentType: 'application/vnd.cyclonedx+json'
49
57
  };
50
58
  }
@@ -66,49 +74,34 @@ function addAllDependencies(source, dep, sbom) {
66
74
  }
67
75
  /**
68
76
  *
69
- * @param nameVersion
70
- * @return {string}
71
- */
72
- function splitToNameVersion(nameVersion) {
73
- let result = [];
74
- if (nameVersion.includes("==")) {
75
- return nameVersion.split("==");
76
- }
77
- const regex = /[^\w\s-_]/g;
78
- let endIndex = nameVersion.search(regex);
79
- if (endIndex === -1) {
80
- return [nameVersion.trim()];
81
- }
82
- result.push(nameVersion.substring(0, endIndex).trim());
83
- return result;
84
- }
85
- /**
86
- *
87
- * @param {string} requirementTxtContent
77
+ * @param {string} manifest - path to requirements.txt
88
78
  * @return {PackageURL []}
89
79
  */
90
- function getIgnoredDependencies(requirementTxtContent) {
91
- let requirementsLines = requirementTxtContent.split(EOL);
92
- return requirementsLines
93
- .filter(line => line.includes("#exhortignore") || line.includes("# exhortignore"))
94
- .map((line) => line.substring(0, line.indexOf("#")).trim())
95
- .map((name) => {
96
- let nameVersion = splitToNameVersion(name);
97
- if (nameVersion.length === 2) {
98
- return toPurl(nameVersion[0], nameVersion[1]);
99
- }
100
- return toPurl(nameVersion[0], undefined);
80
+ async function getIgnoredDependencies(manifest) {
81
+ const [parser, ignoreQuery, pinnedVersionQuery] = await Promise.all([
82
+ getParser(), getIgnoreQuery(), getPinnedVersionQuery()
83
+ ]);
84
+ const content = fs.readFileSync(manifest).toString();
85
+ const tree = parser.parse(content);
86
+ return ignoreQuery.matches(tree.rootNode).map(match => {
87
+ const reqNode = match.captures.find(c => c.name === 'req').node;
88
+ const name = match.captures.find(c => c.name === 'name').node.text;
89
+ const versionMatches = pinnedVersionQuery.matches(reqNode);
90
+ const version = versionMatches.length > 0
91
+ ? versionMatches[0].captures.find(c => c.name === 'version').node.text
92
+ : undefined;
93
+ return toPurl(name, version);
101
94
  });
102
95
  }
103
96
  /**
104
97
  *
105
- * @param {string} requirementTxtContent content of requirments.txt in string
98
+ * @param {string} manifest - path to requirements.txt
106
99
  * @param {Sbom} sbom object to filter out from it exhortignore dependencies.
107
100
  * @param {{Object}} opts - various options and settings for the application
108
101
  * @private
109
102
  */
110
- function handleIgnoredDependencies(requirementTxtContent, sbom, opts = {}) {
111
- let ignoredDeps = getIgnoredDependencies(requirementTxtContent);
103
+ async function handleIgnoredDependencies(manifest, sbom, opts = {}) {
104
+ let ignoredDeps = await getIgnoredDependencies(manifest);
112
105
  let matchManifestVersions = getCustom("MATCH_MANIFEST_VERSIONS", "true", opts);
113
106
  if (matchManifestVersions === "true") {
114
107
  const ignoredDepsVersion = ignoredDeps.filter(dep => dep.version !== undefined);
@@ -172,22 +165,22 @@ const DEFAULT_PIP_ROOT_COMPONENT_VERSION = "0.0.0";
172
165
  * Create sbom json string out of a manifest path for stack analysis.
173
166
  * @param {string} manifest - path for requirements.txt
174
167
  * @param {{}} [opts={}] - optional various options to pass along the application
175
- * @returns {string} the sbom json string content
168
+ * @returns {Promise<string>} the sbom json string content
176
169
  * @private
177
170
  */
178
- function createSbomStackAnalysis(manifest, opts = {}) {
171
+ async function createSbomStackAnalysis(manifest, opts = {}) {
179
172
  let binaries = {};
180
173
  let createVirtualPythonEnv = handlePythonEnvironment(binaries, opts);
181
174
  let pythonController = new Python_controller(createVirtualPythonEnv === "false", binaries.pip, binaries.python, manifest, opts);
182
- let dependencies = pythonController.getDependencies(true);
175
+ let dependencies = await pythonController.getDependencies(true);
183
176
  let sbom = new Sbom();
184
177
  const rootPurl = toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION);
185
- sbom.addRoot(rootPurl);
178
+ const license = readLicenseFromManifest(manifest);
179
+ sbom.addRoot(rootPurl, license);
186
180
  dependencies.forEach(dep => {
187
181
  addAllDependencies(rootPurl, dep, sbom);
188
182
  });
189
- let requirementTxtContent = fs.readFileSync(manifest).toString();
190
- handleIgnoredDependencies(requirementTxtContent, sbom, opts);
183
+ await handleIgnoredDependencies(manifest, sbom, opts);
191
184
  // 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
192
185
  // sbom.removeRootComponent()
193
186
  return sbom.getAsJsonString(opts);
@@ -196,22 +189,22 @@ function createSbomStackAnalysis(manifest, opts = {}) {
196
189
  * Create a sbom json string out of a manifest content for component analysis
197
190
  * @param {string} manifest - path to requirements.txt
198
191
  * @param {{}} [opts={}] - optional various options to pass along the application
199
- * @returns {string} the sbom json string content
192
+ * @returns {Promise<string>} the sbom json string content
200
193
  * @private
201
194
  */
202
- function getSbomForComponentAnalysis(manifest, opts = {}) {
195
+ async function getSbomForComponentAnalysis(manifest, opts = {}) {
203
196
  let binaries = {};
204
197
  let createVirtualPythonEnv = handlePythonEnvironment(binaries, opts);
205
198
  let pythonController = new Python_controller(createVirtualPythonEnv === "false", binaries.pip, binaries.python, manifest, opts);
206
- let dependencies = pythonController.getDependencies(false);
199
+ let dependencies = await pythonController.getDependencies(false);
207
200
  let sbom = new Sbom();
208
201
  const rootPurl = toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION);
209
- sbom.addRoot(rootPurl);
202
+ const license = readLicenseFromManifest(manifest);
203
+ sbom.addRoot(rootPurl, license);
210
204
  dependencies.forEach(dep => {
211
205
  sbom.addDependency(rootPurl, toPurl(dep.name, dep.version));
212
206
  });
213
- let requirementTxtContent = fs.readFileSync(manifest).toString();
214
- handleIgnoredDependencies(requirementTxtContent, sbom, opts);
207
+ await handleIgnoredDependencies(manifest, sbom, opts);
215
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
216
209
  // sbom.removeRootComponent()
217
210
  return sbom.getAsJsonString(opts);
@@ -0,0 +1,6 @@
1
+ export function getParser(): Promise<Parser>;
2
+ export function getRequirementQuery(): Promise<Query>;
3
+ export function getIgnoreQuery(): Promise<Query>;
4
+ export function getPinnedVersionQuery(): Promise<Query>;
5
+ import { Parser } from 'web-tree-sitter';
6
+ import { Query } from 'web-tree-sitter';
@@ -0,0 +1,24 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { Language, Parser, Query } from 'web-tree-sitter';
3
+ const wasmUrl = new URL('./tree-sitter-requirements.wasm', import.meta.url);
4
+ async function init() {
5
+ await Parser.init();
6
+ const wasmBytes = new Uint8Array(await readFile(wasmUrl));
7
+ return await Language.load(wasmBytes);
8
+ }
9
+ export async function getParser() {
10
+ const language = await init();
11
+ return new Parser().setLanguage(language);
12
+ }
13
+ export async function getRequirementQuery() {
14
+ const language = await init();
15
+ return new Query(language, '(requirement (package) @name) @req');
16
+ }
17
+ export async function getIgnoreQuery() {
18
+ const language = await init();
19
+ return new Query(language, '((requirement (package) @name) @req . (comment) @comment (#match? @comment "^#[\\t ]*exhortignore"))');
20
+ }
21
+ export async function getPinnedVersionQuery() {
22
+ const language = await init();
23
+ return new Query(language, '(version_spec (version_cmp) @cmp (version) @version (#eq? @cmp "=="))');
24
+ }
@@ -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;