@trustify-da/trustify-da-javascript-client 0.3.0-ea.a754fa8 → 0.3.0-ea.a87208f
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 +3 -1
- package/dist/src/cyclone_dx_sbom.d.ts +13 -0
- package/dist/src/cyclone_dx_sbom.js +39 -1
- package/dist/src/index.js +7 -24
- package/dist/src/package_version.d.ts +8 -0
- package/dist/src/package_version.js +31 -0
- package/dist/src/providers/base_java.d.ts +53 -0
- package/dist/src/providers/base_java.js +64 -17
- package/dist/src/providers/base_javascript.d.ts +54 -0
- package/dist/src/providers/base_javascript.js +105 -3
- package/dist/src/providers/java_maven.d.ts +28 -3
- package/dist/src/providers/java_maven.js +96 -7
- package/dist/src/providers/javascript_bun.d.ts +12 -0
- package/dist/src/providers/javascript_bun.js +42 -1
- package/dist/src/providers/javascript_npm.d.ts +13 -0
- package/dist/src/providers/javascript_npm.js +40 -1
- package/dist/src/providers/javascript_pnpm.d.ts +13 -0
- package/dist/src/providers/javascript_pnpm.js +47 -1
- package/dist/src/providers/javascript_yarn.d.ts +12 -0
- package/dist/src/providers/javascript_yarn.js +75 -2
- package/dist/src/providers/processors/yarn_berry_processor.d.ts +5 -1
- package/dist/src/providers/processors/yarn_berry_processor.js +3 -2
- package/dist/src/providers/processors/yarn_classic_processor.d.ts +5 -1
- package/dist/src/providers/processors/yarn_classic_processor.js +8 -6
- package/dist/src/remediate.d.ts +1 -1
- package/dist/src/remediation_report.d.ts +1 -1
- package/dist/src/sbom.d.ts +11 -0
- package/dist/src/sbom.js +10 -0
- package/dist/src/tools.js +24 -1
- package/package.json +4 -2
|
@@ -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
|
/**
|
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
export default class Javascript_bun extends Base_javascript {
|
|
2
|
+
/**
|
|
3
|
+
* Parses `bun.lock` into a hash map keyed by `name@version`. Each entry in
|
|
4
|
+
* the `packages` section is an array whose first element is the resolved id
|
|
5
|
+
* (`name@version`) and whose fourth element is the SRI integrity string.
|
|
6
|
+
* @param {string} lockDir - Directory containing bun.lock
|
|
7
|
+
* @returns {Map<string, Array<{alg: string, content: string}>>} Hash map
|
|
8
|
+
* @protected
|
|
9
|
+
*/
|
|
10
|
+
protected _parseLockFileHashes(lockDir: string): Map<string, Array<{
|
|
11
|
+
alg: string;
|
|
12
|
+
content: string;
|
|
13
|
+
}>>;
|
|
2
14
|
_listCmdArgs(): void;
|
|
3
15
|
_buildDependencyTree(includeTransitive: any, opts?: {}): {
|
|
4
16
|
name: any;
|
|
@@ -1,11 +1,51 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { parse } from 'jsonc-parser';
|
|
4
|
-
import Base_javascript from './base_javascript.js';
|
|
4
|
+
import Base_javascript, { sriToHash } from './base_javascript.js';
|
|
5
5
|
export default class Javascript_bun extends Base_javascript {
|
|
6
6
|
_lockFileName() {
|
|
7
7
|
return "bun.lock";
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Parses `bun.lock` into a hash map keyed by `name@version`. Each entry in
|
|
11
|
+
* the `packages` section is an array whose first element is the resolved id
|
|
12
|
+
* (`name@version`) and whose fourth element is the SRI integrity string.
|
|
13
|
+
* @param {string} lockDir - Directory containing bun.lock
|
|
14
|
+
* @returns {Map<string, Array<{alg: string, content: string}>>} Hash map
|
|
15
|
+
* @protected
|
|
16
|
+
*/
|
|
17
|
+
_parseLockFileHashes(lockDir) {
|
|
18
|
+
const map = new Map();
|
|
19
|
+
const lockPath = path.join(lockDir, this._lockFileName());
|
|
20
|
+
if (!fs.existsSync(lockPath)) {
|
|
21
|
+
return map;
|
|
22
|
+
}
|
|
23
|
+
let lockData;
|
|
24
|
+
try {
|
|
25
|
+
lockData = parse(fs.readFileSync(lockPath, 'utf-8'));
|
|
26
|
+
}
|
|
27
|
+
catch (_) {
|
|
28
|
+
return map;
|
|
29
|
+
}
|
|
30
|
+
const packages = lockData?.packages || {};
|
|
31
|
+
for (const entry of Object.values(packages)) {
|
|
32
|
+
if (!Array.isArray(entry) || typeof entry[0] !== 'string' || typeof entry[3] !== 'string') {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const id = entry[0];
|
|
36
|
+
const at = id.lastIndexOf('@');
|
|
37
|
+
if (at <= 0) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const name = id.slice(0, at);
|
|
41
|
+
const version = id.slice(at + 1);
|
|
42
|
+
const hash = sriToHash(entry[3]);
|
|
43
|
+
if (name && version && hash) {
|
|
44
|
+
map.set(`${name}@${version}`, [hash]);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return map;
|
|
48
|
+
}
|
|
9
49
|
_cmdName() {
|
|
10
50
|
return "bun";
|
|
11
51
|
}
|
|
@@ -20,6 +60,7 @@ export default class Javascript_bun extends Base_javascript {
|
|
|
20
60
|
const manifestDir = path.dirname(this._getManifest().manifestPath);
|
|
21
61
|
const lockDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
|
|
22
62
|
this._createLockFile(lockDir);
|
|
63
|
+
this._loadHashes(lockDir);
|
|
23
64
|
const lockContent = fs.readFileSync(path.join(lockDir, 'bun.lock'), 'utf-8');
|
|
24
65
|
const lockData = parse(lockContent);
|
|
25
66
|
const packages = lockData.packages || {};
|
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
export default class Javascript_npm extends Base_javascript {
|
|
2
|
+
/**
|
|
3
|
+
* Parses `package-lock.json` (lockfileVersion 2/3) into a hash map keyed by
|
|
4
|
+
* `name@version`. Each `packages` entry is keyed by its install path
|
|
5
|
+
* (e.g. `node_modules/@hapi/joi`); the package name is the segment after the
|
|
6
|
+
* final `node_modules/`. The `integrity` field holds an SRI string.
|
|
7
|
+
* @param {string} lockDir - Directory containing package-lock.json
|
|
8
|
+
* @returns {Map<string, Array<{alg: string, content: string}>>} Hash map
|
|
9
|
+
* @protected
|
|
10
|
+
*/
|
|
11
|
+
protected _parseLockFileHashes(lockDir: string): Map<string, Array<{
|
|
12
|
+
alg: string;
|
|
13
|
+
content: string;
|
|
14
|
+
}>>;
|
|
2
15
|
_listCmdArgs(includeTransitive: any): string[];
|
|
3
16
|
_buildDependencyTree(includeTransitive: any, opts?: {}): Object | {
|
|
4
17
|
name: any;
|
|
@@ -1,8 +1,47 @@
|
|
|
1
|
-
import
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import Base_javascript, { sriToHash } from './base_javascript.js';
|
|
2
4
|
export default class Javascript_npm extends Base_javascript {
|
|
3
5
|
_lockFileName() {
|
|
4
6
|
return "package-lock.json";
|
|
5
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* Parses `package-lock.json` (lockfileVersion 2/3) into a hash map keyed by
|
|
10
|
+
* `name@version`. Each `packages` entry is keyed by its install path
|
|
11
|
+
* (e.g. `node_modules/@hapi/joi`); the package name is the segment after the
|
|
12
|
+
* final `node_modules/`. The `integrity` field holds an SRI string.
|
|
13
|
+
* @param {string} lockDir - Directory containing package-lock.json
|
|
14
|
+
* @returns {Map<string, Array<{alg: string, content: string}>>} Hash map
|
|
15
|
+
* @protected
|
|
16
|
+
*/
|
|
17
|
+
_parseLockFileHashes(lockDir) {
|
|
18
|
+
const map = new Map();
|
|
19
|
+
const lockPath = path.join(lockDir, this._lockFileName());
|
|
20
|
+
if (!fs.existsSync(lockPath)) {
|
|
21
|
+
return map;
|
|
22
|
+
}
|
|
23
|
+
let lock;
|
|
24
|
+
try {
|
|
25
|
+
lock = JSON.parse(fs.readFileSync(lockPath, 'utf-8'));
|
|
26
|
+
}
|
|
27
|
+
catch (_) {
|
|
28
|
+
return map;
|
|
29
|
+
}
|
|
30
|
+
const packages = lock.packages || {};
|
|
31
|
+
for (const [pkgPath, entry] of Object.entries(packages)) {
|
|
32
|
+
if (!pkgPath || !entry || !entry.integrity || !entry.version) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const marker = 'node_modules/';
|
|
36
|
+
const idx = pkgPath.lastIndexOf(marker);
|
|
37
|
+
const name = idx < 0 ? pkgPath : pkgPath.slice(idx + marker.length);
|
|
38
|
+
const hash = sriToHash(entry.integrity);
|
|
39
|
+
if (name && hash) {
|
|
40
|
+
map.set(`${name}@${entry.version}`, [hash]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return map;
|
|
44
|
+
}
|
|
6
45
|
_cmdName() {
|
|
7
46
|
return "npm";
|
|
8
47
|
}
|
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
export default class Javascript_pnpm extends Base_javascript {
|
|
2
|
+
/**
|
|
3
|
+
* Parses `pnpm-lock.yaml` (lockfileVersion 9) into a hash map keyed by
|
|
4
|
+
* `name@version`. Entries in the `packages` section are keyed by
|
|
5
|
+
* `<name>@<version>` (optionally followed by a `(peer@x)` suffix); the
|
|
6
|
+
* `resolution.integrity` field holds an SRI string.
|
|
7
|
+
* @param {string} lockDir - Directory containing pnpm-lock.yaml
|
|
8
|
+
* @returns {Map<string, Array<{alg: string, content: string}>>} Hash map
|
|
9
|
+
* @protected
|
|
10
|
+
*/
|
|
11
|
+
protected _parseLockFileHashes(lockDir: string): Map<string, Array<{
|
|
12
|
+
alg: string;
|
|
13
|
+
content: string;
|
|
14
|
+
}>>;
|
|
2
15
|
_listCmdArgs(includeTransitive: any): string[];
|
|
3
16
|
_buildDependencyTree(includeTransitive: any, opts?: {}): any;
|
|
4
17
|
}
|
|
@@ -1,8 +1,54 @@
|
|
|
1
|
-
import
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { load as yamlLoad } from 'js-yaml';
|
|
4
|
+
import Base_javascript, { sriToHash } from './base_javascript.js';
|
|
2
5
|
export default class Javascript_pnpm extends Base_javascript {
|
|
3
6
|
_lockFileName() {
|
|
4
7
|
return "pnpm-lock.yaml";
|
|
5
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Parses `pnpm-lock.yaml` (lockfileVersion 9) into a hash map keyed by
|
|
11
|
+
* `name@version`. Entries in the `packages` section are keyed by
|
|
12
|
+
* `<name>@<version>` (optionally followed by a `(peer@x)` suffix); the
|
|
13
|
+
* `resolution.integrity` field holds an SRI string.
|
|
14
|
+
* @param {string} lockDir - Directory containing pnpm-lock.yaml
|
|
15
|
+
* @returns {Map<string, Array<{alg: string, content: string}>>} Hash map
|
|
16
|
+
* @protected
|
|
17
|
+
*/
|
|
18
|
+
_parseLockFileHashes(lockDir) {
|
|
19
|
+
const map = new Map();
|
|
20
|
+
const lockPath = path.join(lockDir, this._lockFileName());
|
|
21
|
+
if (!fs.existsSync(lockPath)) {
|
|
22
|
+
return map;
|
|
23
|
+
}
|
|
24
|
+
let lock;
|
|
25
|
+
try {
|
|
26
|
+
lock = yamlLoad(fs.readFileSync(lockPath, 'utf-8'));
|
|
27
|
+
}
|
|
28
|
+
catch (_) {
|
|
29
|
+
return map;
|
|
30
|
+
}
|
|
31
|
+
const packages = lock?.packages || {};
|
|
32
|
+
for (const [key, entry] of Object.entries(packages)) {
|
|
33
|
+
const integrity = entry?.resolution?.integrity;
|
|
34
|
+
if (!key || !integrity) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
// Strip any peer-dependency suffix, then split name from version
|
|
38
|
+
const base = key.split('(')[0];
|
|
39
|
+
const at = base.lastIndexOf('@');
|
|
40
|
+
if (at <= 0) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const name = base.slice(0, at);
|
|
44
|
+
const version = base.slice(at + 1);
|
|
45
|
+
const hash = sriToHash(integrity);
|
|
46
|
+
if (name && version && hash) {
|
|
47
|
+
map.set(`${name}@${version}`, [hash]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return map;
|
|
51
|
+
}
|
|
6
52
|
_cmdName() {
|
|
7
53
|
return "pnpm";
|
|
8
54
|
}
|
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
export default class Javascript_yarn extends Base_javascript {
|
|
2
2
|
static VERSION_PATTERN: RegExp;
|
|
3
|
+
/**
|
|
4
|
+
* Parses `yarn.lock` into a hash map keyed by `name@version`. Supports both
|
|
5
|
+
* Yarn Classic (v1) entries with an `integrity` SRI field and Yarn Berry
|
|
6
|
+
* (v2+) entries with a `checksum` field.
|
|
7
|
+
* @param {string} lockDir - Directory containing yarn.lock
|
|
8
|
+
* @returns {Map<string, Array<{alg: string, content: string}>>} Hash map
|
|
9
|
+
* @protected
|
|
10
|
+
*/
|
|
11
|
+
protected _parseLockFileHashes(lockDir: string): Map<string, Array<{
|
|
12
|
+
alg: string;
|
|
13
|
+
content: string;
|
|
14
|
+
}>>;
|
|
3
15
|
_listCmdArgs(includeTransitive: any, manifestDir: any): any;
|
|
4
16
|
_updateLockFileCmdArgs(manifestDir: any): any;
|
|
5
17
|
_setUp(manifestPath: any, opts: any): void;
|
|
@@ -1,12 +1,85 @@
|
|
|
1
|
-
import
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { parseSyml } from '@yarnpkg/parsers';
|
|
4
|
+
import Base_javascript, { sriToHash } from './base_javascript.js';
|
|
2
5
|
import Yarn_berry_processor from './processors/yarn_berry_processor.js';
|
|
3
6
|
import Yarn_classic_processor from './processors/yarn_classic_processor.js';
|
|
7
|
+
/**
|
|
8
|
+
* Converts a Yarn Berry `checksum` value to a CycloneDX hash object. Berry
|
|
9
|
+
* checksums are formatted as `<cacheKey>/<hex-sha512>`; the digest is the
|
|
10
|
+
* hex-encoded SHA-512 following the final slash.
|
|
11
|
+
* @param {string} checksum - The checksum field value
|
|
12
|
+
* @returns {{alg: string, content: string}|null} CycloneDX hash, or null
|
|
13
|
+
*/
|
|
14
|
+
function berryChecksumToHash(checksum) {
|
|
15
|
+
if (typeof checksum !== 'string') {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
const slash = checksum.lastIndexOf('/');
|
|
19
|
+
const hex = (slash >= 0 ? checksum.slice(slash + 1) : checksum).trim();
|
|
20
|
+
if (!/^[0-9a-f]+$/i.test(hex)) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
return { alg: 'SHA-512', content: hex.toLowerCase() };
|
|
24
|
+
}
|
|
4
25
|
export default class Javascript_yarn extends Base_javascript {
|
|
5
26
|
static VERSION_PATTERN = /^([0-9]+)\./;
|
|
6
27
|
#processor;
|
|
7
28
|
_lockFileName() {
|
|
8
29
|
return "yarn.lock";
|
|
9
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Parses `yarn.lock` into a hash map keyed by `name@version`. Supports both
|
|
33
|
+
* Yarn Classic (v1) entries with an `integrity` SRI field and Yarn Berry
|
|
34
|
+
* (v2+) entries with a `checksum` field.
|
|
35
|
+
* @param {string} lockDir - Directory containing yarn.lock
|
|
36
|
+
* @returns {Map<string, Array<{alg: string, content: string}>>} Hash map
|
|
37
|
+
* @protected
|
|
38
|
+
*/
|
|
39
|
+
_parseLockFileHashes(lockDir) {
|
|
40
|
+
const map = new Map();
|
|
41
|
+
const lockPath = path.join(lockDir, this._lockFileName());
|
|
42
|
+
if (!fs.existsSync(lockPath)) {
|
|
43
|
+
return map;
|
|
44
|
+
}
|
|
45
|
+
let content;
|
|
46
|
+
try {
|
|
47
|
+
content = fs.readFileSync(lockPath, 'utf-8');
|
|
48
|
+
}
|
|
49
|
+
catch (_) {
|
|
50
|
+
return map;
|
|
51
|
+
}
|
|
52
|
+
const parsed = parseSyml(content);
|
|
53
|
+
for (const [spec, entry] of Object.entries(parsed)) {
|
|
54
|
+
if (spec === '__metadata') {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const version = entry.version;
|
|
58
|
+
if (!version) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
let hash = null;
|
|
62
|
+
if (entry.integrity) {
|
|
63
|
+
hash = sriToHash(entry.integrity);
|
|
64
|
+
}
|
|
65
|
+
else if (entry.checksum) {
|
|
66
|
+
hash = berryChecksumToHash(entry.checksum);
|
|
67
|
+
}
|
|
68
|
+
if (!hash) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
// Handle comma-separated specifiers (e.g., "pkg@^1.0.0, pkg@^2.0.0")
|
|
72
|
+
for (const rawSpec of spec.split(',')) {
|
|
73
|
+
const trimmed = rawSpec.trim();
|
|
74
|
+
const atIndex = trimmed.lastIndexOf('@');
|
|
75
|
+
if (atIndex > 0) {
|
|
76
|
+
const name = trimmed.slice(0, atIndex);
|
|
77
|
+
map.set(`${name}@${version}`, [hash]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return map;
|
|
82
|
+
}
|
|
10
83
|
_cmdName() {
|
|
11
84
|
return "yarn";
|
|
12
85
|
}
|
|
@@ -34,6 +107,6 @@ export default class Javascript_yarn extends Base_javascript {
|
|
|
34
107
|
return this.#processor.parseDepTreeOutput(output);
|
|
35
108
|
}
|
|
36
109
|
_addDependenciesToSbom(sbom, depTree) {
|
|
37
|
-
this.#processor.addDependenciesToSbom(sbom, depTree);
|
|
110
|
+
this.#processor.addDependenciesToSbom(sbom, depTree, purl => this._hashesForPurl(purl));
|
|
38
111
|
}
|
|
39
112
|
}
|
|
@@ -34,8 +34,12 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
34
34
|
* Adds dependencies to the SBOM
|
|
35
35
|
* @param {Sbom} sbom - The SBOM object to add dependencies to
|
|
36
36
|
* @param {Object} depTree - The dependency tree object
|
|
37
|
+
* @param {function(PackageURL): (Array<{alg: string, content: string}>|undefined)} [hashesForPurl] - Resolves lock-file hashes for a purl
|
|
37
38
|
*/
|
|
38
|
-
addDependenciesToSbom(sbom: Sbom, depTree: Object
|
|
39
|
+
addDependenciesToSbom(sbom: Sbom, depTree: Object, hashesForPurl?: (arg0: PackageURL) => (Array<{
|
|
40
|
+
alg: string;
|
|
41
|
+
content: string;
|
|
42
|
+
}> | undefined)): void;
|
|
39
43
|
#private;
|
|
40
44
|
}
|
|
41
45
|
import Yarn_processor from "./yarn_processor.js";
|
|
@@ -78,8 +78,9 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
78
78
|
* Adds dependencies to the SBOM
|
|
79
79
|
* @param {Sbom} sbom - The SBOM object to add dependencies to
|
|
80
80
|
* @param {Object} depTree - The dependency tree object
|
|
81
|
+
* @param {function(PackageURL): (Array<{alg: string, content: string}>|undefined)} [hashesForPurl] - Resolves lock-file hashes for a purl
|
|
81
82
|
*/
|
|
82
|
-
addDependenciesToSbom(sbom, depTree) {
|
|
83
|
+
addDependenciesToSbom(sbom, depTree, hashesForPurl = () => undefined) {
|
|
83
84
|
if (!depTree) {
|
|
84
85
|
return;
|
|
85
86
|
}
|
|
@@ -137,7 +138,7 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
137
138
|
}
|
|
138
139
|
const to = this.#purlFromLocator(d.locator);
|
|
139
140
|
if (to) {
|
|
140
|
-
sbom.addDependency(from, to);
|
|
141
|
+
sbom.addDependency(from, to, undefined, hashesForPurl(to));
|
|
141
142
|
}
|
|
142
143
|
});
|
|
143
144
|
});
|
|
@@ -30,8 +30,12 @@ export default class Yarn_classic_processor extends Yarn_processor {
|
|
|
30
30
|
* Adds dependencies to the SBOM
|
|
31
31
|
* @param {Sbom} sbom - The SBOM object to add dependencies to
|
|
32
32
|
* @param {Object} depTree - The dependency tree object
|
|
33
|
+
* @param {function(PackageURL): (Array<{alg: string, content: string}>|undefined)} [hashesForPurl] - Resolves lock-file hashes for a purl
|
|
33
34
|
*/
|
|
34
|
-
addDependenciesToSbom(sbom: Sbom, depTree: Object
|
|
35
|
+
addDependenciesToSbom(sbom: Sbom, depTree: Object, hashesForPurl?: (arg0: PackageURL) => (Array<{
|
|
36
|
+
alg: string;
|
|
37
|
+
content: string;
|
|
38
|
+
}> | undefined)): void;
|
|
35
39
|
#private;
|
|
36
40
|
}
|
|
37
41
|
import Yarn_processor from "./yarn_processor.js";
|
|
@@ -50,8 +50,9 @@ export default class Yarn_classic_processor extends Yarn_processor {
|
|
|
50
50
|
* Adds dependencies to the SBOM
|
|
51
51
|
* @param {Sbom} sbom - The SBOM object to add dependencies to
|
|
52
52
|
* @param {Object} depTree - The dependency tree object
|
|
53
|
+
* @param {function(PackageURL): (Array<{alg: string, content: string}>|undefined)} [hashesForPurl] - Resolves lock-file hashes for a purl
|
|
53
54
|
*/
|
|
54
|
-
addDependenciesToSbom(sbom, depTree) {
|
|
55
|
+
addDependenciesToSbom(sbom, depTree, hashesForPurl = () => undefined) {
|
|
55
56
|
if (!depTree?.data?.trees) {
|
|
56
57
|
return;
|
|
57
58
|
}
|
|
@@ -60,12 +61,12 @@ export default class Yarn_classic_processor extends Yarn_processor {
|
|
|
60
61
|
depTree.data.trees.forEach(n => {
|
|
61
62
|
const dep = new NodeMetaData(n);
|
|
62
63
|
if (this._manifest.dependencies.includes(dep.name)) {
|
|
63
|
-
sbom.addDependency(rootPurl, dep.purl);
|
|
64
|
+
sbom.addDependency(rootPurl, dep.purl, undefined, hashesForPurl(dep.purl));
|
|
64
65
|
}
|
|
65
66
|
purls.set(dep.name, dep.purl);
|
|
66
67
|
});
|
|
67
68
|
depTree.data.trees.forEach(n => {
|
|
68
|
-
this.#addChildrenToSbom(sbom, n, purls);
|
|
69
|
+
this.#addChildrenToSbom(sbom, n, purls, hashesForPurl);
|
|
69
70
|
});
|
|
70
71
|
}
|
|
71
72
|
/**
|
|
@@ -73,9 +74,10 @@ export default class Yarn_classic_processor extends Yarn_processor {
|
|
|
73
74
|
* @param {Sbom} sbom - The SBOM object to add dependencies to
|
|
74
75
|
* @param {Object} node - The current dependency node
|
|
75
76
|
* @param {Map<string, PackageURL>} purls - Map of dependency names to their PackageURL objects
|
|
77
|
+
* @param {function(PackageURL): (Array<{alg: string, content: string}>|undefined)} hashesForPurl - Resolves lock-file hashes for a purl
|
|
76
78
|
* @private
|
|
77
79
|
*/
|
|
78
|
-
#addChildrenToSbom(sbom, node, purls) {
|
|
80
|
+
#addChildrenToSbom(sbom, node, purls, hashesForPurl) {
|
|
79
81
|
const dep = new NodeMetaData(node);
|
|
80
82
|
const children = node.children ? node.children : [];
|
|
81
83
|
children.forEach(c => {
|
|
@@ -83,9 +85,9 @@ export default class Yarn_classic_processor extends Yarn_processor {
|
|
|
83
85
|
const from = dep.shadow ? purls.get(dep.name) : dep.purl;
|
|
84
86
|
const to = child.shadow ? purls.get(child.name) : child.purl;
|
|
85
87
|
if (from && to) {
|
|
86
|
-
sbom.addDependency(from, to);
|
|
88
|
+
sbom.addDependency(from, to, undefined, hashesForPurl(to));
|
|
87
89
|
}
|
|
88
|
-
this.#addChildrenToSbom(sbom, c, purls);
|
|
90
|
+
this.#addChildrenToSbom(sbom, c, purls, hashesForPurl);
|
|
89
91
|
});
|
|
90
92
|
}
|
|
91
93
|
}
|
package/dist/src/remediate.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export function runRemediation(targetPath: string, options?: {
|
|
|
14
14
|
dryRun?: boolean | undefined;
|
|
15
15
|
providers?: string | undefined;
|
|
16
16
|
sources?: string | undefined;
|
|
17
|
-
groupBy?: "
|
|
17
|
+
groupBy?: "bundle" | "dependency" | undefined;
|
|
18
18
|
}): Promise<{
|
|
19
19
|
exitCode: number;
|
|
20
20
|
output: string;
|
|
@@ -26,7 +26,7 @@ export function generateReport(remediations: Array<{
|
|
|
26
26
|
severity: string;
|
|
27
27
|
cves: string[];
|
|
28
28
|
}>, options?: {
|
|
29
|
-
groupBy?: "
|
|
29
|
+
groupBy?: "bundle" | "dependency" | undefined;
|
|
30
30
|
format?: "markdown" | "json" | undefined;
|
|
31
31
|
dryRun?: boolean | undefined;
|
|
32
32
|
}): string;
|
package/dist/src/sbom.d.ts
CHANGED
|
@@ -33,6 +33,17 @@ export default class Sbom {
|
|
|
33
33
|
alg: string;
|
|
34
34
|
content: string;
|
|
35
35
|
}>): CycloneDxSbom;
|
|
36
|
+
/**
|
|
37
|
+
* Attach hashes to existing components by matching their PURL. Post-processing
|
|
38
|
+
* step used by ecosystem-specific providers (e.g. Maven) to enrich the SBOM
|
|
39
|
+
* with artifact hashes without leaking their concern into the shared parser.
|
|
40
|
+
* @param {Map<string, Array<{alg: string, content: string}>>} hashMap - PURL→hashes map
|
|
41
|
+
* @return {Sbom}
|
|
42
|
+
*/
|
|
43
|
+
attachHashes(hashMap: Map<string, Array<{
|
|
44
|
+
alg: string;
|
|
45
|
+
content: string;
|
|
46
|
+
}>>): Sbom;
|
|
36
47
|
/**
|
|
37
48
|
* @return String sbom json in a string format
|
|
38
49
|
*/
|
package/dist/src/sbom.js
CHANGED
|
@@ -50,6 +50,16 @@ export default class Sbom {
|
|
|
50
50
|
addDependency(sourceRef, targetRef, scope, targetHashes) {
|
|
51
51
|
return this.sbomModel.addDependency(sourceRef, targetRef, scope, targetHashes);
|
|
52
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Attach hashes to existing components by matching their PURL. Post-processing
|
|
55
|
+
* step used by ecosystem-specific providers (e.g. Maven) to enrich the SBOM
|
|
56
|
+
* with artifact hashes without leaking their concern into the shared parser.
|
|
57
|
+
* @param {Map<string, Array<{alg: string, content: string}>>} hashMap - PURL→hashes map
|
|
58
|
+
* @return {Sbom}
|
|
59
|
+
*/
|
|
60
|
+
attachHashes(hashMap) {
|
|
61
|
+
return this.sbomModel.attachHashes(hashMap);
|
|
62
|
+
}
|
|
53
63
|
/**
|
|
54
64
|
* @return String sbom json in a string format
|
|
55
65
|
*/
|