@trustify-da/trustify-da-javascript-client 0.3.0-ea.e12bc82 → 0.3.0-ea.e258fba

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 (97) hide show
  1. package/README.md +367 -13
  2. package/dist/package.json +48 -28
  3. package/dist/src/analysis.d.ts +28 -5
  4. package/dist/src/analysis.js +99 -89
  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 +346 -9
  8. package/dist/src/cyclone_dx_sbom.d.ts +30 -3
  9. package/dist/src/cyclone_dx_sbom.js +95 -9
  10. package/dist/src/index.d.ts +202 -12
  11. package/dist/src/index.js +365 -30
  12. package/dist/src/license/index.d.ts +28 -0
  13. package/dist/src/license/index.js +103 -0
  14. package/dist/src/license/license_utils.d.ts +40 -0
  15. package/dist/src/license/license_utils.js +137 -0
  16. package/dist/src/license/licenses_api.d.ts +34 -0
  17. package/dist/src/license/licenses_api.js +98 -0
  18. package/dist/src/license/project_license.d.ts +20 -0
  19. package/dist/src/license/project_license.js +62 -0
  20. package/dist/src/oci_image/images.d.ts +4 -5
  21. package/dist/src/oci_image/images.js +15 -1
  22. package/dist/src/oci_image/utils.d.ts +7 -7
  23. package/dist/src/oci_image/utils.js +11 -2
  24. package/dist/src/package_version.d.ts +8 -0
  25. package/dist/src/package_version.js +31 -0
  26. package/dist/src/provider.d.ts +18 -5
  27. package/dist/src/provider.js +33 -5
  28. package/dist/src/providers/base_java.d.ts +68 -15
  29. package/dist/src/providers/base_java.js +82 -61
  30. package/dist/src/providers/base_javascript.d.ts +99 -12
  31. package/dist/src/providers/base_javascript.js +243 -27
  32. package/dist/src/providers/base_pyproject.d.ts +158 -0
  33. package/dist/src/providers/base_pyproject.js +322 -0
  34. package/dist/src/providers/containerfile_parser.d.ts +5 -0
  35. package/dist/src/providers/containerfile_parser.js +20 -0
  36. package/dist/src/providers/golang_gomodules.d.ts +30 -13
  37. package/dist/src/providers/golang_gomodules.js +219 -122
  38. package/dist/src/providers/gomod_parser.d.ts +4 -0
  39. package/dist/src/providers/gomod_parser.js +16 -0
  40. package/dist/src/providers/java_gradle.d.ts +76 -3
  41. package/dist/src/providers/java_gradle.js +323 -21
  42. package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
  43. package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
  44. package/dist/src/providers/java_maven.d.ts +45 -10
  45. package/dist/src/providers/java_maven.js +300 -14
  46. package/dist/src/providers/javascript_bun.d.ts +22 -0
  47. package/dist/src/providers/javascript_bun.js +141 -0
  48. package/dist/src/providers/javascript_npm.d.ts +19 -0
  49. package/dist/src/providers/javascript_npm.js +61 -1
  50. package/dist/src/providers/javascript_pnpm.d.ts +14 -1
  51. package/dist/src/providers/javascript_pnpm.js +55 -5
  52. package/dist/src/providers/javascript_yarn.d.ts +12 -0
  53. package/dist/src/providers/javascript_yarn.js +75 -2
  54. package/dist/src/providers/manifest.d.ts +2 -0
  55. package/dist/src/providers/manifest.js +28 -7
  56. package/dist/src/providers/marker_evaluator.d.ts +14 -0
  57. package/dist/src/providers/marker_evaluator.js +191 -0
  58. package/dist/src/providers/oci_dockerfile.d.ts +51 -0
  59. package/dist/src/providers/oci_dockerfile.js +177 -0
  60. package/dist/src/providers/processors/yarn_berry_processor.d.ts +6 -2
  61. package/dist/src/providers/processors/yarn_berry_processor.js +91 -7
  62. package/dist/src/providers/processors/yarn_classic_processor.d.ts +6 -2
  63. package/dist/src/providers/processors/yarn_classic_processor.js +8 -6
  64. package/dist/src/providers/python_controller.d.ts +10 -3
  65. package/dist/src/providers/python_controller.js +122 -63
  66. package/dist/src/providers/python_pip.d.ts +16 -4
  67. package/dist/src/providers/python_pip.js +51 -58
  68. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  69. package/dist/src/providers/python_pip_pyproject.js +146 -0
  70. package/dist/src/providers/python_poetry.d.ts +98 -0
  71. package/dist/src/providers/python_poetry.js +286 -0
  72. package/dist/src/providers/python_uv.d.ts +46 -0
  73. package/dist/src/providers/python_uv.js +269 -0
  74. package/dist/src/providers/requirements_parser.d.ts +6 -0
  75. package/dist/src/providers/requirements_parser.js +24 -0
  76. package/dist/src/providers/rust_cargo.d.ts +56 -0
  77. package/dist/src/providers/rust_cargo.js +682 -0
  78. package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
  79. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  80. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  81. package/dist/src/remediate.d.ts +21 -0
  82. package/dist/src/remediate.js +149 -0
  83. package/dist/src/remediation.d.ts +57 -0
  84. package/dist/src/remediation.js +436 -0
  85. package/dist/src/remediation_report.d.ts +40 -0
  86. package/dist/src/remediation_report.js +159 -0
  87. package/dist/src/sbom.d.ts +28 -2
  88. package/dist/src/sbom.js +26 -4
  89. package/dist/src/tools.d.ts +48 -6
  90. package/dist/src/tools.js +138 -2
  91. package/dist/src/updaters/maven_updater.d.ts +26 -0
  92. package/dist/src/updaters/maven_updater.js +322 -0
  93. package/dist/src/updaters/toml_updater.d.ts +33 -0
  94. package/dist/src/updaters/toml_updater.js +244 -0
  95. package/dist/src/workspace.d.ts +70 -0
  96. package/dist/src/workspace.js +256 -0
  97. package/package.json +49 -29
@@ -1,10 +1,12 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
- import { EOL } from "os";
4
3
  import { PackageURL } from 'packageurl-js';
4
+ import { readLicenseFile } from '../license/license_utils.js';
5
5
  import Sbom from '../sbom.js';
6
6
  import { getCustom, getCustomPath, invokeCommand } from "../tools.js";
7
- export default { isSupported, validateLockFile, provideComponent, provideStack };
7
+ import { filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore } from '../workspace.js';
8
+ import { getParser, getRequireQuery } from './gomod_parser.js';
9
+ export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest, packageManagerName() { return 'go'; } };
8
10
  /** @typedef {import('../provider').Provider} */
9
11
  /** @typedef {import('../provider').Provided} Provided */
10
12
  /** @typedef {{name: string, version: string}} Package */
@@ -12,43 +14,50 @@ export default { isSupported, validateLockFile, provideComponent, provideStack }
12
14
  /**
13
15
  * @type {string} ecosystem for npm-npm is 'maven'
14
16
  * @private
15
- */
17
+ */
16
18
  const ecosystem = 'golang';
17
19
  const defaultMainModuleVersion = "v0.0.0";
18
20
  /**
19
- * @param {string} manifestName - the subject manifest name-type
20
- * @returns {boolean} - return true if `pom.xml` is the manifest name-type
21
- */
21
+ * @param {string} manifestName the subject manifest name-type
22
+ * @returns {boolean} return true if `pom.xml` is the manifest name-type
23
+ */
22
24
  function isSupported(manifestName) {
23
25
  return 'go.mod' === manifestName;
24
26
  }
25
27
  /**
26
- * @param {string} manifestDir - the directory where the manifest lies
28
+ * Go modules have no standard license field in go.mod
29
+ * @param {string} manifestPath path to go.mod
30
+ * @returns {string|null}
31
+ */
32
+ // eslint-disable-next-line no-unused-vars
33
+ function readLicenseFromManifest(manifestPath) { return readLicenseFile(manifestPath); }
34
+ /**
35
+ * @param {string} manifestDir the directory where the manifest lies
27
36
  */
28
37
  function validateLockFile() { return true; }
29
38
  /**
30
39
  * Provide content and content type for maven-maven stack analysis.
31
- * @param {string} manifest - the manifest path or name
32
- * @param {{}} [opts={}] - optional various options to pass along the application
33
- * @returns {Provided}
40
+ * @param {string} manifest the manifest path or name
41
+ * @param {{}} [opts={}] optional various options to pass along the application
42
+ * @returns {Promise<Provided>}
34
43
  */
35
- function provideStack(manifest, opts = {}) {
44
+ async function provideStack(manifest, opts = {}) {
36
45
  return {
37
46
  ecosystem,
38
- content: getSBOM(manifest, opts, true),
47
+ content: await getSBOM(manifest, opts, true),
39
48
  contentType: 'application/vnd.cyclonedx+json'
40
49
  };
41
50
  }
42
51
  /**
43
52
  * Provide content and content type for maven-maven component analysis.
44
- * @param {string} manifest - path to go.mod for component report
45
- * @param {{}} [opts={}] - optional various options to pass along the application
46
- * @returns {Provided}
53
+ * @param {string} manifest path to go.mod for component report
54
+ * @param {{}} [opts={}] optional various options to pass along the application
55
+ * @returns {Promise<Provided>}
47
56
  */
48
- function provideComponent(manifest, opts = {}) {
57
+ async function provideComponent(manifest, opts = {}) {
49
58
  return {
50
59
  ecosystem,
51
- content: getSBOM(manifest, opts, false),
60
+ content: await getSBOM(manifest, opts, false),
52
61
  contentType: 'application/vnd.cyclonedx+json'
53
62
  };
54
63
  }
@@ -69,50 +78,53 @@ function getChildVertexFromEdge(edge) {
69
78
  return edge.split(" ")[1];
70
79
  }
71
80
  /**
72
- *
73
- * @param line one row from go.mod file
74
- * @return {boolean} whether line from go.mod should be considered as ignored or not
81
+ * Check whether a require_spec has a valid ignore marker.
82
+ * Supports both the legacy `exhortignore` marker and the new `trustify-da-ignore` marker.
83
+ * For direct dependencies: `//exhortignore` or `// trustify-da-ignore`
84
+ * For indirect dependencies: `// indirect; exhortignore` or `// indirect; trustify-da-ignore` (semicolon-separated)
85
+ * @param {import('web-tree-sitter').SyntaxNode} specNode
86
+ * @return {boolean}
75
87
  */
76
- function ignoredLine(line) {
77
- let result = false;
78
- if (line.match(".*exhortignore.*")) {
79
- if (line.match(".+//\\s*exhortignore") || line.match(".+//\\sindirect (//)?\\s*exhortignore")) {
80
- let trimmedRow = line.trim();
81
- if (!trimmedRow.startsWith("module ") && !trimmedRow.startsWith("go ") && !trimmedRow.startsWith("require (") && !trimmedRow.startsWith("require(")
82
- && !trimmedRow.startsWith("exclude ") && !trimmedRow.startsWith("replace ") && !trimmedRow.startsWith("retract ") && !trimmedRow.startsWith("use ")
83
- && !trimmedRow.includes("=>")) {
84
- if (trimmedRow.startsWith("require ") || trimmedRow.match("^[a-z.0-9/-]+\\s{1,2}[vV][0-9]\\.[0-9](\\.[0-9]){0,2}.*")) {
85
- result = true;
86
- }
87
- }
88
+ function hasExhortIgnore(specNode) {
89
+ // Ideally this would be the following tree-sitter query instead, but for some
90
+ // reason it throws an error here but not in the playground.
91
+ // (require_spec) ((module_path) @path (version) (comment) @comment (#match? @comment "^//.*exhortignore"))
92
+ // QueryError: Bad pattern structure at offset 53: '(comment) @comment (#match? @comment "^//.*exhortignore")) @spec'...
93
+ let comments = specNode.children.filter(c => c.type === 'comment');
94
+ for (let comment of comments) {
95
+ let text = comment.text;
96
+ if (/^\/\/\s*indirect;\s*(exhortignore|trustify-da-ignore)(?![\w-])/.test(text)) {
97
+ return true;
98
+ }
99
+ if (/^\/\/\s*(exhortignore|trustify-da-ignore)(?![\w-])/.test(text)) {
100
+ return true;
88
101
  }
89
102
  }
90
- return result;
91
- }
92
- /**
93
- * extract package name from go.mod line that contains exhortignore comment.
94
- * @param line a row contains exhortignore as part of a comment
95
- * @return {string} the full package name + group/namespace + version
96
- * @private
97
- */
98
- function extractPackageName(line) {
99
- let trimmedRow = line.trim();
100
- let firstRemarkNotationOccurrence = trimmedRow.indexOf("//");
101
- return trimmedRow.substring(0, firstRemarkNotationOccurrence).trim();
103
+ return false;
102
104
  }
103
105
  /**
104
106
  *
105
- * @param {string } manifest - path to manifest
106
- * @return {[PackageURL]} list of ignored dependencies d
107
+ * @param {string} manifestContent go.mod file contents
108
+ * @param {import('web-tree-sitter').Parser} parser
109
+ * @param {import('web-tree-sitter').Query} requireQuery
110
+ * @return {PackageURL[]} list of ignored dependencies
107
111
  */
108
- function getIgnoredDeps(manifest) {
109
- let goMod = fs.readFileSync(manifest).toString().trim();
110
- let lines = goMod.split(getLineSeparatorGolang());
111
- return lines.filter(line => ignoredLine(line)).map(line => extractPackageName(line)).map(dep => toPurl(dep, /[ ]{1,3}/));
112
+ function getIgnoredDeps(manifestContent, parser, requireQuery) {
113
+ let tree = parser.parse(manifestContent);
114
+ return requireQuery.matches(tree.rootNode)
115
+ .filter(match => {
116
+ let specNode = match.captures.find(c => c.name === 'spec').node;
117
+ return hasExhortIgnore(specNode);
118
+ })
119
+ .map(match => {
120
+ let name = match.captures.find(c => c.name === 'name').node.text;
121
+ let version = match.captures.find(c => c.name === 'version').node.text;
122
+ return toPurl(`${name} ${version}`, /[ ]{1,3}/);
123
+ });
112
124
  }
113
125
  /**
114
126
  *
115
- * @param {[PackageURL]}allIgnoredDeps - list of purls of all dependencies that should be ignored
127
+ * @param {PackageURL[]} allIgnoredDeps list of purls of all dependencies that should be ignored
116
128
  * @param {PackageURL} purl object to be checked if needed to be ignored
117
129
  * @return {boolean}
118
130
  */
@@ -131,59 +143,29 @@ function enforceRemovingIgnoredDepsInCaseOfAutomaticVersionUpdate(ignoredDeps, s
131
143
  }
132
144
  /**
133
145
  *
134
- * @param {[string]} lines - array of lines of go.mod manifest
135
- * @param {string} goMod - content of go.mod manifest
136
- * @return {[string]} all dependencies from go.mod file as array
146
+ * @param {string} manifestContent go.mod file contents
147
+ * @param {import('web-tree-sitter').Parser} parser
148
+ * @param {import('web-tree-sitter').Query} requireQuery
149
+ * @return {string[]} all dependencies from go.mod file as "name version" strings
137
150
  */
138
- function collectAllDepsFromManifest(lines, goMod) {
139
- let result;
140
- // collect all deps that starts with require keyword
141
- result = lines.filter((line) => line.trim().startsWith("require") && !line.includes("(")).map((dep) => dep.substring("require".length).trim());
142
- // collect all deps that are inside `require` blocks
143
- let currentSegmentOfGoMod = goMod;
144
- let requirePositionObject = decideRequireBlockIndex(currentSegmentOfGoMod);
145
- while (requirePositionObject.index > -1) {
146
- let depsInsideRequirementsBlock = currentSegmentOfGoMod.substring(requirePositionObject.index + requirePositionObject.startingOffeset).trim();
147
- let endOfBlockIndex = depsInsideRequirementsBlock.indexOf(")");
148
- let currentIndex = 0;
149
- while (currentIndex < endOfBlockIndex) {
150
- let endOfLinePosition = depsInsideRequirementsBlock.indexOf(EOL, currentIndex);
151
- let dependency = depsInsideRequirementsBlock.substring(currentIndex, endOfLinePosition);
152
- result.push(dependency.trim());
153
- currentIndex = endOfLinePosition + 1;
154
- }
155
- currentSegmentOfGoMod = currentSegmentOfGoMod.substring(endOfBlockIndex + 1).trim();
156
- requirePositionObject = decideRequireBlockIndex(currentSegmentOfGoMod);
157
- }
158
- function decideRequireBlockIndex(goMod) {
159
- let object = {};
160
- let index = goMod.indexOf("require(");
161
- object.startingOffeset = "require(".length;
162
- if (index === -1) {
163
- index = goMod.indexOf("require (");
164
- object.startingOffeset = "require (".length;
165
- if (index === -1) {
166
- index = goMod.indexOf("require (");
167
- object.startingOffeset = "require (".length;
168
- }
169
- }
170
- object.index = index;
171
- return object;
172
- }
173
- return result;
151
+ function collectAllDepsFromManifest(manifestContent, parser, requireQuery) {
152
+ let tree = parser.parse(manifestContent);
153
+ return requireQuery.matches(tree.rootNode).map(match => {
154
+ let name = match.captures.find(c => c.name === 'name').node.text;
155
+ let version = match.captures.find(c => c.name === 'version').node.text;
156
+ return `${name} ${version}`;
157
+ });
174
158
  }
175
159
  /**
176
160
  *
177
161
  * @param {string} rootElementName the rootElementName element of go mod graph, to compare only direct deps from go mod graph against go.mod manifest
178
- * @param{[string]} goModGraphOutputRows the goModGraphOutputRows from go mod graph' output
179
- * @param {string }manifest path to go.mod manifest on file system
162
+ * @param {string[]} goModGraphOutputRows the goModGraphOutputRows from go mod graph' output
163
+ * @param {string} manifestContent go.mod file contents
180
164
  * @private
181
165
  */
182
- function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, manifest) {
183
- let goMod = fs.readFileSync(manifest).toString().trim();
184
- let lines = goMod.split(getLineSeparatorGolang());
166
+ function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, manifestContent, parser, requireQuery) {
185
167
  let comparisonLines = goModGraphOutputRows.filter((line) => line.startsWith(rootElementName)).map((line) => getChildVertexFromEdge(line));
186
- let manifestDeps = collectAllDepsFromManifest(lines, goMod);
168
+ let manifestDeps = collectAllDepsFromManifest(manifestContent, parser, requireQuery);
187
169
  try {
188
170
  comparisonLines.forEach((dependency) => {
189
171
  let parts = dependency.split("@");
@@ -195,7 +177,7 @@ function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, man
195
177
  let currentVersion = components[1];
196
178
  if (currentDepName === depName) {
197
179
  if (currentVersion !== version) {
198
- throw new Error(`versions mismatch for dependency name ${depName}, manifest version=${currentVersion}, installed Version=${version}, if you want to allow version mismatch for analysis between installed and requested packages, set environment variable/setting - MATCH_MANIFEST_VERSIONS=false`);
180
+ throw new Error(`version mismatch for dependency "${depName}", manifest version=${currentVersion}, installed version=${version}, if you want to allow version mismatch for analysis between installed and requested packages, set environment variable/setting MATCH_MANIFEST_VERSIONS=false`);
199
181
  }
200
182
  }
201
183
  });
@@ -206,15 +188,45 @@ function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, man
206
188
  throw error;
207
189
  }
208
190
  }
191
+ /** Parses go.sum to build a hash map of module@version to CycloneDX hashes. */
192
+ function parseGoSumHashes(manifestDir) {
193
+ let goSumPath = path.join(manifestDir, 'go.sum');
194
+ if (!fs.existsSync(goSumPath)) {
195
+ return new Map();
196
+ }
197
+ let hashMap = new Map();
198
+ let content = fs.readFileSync(goSumPath, 'utf-8');
199
+ for (let line of content.split('\n')) {
200
+ line = line.trim();
201
+ if (!line) {
202
+ continue;
203
+ }
204
+ let parts = line.split(/\s+/);
205
+ if (parts.length < 3) {
206
+ continue;
207
+ }
208
+ let [mod, version, hash] = parts;
209
+ if (version.endsWith('/go.mod')) {
210
+ continue;
211
+ }
212
+ if (!hash.startsWith('h1:')) {
213
+ continue;
214
+ }
215
+ let base64Part = hash.slice(3);
216
+ let hexDigest = Buffer.from(base64Part, 'base64').toString('hex');
217
+ hashMap.set(`${mod}@${version}`, [{ alg: "SHA-256", content: hexDigest }]);
218
+ }
219
+ return hashMap;
220
+ }
209
221
  /**
210
222
  * Create SBOM json string for go Module.
211
223
  * @param {string} manifest - path for go.mod
212
224
  * @param {{}} [opts={}] - optional various options to pass along the application
213
225
  * @param {boolean} includeTransitive - whether the sbom should contain transitive dependencies of the main module or not.
214
- * @returns {string} the SBOM json content
226
+ * @returns {Promise<string>} the SBOM json content
215
227
  * @private
216
228
  */
217
- function getSBOM(manifest, opts = {}, includeTransitive) {
229
+ async function getSBOM(manifest, opts = {}, includeTransitive) {
218
230
  // get custom goBin path
219
231
  let goBin = getCustomPath('go', opts);
220
232
  // verify goBin is accessible
@@ -240,17 +252,30 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
240
252
  catch (error) {
241
253
  throw new Error('failed to determine root module name', { cause: error });
242
254
  }
243
- let ignoredDeps = getIgnoredDeps(manifest);
255
+ let manifestContent = fs.readFileSync(manifest).toString();
256
+ let [parser, requireQuery] = await Promise.all([getParser(), getRequireQuery()]);
257
+ let ignoredDeps = getIgnoredDeps(manifestContent, parser, requireQuery);
244
258
  let allIgnoredDeps = ignoredDeps.map((dep) => dep.toString());
245
259
  let sbom = new Sbom();
246
260
  let rows = goGraphOutput.split(getLineSeparatorGolang()).filter(line => !line.includes(' go@'));
247
- let root = getParentVertexFromEdge(goModEditOutput['Module']['Path']);
261
+ let root = goModEditOutput['Module']['Path'];
262
+ // Build set of direct dependency paths from go mod edit -json
263
+ let directDepPaths = new Set();
264
+ if (goModEditOutput['Require']) {
265
+ goModEditOutput['Require'].forEach(req => {
266
+ if (!req['Indirect']) {
267
+ directDepPaths.add(req['Path']);
268
+ }
269
+ });
270
+ }
248
271
  let matchManifestVersions = getCustom("MATCH_MANIFEST_VERSIONS", "false", opts);
249
272
  if (matchManifestVersions === "true") {
250
- performManifestVersionsCheck(root, rows, manifest);
273
+ performManifestVersionsCheck(root, rows, manifestContent, parser, requireQuery);
251
274
  }
275
+ let goSumHashes = parseGoSumHashes(manifestDir);
252
276
  const mainModule = toPurl(root, "@");
253
- sbom.addRoot(mainModule);
277
+ const license = readLicenseFromManifest(manifest);
278
+ sbom.addRoot(mainModule, license);
254
279
  const exhortGoMvsLogicEnabled = getCustom("TRUSTIFY_DA_GO_MVS_LOGIC_ENABLED", "true", opts);
255
280
  if (includeTransitive && exhortGoMvsLogicEnabled === "true") {
256
281
  rows = getFinalPackagesVersionsForModule(rows, manifest, goBin);
@@ -264,8 +289,12 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
264
289
  currentParent = getParentVertexFromEdge(row);
265
290
  source = toPurl(currentParent, "@");
266
291
  }
267
- let target = toPurl(getChildVertexFromEdge(row), "@");
268
- sbom.addDependency(source, target);
292
+ let child = getChildVertexFromEdge(row);
293
+ let target = toPurl(child, "@");
294
+ if (getParentVertexFromEdge(row) === root && !directDepPaths.has(getPackageName(child))) {
295
+ return;
296
+ }
297
+ sbom.addDependency(source, target, undefined, goSumHashes.get(child));
269
298
  });
270
299
  // at the end, filter out all ignored dependencies including versions.
271
300
  sbom.filterIgnoredDepsIncludingVersion(allIgnoredDeps);
@@ -274,20 +303,32 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
274
303
  else {
275
304
  let directDependencies = rows.filter(row => row.startsWith(root));
276
305
  directDependencies.forEach(pair => {
277
- let dependency = getChildVertexFromEdge(pair);
278
- let depPurl = toPurl(dependency, "@");
279
- if (dependencyNotIgnored(ignoredDeps, depPurl)) {
280
- sbom.addDependency(mainModule, depPurl);
306
+ let child = getChildVertexFromEdge(pair);
307
+ let target = toPurl(child, "@");
308
+ if (dependencyNotIgnored(ignoredDeps, target)) {
309
+ if (directDepPaths.has(getPackageName(child))) {
310
+ sbom.addDependency(mainModule, target, undefined, goSumHashes.get(child));
311
+ }
281
312
  }
282
313
  });
283
314
  enforceRemovingIgnoredDepsInCaseOfAutomaticVersionUpdate(ignoredDeps, sbom);
284
315
  }
316
+ // Backfill hashes for components that were only seen as dependency sources
317
+ for (const comp of sbom.sbomModel.components) {
318
+ if (!comp.hashes && comp.purl) {
319
+ let key = decodeURIComponent(comp.purl.replace('pkg:golang/', ''));
320
+ let hashes = goSumHashes.get(key);
321
+ if (hashes) {
322
+ comp.hashes = hashes;
323
+ }
324
+ }
325
+ }
285
326
  return sbom.getAsJsonString(opts);
286
327
  }
287
328
  /**
288
329
  * Utility function for creating Purl String
289
330
 
290
- * @param {string }dependency the name of the artifact, can include a namespace(group) or not - namespace/artifactName.
331
+ * @param {string} dependency the name of the artifact, can include a namespace(group) or not - namespace/artifactName.
291
332
  * @param {RegExp} delimiter delimiter between name of dependency and version
292
333
  * @private
293
334
  * @returns {PackageURL|null} PackageUrl Object ready to be used in SBOM
@@ -312,12 +353,12 @@ function toPurl(dependency, delimiter) {
312
353
  }
313
354
  return pkg;
314
355
  }
315
- /** This function gets rows from go mod graph , and go.mod graph, and selecting for all
356
+ /** This function gets rows from go mod graph, and go.mod graph, and selecting for all
316
357
  * packages the has more than one minor the final versions as selected by golang MVS algorithm.
317
- * @param {[string]}rows all the rows from go modules dependency tree
358
+ * @param {string[]} rows all the rows from go modules dependency tree
318
359
  * @param {string} manifestPath the path of the go.mod file
319
360
  * @param {string} path to go binary
320
- * @return {[string]} rows that contains final versions.
361
+ * @return {string[]} rows that contains final versions.
321
362
  */
322
363
  function getFinalPackagesVersionsForModule(rows, manifestPath, goBin) {
323
364
  let manifestDir = path.dirname(manifestPath);
@@ -330,13 +371,7 @@ function getFinalPackagesVersionsForModule(rows, manifestPath, goBin) {
330
371
  catch (error) {
331
372
  throw new Error('failed to list all modules', { cause: error });
332
373
  }
333
- let finalVersionModules = new Map();
334
- finalVersionsForAllModules.split(getLineSeparatorGolang()).filter(string => string.trim() !== "")
335
- .filter(string => string.trim().split(" ").length === 2)
336
- .forEach((dependency) => {
337
- let dep = dependency.split(" ");
338
- finalVersionModules[dep[0]] = dep[1];
339
- });
374
+ let finalVersionModules = parseModuleVersions(finalVersionsForAllModules);
340
375
  let finalVersionModulesArray = new Array();
341
376
  rows.filter(string => string.trim() !== "").forEach(module => {
342
377
  let child = getChildVertexFromEdge(module);
@@ -372,7 +407,7 @@ function getFinalPackagesVersionsForModule(rows, manifestPath, goBin) {
372
407
  /**
373
408
  *
374
409
  * @param {string} fullPackage - full package with its name and version
375
- * @return -{string} package name only
410
+ * @return {string} package name only
376
411
  * @private
377
412
  */
378
413
  function getPackageName(fullPackage) {
@@ -390,14 +425,76 @@ function isSpecialGoModule(moduleName) {
390
425
  /**
391
426
  *
392
427
  * @param {string} fullPackage - full package with its name and version
393
- * @return -{string} package version only
428
+ * @return {string|undefined} package version only
394
429
  * @private
395
430
  */
396
431
  function getVersionOfPackage(fullPackage) {
397
432
  let parts = fullPackage.split("@");
398
433
  return parts.length > 1 ? parts[1] : undefined;
399
434
  }
435
+ function parseModuleVersions(goListOutput) {
436
+ let modules = {};
437
+ goListOutput.split(getLineSeparatorGolang()).filter(string => string.trim() !== "")
438
+ .forEach((line) => {
439
+ let parts = line.trim().split(" ");
440
+ if (parts.length === 2) {
441
+ modules[parts[0]] = parts[1];
442
+ }
443
+ else if (parts.length >= 4 && parts[2] === "=>") {
444
+ modules[parts[0]] = parts[parts.length - 1];
445
+ }
446
+ });
447
+ return modules;
448
+ }
400
449
  function getLineSeparatorGolang() {
401
450
  let reg = /\n|\r\n/;
402
451
  return reg;
403
452
  }
453
+ /**
454
+ * Discover all go.mod manifest paths in a Go workspace.
455
+ * Uses `go work edit -json` to get workspace members.
456
+ *
457
+ * @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain go.work)
458
+ * @param {import('../index.js').Options} [opts={}]
459
+ * @returns {Promise<string[]>} Paths to go.mod files (absolute)
460
+ */
461
+ export async function discoverGoWorkspaceModules(workspaceRoot, opts = {}) {
462
+ const root = path.resolve(workspaceRoot);
463
+ const goWork = path.join(root, 'go.work');
464
+ if (!fs.existsSync(goWork)) {
465
+ return [];
466
+ }
467
+ const goBin = getCustomPath('go', opts);
468
+ let output;
469
+ try {
470
+ output = invokeCommand(goBin, ['work', 'edit', '-json', goWork], { cwd: root });
471
+ }
472
+ catch {
473
+ return [];
474
+ }
475
+ let workspace;
476
+ try {
477
+ workspace = JSON.parse(output.toString().trim());
478
+ }
479
+ catch {
480
+ return [];
481
+ }
482
+ const useEntries = workspace.Use || [];
483
+ if (useEntries.length === 0) {
484
+ return [];
485
+ }
486
+ const manifestPaths = [];
487
+ for (const entry of useEntries) {
488
+ const diskPath = entry.DiskPath;
489
+ if (!diskPath) {
490
+ continue;
491
+ }
492
+ const moduleDir = path.resolve(root, diskPath);
493
+ const goMod = path.join(moduleDir, 'go.mod');
494
+ if (fs.existsSync(goMod)) {
495
+ manifestPaths.push(goMod);
496
+ }
497
+ }
498
+ const ignorePatterns = resolveWorkspaceDiscoveryIgnore(opts);
499
+ return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
500
+ }
@@ -0,0 +1,4 @@
1
+ export function getParser(): Promise<Parser>;
2
+ export function getRequireQuery(): Promise<Query>;
3
+ import { Parser } from 'web-tree-sitter';
4
+ import { Query } from 'web-tree-sitter';
@@ -0,0 +1,16 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { Language, Parser, Query } from 'web-tree-sitter';
3
+ const wasmUrl = new URL('./tree-sitter-gomod.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 getRequireQuery() {
14
+ const language = await init();
15
+ return new Query(language, '(require_spec (module_path) @name (version) @version) @spec');
16
+ }
@@ -1,3 +1,46 @@
1
+ /**
2
+ * Discover all build.gradle[.kts] manifest paths in a Gradle multi-project build.
3
+ * Uses a custom init script to get structured project listing.
4
+ *
5
+ * @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain settings.gradle[.kts])
6
+ * @param {import('../index.js').Options} [opts={}]
7
+ * @returns {Promise<string[]>} Paths to build.gradle[.kts] files (absolute)
8
+ */
9
+ export function discoverGradleSubprojects(workspaceRoot: string, opts?: import("../index.js").Options): Promise<string[]>;
10
+ /**
11
+ * Parse the structured output from the Gradle init script.
12
+ *
13
+ * @param {string} raw - Raw stdout from gradle
14
+ * @returns {{ path: string, dir: string }[]}
15
+ */
16
+ export function parseGradleInitScriptOutput(raw: string): {
17
+ path: string;
18
+ dir: string;
19
+ }[];
20
+ /**
21
+ * Parse the structured output from the Gradle hash init script.
22
+ * Each recognised line has the form `::DA_HASH::group:name:version::<file-path>`.
23
+ *
24
+ * @param {string} raw - Raw stdout from gradle
25
+ * @returns {{ id: string, file: string }[]} component id (`group:name:version`) and absolute artifact file path
26
+ */
27
+ export function parseGradleHashScriptOutput(raw: string): {
28
+ id: string;
29
+ file: string;
30
+ }[];
31
+ /**
32
+ * Parse a Gradle module component id (`group:name:version`) into its coordinate
33
+ * parts. The caller builds the canonical PURL key from these parts using the same
34
+ * builder {@link parseDep} uses, so the stored key and the lookup key cannot drift.
35
+ *
36
+ * @param {string} id - the Gradle component id
37
+ * @returns {{group: string, name: string, version: string}|null} the parsed coordinate, or null when the id is malformed
38
+ */
39
+ export function parseComponentId(id: string): {
40
+ group: string;
41
+ name: string;
42
+ version: string;
43
+ } | null;
1
44
  /**
2
45
  * This class provides common functionality for Groovy and Kotlin DSL files.
3
46
  */
@@ -15,21 +58,51 @@ export default class Java_gradle extends Base_java {
15
58
  * @param {string} manifestDir - the directory where the manifest lies
16
59
  */
17
60
  validateLockFile(): boolean;
61
+ /**
62
+ * Gradle manifests (build.gradle, build.gradle.kts) have no standard license field.
63
+ * @param {string} manifestPath - path to manifest
64
+ * @returns {null}
65
+ */
66
+ readLicenseFromManifest(manifestPath: string): null;
18
67
  /**
19
68
  * Provide content and content type for stack analysis.
20
69
  * @param {string} manifest - the manifest path or name
21
70
  * @param {{}} [opts={}] - optional various options to pass along the application
22
71
  * @returns {Provided}
23
72
  */
24
- provideStack(manifest: string, opts?: {} | undefined): Provided;
73
+ provideStack(manifest: string, opts?: {}): Provided;
25
74
  /**
26
75
  * Provide content and content type for maven-maven component analysis.
27
76
  * @param {string} manifest - path to pom.xml for component report
28
77
  * @param {{}} [opts={}] - optional various options to pass along the application
29
78
  * @returns {Provided}
30
79
  */
31
- provideComponent(manifest: string, opts?: {} | undefined): Provided;
80
+ provideComponent(manifest: string, opts?: {}): Provided;
81
+ /**
82
+ * Compute SHA-256 hashes for the resolved artifacts of a Gradle manifest.
83
+ *
84
+ * Rather than scanning the local Gradle cache, this asks Gradle itself for
85
+ * the resolved artifact files via an init script (mirroring the pattern used
86
+ * by {@link discoverGradleSubprojects}), then hashes each file with the
87
+ * Node.js `crypto` module. The result is keyed by the canonical PURL string
88
+ * built via {@link Base_java#toPurl} — the same builder {@link parseDep} uses
89
+ * for the lookup — so the stored key and the lookup key cannot drift.
90
+ *
91
+ * Degrades gracefully: if Gradle cannot be invoked, the init script fails, or
92
+ * an artifact has no readable file (e.g. BOM/`platform()` dependencies), the
93
+ * hash for that component is omitted rather than throwing. A warning is emitted
94
+ * on every degradation path so incomplete hash coverage is visible even without
95
+ * `TRUSTIFY_DA_DEBUG` (mirroring the pip/cargo providers).
96
+ *
97
+ * @param {string} manifest - path to build.gradle[.kts]
98
+ * @param {{}} [opts={}] - optional various options to pass along the application
99
+ * @returns {Promise<Map<string, Array<{alg: string, content: string}>>>} map of canonical PURL string to CycloneDX hashes
100
+ */
101
+ parseGradleHashes(manifest: string, opts?: {}): Promise<Map<string, Array<{
102
+ alg: string;
103
+ content: string;
104
+ }>>>;
32
105
  #private;
33
106
  }
34
- export type Provided = import('../provider.js').Provided;
107
+ export type Provided = import("../provider.js").Provided;
35
108
  import Base_java from "./base_java.js";