@trustify-da/trustify-da-javascript-client 0.3.0-ea.62f6bc7 → 0.3.0-ea.63ae5c2

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.
package/dist/package.json CHANGED
@@ -46,13 +46,16 @@
46
46
  "dependencies": {
47
47
  "@babel/core": "^7.23.2",
48
48
  "@cyclonedx/cyclonedx-library": "^6.13.0",
49
+ "eslint-import-resolver-typescript": "^4.4.4",
49
50
  "fast-toml": "^0.5.4",
50
51
  "fast-xml-parser": "^5.3.4",
51
52
  "help": "^3.0.2",
52
53
  "https-proxy-agent": "^7.0.6",
53
54
  "node-fetch": "^3.3.2",
54
55
  "packageurl-js": "~1.0.2",
55
- "yargs": "^17.7.2"
56
+ "tree-sitter-requirements": "github:Strum355/tree-sitter-requirements#d0261ee76b84253997fe70d7d397e78c006c3801",
57
+ "web-tree-sitter": "^0.26.6",
58
+ "yargs": "^18.0.0"
56
59
  },
57
60
  "devDependencies": {
58
61
  "@babel/core": "^7.23.2",
@@ -60,9 +63,10 @@
60
63
  "@types/node": "^20.17.30",
61
64
  "@types/which": "^3.0.4",
62
65
  "babel-plugin-rewire": "^1.2.0",
63
- "c8": "^10.1.3",
66
+ "c8": "^11.0.0",
64
67
  "chai": "^4.3.7",
65
68
  "eslint": "^8.42.0",
69
+ "eslint-import-resolver-typescript": "^4.4.4",
66
70
  "eslint-plugin-editorconfig": "^4.0.3",
67
71
  "eslint-plugin-import": "^2.29.1",
68
72
  "esmock": "^2.6.2",
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Utility function for fetching vendor tokens
3
+ * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
4
+ * @returns {{}}
5
+ */
6
+ export function getTokenHeaders(opts?: import("index.js").Options): {};
1
7
  declare namespace _default {
2
8
  export { requestComponent };
3
9
  export { requestStack };
@@ -5,11 +5,11 @@ import { HttpsProxyAgent } from "https-proxy-agent";
5
5
  import { generateImageSBOM, parseImageRef } from "./oci_image/utils.js";
6
6
  import { RegexNotToBeLogged, getCustom } from "./tools.js";
7
7
  export default { requestComponent, requestStack, requestImages, validateToken };
8
- const rhdaTokenHeader = "rhda-token";
9
- const rhdaTelemetryId = "rhda-telemetry-id";
10
- const rhdaSourceHeader = "rhda-source";
11
- const rhdaOperationTypeHeader = "rhda-operation-type";
12
- const rhdaPackageManagerHeader = "rhda-pkg-manager";
8
+ const rhdaTokenHeader = "trust-da-token";
9
+ const rhdaTelemetryId = "telemetry-anonymous-id";
10
+ const rhdaSourceHeader = "trust-da-source";
11
+ const rhdaOperationTypeHeader = "trust-da-operation-type";
12
+ const rhdaPackageManagerHeader = "trust-da-pkg-manager";
13
13
  /**
14
14
  * Adds proxy agent configuration to fetch options if a proxy URL is specified
15
15
  * @param {RequestInit} options - The base fetch options
@@ -35,7 +35,7 @@ function addProxyAgent(options, opts) {
35
35
  async function requestStack(provider, manifest, url, html = false, opts = {}) {
36
36
  opts["source-manifest"] = Buffer.from(fs.readFileSync(manifest).toString()).toString('base64');
37
37
  opts["manifest-type"] = path.parse(manifest).base;
38
- let provided = provider.provideStack(manifest, opts); // throws error if content providing failed
38
+ let provided = await provider.provideStack(manifest, opts); // throws error if content providing failed
39
39
  opts["source-manifest"] = "";
40
40
  opts[rhdaOperationTypeHeader.toUpperCase().replaceAll("-", "_")] = "stack-analysis";
41
41
  let startTime = new Date();
@@ -94,7 +94,7 @@ async function requestStack(provider, manifest, url, html = false, opts = {}) {
94
94
  */
95
95
  async function requestComponent(provider, manifest, url, opts = {}) {
96
96
  opts["source-manifest"] = Buffer.from(fs.readFileSync(manifest).toString()).toString('base64');
97
- let provided = provider.provideComponent(manifest, opts); // throws error if content providing failed
97
+ let provided = await provider.provideComponent(manifest, opts); // throws error if content providing failed
98
98
  opts["source-manifest"] = "";
99
99
  opts[rhdaOperationTypeHeader.toUpperCase().replaceAll("-", "_")] = "component-analysis";
100
100
  if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
@@ -208,11 +208,12 @@ async function validateToken(url, opts = {}) {
208
208
  *
209
209
  * @param {string} headerName - the header name to populate in request
210
210
  * @param headers
211
- * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
211
+ * @param {string} optsKey - key in the options object to use the value for
212
+ * @param {import("index.js").Options} [opts={}] - options input object to fetch header values from
212
213
  * @private
213
214
  */
214
- function setRhdaHeader(headerName, headers, opts) {
215
- let rhdaHeaderValue = getCustom(headerName.toUpperCase().replaceAll("-", "_"), null, opts);
215
+ function setRhdaHeader(headerName, headers, optsKey, opts) {
216
+ let rhdaHeaderValue = getCustom(optsKey, null, opts);
216
217
  if (rhdaHeaderValue) {
217
218
  headers[headerName] = rhdaHeaderValue;
218
219
  }
@@ -222,25 +223,14 @@ function setRhdaHeader(headerName, headers, opts) {
222
223
  * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
223
224
  * @returns {{}}
224
225
  */
225
- function getTokenHeaders(opts = {}) {
226
- let supportedTokens = ['snyk', 'oss-index'];
226
+ export function getTokenHeaders(opts = {}) {
227
227
  let headers = {};
228
- supportedTokens.forEach(vendor => {
229
- let token = getCustom(`TRUSTIFY_DA_${vendor.replace("-", "_").toUpperCase()}_TOKEN`, null, opts);
230
- if (token) {
231
- headers[`ex-${vendor}-token`] = token;
232
- }
233
- let user = getCustom(`TRUSTIFY_DA_${vendor.replace("-", "_").toUpperCase()}_USER`, null, opts);
234
- if (user) {
235
- headers[`ex-${vendor}-user`] = user;
236
- }
237
- });
238
- setRhdaHeader(rhdaTokenHeader, headers, opts);
239
- setRhdaHeader(rhdaSourceHeader, headers, opts);
240
- setRhdaHeader(rhdaOperationTypeHeader, headers, opts);
241
- setRhdaHeader(rhdaPackageManagerHeader, headers, opts);
242
- setRhdaHeader(rhdaTelemetryId, headers, opts);
243
- if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
228
+ setRhdaHeader(rhdaTokenHeader, headers, 'TRUSTIFY_DA_TOKEN', opts);
229
+ setRhdaHeader(rhdaSourceHeader, headers, 'TRUSTIFY_DA_SOURCE', opts);
230
+ setRhdaHeader(rhdaOperationTypeHeader, headers, rhdaOperationTypeHeader.toUpperCase().replaceAll("-", "_"), opts);
231
+ setRhdaHeader(rhdaPackageManagerHeader, headers, rhdaPackageManagerHeader.toUpperCase().replaceAll("-", "_"), opts);
232
+ setRhdaHeader(rhdaTelemetryId, headers, 'TRUSTIFY_DA_TELEMETRY_ID', opts);
233
+ if (getCustom("TRUSTIFY_DA_DEBUG", null, opts) === "true") {
244
234
  console.log("Headers Values to be sent to Trustify DA backend:" + EOL);
245
235
  for (const headerKey in headers) {
246
236
  if (!headerKey.match(RegexNotToBeLogged)) {
@@ -48,9 +48,9 @@ export type Options = {
48
48
  TRUSTIFY_DA_SYFT_PATH?: string | undefined;
49
49
  TRUSTIFY_DA_YARN_PATH?: string | undefined;
50
50
  MATCH_MANIFEST_VERSIONS?: string | undefined;
51
- RHDA_SOURCE?: string | undefined;
52
- RHDA_TOKEN?: string | undefined;
53
- RHDA_TELEMETRY_ID?: string | undefined;
51
+ TRUSTIFY_DA_SOURCE?: string | undefined;
52
+ TRUSTIFY_DA_TOKEN?: string | undefined;
53
+ TRUSTIFY_DA_TELEMETRY_ID?: string | undefined;
54
54
  [key: string]: string | undefined;
55
55
  };
56
56
  /**
package/dist/src/index.js CHANGED
@@ -36,9 +36,9 @@ export default { componentAnalysis, stackAnalysis, imageAnalysis, validateToken
36
36
  * TRUSTIFY_DA_SYFT_PATH?: string | undefined,
37
37
  * TRUSTIFY_DA_YARN_PATH?: string | undefined,
38
38
  * MATCH_MANIFEST_VERSIONS?: string | undefined,
39
- * RHDA_SOURCE?: string | undefined,
40
- * RHDA_TOKEN?: string | undefined,
41
- * RHDA_TELEMETRY_ID?: string | undefined,
39
+ * TRUSTIFY_DA_SOURCE?: string | undefined,
40
+ * TRUSTIFY_DA_TOKEN?: string | undefined,
41
+ * TRUSTIFY_DA_TELEMETRY_ID?: string | undefined,
42
42
  * [key: string]: string | undefined,
43
43
  * }} Options
44
44
  */
@@ -10,7 +10,7 @@
10
10
  */
11
11
  export function match(manifest: string, providers: [Provider]): Provider;
12
12
  /** @typedef {{ecosystem: string, contentType: string, content: string}} Provided */
13
- /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided, provideStack: function(string, {}): Provided}} Provider */
13
+ /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>}} Provider */
14
14
  /**
15
15
  * MUST include all providers here.
16
16
  * @type {[Provider]}
@@ -24,6 +24,6 @@ export type Provided = {
24
24
  export type Provider = {
25
25
  isSupported: (arg0: string) => boolean;
26
26
  validateLockFile: (arg0: string) => void;
27
- provideComponent: (arg0: string, arg1: {}) => Provided;
28
- provideStack: (arg0: string, arg1: {}) => Provided;
27
+ provideComponent: (arg0: string, arg1: {}) => Provided | Promise<Provided>;
28
+ provideStack: (arg0: string, arg1: {}) => Provided | Promise<Provided>;
29
29
  };
@@ -8,7 +8,7 @@ import Javascript_pnpm from './providers/javascript_pnpm.js';
8
8
  import Javascript_yarn from './providers/javascript_yarn.js';
9
9
  import pythonPipProvider from './providers/python_pip.js';
10
10
  /** @typedef {{ecosystem: string, contentType: string, content: string}} Provided */
11
- /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided, provideStack: function(string, {}): Provided}} Provider */
11
+ /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>}} Provider */
12
12
  /**
13
13
  * MUST include all providers here.
14
14
  * @type {[Provider]}
@@ -15,13 +15,16 @@ export default class Python_controller {
15
15
  realEnvironment: boolean;
16
16
  pathToRequirements: string;
17
17
  options: {};
18
+ parser: Promise<import("web-tree-sitter").Parser>;
19
+ requirementsQuery: Promise<import("web-tree-sitter").Query>;
20
+ pinnedVersionQuery: Promise<import("web-tree-sitter").Query>;
18
21
  prepareEnvironment(): void;
19
22
  /**
20
23
  *
21
24
  * @param {boolean} includeTransitive - whether to return include in returned object transitive dependencies or not
22
- * @return {[DependencyEntry]}
25
+ * @return {Promise<[DependencyEntry]>}
23
26
  */
24
- getDependencies(includeTransitive: boolean): [DependencyEntry];
27
+ getDependencies(includeTransitive: boolean): Promise<[DependencyEntry]>;
25
28
  #private;
26
29
  }
27
30
  export type DependencyEntry = {
@@ -2,6 +2,7 @@ import fs from "node:fs";
2
2
  import path from 'node:path';
3
3
  import os, { EOL } from "os";
4
4
  import { environmentVariableIsPopulated, getCustom, invokeCommand } from "../tools.js";
5
+ import { getParser, getRequirementQuery, getPinnedVersionQuery } from './requirements_parser.js';
5
6
  function getPipFreezeOutput() {
6
7
  try {
7
8
  return environmentVariableIsPopulated("TRUSTIFY_DA_PIP_FREEZE") ? new Buffer.from(process.env["TRUSTIFY_DA_PIP_FREEZE"], 'base64').toString('ascii') : invokeCommand(this.pathToPipBin, ['freeze', '--all']).toString();
@@ -26,6 +27,9 @@ export default class Python_controller {
26
27
  realEnvironment;
27
28
  pathToRequirements;
28
29
  options;
30
+ parser;
31
+ requirementsQuery;
32
+ pinnedVersionQuery;
29
33
  /**
30
34
  * Constructor to create new python controller instance to interact with pip package manager
31
35
  * @param {boolean} realEnvironment - whether to use real environment supplied by client or to create virtual environment
@@ -41,6 +45,9 @@ export default class Python_controller {
41
45
  this.prepareEnvironment();
42
46
  this.pathToRequirements = pathToRequirements;
43
47
  this.options = options;
48
+ this.parser = getParser();
49
+ this.requirementsQuery = getRequirementQuery();
50
+ this.pinnedVersionQuery = getPinnedVersionQuery();
44
51
  }
45
52
  prepareEnvironment() {
46
53
  if (!this.realEnvironment) {
@@ -86,6 +93,23 @@ export default class Python_controller {
86
93
  }
87
94
  }
88
95
  }
96
+ /**
97
+ * Parse the requirements.txt file using tree-sitter and return structured requirement data.
98
+ * @return {Promise<{name: string, version: string|null}[]>}
99
+ */
100
+ async #parseRequirements() {
101
+ const content = fs.readFileSync(this.pathToRequirements).toString();
102
+ const tree = (await this.parser).parse(content);
103
+ return Promise.all((await this.requirementsQuery).matches(tree.rootNode).map(async (match) => {
104
+ const reqNode = match.captures.find(c => c.name === 'req').node;
105
+ const name = match.captures.find(c => c.name === 'name').node.text;
106
+ const versionMatches = (await this.pinnedVersionQuery).matches(reqNode);
107
+ const version = versionMatches.length > 0
108
+ ? versionMatches[0].captures.find(c => c.name === 'version').node.text
109
+ : null;
110
+ return { name, version };
111
+ }));
112
+ }
89
113
  #decideIfWindowsOrLinuxPath(fileName) {
90
114
  if (os.platform() === "win32") {
91
115
  return fileName + ".exe";
@@ -97,9 +121,9 @@ export default class Python_controller {
97
121
  /**
98
122
  *
99
123
  * @param {boolean} includeTransitive - whether to return include in returned object transitive dependencies or not
100
- * @return {[DependencyEntry]}
124
+ * @return {Promise<[DependencyEntry]>}
101
125
  */
102
- getDependencies(includeTransitive) {
126
+ async getDependencies(includeTransitive) {
103
127
  let startingTime;
104
128
  let endingTime;
105
129
  if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
@@ -124,10 +148,10 @@ export default class Python_controller {
124
148
  if (matchManifestVersions === "true") {
125
149
  throw new Error("Conflicting settings, TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS=true can only work with MATCH_MANIFEST_VERSIONS=false");
126
150
  }
127
- this.#installingRequirementsOneByOne();
151
+ await this.#installingRequirementsOneByOne();
128
152
  }
129
153
  }
130
- let dependencies = this.#getDependenciesImpl(includeTransitive);
154
+ let dependencies = await this.#getDependenciesImpl(includeTransitive);
131
155
  this.#cleanEnvironment();
132
156
  if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
133
157
  endingTime = new Date();
@@ -137,16 +161,14 @@ export default class Python_controller {
137
161
  }
138
162
  return dependencies;
139
163
  }
140
- #installingRequirementsOneByOne() {
141
- let requirementsContent = fs.readFileSync(this.pathToRequirements);
142
- let requirementsRows = requirementsContent.toString().split(EOL);
143
- requirementsRows.filter((line) => !line.trim().startsWith("#")).filter((line) => line.trim() !== "").forEach((dependency) => {
144
- let dependencyName = getDependencyName(dependency);
164
+ async #installingRequirementsOneByOne() {
165
+ const requirements = await this.#parseRequirements();
166
+ requirements.forEach(({ name }) => {
145
167
  try {
146
- invokeCommand(this.pathToPipBin, ['install', dependencyName]);
168
+ invokeCommand(this.pathToPipBin, ['install', name]);
147
169
  }
148
170
  catch (error) {
149
- throw new Error(`Failed in best-effort installing ${dependencyName} in virtual python environment`, { cause: error });
171
+ throw new Error(`Failed in best-effort installing ${name} in virtual python environment`, { cause: error });
150
172
  }
151
173
  });
152
174
  }
@@ -163,34 +185,26 @@ export default class Python_controller {
163
185
  }
164
186
  }
165
187
  }
166
- #getDependenciesImpl(includeTransitive) {
167
- let dependencies = new Array();
188
+ async #getDependenciesImpl(includeTransitive) {
189
+ let dependencies = [];
168
190
  let usePipDepTree = getCustom("TRUSTIFY_DA_PIP_USE_DEP_TREE", "false", this.options);
169
- let freezeOutput;
170
- let lines;
171
- let depNames;
172
- let pipShowOutput;
173
191
  let allPipShowDeps;
174
192
  let pipDepTreeJsonArrayOutput;
175
193
  if (usePipDepTree !== "true") {
176
- freezeOutput = getPipFreezeOutput.call(this);
177
- lines = freezeOutput.split(EOL);
178
- depNames = lines.map(line => getDependencyName(line));
194
+ const freezeOutput = getPipFreezeOutput.call(this);
195
+ const lines = freezeOutput.split(EOL);
196
+ const depNames = lines.map(line => getDependencyName(line));
197
+ const pipShowOutput = getPipShowOutput.call(this, depNames);
198
+ allPipShowDeps = pipShowOutput.split(EOL + "---" + EOL);
179
199
  }
180
200
  else {
181
201
  pipDepTreeJsonArrayOutput = getDependencyTreeJsonFromPipDepTree(this.pathToPipBin, this.pathToPythonBin);
182
202
  }
183
- if (usePipDepTree !== "true") {
184
- pipShowOutput = getPipShowOutput.call(this, depNames);
185
- allPipShowDeps = pipShowOutput.split(EOL + "---" + EOL);
186
- }
187
- //debug
188
- // pipShowOutput = "alternative pip show output goes here for debugging"
189
203
  let matchManifestVersions = getCustom("MATCH_MANIFEST_VERSIONS", "true", this.options);
190
- let linesOfRequirements = fs.readFileSync(this.pathToRequirements).toString().split(EOL).filter((line) => !line.trim().startsWith("#")).map(line => line.trim());
204
+ let parsedRequirements = await this.#parseRequirements();
191
205
  let CachedEnvironmentDeps = {};
192
206
  if (usePipDepTree !== "true") {
193
- allPipShowDeps.forEach((record) => {
207
+ allPipShowDeps.forEach(record => {
194
208
  let dependencyName = getDependencyNameShow(record).toLowerCase();
195
209
  CachedEnvironmentDeps[dependencyName] = record;
196
210
  CachedEnvironmentDeps[dependencyName.replace("-", "_")] = record;
@@ -210,40 +224,24 @@ export default class Python_controller {
210
224
  CachedEnvironmentDeps[packageName.replace("_", "-")] = pipDepTreeEntryForCache;
211
225
  });
212
226
  }
213
- linesOfRequirements.forEach((dep) => {
214
- // if matchManifestVersions setting is turned on , then
215
- if (matchManifestVersions === "true") {
216
- let dependencyName;
217
- let manifestVersion;
227
+ parsedRequirements.forEach(({ name: depName, version: manifestVersion }) => {
228
+ if (matchManifestVersions === "true" && manifestVersion != null) {
218
229
  let installedVersion;
219
- let doubleEqualSignPosition;
220
- if (dep.includes("==")) {
221
- doubleEqualSignPosition = dep.indexOf("==");
222
- manifestVersion = dep.substring(doubleEqualSignPosition + 2).trim();
223
- if (manifestVersion.includes("#")) {
224
- let hashCharIndex = manifestVersion.indexOf("#");
225
- manifestVersion = manifestVersion.substring(0, hashCharIndex);
230
+ if (CachedEnvironmentDeps[depName.toLowerCase()] !== undefined) {
231
+ if (usePipDepTree !== "true") {
232
+ installedVersion = getDependencyVersion(CachedEnvironmentDeps[depName.toLowerCase()]);
226
233
  }
227
- dependencyName = getDependencyName(dep);
228
- // only compare between declared version in manifest to installed version , if the package is installed.
229
- if (CachedEnvironmentDeps[dependencyName.toLowerCase()] !== undefined) {
230
- if (usePipDepTree !== "true") {
231
- installedVersion = getDependencyVersion(CachedEnvironmentDeps[dependencyName.toLowerCase()]);
232
- }
233
- else {
234
- installedVersion = CachedEnvironmentDeps[dependencyName.toLowerCase()].version;
235
- }
234
+ else {
235
+ installedVersion = CachedEnvironmentDeps[depName.toLowerCase()].version;
236
236
  }
237
- if (installedVersion) {
238
- if (manifestVersion.trim() !== installedVersion.trim()) {
239
- throw new Error(`Can't continue with analysis - versions mismatch for dependency name ${dependencyName} (manifest version=${manifestVersion}, installed version=${installedVersion}).If you want to allow version mismatch for analysis between installed and requested packages, set environment variable/setting MATCH_MANIFEST_VERSIONS=false`);
240
- }
237
+ }
238
+ if (installedVersion) {
239
+ if (manifestVersion.trim() !== installedVersion.trim()) {
240
+ throw new Error(`Can't continue with analysis - versions mismatch for dependency name ${depName} (manifest version=${manifestVersion}, installed version=${installedVersion}).If you want to allow version mismatch for analysis between installed and requested packages, set environment variable/setting MATCH_MANIFEST_VERSIONS=false`);
241
241
  }
242
242
  }
243
243
  }
244
- let path = new Array();
245
- let depName = getDependencyName(dep);
246
- //array to track a path for each branch in the dependency tree
244
+ let path = [];
247
245
  path.push(depName.toLowerCase());
248
246
  bringAllDependencies(dependencies, depName, CachedEnvironmentDeps, includeTransitive, path, usePipDepTree);
249
247
  });
@@ -347,11 +345,11 @@ function bringAllDependencies(dependencies, dependencyName, cachedEnvironmentDep
347
345
  version = record.version;
348
346
  directDeps = record.dependencies;
349
347
  }
350
- let targetDeps = new Array();
348
+ let targetDeps = [];
351
349
  let entry = { "name": depName, "version": version, "dependencies": [] };
352
350
  dependencies.push(entry);
353
351
  directDeps.forEach((dep) => {
354
- let depArray = new Array();
352
+ let depArray = [];
355
353
  // to avoid infinite loop, check if the dependency not already on current path, before going recursively resolving its dependencies.
356
354
  if (!path.includes(dep.toLowerCase())) {
357
355
  // send to recurrsion the path + the current dep
@@ -23,13 +23,13 @@ declare function validateLockFile(): boolean;
23
23
  * Provide content and content type for python-pip component analysis.
24
24
  * @param {string} manifest - path to requirements.txt for component report
25
25
  * @param {{}} [opts={}] - optional various options to pass along the application
26
- * @returns {Provided}
26
+ * @returns {Promise<Provided>}
27
27
  */
28
- declare function provideComponent(manifest: string, opts?: {}): Provided;
28
+ declare function provideComponent(manifest: string, opts?: {}): Promise<Provided>;
29
29
  /**
30
30
  * Provide content and content type for python-pip stack analysis.
31
31
  * @param {string} manifest - the manifest path or name
32
32
  * @param {{}} [opts={}] - optional various options to pass along the application
33
- * @returns {Provided}
33
+ * @returns {Promise<Provided>}
34
34
  */
35
- declare function provideStack(manifest: string, opts?: {}): Provided;
35
+ declare function provideStack(manifest: string, opts?: {}): Promise<Provided>;
@@ -1,9 +1,9 @@
1
1
  import fs from 'node:fs';
2
- import { EOL } from 'os';
3
2
  import { PackageURL } from 'packageurl-js';
4
3
  import Sbom from '../sbom.js';
5
4
  import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand } from "../tools.js";
6
5
  import Python_controller from './python_controller.js';
6
+ import { getParser, getIgnoreQuery, getPinnedVersionQuery } from './requirements_parser.js';
7
7
  export default { isSupported, validateLockFile, provideComponent, provideStack };
8
8
  /** @typedef {{name: string, version: string, dependencies: DependencyEntry[]}} DependencyEntry */
9
9
  /**
@@ -26,12 +26,12 @@ function validateLockFile() { return true; }
26
26
  * Provide content and content type for python-pip stack analysis.
27
27
  * @param {string} manifest - the manifest path or name
28
28
  * @param {{}} [opts={}] - optional various options to pass along the application
29
- * @returns {Provided}
29
+ * @returns {Promise<Provided>}
30
30
  */
31
- function provideStack(manifest, opts = {}) {
31
+ async function provideStack(manifest, opts = {}) {
32
32
  return {
33
33
  ecosystem,
34
- content: createSbomStackAnalysis(manifest, opts),
34
+ content: await createSbomStackAnalysis(manifest, opts),
35
35
  contentType: 'application/vnd.cyclonedx+json'
36
36
  };
37
37
  }
@@ -39,12 +39,12 @@ function provideStack(manifest, opts = {}) {
39
39
  * Provide content and content type for python-pip component analysis.
40
40
  * @param {string} manifest - path to requirements.txt for component report
41
41
  * @param {{}} [opts={}] - optional various options to pass along the application
42
- * @returns {Provided}
42
+ * @returns {Promise<Provided>}
43
43
  */
44
- function provideComponent(manifest, opts = {}) {
44
+ async function provideComponent(manifest, opts = {}) {
45
45
  return {
46
46
  ecosystem,
47
- content: getSbomForComponentAnalysis(manifest, opts),
47
+ content: await getSbomForComponentAnalysis(manifest, opts),
48
48
  contentType: 'application/vnd.cyclonedx+json'
49
49
  };
50
50
  }
@@ -66,49 +66,34 @@ function addAllDependencies(source, dep, sbom) {
66
66
  }
67
67
  /**
68
68
  *
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
69
+ * @param {string} manifest - path to requirements.txt
88
70
  * @return {PackageURL []}
89
71
  */
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);
72
+ async function getIgnoredDependencies(manifest) {
73
+ const [parser, ignoreQuery, pinnedVersionQuery] = await Promise.all([
74
+ getParser(), getIgnoreQuery(), getPinnedVersionQuery()
75
+ ]);
76
+ const content = fs.readFileSync(manifest).toString();
77
+ const tree = parser.parse(content);
78
+ return ignoreQuery.matches(tree.rootNode).map(match => {
79
+ const reqNode = match.captures.find(c => c.name === 'req').node;
80
+ const name = match.captures.find(c => c.name === 'name').node.text;
81
+ const versionMatches = pinnedVersionQuery.matches(reqNode);
82
+ const version = versionMatches.length > 0
83
+ ? versionMatches[0].captures.find(c => c.name === 'version').node.text
84
+ : undefined;
85
+ return toPurl(name, version);
101
86
  });
102
87
  }
103
88
  /**
104
89
  *
105
- * @param {string} requirementTxtContent content of requirments.txt in string
90
+ * @param {string} manifest - path to requirements.txt
106
91
  * @param {Sbom} sbom object to filter out from it exhortignore dependencies.
107
92
  * @param {{Object}} opts - various options and settings for the application
108
93
  * @private
109
94
  */
110
- function handleIgnoredDependencies(requirementTxtContent, sbom, opts = {}) {
111
- let ignoredDeps = getIgnoredDependencies(requirementTxtContent);
95
+ async function handleIgnoredDependencies(manifest, sbom, opts = {}) {
96
+ let ignoredDeps = await getIgnoredDependencies(manifest);
112
97
  let matchManifestVersions = getCustom("MATCH_MANIFEST_VERSIONS", "true", opts);
113
98
  if (matchManifestVersions === "true") {
114
99
  const ignoredDepsVersion = ignoredDeps.filter(dep => dep.version !== undefined);
@@ -172,22 +157,21 @@ const DEFAULT_PIP_ROOT_COMPONENT_VERSION = "0.0.0";
172
157
  * Create sbom json string out of a manifest path for stack analysis.
173
158
  * @param {string} manifest - path for requirements.txt
174
159
  * @param {{}} [opts={}] - optional various options to pass along the application
175
- * @returns {string} the sbom json string content
160
+ * @returns {Promise<string>} the sbom json string content
176
161
  * @private
177
162
  */
178
- function createSbomStackAnalysis(manifest, opts = {}) {
163
+ async function createSbomStackAnalysis(manifest, opts = {}) {
179
164
  let binaries = {};
180
165
  let createVirtualPythonEnv = handlePythonEnvironment(binaries, opts);
181
166
  let pythonController = new Python_controller(createVirtualPythonEnv === "false", binaries.pip, binaries.python, manifest, opts);
182
- let dependencies = pythonController.getDependencies(true);
167
+ let dependencies = await pythonController.getDependencies(true);
183
168
  let sbom = new Sbom();
184
169
  const rootPurl = toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION);
185
170
  sbom.addRoot(rootPurl);
186
171
  dependencies.forEach(dep => {
187
172
  addAllDependencies(rootPurl, dep, sbom);
188
173
  });
189
- let requirementTxtContent = fs.readFileSync(manifest).toString();
190
- handleIgnoredDependencies(requirementTxtContent, sbom, opts);
174
+ await handleIgnoredDependencies(manifest, sbom, opts);
191
175
  // 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
176
  // sbom.removeRootComponent()
193
177
  return sbom.getAsJsonString(opts);
@@ -196,22 +180,21 @@ function createSbomStackAnalysis(manifest, opts = {}) {
196
180
  * Create a sbom json string out of a manifest content for component analysis
197
181
  * @param {string} manifest - path to requirements.txt
198
182
  * @param {{}} [opts={}] - optional various options to pass along the application
199
- * @returns {string} the sbom json string content
183
+ * @returns {Promise<string>} the sbom json string content
200
184
  * @private
201
185
  */
202
- function getSbomForComponentAnalysis(manifest, opts = {}) {
186
+ async function getSbomForComponentAnalysis(manifest, opts = {}) {
203
187
  let binaries = {};
204
188
  let createVirtualPythonEnv = handlePythonEnvironment(binaries, opts);
205
189
  let pythonController = new Python_controller(createVirtualPythonEnv === "false", binaries.pip, binaries.python, manifest, opts);
206
- let dependencies = pythonController.getDependencies(false);
190
+ let dependencies = await pythonController.getDependencies(false);
207
191
  let sbom = new Sbom();
208
192
  const rootPurl = toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION);
209
193
  sbom.addRoot(rootPurl);
210
194
  dependencies.forEach(dep => {
211
195
  sbom.addDependency(rootPurl, toPurl(dep.name, dep.version));
212
196
  });
213
- let requirementTxtContent = fs.readFileSync(manifest).toString();
214
- handleIgnoredDependencies(requirementTxtContent, sbom, opts);
197
+ await handleIgnoredDependencies(manifest, sbom, opts);
215
198
  // 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
199
  // sbom.removeRootComponent()
217
200
  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,27 @@
1
+ import { createRequire } from 'node:module';
2
+ import { Language, Parser, Query } from 'web-tree-sitter';
3
+ const require = createRequire(import.meta.url);
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'));
11
+ }
12
+ export async function getParser() {
13
+ const language = await init();
14
+ return new Parser().setLanguage(language);
15
+ }
16
+ export async function getRequirementQuery() {
17
+ const language = await init();
18
+ return new Query(language, '(requirement (package) @name) @req');
19
+ }
20
+ export async function getIgnoreQuery() {
21
+ const language = await init();
22
+ return new Query(language, '((requirement (package) @name) @req . (comment) @comment (#match? @comment "^#[\\t ]*exhortignore"))');
23
+ }
24
+ export async function getPinnedVersionQuery() {
25
+ const language = await init();
26
+ return new Query(language, '(version_spec (version_cmp) @cmp (version) @version (#eq? @cmp "=="))');
27
+ }
package/dist/src/tools.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { execFileSync } from "child_process";
2
2
  import { EOL } from "os";
3
3
  import { PackageURL } from "packageurl-js";
4
- export const RegexNotToBeLogged = /TRUSTIFY_DA_.*_TOKEN|ex-.*-token/;
4
+ export const RegexNotToBeLogged = /TRUSTIFY_DA_(.*_)?TOKEN|ex-.*-token|trust-.*-token/;
5
5
  /**
6
6
  *
7
7
  * @param {string} key to log its value from environment variables and from opts, if it exists
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trustify-da/trustify-da-javascript-client",
3
- "version": "0.3.0-ea.62f6bc7",
3
+ "version": "0.3.0-ea.63ae5c2",
4
4
  "description": "Code-Ready Dependency Analytics JavaScript API.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/guacsec/trustify-da-javascript-client#README.md",
@@ -46,13 +46,16 @@
46
46
  "dependencies": {
47
47
  "@babel/core": "^7.23.2",
48
48
  "@cyclonedx/cyclonedx-library": "^6.13.0",
49
+ "eslint-import-resolver-typescript": "^4.4.4",
49
50
  "fast-toml": "^0.5.4",
50
51
  "fast-xml-parser": "^5.3.4",
51
52
  "help": "^3.0.2",
52
53
  "https-proxy-agent": "^7.0.6",
53
54
  "node-fetch": "^3.3.2",
54
55
  "packageurl-js": "~1.0.2",
55
- "yargs": "^17.7.2"
56
+ "tree-sitter-requirements": "github:Strum355/tree-sitter-requirements#d0261ee76b84253997fe70d7d397e78c006c3801",
57
+ "web-tree-sitter": "^0.26.6",
58
+ "yargs": "^18.0.0"
56
59
  },
57
60
  "devDependencies": {
58
61
  "@babel/core": "^7.23.2",
@@ -60,9 +63,10 @@
60
63
  "@types/node": "^20.17.30",
61
64
  "@types/which": "^3.0.4",
62
65
  "babel-plugin-rewire": "^1.2.0",
63
- "c8": "^10.1.3",
66
+ "c8": "^11.0.0",
64
67
  "chai": "^4.3.7",
65
68
  "eslint": "^8.42.0",
69
+ "eslint-import-resolver-typescript": "^4.4.4",
66
70
  "eslint-plugin-editorconfig": "^4.0.3",
67
71
  "eslint-plugin-import": "^2.29.1",
68
72
  "esmock": "^2.6.2",