also looking at this
fix(sbom): preserve the full dependency inventory when merging
#297
4 files
+106
-120
| @@ -0,0 +1,69 @@ | ||
| 1 | import { randomUUID } from 'node:crypto' | |
| 2 | import { readFileSync, writeFileSync } from 'node:fs' | |
| 3 | import { join, resolve } from 'node:path' | |
| 4 | ||
| 5 | type Component = { name: string, 'bom-ref'?: string, [key: string]: unknown } | |
| 6 | type Bom = { | |
| 7 | bomFormat: string | |
| 8 | specVersion: string | |
| 9 | metadata?: { component?: Component, [key: string]: unknown } | |
| 10 | components: Component[] | |
| 11 | dependencies?: { ref: string, dependsOn?: string[] }[] | |
| 12 | [key: string]: unknown | |
| 13 | } | |
| 14 | ||
| 15 | export function mergeSbom(source: Bom, version: string, zigVersion: string): { combined: Bom, zig: Bom, platform: Bom } { | |
| 16 | if (source.bomFormat !== 'CycloneDX' || !Array.isArray(source.components) || source.components.length === 0) | |
| 17 | throw new Error('A populated CycloneDX inventory is required before merging') | |
| 18 | const timestamp = new Date().toISOString() | |
| 19 | const supplement = (name: string, components: Component[]): Bom => ({ | |
| 20 | bomFormat: 'CycloneDX', specVersion: source.specVersion, version: 1, | |
| 21 | metadata: { timestamp, component: { type: 'application', name, version } }, | |
| 22 | components, | |
| 23 | }) | |
| 24 | const zig = supplement('craft-zig-core', [{ | |
| 25 | 'bom-ref': 'urn:craft:zig-std', type: 'library', name: 'zig-std', version: zigVersion, | |
| 26 | description: 'Standard library for the repository-declared Zig toolchain', | |
| 27 | properties: [{ name: 'craft:version-source', value: 'pantry.jsonc dependencies.ziglang.org' }], | |
| 28 | licenses: [{ license: { id: 'MIT' } }], | |
| 29 | }]) | |
| 30 | const platform = supplement('craft-platform-deps', [ | |
| 31 | { 'bom-ref': 'urn:craft:webkit', type: 'library', name: 'WebKit', description: 'macOS WebKit system framework', properties: [{ name: 'platform', value: 'macos' }] }, | |
| 32 | { 'bom-ref': 'urn:craft:gtk3', type: 'library', name: 'GTK3', description: 'GTK 3 system toolkit', properties: [{ name: 'platform', value: 'linux' }] }, | |
| 33 | { 'bom-ref': 'urn:craft:webkit2gtk', type: 'library', name: 'WebKit2GTK', description: 'WebKit2GTK 4.1 API system dependency', properties: [{ name: 'platform', value: 'linux' }] }, | |
| 34 | { 'bom-ref': 'urn:craft:webview2', type: 'library', name: 'WebView2', description: 'Microsoft Edge WebView2 system runtime', properties: [{ name: 'platform', value: 'windows' }] }, | |
| 35 | ]) | |
| 36 | const combined = structuredClone(source) | |
| 37 | const extra = [...zig.components, ...platform.components] | |
| 38 | const existing = new Set(combined.components.map(component => component['bom-ref']).filter(Boolean)) | |
| 39 | if (extra.some(component => existing.has(component['bom-ref']!))) | |
| 40 | throw new Error('Supplement component reference collides with the source inventory') | |
| 41 | const rootRef = source.metadata?.component?.['bom-ref'] ?? 'urn:craft:root' | |
| 42 | combined.serialNumber = `urn:uuid:${randomUUID()}` | |
| 43 | combined.metadata = { | |
| 44 | ...combined.metadata, | |
| 45 | timestamp, | |
| 46 | component: { 'bom-ref': rootRef, type: 'application', name: 'craft', version: `v${version}`, licenses: [{ license: { id: 'MIT' } }] }, | |
| 47 | } | |
| 48 | combined.components.push(...extra) | |
| 49 | combined.dependencies ??= [] | |
| 50 | let rootDependency = combined.dependencies.find(dependency => dependency.ref === rootRef) | |
| 51 | if (!rootDependency) { | |
| 52 | rootDependency = { ref: rootRef, dependsOn: [] } | |
| 53 | combined.dependencies.push(rootDependency) | |
| 54 | } | |
| 55 | rootDependency.dependsOn = [...new Set([...(rootDependency.dependsOn ?? []), ...extra.map(component => component['bom-ref']!)])] | |
| 56 | return { combined, zig, platform } | |
| 57 | } | |
| 58 | ||
| 59 | if (import.meta.main) { | |
| 60 | const directory = resolve(Bun.argv[2] ?? 'sbom') | |
| 61 | const root = resolve(import.meta.dir, '..') | |
| 62 | const { version } = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')) | |
| 63 | const toolchain = Bun.JSONC.parse(readFileSync(join(root, 'pantry.jsonc'), 'utf8')).dependencies['ziglang.org'] | |
| 64 | const source = JSON.parse(readFileSync(join(directory, 'full-sbom.cyclonedx.json'), 'utf8')) | |
| 65 | const { combined, zig, platform } = mergeSbom(source, version, toolchain) | |
| 66 | for (const [name, document] of Object.entries({ 'craft-sbom.json': combined, 'zig-dependencies.json': zig, 'platform-dependencies.json': platform })) | |
| 67 | writeFileSync(join(directory, name), `${JSON.stringify(document, null, 2)}\n`) | |
| 68 | console.log(`Merged ${source.components.length} scanned and ${zig.components.length + platform.components.length} declared runtime components`) | |
| 69 | } | |