@trustify-da/trustify-da-javascript-client 0.3.0-ea.0c4e031 → 0.3.0-ea.0fbc1c1
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/README.md +1 -1
- package/dist/src/analysis.d.ts +7 -0
- package/dist/src/analysis.js +23 -13
- package/dist/src/cli.js +106 -2
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +4 -0
- package/dist/src/providers/python_controller.js +4 -1
- package/dist/src/providers/rust_cargo.js +74 -15
- package/dist/src/remediate.d.ts +21 -0
- package/dist/src/remediate.js +149 -0
- package/dist/src/remediation.d.ts +57 -0
- package/dist/src/remediation.js +436 -0
- package/dist/src/remediation_report.d.ts +40 -0
- package/dist/src/remediation_report.js +159 -0
- package/dist/src/updaters/toml_updater.d.ts +33 -0
- package/dist/src/updaters/toml_updater.js +244 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -688,7 +688,7 @@ This increasing the chances and the probability that the automatic installation
|
|
|
688
688
|
###### Usage
|
|
689
689
|
A New setting is introduced - `TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS` (as both env variable/key in `options` object)
|
|
690
690
|
1. `TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS`="false" - install requirements.txt while respecting declared versions for all packages.
|
|
691
|
-
2. `TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS`="true" - install all packages from requirements.txt, not respecting the declared version, but trying to install a version tailored for the used python version. When using this setting, you must set
|
|
691
|
+
2. `TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS`="true" - install all packages from requirements.txt, not respecting the declared version, but trying to install a version tailored for the used python version. When using this setting, you must set `TRUSTIFY_DA_PYTHON_VIRTUAL_ENV` to 'true' and `MATCH_MANIFEST_VERSIONS` to 'false'.
|
|
692
692
|
|
|
693
693
|
##### Using `pipdeptree`
|
|
694
694
|
By default, The API algorithm will use native commands of PIP installer as data source to build the dependency tree.
|
package/dist/src/analysis.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ declare namespace _default {
|
|
|
6
6
|
export { requestStackBatch };
|
|
7
7
|
export { requestImages };
|
|
8
8
|
export { validateToken };
|
|
9
|
+
export { appendAnalysisQueryParams };
|
|
9
10
|
}
|
|
10
11
|
export default _default;
|
|
11
12
|
/**
|
|
@@ -57,3 +58,9 @@ declare function requestImages(imageRefs: Array<string>, url: string, html?: boo
|
|
|
57
58
|
* @return {Promise<number>} return the HTTP status Code of the response from the validate token request.
|
|
58
59
|
*/
|
|
59
60
|
declare function validateToken(url: any, opts?: any): Promise<number>;
|
|
61
|
+
/**
|
|
62
|
+
* Append common analysis query parameters (recommend, providers, sources) to a URL.
|
|
63
|
+
* @param {URL} url - the URL object to append query parameters to
|
|
64
|
+
* @param {import("index.js").Options} [opts={}] - options containing parameter overrides
|
|
65
|
+
*/
|
|
66
|
+
declare function appendAnalysisQueryParams(url: URL, opts?: any): void;
|
package/dist/src/analysis.js
CHANGED
|
@@ -6,7 +6,7 @@ import { generateImageSBOM, parseImageRef } from "./oci_image/utils.js";
|
|
|
6
6
|
import { addProxyAgent, getCustom, getTokenHeaders, TRUSTIFY_DA_OPERATION_TYPE_HEADER, TRUSTIFY_DA_PACKAGE_MANAGER_HEADER } from "./tools.js";
|
|
7
7
|
/** Media type for CycloneDX JSON batch payloads (batch-analysis API). */
|
|
8
8
|
export const CYCLONEDX_JSON_MEDIA_TYPE = 'application/vnd.cyclonedx+json';
|
|
9
|
-
export default { requestComponent, requestStack, requestStackBatch, requestImages, validateToken };
|
|
9
|
+
export default { requestComponent, requestStack, requestStackBatch, requestImages, validateToken, appendAnalysisQueryParams };
|
|
10
10
|
/**
|
|
11
11
|
* Send a stack analysis request and get the report as 'text/html' or 'application/json'.
|
|
12
12
|
* @param {import('./provider').Provider} provider - the provided data for constructing the request
|
|
@@ -41,9 +41,7 @@ async function requestStack(provider, manifest, url, html = false, opts = {}) {
|
|
|
41
41
|
body: provided.content
|
|
42
42
|
}, opts);
|
|
43
43
|
const finalUrl = new URL(`${url}/api/v5/analysis`);
|
|
44
|
-
|
|
45
|
-
finalUrl.searchParams.append('recommend', 'false');
|
|
46
|
-
}
|
|
44
|
+
appendAnalysisQueryParams(finalUrl, opts);
|
|
47
45
|
let resp = await fetch(finalUrl, fetchOptions);
|
|
48
46
|
let result;
|
|
49
47
|
if (resp.status === 200) {
|
|
@@ -101,9 +99,7 @@ async function requestComponent(provider, manifest, url, opts = {}) {
|
|
|
101
99
|
body: provided.content
|
|
102
100
|
}, opts);
|
|
103
101
|
const finalUrl = new URL(`${url}/api/v5/analysis`);
|
|
104
|
-
|
|
105
|
-
finalUrl.searchParams.append('recommend', 'false');
|
|
106
|
-
}
|
|
102
|
+
appendAnalysisQueryParams(finalUrl, opts);
|
|
107
103
|
let resp = await fetch(finalUrl, fetchOptions);
|
|
108
104
|
let result;
|
|
109
105
|
if (resp.status === 200) {
|
|
@@ -142,9 +138,7 @@ async function requestComponent(provider, manifest, url, opts = {}) {
|
|
|
142
138
|
*/
|
|
143
139
|
async function requestStackBatch(sbomByPurl, url, html = false, opts = {}) {
|
|
144
140
|
const finalUrl = new URL(`${url}/api/v5/batch-analysis`);
|
|
145
|
-
|
|
146
|
-
finalUrl.searchParams.append('recommend', 'false');
|
|
147
|
-
}
|
|
141
|
+
appendAnalysisQueryParams(finalUrl, opts);
|
|
148
142
|
const fetchOptions = addProxyAgent({
|
|
149
143
|
method: 'POST',
|
|
150
144
|
headers: {
|
|
@@ -192,9 +186,7 @@ async function requestImages(imageRefs, url, html = false, opts = {}) {
|
|
|
192
186
|
imageSboms[parsedImageRef.getPackageURL().toString()] = generateImageSBOM(parsedImageRef, opts);
|
|
193
187
|
}
|
|
194
188
|
const finalUrl = new URL(`${url}/api/v5/batch-analysis`);
|
|
195
|
-
|
|
196
|
-
finalUrl.searchParams.append('recommend', 'false');
|
|
197
|
-
}
|
|
189
|
+
appendAnalysisQueryParams(finalUrl, opts);
|
|
198
190
|
const fetchOptions = addProxyAgent({
|
|
199
191
|
method: 'POST',
|
|
200
192
|
headers: {
|
|
@@ -228,6 +220,24 @@ async function requestImages(imageRefs, url, html = false, opts = {}) {
|
|
|
228
220
|
throw new Error(`Got error response from Trustify DA backend - http return code : ${resp.status}, ex-request-id: ${resp.headers.get("ex-request-id")} error message => ${await resp.text()}`);
|
|
229
221
|
}
|
|
230
222
|
}
|
|
223
|
+
/**
|
|
224
|
+
* Append common analysis query parameters (recommend, providers, sources) to a URL.
|
|
225
|
+
* @param {URL} url - the URL object to append query parameters to
|
|
226
|
+
* @param {import("index.js").Options} [opts={}] - options containing parameter overrides
|
|
227
|
+
*/
|
|
228
|
+
function appendAnalysisQueryParams(url, opts = {}) {
|
|
229
|
+
if (getCustom('TRUSTIFY_DA_RECOMMEND', 'true', opts) === 'false') {
|
|
230
|
+
url.searchParams.append('recommend', 'false');
|
|
231
|
+
}
|
|
232
|
+
const providers = getCustom('TRUSTIFY_DA_PROVIDERS', null, opts);
|
|
233
|
+
if (providers) {
|
|
234
|
+
url.searchParams.append('providers', providers);
|
|
235
|
+
}
|
|
236
|
+
const sources = getCustom('TRUSTIFY_DA_SOURCES', null, opts);
|
|
237
|
+
if (sources) {
|
|
238
|
+
url.searchParams.append('sources', sources);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
231
241
|
/**
|
|
232
242
|
*
|
|
233
243
|
* @param url the backend url to send the request to
|
package/dist/src/cli.js
CHANGED
|
@@ -4,6 +4,7 @@ import * as path from "path";
|
|
|
4
4
|
import yargs from 'yargs';
|
|
5
5
|
import { hideBin } from 'yargs/helpers';
|
|
6
6
|
import { getProjectLicense, getLicenseDetails } from './license/index.js';
|
|
7
|
+
import { runRemediation } from './remediate.js';
|
|
7
8
|
import client, { selectTrustifyDABackend, generateSbom } from './index.js';
|
|
8
9
|
// command for component analysis take manifest type and content
|
|
9
10
|
const component = {
|
|
@@ -19,11 +20,25 @@ const component = {
|
|
|
19
20
|
desc: 'Workspace root directory (for monorepos; lock file is expected here)',
|
|
20
21
|
type: 'string',
|
|
21
22
|
normalize: true,
|
|
23
|
+
},
|
|
24
|
+
providers: {
|
|
25
|
+
desc: 'Comma-separated list of vulnerability providers (env: TRUSTIFY_DA_PROVIDERS)',
|
|
26
|
+
type: 'string',
|
|
27
|
+
},
|
|
28
|
+
sources: {
|
|
29
|
+
desc: 'Comma-separated list of vulnerability sources (env: TRUSTIFY_DA_SOURCES)',
|
|
30
|
+
type: 'string',
|
|
22
31
|
}
|
|
23
32
|
}),
|
|
24
33
|
handler: async (args) => {
|
|
25
34
|
let manifestName = args['/path/to/manifest'];
|
|
26
35
|
const opts = args.workspaceDir ? { TRUSTIFY_DA_WORKSPACE_DIR: args.workspaceDir } : {};
|
|
36
|
+
if (args.providers) {
|
|
37
|
+
opts.TRUSTIFY_DA_PROVIDERS = args.providers;
|
|
38
|
+
}
|
|
39
|
+
if (args.sources) {
|
|
40
|
+
opts.TRUSTIFY_DA_SOURCES = args.sources;
|
|
41
|
+
}
|
|
27
42
|
let res = await client.componentAnalysis(manifestName, opts);
|
|
28
43
|
console.log(JSON.stringify(res, null, 2));
|
|
29
44
|
}
|
|
@@ -72,6 +87,14 @@ const image = {
|
|
|
72
87
|
desc: 'For JSON report, get only the \'summary\'',
|
|
73
88
|
type: 'boolean',
|
|
74
89
|
conflicts: 'html'
|
|
90
|
+
},
|
|
91
|
+
providers: {
|
|
92
|
+
desc: 'Comma-separated list of vulnerability providers (env: TRUSTIFY_DA_PROVIDERS)',
|
|
93
|
+
type: 'string',
|
|
94
|
+
},
|
|
95
|
+
sources: {
|
|
96
|
+
desc: 'Comma-separated list of vulnerability sources (env: TRUSTIFY_DA_SOURCES)',
|
|
97
|
+
type: 'string',
|
|
75
98
|
}
|
|
76
99
|
}),
|
|
77
100
|
handler: async (args) => {
|
|
@@ -81,7 +104,14 @@ const image = {
|
|
|
81
104
|
}
|
|
82
105
|
let html = args['html'];
|
|
83
106
|
let summary = args['summary'];
|
|
84
|
-
|
|
107
|
+
const opts = {};
|
|
108
|
+
if (args.providers) {
|
|
109
|
+
opts.TRUSTIFY_DA_PROVIDERS = args.providers;
|
|
110
|
+
}
|
|
111
|
+
if (args.sources) {
|
|
112
|
+
opts.TRUSTIFY_DA_SOURCES = args.sources;
|
|
113
|
+
}
|
|
114
|
+
let res = await client.imageAnalysis(imageRefs, html, opts);
|
|
85
115
|
if (summary && !html) {
|
|
86
116
|
let summaries = {};
|
|
87
117
|
for (let [imageRef, report] of Object.entries(res)) {
|
|
@@ -132,6 +162,14 @@ const stack = {
|
|
|
132
162
|
desc: 'Workspace root directory (for monorepos; lock file is expected here)',
|
|
133
163
|
type: 'string',
|
|
134
164
|
normalize: true,
|
|
165
|
+
},
|
|
166
|
+
providers: {
|
|
167
|
+
desc: 'Comma-separated list of vulnerability providers (env: TRUSTIFY_DA_PROVIDERS)',
|
|
168
|
+
type: 'string',
|
|
169
|
+
},
|
|
170
|
+
sources: {
|
|
171
|
+
desc: 'Comma-separated list of vulnerability sources (env: TRUSTIFY_DA_SOURCES)',
|
|
172
|
+
type: 'string',
|
|
135
173
|
}
|
|
136
174
|
}),
|
|
137
175
|
handler: async (args) => {
|
|
@@ -139,6 +177,12 @@ const stack = {
|
|
|
139
177
|
let html = args['html'];
|
|
140
178
|
let summary = args['summary'];
|
|
141
179
|
const opts = args.workspaceDir ? { TRUSTIFY_DA_WORKSPACE_DIR: args.workspaceDir } : {};
|
|
180
|
+
if (args.providers) {
|
|
181
|
+
opts.TRUSTIFY_DA_PROVIDERS = args.providers;
|
|
182
|
+
}
|
|
183
|
+
if (args.sources) {
|
|
184
|
+
opts.TRUSTIFY_DA_SOURCES = args.sources;
|
|
185
|
+
}
|
|
142
186
|
let theProvidersSummary = new Map();
|
|
143
187
|
let theProvidersObject = {};
|
|
144
188
|
let res = await client.stackAnalysis(manifest, html, opts);
|
|
@@ -201,6 +245,14 @@ const stackBatch = {
|
|
|
201
245
|
desc: 'Stop on first invalid package.json or SBOM error (env: TRUSTIFY_DA_CONTINUE_ON_ERROR=false)',
|
|
202
246
|
type: 'boolean',
|
|
203
247
|
default: false,
|
|
248
|
+
},
|
|
249
|
+
providers: {
|
|
250
|
+
desc: 'Comma-separated list of vulnerability providers (env: TRUSTIFY_DA_PROVIDERS)',
|
|
251
|
+
type: 'string',
|
|
252
|
+
},
|
|
253
|
+
sources: {
|
|
254
|
+
desc: 'Comma-separated list of vulnerability sources (env: TRUSTIFY_DA_SOURCES)',
|
|
255
|
+
type: 'string',
|
|
204
256
|
}
|
|
205
257
|
}),
|
|
206
258
|
handler: async (args) => {
|
|
@@ -221,6 +273,12 @@ const stackBatch = {
|
|
|
221
273
|
if (args.failFast) {
|
|
222
274
|
opts.continueOnError = false;
|
|
223
275
|
}
|
|
276
|
+
if (args.providers) {
|
|
277
|
+
opts.TRUSTIFY_DA_PROVIDERS = args.providers;
|
|
278
|
+
}
|
|
279
|
+
if (args.sources) {
|
|
280
|
+
opts.TRUSTIFY_DA_SOURCES = args.sources;
|
|
281
|
+
}
|
|
224
282
|
let res = await client.stackAnalysisBatch(workspaceRoot, html, opts);
|
|
225
283
|
const batchAnalysis = res && typeof res === 'object' && res != null && 'analysis' in res ? res.analysis : res;
|
|
226
284
|
if (summary && !html && typeof batchAnalysis === 'object') {
|
|
@@ -373,9 +431,54 @@ const sbom = {
|
|
|
373
431
|
}
|
|
374
432
|
}
|
|
375
433
|
};
|
|
434
|
+
const remediate = {
|
|
435
|
+
command: 'remediate <path>',
|
|
436
|
+
desc: 'Scan and apply vulnerability remediations to manifest files',
|
|
437
|
+
builder: yargs => yargs.positional('path', {
|
|
438
|
+
desc: 'Path to manifest file or directory',
|
|
439
|
+
type: 'string',
|
|
440
|
+
normalize: true,
|
|
441
|
+
}).options({
|
|
442
|
+
'dry-run': {
|
|
443
|
+
alias: 'd',
|
|
444
|
+
type: 'boolean',
|
|
445
|
+
desc: 'Preview changes without modifying files',
|
|
446
|
+
},
|
|
447
|
+
providers: {
|
|
448
|
+
desc: 'Comma-separated list of vulnerability providers (env: TRUSTIFY_DA_PROVIDERS)',
|
|
449
|
+
type: 'string',
|
|
450
|
+
},
|
|
451
|
+
sources: {
|
|
452
|
+
desc: 'Comma-separated list of vulnerability sources (env: TRUSTIFY_DA_SOURCES)',
|
|
453
|
+
type: 'string',
|
|
454
|
+
},
|
|
455
|
+
'group-by': {
|
|
456
|
+
type: 'string',
|
|
457
|
+
choices: ['dependency', 'bundle'],
|
|
458
|
+
default: 'dependency',
|
|
459
|
+
desc: 'Report grouping strategy',
|
|
460
|
+
},
|
|
461
|
+
}),
|
|
462
|
+
handler: async (args) => {
|
|
463
|
+
try {
|
|
464
|
+
const result = await runRemediation(args.path, {
|
|
465
|
+
dryRun: args['dry-run'],
|
|
466
|
+
providers: args.providers,
|
|
467
|
+
sources: args.sources,
|
|
468
|
+
groupBy: args['group-by'],
|
|
469
|
+
});
|
|
470
|
+
console.log(result.output);
|
|
471
|
+
process.exit(result.exitCode);
|
|
472
|
+
}
|
|
473
|
+
catch (err) {
|
|
474
|
+
console.error(err.message);
|
|
475
|
+
process.exit(1);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
};
|
|
376
479
|
// parse and invoke the command
|
|
377
480
|
yargs(hideBin(process.argv))
|
|
378
|
-
.usage(`Usage: ${process.argv[0].includes("node") ? path.parse(process.argv[1]).base : path.parse(process.argv[0]).base} {component|stack|stack-batch|image|validate-token|license|sbom}`)
|
|
481
|
+
.usage(`Usage: ${process.argv[0].includes("node") ? path.parse(process.argv[1]).base : path.parse(process.argv[0]).base} {component|stack|stack-batch|image|validate-token|license|sbom|remediate}`)
|
|
379
482
|
.command(stack)
|
|
380
483
|
.command(stackBatch)
|
|
381
484
|
.command(component)
|
|
@@ -383,6 +486,7 @@ yargs(hideBin(process.argv))
|
|
|
383
486
|
.command(validateToken)
|
|
384
487
|
.command(license)
|
|
385
488
|
.command(sbom)
|
|
489
|
+
.command(remediate)
|
|
386
490
|
.scriptName('')
|
|
387
491
|
.version(false)
|
|
388
492
|
.demandCommand(1)
|
package/dist/src/index.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export function selectTrustifyDABackend(opts?: {
|
|
|
24
24
|
export function generateSbom(manifestPath: string, opts?: Options): Promise<object>;
|
|
25
25
|
export { parseImageRef } from "./oci_image/utils.js";
|
|
26
26
|
export { ImageRef } from "./oci_image/images.js";
|
|
27
|
+
export { extractRemediations } from "./remediation.js";
|
|
27
28
|
declare namespace _default {
|
|
28
29
|
export { componentAnalysis };
|
|
29
30
|
export { stackAnalysis };
|
|
@@ -53,7 +54,9 @@ export type Options = {
|
|
|
53
54
|
TRUSTIFY_DA_PYTHON_PATH?: string | undefined;
|
|
54
55
|
TRUSTIFY_DA_PYTHON_VIRTUAL_ENV?: string | undefined;
|
|
55
56
|
TRUSTIFY_DA_PYTHON3_PATH?: string | undefined;
|
|
57
|
+
TRUSTIFY_DA_PROVIDERS?: string | undefined;
|
|
56
58
|
TRUSTIFY_DA_RECOMMEND?: string | undefined;
|
|
59
|
+
TRUSTIFY_DA_SOURCES?: string | undefined;
|
|
57
60
|
TRUSTIFY_DA_SKOPEO_CONFIG_PATH?: string | undefined;
|
|
58
61
|
TRUSTIFY_DA_SKOPEO_PATH?: string | undefined;
|
|
59
62
|
TRUSTIFY_DA_SYFT_CONFIG_PATH?: string | undefined;
|
|
@@ -264,3 +267,4 @@ import { resolveContinueOnError } from './batch_opts.js';
|
|
|
264
267
|
import { resolveBatchMetadata } from './batch_opts.js';
|
|
265
268
|
export { discoverMavenModules, discoverGradleSubprojects, discoverGoWorkspaceModules, discoverUvWorkspaceMembers, discoverWorkspacePackages, discoverWorkspaceCrates, validatePackageJson, resolveWorkspaceDiscoveryIgnore, filterManifestPathsByDiscoveryIgnore, resolveContinueOnError, resolveBatchMetadata };
|
|
266
269
|
export { getProjectLicense, findLicenseFilePath, identifyLicense, getLicenseDetails, licensesFromReport, normalizeLicensesResponse, runLicenseCheck, getCompatibility } from "./license/index.js";
|
|
270
|
+
export { generateReport, generateDeduplicationKey } from "./remediation_report.js";
|
package/dist/src/index.js
CHANGED
|
@@ -16,6 +16,8 @@ import * as url from 'url';
|
|
|
16
16
|
export { parseImageRef } from "./oci_image/utils.js";
|
|
17
17
|
export { ImageRef } from "./oci_image/images.js";
|
|
18
18
|
export { getProjectLicense, findLicenseFilePath, identifyLicense, getLicenseDetails, licensesFromReport, normalizeLicensesResponse, runLicenseCheck, getCompatibility } from "./license/index.js";
|
|
19
|
+
export { extractRemediations } from "./remediation.js";
|
|
20
|
+
export { generateReport, generateDeduplicationKey } from './remediation_report.js';
|
|
19
21
|
export default { componentAnalysis, stackAnalysis, stackAnalysisBatch, imageAnalysis, validateToken, generateSbom };
|
|
20
22
|
export { discoverMavenModules, discoverGradleSubprojects, discoverGoWorkspaceModules, discoverUvWorkspaceMembers, discoverWorkspacePackages, discoverWorkspaceCrates, validatePackageJson, resolveWorkspaceDiscoveryIgnore, filterManifestPathsByDiscoveryIgnore, resolveContinueOnError, resolveBatchMetadata, };
|
|
21
23
|
/**
|
|
@@ -39,7 +41,9 @@ export { discoverMavenModules, discoverGradleSubprojects, discoverGoWorkspaceMod
|
|
|
39
41
|
* TRUSTIFY_DA_PYTHON_PATH?: string | undefined,
|
|
40
42
|
* TRUSTIFY_DA_PYTHON_VIRTUAL_ENV?: string | undefined,
|
|
41
43
|
* TRUSTIFY_DA_PYTHON3_PATH?: string | undefined,
|
|
44
|
+
* TRUSTIFY_DA_PROVIDERS?: string | undefined,
|
|
42
45
|
* TRUSTIFY_DA_RECOMMEND?: string | undefined,
|
|
46
|
+
* TRUSTIFY_DA_SOURCES?: string | undefined,
|
|
43
47
|
* TRUSTIFY_DA_SKOPEO_CONFIG_PATH?: string | undefined,
|
|
44
48
|
* TRUSTIFY_DA_SKOPEO_PATH?: string | undefined,
|
|
45
49
|
* TRUSTIFY_DA_SYFT_CONFIG_PATH?: string | undefined,
|
|
@@ -178,8 +178,11 @@ export default class Python_controller {
|
|
|
178
178
|
startingTime = new Date();
|
|
179
179
|
console.log("Starting time to get requirements.txt dependency tree = " + startingTime);
|
|
180
180
|
}
|
|
181
|
+
let installBestEfforts = getCustom("TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS", "false", this.options);
|
|
182
|
+
if (installBestEfforts === "true" && this.realEnvironment) {
|
|
183
|
+
throw new Error("Conflicting settings, TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS=true requires TRUSTIFY_DA_PYTHON_VIRTUAL_ENV=true");
|
|
184
|
+
}
|
|
181
185
|
if (!this.realEnvironment) {
|
|
182
|
-
let installBestEfforts = getCustom("TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS", "false", this.options);
|
|
183
186
|
if (installBestEfforts === "false") {
|
|
184
187
|
try {
|
|
185
188
|
invokeCommand(this.pathToPipBin, ['install', '-r', this.pathToRequirements]);
|
|
@@ -201,12 +201,13 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
|
|
|
201
201
|
let ignoredDeps = getIgnoredDeps(manifest, metadata);
|
|
202
202
|
let crateType = detectCrateType(metadata);
|
|
203
203
|
let license = readLicenseFromManifest(manifest, metadata);
|
|
204
|
+
let hashMap = parseCargoLockHashes(manifestDir);
|
|
204
205
|
let sbom;
|
|
205
206
|
if (crateType === CrateType.WORKSPACE_VIRTUAL) {
|
|
206
|
-
sbom = handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license);
|
|
207
|
+
sbom = handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license, hashMap);
|
|
207
208
|
}
|
|
208
209
|
else {
|
|
209
|
-
sbom = handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license);
|
|
210
|
+
sbom = handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license, hashMap);
|
|
210
211
|
}
|
|
211
212
|
return sbom;
|
|
212
213
|
}
|
|
@@ -244,6 +245,63 @@ function executeCargoMetadata(cargoBin, manifestDir) {
|
|
|
244
245
|
throw new Error('failed to execute cargo metadata', { cause: error });
|
|
245
246
|
}
|
|
246
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* Parses Cargo.lock to extract SHA-256 checksums for each package.
|
|
250
|
+
* @param {string} manifestDir - directory containing Cargo.toml (Cargo.lock is searched upward)
|
|
251
|
+
* @returns {Map<string, Array<{alg: string, content: string}>>} map of "name@version" to CycloneDX hashes
|
|
252
|
+
* @private
|
|
253
|
+
*/
|
|
254
|
+
function parseCargoLockHashes(manifestDir) {
|
|
255
|
+
let hashMap = new Map();
|
|
256
|
+
let lockPath = findCargoLock(manifestDir);
|
|
257
|
+
if (!lockPath) {
|
|
258
|
+
return hashMap;
|
|
259
|
+
}
|
|
260
|
+
try {
|
|
261
|
+
let content = fs.readFileSync(lockPath, 'utf-8');
|
|
262
|
+
let parsed = parseToml(content);
|
|
263
|
+
for (let pkg of (parsed.package || [])) {
|
|
264
|
+
if (pkg.name && pkg.version && pkg.checksum) {
|
|
265
|
+
hashMap.set(`${pkg.name}@${pkg.version}`, [{ alg: 'SHA-256', content: pkg.checksum }]);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
console.warn('Failed to parse Cargo.lock for hashes, SBOM will be generated without hashes');
|
|
271
|
+
}
|
|
272
|
+
return hashMap;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Searches upward from the manifest directory to find the nearest Cargo.lock.
|
|
276
|
+
* @param {string} manifestDir - starting directory
|
|
277
|
+
* @returns {string|null} absolute path to Cargo.lock or null if not found
|
|
278
|
+
* @private
|
|
279
|
+
*/
|
|
280
|
+
function findCargoLock(manifestDir) {
|
|
281
|
+
let dir = path.resolve(manifestDir);
|
|
282
|
+
let parent = dir;
|
|
283
|
+
do {
|
|
284
|
+
dir = parent;
|
|
285
|
+
let lockFile = path.join(dir, 'Cargo.lock');
|
|
286
|
+
if (fs.existsSync(lockFile)) {
|
|
287
|
+
return lockFile;
|
|
288
|
+
}
|
|
289
|
+
let cargoToml = path.join(dir, 'Cargo.toml');
|
|
290
|
+
if (fs.existsSync(cargoToml)) {
|
|
291
|
+
try {
|
|
292
|
+
let content = fs.readFileSync(cargoToml, 'utf-8');
|
|
293
|
+
if (/\[workspace\]/.test(content)) {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
catch (_) {
|
|
298
|
+
// ignore read errors, keep searching
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
parent = path.dirname(dir);
|
|
302
|
+
} while (parent !== dir);
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
247
305
|
/**
|
|
248
306
|
* Detects the type of Cargo project from metadata.
|
|
249
307
|
* @param {object} metadata - parsed cargo metadata
|
|
@@ -275,7 +333,7 @@ function detectCrateType(metadata) {
|
|
|
275
333
|
* @returns {string} SBOM json string
|
|
276
334
|
* @private
|
|
277
335
|
*/
|
|
278
|
-
function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license) {
|
|
336
|
+
function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, license, hashMap) {
|
|
279
337
|
let rootPackageId = metadata.resolve.root;
|
|
280
338
|
let rootPackage = findPackageById(metadata, rootPackageId);
|
|
281
339
|
let rootPurl = toPurl(rootPackage.name, rootPackage.version);
|
|
@@ -286,10 +344,10 @@ function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, licen
|
|
|
286
344
|
return sbom.getAsJsonString(opts);
|
|
287
345
|
}
|
|
288
346
|
if (includeTransitive) {
|
|
289
|
-
addTransitiveDeps(sbom, metadata, rootPackageId, ignoredDeps, new Set(), rootPurl);
|
|
347
|
+
addTransitiveDeps(sbom, metadata, rootPackageId, ignoredDeps, new Set(), hashMap, rootPurl);
|
|
290
348
|
}
|
|
291
349
|
else {
|
|
292
|
-
addDirectDeps(sbom, metadata, rootPackageId, rootPurl, ignoredDeps);
|
|
350
|
+
addDirectDeps(sbom, metadata, rootPackageId, rootPurl, ignoredDeps, hashMap);
|
|
293
351
|
}
|
|
294
352
|
return sbom.getAsJsonString(opts);
|
|
295
353
|
}
|
|
@@ -315,7 +373,7 @@ function handleSingleCrate(metadata, ignoredDeps, includeTransitive, opts, licen
|
|
|
315
373
|
* @returns {string} SBOM json string
|
|
316
374
|
* @private
|
|
317
375
|
*/
|
|
318
|
-
function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license) {
|
|
376
|
+
function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransitive, opts, license, hashMap) {
|
|
319
377
|
let workspaceRoot = metadata.workspace_root;
|
|
320
378
|
let rootName = path.basename(workspaceRoot);
|
|
321
379
|
let workspaceVersion = getWorkspaceVersion(metadata);
|
|
@@ -323,7 +381,6 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
323
381
|
let sbom = new Sbom();
|
|
324
382
|
sbom.addRoot(rootPurl, license);
|
|
325
383
|
if (includeTransitive) {
|
|
326
|
-
// Stack analysis: walk all members and their full dependency trees
|
|
327
384
|
let workspaceMembers = metadata.workspace_members || [];
|
|
328
385
|
for (let memberId of workspaceMembers) {
|
|
329
386
|
let memberPackage = findPackageById(metadata, memberId);
|
|
@@ -334,11 +391,10 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
334
391
|
? toPathDepPurl(memberPackage.name, memberPackage.version)
|
|
335
392
|
: toPurl(memberPackage.name, memberPackage.version);
|
|
336
393
|
sbom.addDependency(rootPurl, memberPurl);
|
|
337
|
-
addTransitiveDeps(sbom, metadata, memberId, ignoredDeps, new Set(), memberPurl);
|
|
394
|
+
addTransitiveDeps(sbom, metadata, memberId, ignoredDeps, new Set(), hashMap, memberPurl);
|
|
338
395
|
}
|
|
339
396
|
}
|
|
340
397
|
else {
|
|
341
|
-
// Component analysis: only [workspace.dependencies] from root Cargo.toml
|
|
342
398
|
let workspaceDeps = getWorkspaceDepsFromManifest(manifest);
|
|
343
399
|
for (let depName of workspaceDeps) {
|
|
344
400
|
if (isDepIgnored(depName, ignoredDeps)) {
|
|
@@ -355,7 +411,8 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
355
411
|
let depPurl = pkg.source == null
|
|
356
412
|
? toPathDepPurl(pkg.name, pkg.version)
|
|
357
413
|
: toPurl(pkg.name, pkg.version);
|
|
358
|
-
|
|
414
|
+
let hashes = hashMap.get(`${pkg.name}@${pkg.version}`);
|
|
415
|
+
sbom.addDependency(rootPurl, depPurl, undefined, hashes);
|
|
359
416
|
}
|
|
360
417
|
}
|
|
361
418
|
return sbom.getAsJsonString(opts);
|
|
@@ -374,7 +431,7 @@ function handleVirtualWorkspace(manifest, metadata, ignoredDeps, includeTransiti
|
|
|
374
431
|
* so callers can ensure it matches the purl already added to the SBOM
|
|
375
432
|
* @private
|
|
376
433
|
*/
|
|
377
|
-
function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, startingPurl) {
|
|
434
|
+
function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, hashMap, startingPurl) {
|
|
378
435
|
if (visited.has(packageId)) {
|
|
379
436
|
return;
|
|
380
437
|
}
|
|
@@ -402,8 +459,9 @@ function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, star
|
|
|
402
459
|
let depPurl = depPackage.source == null
|
|
403
460
|
? toPathDepPurl(depPackage.name, depPackage.version)
|
|
404
461
|
: toPurl(depPackage.name, depPackage.version);
|
|
405
|
-
|
|
406
|
-
|
|
462
|
+
let hashes = hashMap ? hashMap.get(`${depPackage.name}@${depPackage.version}`) : undefined;
|
|
463
|
+
sbom.addDependency(sourcePurl, depPurl, undefined, hashes);
|
|
464
|
+
addTransitiveDeps(sbom, metadata, depId, ignoredDeps, visited, hashMap);
|
|
407
465
|
}
|
|
408
466
|
}
|
|
409
467
|
/**
|
|
@@ -416,7 +474,7 @@ function addTransitiveDeps(sbom, metadata, packageId, ignoredDeps, visited, star
|
|
|
416
474
|
* @param {Set<string>} ignoredDeps - set of ignored dependency names
|
|
417
475
|
* @private
|
|
418
476
|
*/
|
|
419
|
-
function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps) {
|
|
477
|
+
function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps, hashMap) {
|
|
420
478
|
let resolveNode = findResolveNode(metadata, packageId);
|
|
421
479
|
if (!resolveNode) {
|
|
422
480
|
return;
|
|
@@ -433,7 +491,8 @@ function addDirectDeps(sbom, metadata, packageId, parentPurl, ignoredDeps) {
|
|
|
433
491
|
let depPurl = depPackage.source == null
|
|
434
492
|
? toPathDepPurl(depPackage.name, depPackage.version)
|
|
435
493
|
: toPurl(depPackage.name, depPackage.version);
|
|
436
|
-
|
|
494
|
+
let hashes = hashMap ? hashMap.get(`${depPackage.name}@${depPackage.version}`) : undefined;
|
|
495
|
+
sbom.addDependency(parentPurl, depPurl, undefined, hashes);
|
|
437
496
|
}
|
|
438
497
|
}
|
|
439
498
|
/**
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orchestrates the full remediation pipeline for a single manifest or directory:
|
|
3
|
+
* discover manifests → scan via DA backend → extract remediations → apply or preview.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} targetPath - path to a manifest file or directory
|
|
6
|
+
* @param {object} [options]
|
|
7
|
+
* @param {boolean} [options.dryRun=false] - preview changes without modifying files (applies by default)
|
|
8
|
+
* @param {string} [options.providers] - comma-separated provider list
|
|
9
|
+
* @param {string} [options.sources] - comma-separated source list
|
|
10
|
+
* @param {'dependency'|'bundle'} [options.groupBy='dependency'] - report grouping strategy
|
|
11
|
+
* @returns {Promise<{exitCode: number, output: string}>}
|
|
12
|
+
*/
|
|
13
|
+
export function runRemediation(targetPath: string, options?: {
|
|
14
|
+
dryRun?: boolean | undefined;
|
|
15
|
+
providers?: string | undefined;
|
|
16
|
+
sources?: string | undefined;
|
|
17
|
+
groupBy?: "dependency" | "bundle" | undefined;
|
|
18
|
+
}): Promise<{
|
|
19
|
+
exitCode: number;
|
|
20
|
+
output: string;
|
|
21
|
+
}>;
|