@trustify-da/trustify-da-javascript-client 0.3.0-ea.b8af0f8 → 0.3.0-ea.c2f6c64

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.
@@ -1,10 +1,10 @@
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';
5
4
  import { readLicenseFile } from '../license/license_utils.js';
6
5
  import Sbom from '../sbom.js';
7
6
  import { getCustom, getCustomPath, invokeCommand } from "../tools.js";
7
+ import { getParser, getRequireQuery } from './gomod_parser.js';
8
8
  export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
9
9
  /** @typedef {import('../provider').Provider} */
10
10
  /** @typedef {import('../provider').Provided} Provided */
@@ -17,46 +17,46 @@ export default { isSupported, validateLockFile, provideComponent, provideStack,
17
17
  const ecosystem = 'golang';
18
18
  const defaultMainModuleVersion = "v0.0.0";
19
19
  /**
20
- * @param {string} manifestName - the subject manifest name-type
21
- * @returns {boolean} - return true if `pom.xml` is the manifest name-type
20
+ * @param {string} manifestName the subject manifest name-type
21
+ * @returns {boolean} return true if `pom.xml` is the manifest name-type
22
22
  */
23
23
  function isSupported(manifestName) {
24
24
  return 'go.mod' === manifestName;
25
25
  }
26
26
  /**
27
27
  * Go modules have no standard license field in go.mod
28
- * @param {string} manifestPath - path to go.mod
28
+ * @param {string} manifestPath path to go.mod
29
29
  * @returns {string|null}
30
30
  */
31
31
  // eslint-disable-next-line no-unused-vars
32
32
  function readLicenseFromManifest(manifestPath) { return readLicenseFile(manifestPath); }
33
33
  /**
34
- * @param {string} manifestDir - the directory where the manifest lies
34
+ * @param {string} manifestDir the directory where the manifest lies
35
35
  */
36
36
  function validateLockFile() { return true; }
37
37
  /**
38
38
  * Provide content and content type for maven-maven stack analysis.
39
- * @param {string} manifest - the manifest path or name
40
- * @param {{}} [opts={}] - optional various options to pass along the application
41
- * @returns {Provided}
39
+ * @param {string} manifest the manifest path or name
40
+ * @param {{}} [opts={}] optional various options to pass along the application
41
+ * @returns {Promise<Provided>}
42
42
  */
43
- function provideStack(manifest, opts = {}) {
43
+ async function provideStack(manifest, opts = {}) {
44
44
  return {
45
45
  ecosystem,
46
- content: getSBOM(manifest, opts, true),
46
+ content: await getSBOM(manifest, opts, true),
47
47
  contentType: 'application/vnd.cyclonedx+json'
48
48
  };
49
49
  }
50
50
  /**
51
51
  * Provide content and content type for maven-maven component analysis.
52
- * @param {string} manifest - path to go.mod for component report
53
- * @param {{}} [opts={}] - optional various options to pass along the application
54
- * @returns {Provided}
52
+ * @param {string} manifest path to go.mod for component report
53
+ * @param {{}} [opts={}] optional various options to pass along the application
54
+ * @returns {Promise<Provided>}
55
55
  */
56
- function provideComponent(manifest, opts = {}) {
56
+ async function provideComponent(manifest, opts = {}) {
57
57
  return {
58
58
  ecosystem,
59
- content: getSBOM(manifest, opts, false),
59
+ content: await getSBOM(manifest, opts, false),
60
60
  contentType: 'application/vnd.cyclonedx+json'
61
61
  };
62
62
  }
@@ -77,50 +77,52 @@ function getChildVertexFromEdge(edge) {
77
77
  return edge.split(" ")[1];
78
78
  }
79
79
  /**
80
- *
81
- * @param line one row from go.mod file
82
- * @return {boolean} whether line from go.mod should be considered as ignored or not
80
+ * Check whether a require_spec has a valid exhortignore marker.
81
+ * For direct dependencies: `//exhortignore` or `// exhortignore`
82
+ * For indirect dependencies: `// indirect; exhortignore` (semicolon-separated)
83
+ * @param {import('web-tree-sitter').SyntaxNode} specNode
84
+ * @return {boolean}
83
85
  */
84
- function ignoredLine(line) {
85
- let result = false;
86
- if (line.match(".*exhortignore.*")) {
87
- if (line.match(".+//\\s*exhortignore") || line.match(".+//\\sindirect (//)?\\s*exhortignore")) {
88
- let trimmedRow = line.trim();
89
- if (!trimmedRow.startsWith("module ") && !trimmedRow.startsWith("go ") && !trimmedRow.startsWith("require (") && !trimmedRow.startsWith("require(")
90
- && !trimmedRow.startsWith("exclude ") && !trimmedRow.startsWith("replace ") && !trimmedRow.startsWith("retract ") && !trimmedRow.startsWith("use ")
91
- && !trimmedRow.includes("=>")) {
92
- if (trimmedRow.startsWith("require ") || trimmedRow.match("^[a-z.0-9/-]+\\s{1,2}[vV][0-9]\\.[0-9](\\.[0-9]){0,2}.*")) {
93
- result = true;
94
- }
95
- }
86
+ function hasExhortIgnore(specNode) {
87
+ // Ideally this would be the following tree-sitter query instead, but for some
88
+ // reason it throws an error here but not in the playground.
89
+ // (require_spec) ((module_path) @path (version) (comment) @comment (#match? @comment "^//.*exhortignore"))
90
+ // QueryError: Bad pattern structure at offset 53: '(comment) @comment (#match? @comment "^//.*exhortignore")) @spec'...
91
+ let comments = specNode.children.filter(c => c.type === 'comment');
92
+ for (let comment of comments) {
93
+ let text = comment.text;
94
+ if (/^\/\/\s*indirect;\s*exhortignore/.test(text)) {
95
+ return true;
96
+ }
97
+ if (/^\/\/\s*exhortignore/.test(text)) {
98
+ return true;
96
99
  }
97
100
  }
98
- return result;
99
- }
100
- /**
101
- * extract package name from go.mod line that contains exhortignore comment.
102
- * @param line a row contains exhortignore as part of a comment
103
- * @return {string} the full package name + group/namespace + version
104
- * @private
105
- */
106
- function extractPackageName(line) {
107
- let trimmedRow = line.trim();
108
- let firstRemarkNotationOccurrence = trimmedRow.indexOf("//");
109
- return trimmedRow.substring(0, firstRemarkNotationOccurrence).trim();
101
+ return false;
110
102
  }
111
103
  /**
112
104
  *
113
- * @param {string } manifest - path to manifest
114
- * @return {[PackageURL]} list of ignored dependencies d
105
+ * @param {string} manifestContent go.mod file contents
106
+ * @param {import('web-tree-sitter').Parser} parser
107
+ * @param {import('web-tree-sitter').Query} requireQuery
108
+ * @return {PackageURL[]} list of ignored dependencies
115
109
  */
116
- function getIgnoredDeps(manifest) {
117
- let goMod = fs.readFileSync(manifest).toString().trim();
118
- let lines = goMod.split(getLineSeparatorGolang());
119
- return lines.filter(line => ignoredLine(line)).map(line => extractPackageName(line)).map(dep => toPurl(dep, /[ ]{1,3}/));
110
+ function getIgnoredDeps(manifestContent, parser, requireQuery) {
111
+ let tree = parser.parse(manifestContent);
112
+ return requireQuery.matches(tree.rootNode)
113
+ .filter(match => {
114
+ let specNode = match.captures.find(c => c.name === 'spec').node;
115
+ return hasExhortIgnore(specNode);
116
+ })
117
+ .map(match => {
118
+ let name = match.captures.find(c => c.name === 'name').node.text;
119
+ let version = match.captures.find(c => c.name === 'version').node.text;
120
+ return toPurl(`${name} ${version}`, /[ ]{1,3}/);
121
+ });
120
122
  }
121
123
  /**
122
124
  *
123
- * @param {[PackageURL]}allIgnoredDeps - list of purls of all dependencies that should be ignored
125
+ * @param {PackageURL[]} allIgnoredDeps list of purls of all dependencies that should be ignored
124
126
  * @param {PackageURL} purl object to be checked if needed to be ignored
125
127
  * @return {boolean}
126
128
  */
@@ -139,59 +141,29 @@ function enforceRemovingIgnoredDepsInCaseOfAutomaticVersionUpdate(ignoredDeps, s
139
141
  }
140
142
  /**
141
143
  *
142
- * @param {[string]} lines - array of lines of go.mod manifest
143
- * @param {string} goMod - content of go.mod manifest
144
- * @return {[string]} all dependencies from go.mod file as array
144
+ * @param {string} manifestContent go.mod file contents
145
+ * @param {import('web-tree-sitter').Parser} parser
146
+ * @param {import('web-tree-sitter').Query} requireQuery
147
+ * @return {string[]} all dependencies from go.mod file as "name version" strings
145
148
  */
146
- function collectAllDepsFromManifest(lines, goMod) {
147
- let result;
148
- // collect all deps that starts with require keyword
149
- result = lines.filter((line) => line.trim().startsWith("require") && !line.includes("(")).map((dep) => dep.substring("require".length).trim());
150
- // collect all deps that are inside `require` blocks
151
- let currentSegmentOfGoMod = goMod;
152
- let requirePositionObject = decideRequireBlockIndex(currentSegmentOfGoMod);
153
- while (requirePositionObject.index > -1) {
154
- let depsInsideRequirementsBlock = currentSegmentOfGoMod.substring(requirePositionObject.index + requirePositionObject.startingOffeset).trim();
155
- let endOfBlockIndex = depsInsideRequirementsBlock.indexOf(")");
156
- let currentIndex = 0;
157
- while (currentIndex < endOfBlockIndex) {
158
- let endOfLinePosition = depsInsideRequirementsBlock.indexOf(EOL, currentIndex);
159
- let dependency = depsInsideRequirementsBlock.substring(currentIndex, endOfLinePosition);
160
- result.push(dependency.trim());
161
- currentIndex = endOfLinePosition + 1;
162
- }
163
- currentSegmentOfGoMod = currentSegmentOfGoMod.substring(endOfBlockIndex + 1).trim();
164
- requirePositionObject = decideRequireBlockIndex(currentSegmentOfGoMod);
165
- }
166
- function decideRequireBlockIndex(goMod) {
167
- let object = {};
168
- let index = goMod.indexOf("require(");
169
- object.startingOffeset = "require(".length;
170
- if (index === -1) {
171
- index = goMod.indexOf("require (");
172
- object.startingOffeset = "require (".length;
173
- if (index === -1) {
174
- index = goMod.indexOf("require (");
175
- object.startingOffeset = "require (".length;
176
- }
177
- }
178
- object.index = index;
179
- return object;
180
- }
181
- return result;
149
+ function collectAllDepsFromManifest(manifestContent, parser, requireQuery) {
150
+ let tree = parser.parse(manifestContent);
151
+ return requireQuery.matches(tree.rootNode).map(match => {
152
+ let name = match.captures.find(c => c.name === 'name').node.text;
153
+ let version = match.captures.find(c => c.name === 'version').node.text;
154
+ return `${name} ${version}`;
155
+ });
182
156
  }
183
157
  /**
184
158
  *
185
159
  * @param {string} rootElementName the rootElementName element of go mod graph, to compare only direct deps from go mod graph against go.mod manifest
186
- * @param{[string]} goModGraphOutputRows the goModGraphOutputRows from go mod graph' output
187
- * @param {string }manifest path to go.mod manifest on file system
160
+ * @param {string[]} goModGraphOutputRows the goModGraphOutputRows from go mod graph' output
161
+ * @param {string} manifestContent go.mod file contents
188
162
  * @private
189
163
  */
190
- function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, manifest) {
191
- let goMod = fs.readFileSync(manifest).toString().trim();
192
- let lines = goMod.split(getLineSeparatorGolang());
164
+ function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, manifestContent, parser, requireQuery) {
193
165
  let comparisonLines = goModGraphOutputRows.filter((line) => line.startsWith(rootElementName)).map((line) => getChildVertexFromEdge(line));
194
- let manifestDeps = collectAllDepsFromManifest(lines, goMod);
166
+ let manifestDeps = collectAllDepsFromManifest(manifestContent, parser, requireQuery);
195
167
  try {
196
168
  comparisonLines.forEach((dependency) => {
197
169
  let parts = dependency.split("@");
@@ -203,7 +175,7 @@ function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, man
203
175
  let currentVersion = components[1];
204
176
  if (currentDepName === depName) {
205
177
  if (currentVersion !== version) {
206
- 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`);
178
+ 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`);
207
179
  }
208
180
  }
209
181
  });
@@ -219,10 +191,10 @@ function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, man
219
191
  * @param {string} manifest - path for go.mod
220
192
  * @param {{}} [opts={}] - optional various options to pass along the application
221
193
  * @param {boolean} includeTransitive - whether the sbom should contain transitive dependencies of the main module or not.
222
- * @returns {string} the SBOM json content
194
+ * @returns {Promise<string>} the SBOM json content
223
195
  * @private
224
196
  */
225
- function getSBOM(manifest, opts = {}, includeTransitive) {
197
+ async function getSBOM(manifest, opts = {}, includeTransitive) {
226
198
  // get custom goBin path
227
199
  let goBin = getCustomPath('go', opts);
228
200
  // verify goBin is accessible
@@ -248,14 +220,25 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
248
220
  catch (error) {
249
221
  throw new Error('failed to determine root module name', { cause: error });
250
222
  }
251
- let ignoredDeps = getIgnoredDeps(manifest);
223
+ let manifestContent = fs.readFileSync(manifest).toString();
224
+ let [parser, requireQuery] = await Promise.all([getParser(), getRequireQuery()]);
225
+ let ignoredDeps = getIgnoredDeps(manifestContent, parser, requireQuery);
252
226
  let allIgnoredDeps = ignoredDeps.map((dep) => dep.toString());
253
227
  let sbom = new Sbom();
254
228
  let rows = goGraphOutput.split(getLineSeparatorGolang()).filter(line => !line.includes(' go@'));
255
- let root = getParentVertexFromEdge(goModEditOutput['Module']['Path']);
229
+ let root = goModEditOutput['Module']['Path'];
230
+ // Build set of direct dependency paths from go mod edit -json
231
+ let directDepPaths = new Set();
232
+ if (goModEditOutput['Require']) {
233
+ goModEditOutput['Require'].forEach(req => {
234
+ if (!req['Indirect']) {
235
+ directDepPaths.add(req['Path']);
236
+ }
237
+ });
238
+ }
256
239
  let matchManifestVersions = getCustom("MATCH_MANIFEST_VERSIONS", "false", opts);
257
240
  if (matchManifestVersions === "true") {
258
- performManifestVersionsCheck(root, rows, manifest);
241
+ performManifestVersionsCheck(root, rows, manifestContent, parser, requireQuery);
259
242
  }
260
243
  const mainModule = toPurl(root, "@");
261
244
  const license = readLicenseFromManifest(manifest);
@@ -273,7 +256,11 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
273
256
  currentParent = getParentVertexFromEdge(row);
274
257
  source = toPurl(currentParent, "@");
275
258
  }
276
- let target = toPurl(getChildVertexFromEdge(row), "@");
259
+ let child = getChildVertexFromEdge(row);
260
+ let target = toPurl(child, "@");
261
+ if (getParentVertexFromEdge(row) === root && !directDepPaths.has(getPackageName(child))) {
262
+ return;
263
+ }
277
264
  sbom.addDependency(source, target);
278
265
  });
279
266
  // at the end, filter out all ignored dependencies including versions.
@@ -283,10 +270,12 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
283
270
  else {
284
271
  let directDependencies = rows.filter(row => row.startsWith(root));
285
272
  directDependencies.forEach(pair => {
286
- let dependency = getChildVertexFromEdge(pair);
287
- let depPurl = toPurl(dependency, "@");
288
- if (dependencyNotIgnored(ignoredDeps, depPurl)) {
289
- sbom.addDependency(mainModule, depPurl);
273
+ let child = getChildVertexFromEdge(pair);
274
+ let target = toPurl(child, "@");
275
+ if (dependencyNotIgnored(ignoredDeps, target)) {
276
+ if (directDepPaths.has(getPackageName(child))) {
277
+ sbom.addDependency(mainModule, target);
278
+ }
290
279
  }
291
280
  });
292
281
  enforceRemovingIgnoredDepsInCaseOfAutomaticVersionUpdate(ignoredDeps, sbom);
@@ -296,7 +285,7 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
296
285
  /**
297
286
  * Utility function for creating Purl String
298
287
 
299
- * @param {string }dependency the name of the artifact, can include a namespace(group) or not - namespace/artifactName.
288
+ * @param {string} dependency the name of the artifact, can include a namespace(group) or not - namespace/artifactName.
300
289
  * @param {RegExp} delimiter delimiter between name of dependency and version
301
290
  * @private
302
291
  * @returns {PackageURL|null} PackageUrl Object ready to be used in SBOM
@@ -321,12 +310,12 @@ function toPurl(dependency, delimiter) {
321
310
  }
322
311
  return pkg;
323
312
  }
324
- /** This function gets rows from go mod graph , and go.mod graph, and selecting for all
313
+ /** This function gets rows from go mod graph, and go.mod graph, and selecting for all
325
314
  * packages the has more than one minor the final versions as selected by golang MVS algorithm.
326
- * @param {[string]}rows all the rows from go modules dependency tree
315
+ * @param {string[]} rows all the rows from go modules dependency tree
327
316
  * @param {string} manifestPath the path of the go.mod file
328
317
  * @param {string} path to go binary
329
- * @return {[string]} rows that contains final versions.
318
+ * @return {string[]} rows that contains final versions.
330
319
  */
331
320
  function getFinalPackagesVersionsForModule(rows, manifestPath, goBin) {
332
321
  let manifestDir = path.dirname(manifestPath);
@@ -381,7 +370,7 @@ function getFinalPackagesVersionsForModule(rows, manifestPath, goBin) {
381
370
  /**
382
371
  *
383
372
  * @param {string} fullPackage - full package with its name and version
384
- * @return -{string} package name only
373
+ * @return {string} package name only
385
374
  * @private
386
375
  */
387
376
  function getPackageName(fullPackage) {
@@ -399,7 +388,7 @@ function isSpecialGoModule(moduleName) {
399
388
  /**
400
389
  *
401
390
  * @param {string} fullPackage - full package with its name and version
402
- * @return -{string} package version only
391
+ * @return {string|undefined} package version only
403
392
  * @private
404
393
  */
405
394
  function getVersionOfPackage(fullPackage) {
@@ -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
+ }
@@ -2,6 +2,8 @@ export default class Manifest {
2
2
  constructor(manifestPath: any);
3
3
  manifestPath: any;
4
4
  dependencies: any[];
5
+ peerDependencies: any;
6
+ optionalDependencies: any;
5
7
  name: any;
6
8
  version: any;
7
9
  ignored: any[];
@@ -9,6 +9,8 @@ export default class Manifest {
9
9
  this.manifestPath = manifestPath;
10
10
  const content = this.loadManifest();
11
11
  this.dependencies = this.loadDependencies(content);
12
+ this.peerDependencies = content.peerDependencies || {};
13
+ this.optionalDependencies = content.optionalDependencies || {};
12
14
  this.name = content.name;
13
15
  this.version = content.version || DEFAULT_VERSION;
14
16
  this.ignored = this.loadIgnored(content);
@@ -27,11 +29,27 @@ export default class Manifest {
27
29
  }
28
30
  loadDependencies(content) {
29
31
  let deps = [];
30
- if (!content.dependencies) {
31
- return deps;
32
+ const depSources = [
33
+ content.dependencies,
34
+ content.peerDependencies,
35
+ content.optionalDependencies,
36
+ ];
37
+ for (const source of depSources) {
38
+ if (source) {
39
+ for (let dep in source) {
40
+ if (!deps.includes(dep)) {
41
+ deps.push(dep);
42
+ }
43
+ }
44
+ }
32
45
  }
33
- for (let dep in content.dependencies) {
34
- deps.push(dep);
46
+ // bundledDependencies is an array of package names (subset of dependencies)
47
+ if (Array.isArray(content.bundledDependencies)) {
48
+ for (const dep of content.bundledDependencies) {
49
+ if (!deps.includes(dep)) {
50
+ deps.push(dep);
51
+ }
52
+ }
35
53
  }
36
54
  return deps;
37
55
  }
@@ -48,13 +48,15 @@ export default class Yarn_berry_processor extends Yarn_processor {
48
48
  if (!depTree) {
49
49
  return new Map();
50
50
  }
51
- return new Map(depTree.filter(dep => !this.#isRoot(dep.value)).map(dep => {
51
+ return new Map(depTree.filter(dep => !this.#isRoot(dep.value))
52
+ .map(dep => {
52
53
  const depName = dep.value;
53
54
  const idx = depName.lastIndexOf('@');
54
55
  const name = depName.substring(0, idx);
55
56
  const version = dep.children.Version;
56
57
  return [name, toPurl(purlType, name, version)];
57
- }));
58
+ })
59
+ .filter(([name]) => this._manifest.dependencies.includes(name)));
58
60
  }
59
61
  /**
60
62
  * Checks if a dependency is the root package
@@ -77,14 +79,58 @@ export default class Yarn_berry_processor extends Yarn_processor {
77
79
  if (!depTree) {
78
80
  return;
79
81
  }
82
+ // Build index of nodes by their value for quick lookup
83
+ const nodeIndex = new Map();
84
+ depTree.forEach(n => nodeIndex.set(n.value, n));
85
+ // Determine the set of node values reachable from root via production deps
86
+ const prodDeps = new Set(this._manifest.dependencies);
87
+ const reachable = new Set();
88
+ const queue = [];
89
+ // Seed with root's production dependencies
90
+ const rootNode = depTree.find(n => this.#isRoot(n.value));
91
+ if (rootNode?.children?.Dependencies) {
92
+ for (const d of rootNode.children.Dependencies) {
93
+ const to = this.#purlFromLocator(d.locator);
94
+ if (to) {
95
+ const fullName = to.namespace ? `${to.namespace}/${to.name}` : to.name;
96
+ if (prodDeps.has(fullName)) {
97
+ queue.push(d.locator);
98
+ }
99
+ }
100
+ }
101
+ }
102
+ // BFS to find all transitively reachable packages
103
+ while (queue.length > 0) {
104
+ const locator = queue.shift();
105
+ if (reachable.has(locator)) {
106
+ continue;
107
+ }
108
+ reachable.add(locator);
109
+ const node = nodeIndex.get(this.#nodeValueFromLocator(locator));
110
+ if (node?.children?.Dependencies) {
111
+ for (const d of node.children.Dependencies) {
112
+ if (!reachable.has(d.locator)) {
113
+ queue.push(d.locator);
114
+ }
115
+ }
116
+ }
117
+ }
118
+ // Only emit edges for root and reachable nodes
80
119
  depTree.forEach(n => {
81
120
  const depName = n.value;
82
- const from = this.#isRoot(depName) ? toPurlFromString(sbom.getRoot().purl) : this.#purlFromNode(depName, n);
121
+ const isRoot = this.#isRoot(depName);
122
+ if (!isRoot && !this.#isReachableNode(depName, reachable)) {
123
+ return;
124
+ }
125
+ const from = isRoot ? toPurlFromString(sbom.getRoot().purl) : this.#purlFromNode(depName, n);
83
126
  const deps = n.children?.Dependencies;
84
127
  if (!deps) {
85
128
  return;
86
129
  }
87
130
  deps.forEach(d => {
131
+ if (!reachable.has(d.locator)) {
132
+ return;
133
+ }
88
134
  const to = this.#purlFromLocator(d.locator);
89
135
  if (to) {
90
136
  sbom.addDependency(from, to);
@@ -92,6 +138,39 @@ export default class Yarn_berry_processor extends Yarn_processor {
92
138
  });
93
139
  });
94
140
  }
141
+ /**
142
+ * Converts a locator to the node value format used in yarn info output
143
+ * @param {string} locator - e.g. "express@npm:4.17.1"
144
+ * @returns {string} The node value, same as locator for non-virtual
145
+ * @private
146
+ */
147
+ #nodeValueFromLocator(locator) {
148
+ // Virtual locators: "@scope/name@virtual:hash#npm:version" → "@scope/name@npm:version"
149
+ const virtualMatch = Yarn_berry_processor.VIRTUAL_LOCATOR_PATTERN.exec(locator);
150
+ if (virtualMatch) {
151
+ return `${virtualMatch[1]}@npm:${virtualMatch[2]}`;
152
+ }
153
+ return locator;
154
+ }
155
+ /**
156
+ * Checks if a node is in the reachable set by matching its value against reachable locators
157
+ * @param {string} depName - The node value (e.g. "express@npm:4.17.1")
158
+ * @param {Set<string>} reachable - Set of reachable locators
159
+ * @returns {boolean}
160
+ * @private
161
+ */
162
+ #isReachableNode(depName, reachable) {
163
+ if (reachable.has(depName)) {
164
+ return true;
165
+ }
166
+ // Check if any reachable locator resolves to this node value
167
+ for (const locator of reachable) {
168
+ if (this.#nodeValueFromLocator(locator) === depName) {
169
+ return true;
170
+ }
171
+ }
172
+ return false;
173
+ }
95
174
  /**
96
175
  * Creates a PackageURL from a dependency locator
97
176
  * @param {string} locator - The dependency locator
@@ -75,7 +75,7 @@ function addAllDependencies(source, dep, sbom) {
75
75
  /**
76
76
  *
77
77
  * @param {string} manifest - path to requirements.txt
78
- * @return {PackageURL []}
78
+ * @return {Promise<PackageURL[]>}
79
79
  */
80
80
  async function getIgnoredDependencies(manifest) {
81
81
  const [parser, ignoreQuery, pinnedVersionQuery] = await Promise.all([
@@ -0,0 +1,42 @@
1
+ export default class Python_poetry extends Base_pyproject {
2
+ /**
3
+ * Get poetry show --tree output.
4
+ * @param {string} manifestDir
5
+ * @param {Object} opts
6
+ * @returns {string}
7
+ */
8
+ _getPoetryShowTreeOutput(manifestDir: string, opts: any): string;
9
+ /**
10
+ * Get poetry show --all output (flat list with resolved versions).
11
+ * @param {string} manifestDir
12
+ * @param {Object} opts
13
+ * @returns {string}
14
+ */
15
+ _getPoetryShowAllOutput(manifestDir: string, opts: any): string;
16
+ /**
17
+ * Parse poetry show --all output into a version map.
18
+ * Lines look like: "name (!) 1.2.3 Description text..."
19
+ * or: "name 1.2.3 Description text..."
20
+ * @param {string} output
21
+ * @returns {Map<string, string>} canonical name -> version
22
+ */
23
+ _parsePoetryShowAll(output: string): Map<string, string>;
24
+ /**
25
+ * Parse poetry show --tree output into a dependency graph structure.
26
+ * Top-level lines (no indentation/tree chars) are direct deps: "name version description"
27
+ * Indented lines are transitive deps with tree chars: "├── name >=constraint"
28
+ *
29
+ * @param {string} treeOutput
30
+ * @param {Map<string, string>} versionMap - canonical name -> resolved version
31
+ * @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
32
+ */
33
+ _parsePoetryTree(treeOutput: string, versionMap: Map<string, string>): {
34
+ directDeps: string[];
35
+ graph: Map<string, {
36
+ name: string;
37
+ version: string;
38
+ children: string[];
39
+ }>;
40
+ };
41
+ }
42
+ import Base_pyproject from './base_pyproject.js';