@trustify-da/trustify-da-javascript-client 0.3.0-ea.0c4e031 → 0.3.0-ea.0d78462
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 +176 -2
- 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/cyclone_dx_sbom.d.ts +13 -0
- package/dist/src/cyclone_dx_sbom.js +24 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +4 -0
- package/dist/src/providers/base_java.d.ts +53 -0
- package/dist/src/providers/base_java.js +64 -17
- package/dist/src/providers/golang_gomodules.js +43 -2
- package/dist/src/providers/java_maven.d.ts +28 -3
- package/dist/src/providers/java_maven.js +96 -7
- 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/sbom.d.ts +11 -0
- package/dist/src/sbom.js +10 -0
- package/dist/src/tools.js +24 -1
- package/dist/src/updaters/toml_updater.d.ts +33 -0
- package/dist/src/updaters/toml_updater.js +244 -0
- package/package.json +1 -1
|
@@ -14,6 +14,13 @@ export const ecosystem_gradle = 'gradle';
|
|
|
14
14
|
export default class Base_Java {
|
|
15
15
|
DEP_REGEX = /(([-a-zA-Z0-9._]{2,})|[0-9])/g;
|
|
16
16
|
CONFLICT_REGEX = /.*omitted for conflict with (\S+)\)/;
|
|
17
|
+
/**
|
|
18
|
+
* Maven dependency scopes. Used to detect whether the trailing column of a
|
|
19
|
+
* dependency-tree line is a scope keyword — which in turn signals that an
|
|
20
|
+
* optional classifier column is present between the packaging and version.
|
|
21
|
+
* @type {string[]}
|
|
22
|
+
*/
|
|
23
|
+
static MAVEN_SCOPES = ['compile', 'provided', 'runtime', 'test', 'system', 'import'];
|
|
17
24
|
globalBinary;
|
|
18
25
|
localWrapper;
|
|
19
26
|
/**
|
|
@@ -82,31 +89,71 @@ export default class Base_Java {
|
|
|
82
89
|
}
|
|
83
90
|
return ((line.indexOf('-') - 1) / 3) + 1;
|
|
84
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Parse a single dependency-tree line into its Maven coordinate parts.
|
|
94
|
+
*
|
|
95
|
+
* This is the single source of truth for interpreting a dependency-tree
|
|
96
|
+
* line. Both {@link parseDep} (used to build SBOM component PURLs) and the
|
|
97
|
+
* Maven hash-map builder rely on it, so the PURL key and the artifact file
|
|
98
|
+
* path can never drift from one another.
|
|
99
|
+
*
|
|
100
|
+
* A line has the shape `groupId:artifactId:packaging[:classifier]:version[:scope]`.
|
|
101
|
+
* The classifier column is only present when a sixth column holds a known
|
|
102
|
+
* Maven scope keyword; otherwise the fourth column is the version.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} line - line to parse from a dependency tree
|
|
105
|
+
* @returns {{groupId: string, artifactId: string, packaging: string, classifier: (string|null), version: string, scope: (string|null), overridden: boolean}}
|
|
106
|
+
* Parsed coordinate. `version` is the resolved concrete version — when the
|
|
107
|
+
* line carries a conflict override the override version replaces it and
|
|
108
|
+
* `overridden` is set, mirroring how Maven records the winning version.
|
|
109
|
+
*/
|
|
110
|
+
parseCoordinate(line) {
|
|
111
|
+
const parts = line.split(':').map(part => part ? part.match(this.DEP_REGEX)?.[0] ?? '' : '');
|
|
112
|
+
const groupId = parts[0] ?? '';
|
|
113
|
+
const artifactId = parts[1] ?? '';
|
|
114
|
+
const packaging = parts[2] ?? '';
|
|
115
|
+
// A classifier column exists only when a sixth column holds a scope keyword.
|
|
116
|
+
const hasClassifier = parts.length >= 6 && Base_Java.MAVEN_SCOPES.includes(parts[5]);
|
|
117
|
+
const classifier = hasClassifier ? parts[3] : null;
|
|
118
|
+
let version = (hasClassifier ? parts[4] : parts[3]) ?? '';
|
|
119
|
+
const scope = hasClassifier ? parts[5] : (parts.length >= 5 ? parts[4] : null);
|
|
120
|
+
// A conflict override replaces the resolved version entirely.
|
|
121
|
+
const override = line.match(this.CONFLICT_REGEX);
|
|
122
|
+
const overridden = Boolean(override);
|
|
123
|
+
if (overridden) {
|
|
124
|
+
version = override[1];
|
|
125
|
+
}
|
|
126
|
+
return { groupId, artifactId, packaging, classifier, version, scope, overridden };
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Build the canonical PackageURL for a parsed coordinate.
|
|
130
|
+
*
|
|
131
|
+
* The classifier is folded into the version component (e.g.
|
|
132
|
+
* `4.1.0-linux-x86_64`) except when the version came from a conflict
|
|
133
|
+
* override, in which case the override version stands alone — matching the
|
|
134
|
+
* historical behavior of {@link parseDep}.
|
|
135
|
+
*
|
|
136
|
+
* @param {{groupId: string, artifactId: string, classifier: (string|null), version: string, overridden: boolean}} coord
|
|
137
|
+
* @returns {PackageURL} The canonical packageURL for the coordinate
|
|
138
|
+
* @protected
|
|
139
|
+
*/
|
|
140
|
+
_coordinateToPurl(coord) {
|
|
141
|
+
const purlVersion = (coord.classifier && !coord.overridden)
|
|
142
|
+
? `${coord.version}-${coord.classifier}`
|
|
143
|
+
: coord.version;
|
|
144
|
+
return this.toPurl(coord.groupId, coord.artifactId, purlVersion);
|
|
145
|
+
}
|
|
85
146
|
/**
|
|
86
147
|
* Create a PackageURL from any line in a Text Graph dependency tree for a manifest path.
|
|
87
148
|
* @param {string} line - line to parse from a dependencies.txt file
|
|
88
149
|
* @returns {PackageURL} The parsed packageURL
|
|
89
150
|
*/
|
|
90
151
|
parseDep(line) {
|
|
91
|
-
|
|
92
|
-
if (
|
|
93
|
-
throw new Error(`Unable generate SBOM from dependency tree. Line: ${line} cannot be parsed into a PackageURL`);
|
|
94
|
-
}
|
|
95
|
-
let version;
|
|
96
|
-
if (match.length >= 5 && ['compile', 'provided', 'runtime'].includes(match[5])) {
|
|
97
|
-
version = `${match[4]}-${match[3]}`;
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
version = match[3];
|
|
101
|
-
}
|
|
102
|
-
let override = line.match(this.CONFLICT_REGEX);
|
|
103
|
-
if (override) {
|
|
104
|
-
version = override[1];
|
|
105
|
-
}
|
|
106
|
-
if (match[0].trim() === '') {
|
|
152
|
+
const coord = this.parseCoordinate(line);
|
|
153
|
+
if (coord.groupId.trim() === '') {
|
|
107
154
|
throw new Error(`Artifact coordinates should have a non-empty group ID: ${line}`);
|
|
108
155
|
}
|
|
109
|
-
return this.
|
|
156
|
+
return this._coordinateToPurl(coord);
|
|
110
157
|
}
|
|
111
158
|
/**
|
|
112
159
|
* Returns a PackageUrl For Java maven dependencies
|
|
@@ -187,6 +187,36 @@ function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, man
|
|
|
187
187
|
throw error;
|
|
188
188
|
}
|
|
189
189
|
}
|
|
190
|
+
/** Parses go.sum to build a hash map of module@version to CycloneDX hashes. */
|
|
191
|
+
function parseGoSumHashes(manifestDir) {
|
|
192
|
+
let goSumPath = path.join(manifestDir, 'go.sum');
|
|
193
|
+
if (!fs.existsSync(goSumPath)) {
|
|
194
|
+
return new Map();
|
|
195
|
+
}
|
|
196
|
+
let hashMap = new Map();
|
|
197
|
+
let content = fs.readFileSync(goSumPath, 'utf-8');
|
|
198
|
+
for (let line of content.split('\n')) {
|
|
199
|
+
line = line.trim();
|
|
200
|
+
if (!line) {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
let parts = line.split(/\s+/);
|
|
204
|
+
if (parts.length < 3) {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
let [mod, version, hash] = parts;
|
|
208
|
+
if (version.endsWith('/go.mod')) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (!hash.startsWith('h1:')) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
let base64Part = hash.slice(3);
|
|
215
|
+
let hexDigest = Buffer.from(base64Part, 'base64').toString('hex');
|
|
216
|
+
hashMap.set(`${mod}@${version}`, [{ alg: "SHA-256", content: hexDigest }]);
|
|
217
|
+
}
|
|
218
|
+
return hashMap;
|
|
219
|
+
}
|
|
190
220
|
/**
|
|
191
221
|
* Create SBOM json string for go Module.
|
|
192
222
|
* @param {string} manifest - path for go.mod
|
|
@@ -241,6 +271,7 @@ async function getSBOM(manifest, opts = {}, includeTransitive) {
|
|
|
241
271
|
if (matchManifestVersions === "true") {
|
|
242
272
|
performManifestVersionsCheck(root, rows, manifestContent, parser, requireQuery);
|
|
243
273
|
}
|
|
274
|
+
let goSumHashes = parseGoSumHashes(manifestDir);
|
|
244
275
|
const mainModule = toPurl(root, "@");
|
|
245
276
|
const license = readLicenseFromManifest(manifest);
|
|
246
277
|
sbom.addRoot(mainModule, license);
|
|
@@ -262,7 +293,7 @@ async function getSBOM(manifest, opts = {}, includeTransitive) {
|
|
|
262
293
|
if (getParentVertexFromEdge(row) === root && !directDepPaths.has(getPackageName(child))) {
|
|
263
294
|
return;
|
|
264
295
|
}
|
|
265
|
-
sbom.addDependency(source, target);
|
|
296
|
+
sbom.addDependency(source, target, undefined, goSumHashes.get(child));
|
|
266
297
|
});
|
|
267
298
|
// at the end, filter out all ignored dependencies including versions.
|
|
268
299
|
sbom.filterIgnoredDepsIncludingVersion(allIgnoredDeps);
|
|
@@ -275,12 +306,22 @@ async function getSBOM(manifest, opts = {}, includeTransitive) {
|
|
|
275
306
|
let target = toPurl(child, "@");
|
|
276
307
|
if (dependencyNotIgnored(ignoredDeps, target)) {
|
|
277
308
|
if (directDepPaths.has(getPackageName(child))) {
|
|
278
|
-
sbom.addDependency(mainModule, target);
|
|
309
|
+
sbom.addDependency(mainModule, target, undefined, goSumHashes.get(child));
|
|
279
310
|
}
|
|
280
311
|
}
|
|
281
312
|
});
|
|
282
313
|
enforceRemovingIgnoredDepsInCaseOfAutomaticVersionUpdate(ignoredDeps, sbom);
|
|
283
314
|
}
|
|
315
|
+
// Backfill hashes for components that were only seen as dependency sources
|
|
316
|
+
for (const comp of sbom.sbomModel.components) {
|
|
317
|
+
if (!comp.hashes && comp.purl) {
|
|
318
|
+
let key = decodeURIComponent(comp.purl.replace('pkg:golang/', ''));
|
|
319
|
+
let hashes = goSumHashes.get(key);
|
|
320
|
+
if (hashes) {
|
|
321
|
+
comp.hashes = hashes;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
284
325
|
return sbom.getAsJsonString(opts);
|
|
285
326
|
}
|
|
286
327
|
/**
|
|
@@ -11,6 +11,10 @@ export function discoverMavenModules(workspaceRoot: string, opts?: object): Prom
|
|
|
11
11
|
/** @typedef {{name: string, version: string}} Package */
|
|
12
12
|
/** @typedef {{groupId: string, artifactId: string, version: string, scope: string, ignore: boolean}} Dependency */
|
|
13
13
|
export default class Java_maven extends Base_java {
|
|
14
|
+
/** @type {Object<string, string>} Packaging types that produce .jar files despite non-jar packaging names. */
|
|
15
|
+
static PACKAGING_TO_JAR: {
|
|
16
|
+
[x: string]: string;
|
|
17
|
+
};
|
|
14
18
|
constructor();
|
|
15
19
|
/**
|
|
16
20
|
* @param {string} manifestName - the subject manifest name-type
|
|
@@ -25,9 +29,9 @@ export default class Java_maven extends Base_java {
|
|
|
25
29
|
* Provide content and content type for maven-maven stack analysis.
|
|
26
30
|
* @param {string} manifest - the manifest path or name
|
|
27
31
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
28
|
-
* @returns {Provided}
|
|
32
|
+
* @returns {Promise<Provided>}
|
|
29
33
|
*/
|
|
30
|
-
provideStack(manifest: string, opts?: {}): Provided
|
|
34
|
+
provideStack(manifest: string, opts?: {}): Promise<Provided>;
|
|
31
35
|
/**
|
|
32
36
|
* Provide content and content type for maven-maven component analysis.
|
|
33
37
|
* @param {string} manifest - path to the manifest file
|
|
@@ -42,13 +46,34 @@ export default class Java_maven extends Base_java {
|
|
|
42
46
|
*/
|
|
43
47
|
readLicenseFromManifest(manifestPath: string): string | null;
|
|
44
48
|
/**
|
|
49
|
+
* Build a Map of PURL string → CycloneDX hash entries by reading artifact files from the local Maven repository.
|
|
50
|
+
*
|
|
51
|
+
* Both the artifact file path and the hash-map key are derived from the
|
|
52
|
+
* shared {@link Base_java#parseCoordinate} parser: the file path uses the
|
|
53
|
+
* resolved version and raw classifier, while the key uses the same canonical
|
|
54
|
+
* PURL builder as {@link Base_java#parseDep}. This guarantees the key always
|
|
55
|
+
* matches the PURL that {@link Base_java#parseDependencyTree} looks up.
|
|
45
56
|
*
|
|
57
|
+
* @param {string} depTreeText Raw dependency tree text from mvn dependency:tree
|
|
58
|
+
* @param {{}} [opts={}] Options bag (may contain TRUSTIFY_DA_MVN_REPO)
|
|
59
|
+
* @returns {Promise<Map<string, Array<{alg: string, content: string}>>>}
|
|
60
|
+
*/
|
|
61
|
+
_buildMavenHashMap(depTreeText: string, opts?: {}): Promise<Map<string, Array<{
|
|
62
|
+
alg: string;
|
|
63
|
+
content: string;
|
|
64
|
+
}>>>;
|
|
65
|
+
/**
|
|
46
66
|
* @param {String} textGraphList Text graph String of the manifest
|
|
47
67
|
* @param {[String]} ignoredDeps List of ignored dependencies to be omitted from sbom
|
|
68
|
+
* @param {{}} opts Options
|
|
48
69
|
* @param {String} manifestPath Path to the pom.xml manifest
|
|
70
|
+
* @param {Map<string, Array<{alg: string, content: string}>>} [hashMap] PURL→hashes map
|
|
49
71
|
* @return {String} formatted sbom Json String with all dependencies
|
|
50
72
|
*/
|
|
51
|
-
createSbomFileFromTextFormat(textGraphList: string, ignoredDeps: [string], opts:
|
|
73
|
+
createSbomFileFromTextFormat(textGraphList: string, ignoredDeps: [string], opts: {}, manifestPath: string, hashMap?: Map<string, Array<{
|
|
74
|
+
alg: string;
|
|
75
|
+
content: string;
|
|
76
|
+
}>>): string;
|
|
52
77
|
#private;
|
|
53
78
|
}
|
|
54
79
|
export type Java_maven = any;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
1
2
|
import fs from 'node:fs';
|
|
2
3
|
import os from 'node:os';
|
|
3
4
|
import path from 'node:path';
|
|
@@ -31,12 +32,12 @@ export default class Java_maven extends Base_java {
|
|
|
31
32
|
* Provide content and content type for maven-maven stack analysis.
|
|
32
33
|
* @param {string} manifest - the manifest path or name
|
|
33
34
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
34
|
-
* @returns {Provided}
|
|
35
|
+
* @returns {Promise<Provided>}
|
|
35
36
|
*/
|
|
36
|
-
provideStack(manifest, opts = {}) {
|
|
37
|
+
async provideStack(manifest, opts = {}) {
|
|
37
38
|
return {
|
|
38
39
|
ecosystem: ecosystem_maven,
|
|
39
|
-
content: this.#createSbomStackAnalysis(manifest, opts),
|
|
40
|
+
content: await this.#createSbomStackAnalysis(manifest, opts),
|
|
40
41
|
contentType: 'application/vnd.cyclonedx+json'
|
|
41
42
|
};
|
|
42
43
|
}
|
|
@@ -81,10 +82,10 @@ export default class Java_maven extends Base_java {
|
|
|
81
82
|
* Create a Dot Graph dependency tree for a manifest path.
|
|
82
83
|
* @param {string} manifest - path for pom.xml
|
|
83
84
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
84
|
-
* @returns {string} the Dot Graph content
|
|
85
|
+
* @returns {Promise<string>} the Dot Graph content
|
|
85
86
|
* @private
|
|
86
87
|
*/
|
|
87
|
-
#createSbomStackAnalysis(manifest, opts = {}) {
|
|
88
|
+
async #createSbomStackAnalysis(manifest, opts = {}) {
|
|
88
89
|
const manifestDir = path.dirname(manifest);
|
|
89
90
|
const mvn = this.selectToolBinary(manifest, opts);
|
|
90
91
|
const mvnArgs = JSON.parse(getCustom('TRUSTIFY_DA_MVN_ARGS', '[]', opts));
|
|
@@ -131,20 +132,105 @@ export default class Java_maven extends Base_java {
|
|
|
131
132
|
if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
|
|
132
133
|
console.error("Dependency tree that will be used as input for creating the BOM =>" + EOL + EOL + content.toString());
|
|
133
134
|
}
|
|
134
|
-
|
|
135
|
+
const depTreeContent = content.toString();
|
|
136
|
+
const hashMap = await this._buildMavenHashMap(depTreeContent, opts);
|
|
137
|
+
let sbom = this.createSbomFileFromTextFormat(depTreeContent, ignoredDeps, opts, manifest, hashMap);
|
|
135
138
|
// delete temp file and directory
|
|
136
139
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
137
140
|
// return dependency graph as string
|
|
138
141
|
return sbom;
|
|
139
142
|
}
|
|
143
|
+
/** @type {Object<string, string>} Packaging types that produce .jar files despite non-jar packaging names. */
|
|
144
|
+
static PACKAGING_TO_JAR = { 'bundle': 'jar', 'eclipse-plugin': 'jar' };
|
|
140
145
|
/**
|
|
146
|
+
* Build a Map of PURL string → CycloneDX hash entries by reading artifact files from the local Maven repository.
|
|
147
|
+
*
|
|
148
|
+
* Both the artifact file path and the hash-map key are derived from the
|
|
149
|
+
* shared {@link Base_java#parseCoordinate} parser: the file path uses the
|
|
150
|
+
* resolved version and raw classifier, while the key uses the same canonical
|
|
151
|
+
* PURL builder as {@link Base_java#parseDep}. This guarantees the key always
|
|
152
|
+
* matches the PURL that {@link Base_java#parseDependencyTree} looks up.
|
|
141
153
|
*
|
|
154
|
+
* @param {string} depTreeText Raw dependency tree text from mvn dependency:tree
|
|
155
|
+
* @param {{}} [opts={}] Options bag (may contain TRUSTIFY_DA_MVN_REPO)
|
|
156
|
+
* @returns {Promise<Map<string, Array<{alg: string, content: string}>>>}
|
|
157
|
+
*/
|
|
158
|
+
async _buildMavenHashMap(depTreeText, opts = {}) {
|
|
159
|
+
const m2Repo = getCustom('TRUSTIFY_DA_MVN_REPO', path.join(os.homedir(), '.m2', 'repository'), opts);
|
|
160
|
+
const hashMap = new Map();
|
|
161
|
+
const lines = depTreeText.split(EOL);
|
|
162
|
+
// Track hash coverage so an incomplete .m2 cache (e.g. ephemeral CI
|
|
163
|
+
// containers or resolve-only phases) surfaces a summary warning instead
|
|
164
|
+
// of silently producing an SBOM with missing hashes.
|
|
165
|
+
let attempted = 0;
|
|
166
|
+
let missed = 0;
|
|
167
|
+
for (const rawLine of lines) {
|
|
168
|
+
const trimmed = rawLine.trim();
|
|
169
|
+
// Strip leading tree-drawing characters (e.g. "\-", "+-", "|") so the
|
|
170
|
+
// parenthesized-line check sees the coordinate itself. Omitted
|
|
171
|
+
// duplicate/conflict lines start with these characters after trim(),
|
|
172
|
+
// then an opening "(" — without stripping, the "(" check never fires.
|
|
173
|
+
const cleaned = trimmed.replace(/^[|+\\\- ]+/, '');
|
|
174
|
+
if (!cleaned || cleaned.startsWith('(')) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
const coord = this.parseCoordinate(rawLine);
|
|
178
|
+
if (!coord.groupId || !coord.artifactId || !coord.packaging) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (coord.packaging === 'pom') {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
// Key by the exact PURL parseDep() produces, so the lookup in
|
|
185
|
+
// parseDependencyTree (hashMap.get(to.toString())) always hits.
|
|
186
|
+
const purl = this._coordinateToPurl(coord).toString();
|
|
187
|
+
// The same artifact recurs across dependency-tree branches — notably in
|
|
188
|
+
// multi-module reactor builds, where every module re-lists shared deps.
|
|
189
|
+
// The digest is deterministic per PURL, so skip the redundant file read
|
|
190
|
+
// and SHA-256 computation once this PURL is already hashed.
|
|
191
|
+
if (hashMap.has(purl)) {
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
const ext = Java_maven.PACKAGING_TO_JAR[coord.packaging] || coord.packaging;
|
|
195
|
+
const groupPath = coord.groupId.replaceAll('.', path.sep);
|
|
196
|
+
const fileName = coord.classifier
|
|
197
|
+
? `${coord.artifactId}-${coord.version}-${coord.classifier}.${ext}`
|
|
198
|
+
: `${coord.artifactId}-${coord.version}.${ext}`;
|
|
199
|
+
const artifactPath = path.join(m2Repo, groupPath, coord.artifactId, coord.version, fileName);
|
|
200
|
+
attempted++;
|
|
201
|
+
try {
|
|
202
|
+
const stream = fs.createReadStream(artifactPath);
|
|
203
|
+
const hash = crypto.createHash('sha256');
|
|
204
|
+
for await (const chunk of stream) {
|
|
205
|
+
hash.update(chunk);
|
|
206
|
+
}
|
|
207
|
+
const digest = hash.digest('hex');
|
|
208
|
+
hashMap.set(purl, [{ alg: 'SHA-256', content: digest }]);
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
missed++;
|
|
212
|
+
if (process.env['TRUSTIFY_DA_DEBUG'] === 'true') {
|
|
213
|
+
console.error(`Maven hash: artifact not found at ${artifactPath}, omitting hash`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
// Mirror the pip provider's convention (python_controller.js): surface an
|
|
218
|
+
// unconditional warning when hashes could not be computed, so incomplete
|
|
219
|
+
// hash coverage is visible even without TRUSTIFY_DA_DEBUG.
|
|
220
|
+
if (missed > 0) {
|
|
221
|
+
console.warn(`Maven hash: ${missed} of ${attempted} artifacts could not be read from the local .m2 cache; SBOM will be generated without hashes for those components.`);
|
|
222
|
+
}
|
|
223
|
+
return hashMap;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
142
226
|
* @param {String} textGraphList Text graph String of the manifest
|
|
143
227
|
* @param {[String]} ignoredDeps List of ignored dependencies to be omitted from sbom
|
|
228
|
+
* @param {{}} opts Options
|
|
144
229
|
* @param {String} manifestPath Path to the pom.xml manifest
|
|
230
|
+
* @param {Map<string, Array<{alg: string, content: string}>>} [hashMap] PURL→hashes map
|
|
145
231
|
* @return {String} formatted sbom Json String with all dependencies
|
|
146
232
|
*/
|
|
147
|
-
createSbomFileFromTextFormat(textGraphList, ignoredDeps, opts, manifestPath) {
|
|
233
|
+
createSbomFileFromTextFormat(textGraphList, ignoredDeps, opts, manifestPath, hashMap) {
|
|
148
234
|
let lines = textGraphList.split(EOL);
|
|
149
235
|
// get root component
|
|
150
236
|
let root = lines[0];
|
|
@@ -153,6 +239,9 @@ export default class Java_maven extends Base_java {
|
|
|
153
239
|
let sbom = new Sbom();
|
|
154
240
|
sbom.addRoot(rootPurl, license);
|
|
155
241
|
this.parseDependencyTree(root, 0, lines.slice(1), sbom);
|
|
242
|
+
// Attach Maven artifact hashes as a post-processing step, keeping this
|
|
243
|
+
// Maven-specific concern out of the shared dependency-tree parser.
|
|
244
|
+
sbom.attachHashes(hashMap);
|
|
156
245
|
return sbom.filterIgnoredDeps(ignoredDeps).getAsJsonString(opts);
|
|
157
246
|
}
|
|
158
247
|
/**
|
|
@@ -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?: "bundle" | "dependency" | undefined;
|
|
18
|
+
}): Promise<{
|
|
19
|
+
exitCode: number;
|
|
20
|
+
output: string;
|
|
21
|
+
}>;
|