-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
binary.js
855 lines (838 loc) · 25.3 KB
/
binary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
import { Buffer } from "node:buffer";
import { spawnSync } from "node:child_process";
import {
existsSync,
lstatSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
} from "node:fs";
import { arch as _arch, platform as _platform, homedir, tmpdir } from "node:os";
import { basename, dirname, join, resolve } from "node:path";
import process from "node:process";
import { PackageURL } from "packageurl-js";
import {
DEBUG_MODE,
TIMEOUT_MS,
adjustLicenseInformation,
findLicenseId,
isSpdxLicenseExpression,
} from "./utils.js";
import { URL, fileURLToPath } from "node:url";
let url = import.meta.url;
if (!url.startsWith("file://")) {
url = new URL(`file://${import.meta.url}`).toString();
}
const dirName = import.meta ? dirname(fileURLToPath(url)) : __dirname;
const isWin = _platform() === "win32";
let platform = _platform();
let extn = "";
let pluginsBinSuffix = "";
if (platform === "win32") {
platform = "windows";
extn = ".exe";
}
let arch = _arch();
switch (arch) {
case "x32":
arch = "386";
break;
case "x64":
arch = "amd64";
if (platform === "windows") {
pluginsBinSuffix = "-windows-amd64";
} else if (platform === "darwin") {
pluginsBinSuffix = "-darwin-amd64";
}
break;
case "arm64":
pluginsBinSuffix = "-arm64";
if (platform === "windows") {
pluginsBinSuffix = "-windows-arm64";
} else if (platform === "darwin") {
pluginsBinSuffix = "-darwin-arm64";
}
break;
case "ppc64":
arch = "ppc64le";
pluginsBinSuffix = "-ppc64";
break;
}
// Retrieve the cdxgen plugins directory
let CDXGEN_PLUGINS_DIR = process.env.CDXGEN_PLUGINS_DIR;
// Is there a non-empty local plugins directory
if (
!CDXGEN_PLUGINS_DIR &&
existsSync(join(dirName, "plugins")) &&
existsSync(join(dirName, "plugins", "trivy"))
) {
CDXGEN_PLUGINS_DIR = join(dirName, "plugins");
}
// Is there a non-empty local node_modules directory
if (
!CDXGEN_PLUGINS_DIR &&
existsSync(
join(
dirName,
"node_modules",
"@cyclonedx",
`cdxgen-plugins-bin${pluginsBinSuffix}`,
"plugins",
),
) &&
existsSync(
join(
dirName,
"node_modules",
"@cyclonedx",
`cdxgen-plugins-bin${pluginsBinSuffix}`,
"plugins",
"trivy",
),
)
) {
CDXGEN_PLUGINS_DIR = join(
dirName,
"node_modules",
"@cyclonedx",
`cdxgen-plugins-bin${pluginsBinSuffix}`,
"plugins",
);
}
if (!CDXGEN_PLUGINS_DIR) {
let globalNodePath = process.env.GLOBAL_NODE_MODULES_PATH || undefined;
if (!globalNodePath) {
const result = spawnSync(
isWin ? "npm.cmd" : "npm",
["root", "--quiet", "-g"],
{
encoding: "utf-8",
},
);
if (result) {
const stdout = result.stdout;
if (stdout) {
globalNodePath = `${Buffer.from(stdout).toString().trim()}/`;
}
}
}
if (globalNodePath) {
const globalPlugins = join(
globalNodePath,
"@cyclonedx",
`cdxgen-plugins-bin${pluginsBinSuffix}`,
"plugins",
);
if (existsSync(globalPlugins)) {
CDXGEN_PLUGINS_DIR = globalPlugins;
if (DEBUG_MODE) {
console.log("Found global plugins", CDXGEN_PLUGINS_DIR);
}
}
}
}
if (!CDXGEN_PLUGINS_DIR) {
if (DEBUG_MODE) {
console.warn(
"cdxgen plugins was not found. Please install with npm install -g @cyclonedx/cdxgen-plugins-bin",
);
}
CDXGEN_PLUGINS_DIR = "";
}
let GOVERSION_BIN = null;
if (existsSync(join(CDXGEN_PLUGINS_DIR, "goversion"))) {
GOVERSION_BIN = join(
CDXGEN_PLUGINS_DIR,
"goversion",
`goversion-${platform}-${arch}${extn}`,
);
}
let TRIVY_BIN = null;
if (existsSync(join(CDXGEN_PLUGINS_DIR, "trivy"))) {
TRIVY_BIN = join(
CDXGEN_PLUGINS_DIR,
"trivy",
`trivy-cdxgen-${platform}-${arch}${extn}`,
);
} else if (process.env.TRIVY_CMD) {
TRIVY_BIN = process.env.TRIVY_CMD;
}
let CARGO_AUDITABLE_BIN = null;
if (existsSync(join(CDXGEN_PLUGINS_DIR, "cargo-auditable"))) {
CARGO_AUDITABLE_BIN = join(
CDXGEN_PLUGINS_DIR,
"cargo-auditable",
`cargo-auditable-cdxgen-${platform}-${arch}${extn}`,
);
} else if (process.env.CARGO_AUDITABLE_CMD) {
CARGO_AUDITABLE_BIN = process.env.CARGO_AUDITABLE_CMD;
}
let OSQUERY_BIN = null;
if (existsSync(join(CDXGEN_PLUGINS_DIR, "osquery"))) {
OSQUERY_BIN = join(
CDXGEN_PLUGINS_DIR,
"osquery",
`osqueryi-${platform}-${arch}${extn}`,
);
// osqueryi-darwin-amd64.app/Contents/MacOS/osqueryd
if (platform === "darwin") {
OSQUERY_BIN = `${OSQUERY_BIN}.app/Contents/MacOS/osqueryd`;
}
} else if (process.env.OSQUERY_CMD) {
OSQUERY_BIN = process.env.OSQUERY_CMD;
}
let DOSAI_BIN = null;
if (existsSync(join(CDXGEN_PLUGINS_DIR, "dosai"))) {
DOSAI_BIN = join(
CDXGEN_PLUGINS_DIR,
"dosai",
`dosai-${platform}-${arch}${extn}`,
);
} else if (process.env.DOSAI_CMD) {
DOSAI_BIN = process.env.DOSAI_CMD;
}
// Blint bin
const BLINT_BIN = process.env.BLINT_CMD || "blint";
// Keep this list updated every year
const OS_DISTRO_ALIAS = {
"ubuntu-4.10": "warty",
"ubuntu-5.04": "hoary",
"ubuntu-5.10": "breezy",
"ubuntu-6.06": "dapper",
"ubuntu-6.10": "edgy",
"ubuntu-7.04": "feisty",
"ubuntu-7.10": "gutsy",
"ubuntu-8.04": "hardy",
"ubuntu-8.10": "intrepid",
"ubuntu-9.04": "jaunty",
"ubuntu-9.10": "karmic",
"ubuntu-10.04": "lucid",
"ubuntu-10.10": "maverick",
"ubuntu-11.04": "natty",
"ubuntu-11.10": "oneiric",
"ubuntu-12.04": "precise",
"ubuntu-12.10": "quantal",
"ubuntu-13.04": "raring",
"ubuntu-13.10": "saucy",
"ubuntu-14.04": "trusty",
"ubuntu-14.10": "utopic",
"ubuntu-15.04": "vivid",
"ubuntu-15.10": "wily",
"ubuntu-16.04": "xenial",
"ubuntu-16.10": "yakkety",
"ubuntu-17.04": "zesty",
"ubuntu-17.10": "artful",
"ubuntu-18.04": "bionic",
"ubuntu-18.10": "cosmic",
"ubuntu-19.04": "disco",
"ubuntu-19.10": "eoan",
"ubuntu-20.04": "focal",
"ubuntu-20.10": "groovy",
"ubuntu-22.04": "jammy",
"ubuntu-23.04": "lunar",
"ubuntu-23.10": "mantic",
"ubuntu-24.04": "noble",
"debian-14": "forky",
"debian-14.5": "forky",
"debian-13": "trixie",
"debian-13.5": "trixie",
"debian-12": "bookworm",
"debian-12.5": "bookworm",
"debian-11": "bullseye",
"debian-11.5": "bullseye",
"debian-10": "buster",
"debian-10.5": "buster",
"debian-9": "stretch",
"debian-9.5": "stretch",
"debian-8": "jessie",
"debian-8.5": "jessie",
"debian-7": "wheezy",
"debian-7.5": "wheezy",
"debian-6": "squeeze",
"debian-5": "lenny",
"debian-4": "etch",
"debian-3.1": "sarge",
"debian-3": "woody",
"debian-2.2": "potato",
"debian-2.1": "slink",
"debian-2": "hamm",
"debian-1.3": "bo",
"debian-1.2": "rex",
"debian-1.1": "buzz",
};
export function getGoBuildInfo(src) {
if (GOVERSION_BIN) {
let result = spawnSync(GOVERSION_BIN, [src], {
encoding: "utf-8",
});
if (result.status !== 0 || result.error || !result.stdout) {
if (result.stdout || result.stderr) {
console.error(result.stdout, result.stderr);
}
if (DEBUG_MODE) {
console.log("Falling back to go version command");
}
result = spawnSync("go", ["version", "-v", "-m", src], {
encoding: "utf-8",
});
if (result.status !== 0 || result.error) {
if (result.stdout || result.stderr) {
console.error(result.stdout, result.stderr);
}
}
}
if (result) {
const stdout = result.stdout;
if (stdout) {
const cmdOutput = Buffer.from(stdout).toString();
return cmdOutput;
}
}
}
return undefined;
}
export function getCargoAuditableInfo(src) {
if (CARGO_AUDITABLE_BIN) {
const result = spawnSync(CARGO_AUDITABLE_BIN, [src], {
encoding: "utf-8",
});
if (result.status !== 0 || result.error) {
if (result.stdout || result.stderr) {
console.error(result.stdout, result.stderr);
}
}
if (result) {
const stdout = result.stdout;
if (stdout) {
const cmdOutput = Buffer.from(stdout).toString();
return cmdOutput;
}
}
}
return undefined;
}
export function getOSPackages(src) {
const pkgList = [];
const dependenciesList = [];
const allTypes = new Set();
if (TRIVY_BIN) {
let imageType = "image";
const trivyCacheDir = join(homedir(), ".cache", "trivy");
try {
mkdirSync(join(trivyCacheDir, "db"), { recursive: true });
mkdirSync(join(trivyCacheDir, "java-db"), { recursive: true });
} catch (err) {
// ignore errors
}
if (existsSync(src)) {
imageType = "rootfs";
}
const tempDir = mkdtempSync(join(tmpdir(), "trivy-cdxgen-"));
const bomJsonFile = join(tempDir, "trivy-bom.json");
const args = [
imageType,
"--skip-db-update",
"--skip-java-db-update",
"--offline-scan",
"--skip-files",
"**/*.jar",
"--no-progress",
"--exit-code",
"0",
"--format",
"cyclonedx",
"--cache-dir",
trivyCacheDir,
"--output",
bomJsonFile,
];
if (!DEBUG_MODE) {
args.push("-q");
}
args.push(src);
if (DEBUG_MODE) {
console.log("Executing", TRIVY_BIN, args.join(" "));
}
const result = spawnSync(TRIVY_BIN, args, {
encoding: "utf-8",
});
if (result.status !== 0 || result.error) {
if (result.stdout || result.stderr) {
console.error(result.stdout, result.stderr);
}
}
if (existsSync(bomJsonFile)) {
let tmpBom = {};
try {
tmpBom = JSON.parse(
readFileSync(bomJsonFile, {
encoding: "utf-8",
}),
);
} catch (e) {
// ignore errors
}
// Clean up
if (tempDir?.startsWith(tmpdir())) {
if (DEBUG_MODE) {
console.log(`Cleaning up ${tempDir}`);
}
if (rmSync) {
rmSync(tempDir, { recursive: true, force: true });
}
}
const osReleaseData = {};
let osReleaseFile = undefined;
// Let's try to read the os-release file from various locations
if (existsSync(join(src, "etc", "os-release"))) {
osReleaseFile = join(src, "etc", "os-release");
} else if (existsSync(join(src, "usr", "lib", "os-release"))) {
osReleaseFile = join(src, "usr", "lib", "os-release");
}
if (osReleaseFile) {
const osReleaseInfo = readFileSync(osReleaseFile, "utf-8");
if (osReleaseInfo) {
osReleaseInfo.split("\n").forEach((l) => {
if (!l.startsWith("#") && l.includes("=")) {
const tmpA = l.split("=");
osReleaseData[tmpA[0]] = tmpA[1].replace(/"/g, "");
}
});
}
}
if (DEBUG_MODE) {
console.log(osReleaseData);
}
let distro_codename =
osReleaseData["VERSION_CODENAME"] ||
osReleaseData["CENTOS_MANTISBT_PROJECT"] ||
osReleaseData["REDHAT_SUPPORT_PRODUCT"] ||
"";
distro_codename = distro_codename.toLowerCase();
let distro_id = osReleaseData["ID"] || "";
const distro_id_like = osReleaseData["ID_LIKE"] || "";
let purl_type = "rpm";
switch (distro_id) {
case "debian":
case "ubuntu":
case "pop":
purl_type = "deb";
break;
case "alpine":
purl_type = "apk";
if (osReleaseData.VERSION_ID) {
const versionParts = osReleaseData["VERSION_ID"].split(".");
if (versionParts.length >= 2) {
distro_codename = `alpine-${versionParts[0]}.${versionParts[1]}`;
}
}
break;
default:
if (distro_id_like.includes("debian")) {
purl_type = "deb";
} else if (
distro_id_like.includes("rhel") ||
distro_id_like.includes("centos") ||
distro_id_like.includes("fedora")
) {
purl_type = "rpm";
}
break;
}
if (osReleaseData["VERSION_ID"]) {
distro_id = `${distro_id}-${osReleaseData["VERSION_ID"]}`;
}
const tmpDependencies = {};
(tmpBom.dependencies || []).forEach((d) => {
tmpDependencies[d.ref] = d.dependsOn;
});
if (tmpBom?.components) {
for (const comp of tmpBom.components) {
if (comp.purl) {
// Retain go components alone from trivy
if (
comp.purl.startsWith("pkg:npm") ||
comp.purl.startsWith("pkg:maven") ||
comp.purl.startsWith("pkg:pypi") ||
comp.purl.startsWith("pkg:cargo") ||
comp.purl.startsWith("pkg:composer") ||
comp.purl.startsWith("pkg:gem") ||
comp.purl.startsWith("pkg:nuget") ||
comp.purl.startsWith("pkg:pub") ||
comp.purl.startsWith("pkg:hackage") ||
comp.purl.startsWith("pkg:hex") ||
comp.purl.startsWith("pkg:conan") ||
comp.purl.startsWith("pkg:clojars") ||
comp.purl.startsWith("pkg:github")
) {
continue;
}
const origBomRef = comp["bom-ref"];
// Fix the group
let group = dirname(comp.name);
const name = basename(comp.name);
let purlObj = undefined;
if (group === ".") {
group = "";
}
comp.group = group;
comp.name = name;
if (group === "") {
try {
purlObj = PackageURL.fromString(comp.purl);
if (purlObj.namespace && purlObj.namespace !== "") {
group = purlObj.namespace;
comp.group = group;
purlObj.namespace = group;
}
purlObj.qualifiers = purlObj.qualifiers || {};
if (distro_id?.length) {
purlObj.qualifiers["distro"] = distro_id;
}
if (distro_codename?.length) {
purlObj.qualifiers["distro_name"] = distro_codename;
}
// Remove any epoch values
delete purlObj.qualifiers.epoch;
// Bug fix for mageia and oracle linux
// Type is being returned as none for ubuntu as well!
if (purlObj.type === "none") {
purlObj["type"] = purl_type;
purlObj["namespace"] = "";
comp.group = "";
if (comp.purl?.includes(".mga")) {
purlObj["namespace"] = "mageia";
comp.group = "mageia";
purlObj.qualifiers["distro"] = "mageia";
distro_codename = "mga";
}
comp.purl = new PackageURL(
purlObj.type,
purlObj.namespace,
name,
purlObj.version,
purlObj.qualifiers,
purlObj.subpath,
).toString();
comp["bom-ref"] = decodeURIComponent(comp.purl);
}
if (purlObj.type !== "none") {
allTypes.add(purlObj.type);
}
// Prefix distro codename for ubuntu
if (purlObj.qualifiers?.distro) {
allTypes.add(purlObj.qualifiers.distro);
if (OS_DISTRO_ALIAS[purlObj.qualifiers.distro]) {
distro_codename =
OS_DISTRO_ALIAS[purlObj.qualifiers.distro];
} else if (group === "alpine") {
const dtmpA = purlObj.qualifiers.distro.split(".");
if (dtmpA && dtmpA.length > 2) {
distro_codename = `${dtmpA[0]}.${dtmpA[1]}`;
}
} else if (group === "photon") {
const dtmpA = purlObj.qualifiers.distro.split("-");
if (dtmpA && dtmpA.length > 1) {
distro_codename = dtmpA[0];
}
} else if (group === "redhat") {
const dtmpA = purlObj.qualifiers.distro.split(".");
if (dtmpA && dtmpA.length > 1) {
distro_codename = dtmpA[0].replace(
"redhat",
"enterprise_linux",
);
}
}
}
if (distro_codename !== "") {
allTypes.add(distro_codename);
allTypes.add(purlObj.namespace);
comp.purl = new PackageURL(
purlObj.type,
purlObj.namespace,
name,
purlObj.version,
purlObj.qualifiers,
purlObj.subpath,
).toString();
comp["bom-ref"] = decodeURIComponent(comp.purl);
}
} catch (err) {
// continue regardless of error
}
}
if (comp.purl.includes("epoch=")) {
try {
purlObj = PackageURL.fromString(comp.purl);
purlObj.qualifiers = purlObj.qualifiers || {};
if (distro_id?.length) {
purlObj.qualifiers["distro"] = distro_id;
}
if (distro_codename?.length) {
purlObj.qualifiers["distro_name"] = distro_codename;
}
delete purlObj.qualifiers.epoch;
allTypes.add(purlObj.namespace);
comp.purl = new PackageURL(
purlObj.type,
purlObj.namespace,
name,
purlObj.version,
purlObj.qualifiers,
purlObj.subpath,
).toString();
comp["bom-ref"] = decodeURIComponent(comp.purl);
} catch (err) {
// continue regardless of error
console.log(err);
}
}
// Fix licenses
if (
comp.licenses &&
Array.isArray(comp.licenses) &&
comp.licenses.length
) {
const newLicenses = [];
for (const aLic of comp.licenses) {
if (aLic.license.name) {
if (isSpdxLicenseExpression(aLic.license.name)) {
newLicenses.push({ expression: aLic.license.name });
} else {
const possibleId = findLicenseId(aLic.license.name);
if (possibleId !== aLic.license.name) {
newLicenses.push({ license: { id: possibleId } });
} else {
newLicenses.push({
license: { name: aLic.license.name },
});
}
}
} else if (
Object.keys(aLic).length &&
Object.keys(aLic.license).length
) {
newLicenses.push(aLic);
}
}
comp.licenses = adjustLicenseInformation(newLicenses);
}
// Fix hashes
if (
comp.hashes &&
Array.isArray(comp.hashes) &&
comp.hashes.length
) {
const hashContent = comp.hashes[0].content;
if (!hashContent || hashContent.length < 32) {
delete comp.hashes;
}
}
const compProperties = comp.properties;
let srcName = undefined;
let srcVersion = undefined;
if (compProperties && Array.isArray(compProperties)) {
for (const aprop of compProperties) {
if (aprop.name.endsWith("SrcName")) {
srcName = aprop.value;
}
if (aprop.name.endsWith("SrcVersion")) {
srcVersion = aprop.value;
}
}
}
delete comp.properties;
pkgList.push(comp);
const compDeps = retrieveDependencies(
tmpDependencies,
origBomRef,
comp,
);
if (compDeps) {
dependenciesList.push(compDeps);
}
// If there is a source package defined include it as well
if (srcName && srcVersion && srcName !== comp.name) {
const newComp = Object.assign({}, comp);
newComp.name = srcName;
newComp.version = srcVersion;
if (purlObj) {
newComp.purl = new PackageURL(
purlObj.type,
purlObj.namespace,
srcName,
srcVersion,
purlObj.qualifiers,
purlObj.subpath,
).toString();
}
newComp["bom-ref"] = decodeURIComponent(newComp.purl);
pkgList.push(newComp);
}
}
}
}
}
}
return {
osPackages: pkgList,
dependenciesList,
allTypes: Array.from(allTypes),
};
}
const retrieveDependencies = (tmpDependencies, origBomRef, comp) => {
try {
const tmpDependsOn = tmpDependencies[origBomRef] || [];
const dependsOn = new Set();
tmpDependsOn.forEach((d) => {
try {
const compPurl = PackageURL.fromString(comp.purl);
const tmpPurl = PackageURL.fromString(d.replace("none", compPurl.type));
tmpPurl.type = compPurl.type;
tmpPurl.namespace = compPurl.namespace;
tmpPurl.qualifiers = tmpPurl.qualifiers || {};
if (compPurl.qualifiers) {
if (compPurl.qualifiers.distro_name) {
tmpPurl.qualifiers.distro_name = compPurl.qualifiers.distro_name;
}
if (compPurl.qualifiers.distro) {
tmpPurl.qualifiers.distro = compPurl.qualifiers.distro;
}
delete tmpPurl.qualifiers.epoch;
}
dependsOn.add(decodeURIComponent(tmpPurl.toString()));
} catch (e) {
// ignore
}
});
return { ref: comp["bom-ref"], dependsOn: Array.from(dependsOn).sort() };
} catch (e) {
// ignore
}
return undefined;
};
export function executeOsQuery(query) {
if (OSQUERY_BIN) {
if (!query.endsWith(";")) {
query = `${query};`;
}
const args = ["--json", query];
// On darwin, we need to disable the safety check and run cdxgen with sudo
// https://github.com/osquery/osquery/issues/1382
if (platform === "darwin") {
args.push("--allow_unsafe");
args.push("--disable_logging");
args.push("--disable_events");
}
if (DEBUG_MODE) {
console.log("Executing", OSQUERY_BIN, args.join(" "));
}
const result = spawnSync(OSQUERY_BIN, args, {
encoding: "utf-8",
maxBuffer: 50 * 1024 * 1024,
timeout: 60 * 1000,
});
if (result.status !== 0 || result.error) {
if (
DEBUG_MODE &&
result.stderr &&
!result.stderr.includes("no such table")
) {
console.error(result.stdout, result.stderr);
}
}
if (result) {
const stdout = result.stdout;
if (stdout) {
const cmdOutput = Buffer.from(stdout).toString();
if (cmdOutput !== "") {
try {
return JSON.parse(cmdOutput);
} catch (err) {
// ignore
if (DEBUG_MODE) {
console.log("Unable to parse the output from query", query);
console.log(
"This could be due to the amount of data returned or the query being invalid for the given platform.",
);
}
}
}
return undefined;
}
}
}
return undefined;
}
/**
* Method to execute dosai to create slices for dotnet
*
* @param {string} src Source Path
* @param {string} slicesFile Slices file name
* @returns boolean
*/
export function getDotnetSlices(src, slicesFile) {
if (!DOSAI_BIN) {
return false;
}
const args = ["methods", "--path", src, "--o", slicesFile];
if (DEBUG_MODE) {
console.log("Executing", DOSAI_BIN, args.join(" "));
}
const result = spawnSync(DOSAI_BIN, args, {
encoding: "utf-8",
timeout: TIMEOUT_MS,
cwd: src,
});
if (result.status !== 0 || result.error) {
if (DEBUG_MODE && result.error) {
if (result.stderr) {
console.error(result.stdout, result.stderr);
} else {
console.log("Check if dosai plugin was installed successfully.");
}
}
return false;
}
return true;
}
/**
* Method to generate binary SBOM using blint
*
* @param {string} src Path to binary or its directory
* @param {string} binaryBomFile Path to binary
* @param {boolean} deepMode Deep mode flag
*
* @return {boolean} Result of the generation
*/
export function getBinaryBom(src, binaryBomFile, deepMode) {
if (!BLINT_BIN) {
return false;
}
const args = ["sbom", "-i", resolve(src), "-o", binaryBomFile];
if (deepMode) {
args.push("--deep");
}
if (DEBUG_MODE) {
console.log("Executing", BLINT_BIN, args.join(" "));
}
const cwd = lstatSync(src).isDirectory() ? src : dirname(src);
const result = spawnSync(BLINT_BIN, args, {
encoding: "utf-8",
timeout: TIMEOUT_MS,
cwd,
});
if (result.status !== 0 || result.error) {
if (result.stderr) {
console.error(result.stdout, result.stderr);
} else {
console.log(
"Install blint using 'pip install blint' or use the cdxgen container image.",
);
}
return false;
}
return true;
}