ReviewOS

also looking at this

craft-native/craft

Gate releases on shipped artifact findings

#316
Merged glennmichael123 wants to merge codex/gate-shipped-artifacts into main
11 files +454 -25
scripts/scan-release-artifacts.tsadded+172-0
Changes to scripts/scan-release-artifacts.ts
@@ -0,0 +1,172 @@
1import { createHash } from 'node:crypto'
2import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
3import { tmpdir } from 'node:os'
4import { join, resolve } from 'node:path'
5import { enforceScanPolicy, scanSbom } from './scan-sbom'
6
7type Archive = { name: string, version: string, file: string, sha256: string }
8type Component = { name: string, version?: string, 'bom-ref'?: string, [key: string]: unknown }
9type Inventory = { bomFormat: string, components: Component[], [key: string]: unknown }
10
11const root = resolve(import.meta.dir, '..')
12const sha256 = (path: string) => createHash('sha256').update(readFileSync(path)).digest('hex')
13
14function run(args: string[], cwd = root) {
15 const result = Bun.spawnSync(args, { cwd, stdout: 'pipe', stderr: 'pipe' })
16 if (result.exitCode !== 0)
17 throw new Error(`${args[0]} failed (${result.exitCode}): ${result.stderr.toString()}`)
18 return result.stdout.toString()
19}
20
21export function expectedNpmPackages(): string[] {
22 const names: string[] = []
23 for (const path of new Bun.Glob('packages/*/package.json').scanSync({ cwd: root })) {
24 const pkg = JSON.parse(readFileSync(join(root, path), 'utf8')) as { name?: string, private?: boolean }
25 if (!pkg.private) {
26 if (!pkg.name) throw new Error(`${path}: public package has no name`)
27 names.push(pkg.name)
28 }
29 }
30 return names.sort()
31}
32
33export function readArchiveManifest(directory: string, expected: string[] = expectedNpmPackages()): Archive[] {
34 const manifest = JSON.parse(readFileSync(join(directory, 'manifest.json'), 'utf8')) as { packages?: Archive[] }
35 if (!Array.isArray(manifest.packages) || manifest.packages.length !== expected.length)
36 throw new Error('Release archive manifest does not cover every public npm package')
37 const names = new Set<string>()
38 const files = new Set<string>()
39 for (const archive of manifest.packages) {
40 if (!archive || typeof archive.name !== 'string' || !archive.name || typeof archive.version !== 'string' || !archive.version
41 || typeof archive.file !== 'string' || !/^\d+\.tgz$/.test(archive.file)
42 || typeof archive.sha256 !== 'string' || !/^[a-f0-9]{64}$/.test(archive.sha256))
43 throw new Error('Malformed release archive manifest')
44 if (names.has(archive.name) || files.has(archive.file)) throw new Error('Duplicate release archive identity')
45 names.add(archive.name)
46 files.add(archive.file)
47 const path = join(directory, archive.file)
48 if (!existsSync(path) || !statSync(path).isFile() || sha256(path) !== archive.sha256)
49 throw new Error(`${archive.name}: release archive hash mismatch`)
50 }
51 if (expected.some(name => !names.has(name)))
52 throw new Error('Release archive manifest does not cover every public npm package')
53 return manifest.packages
54}
55
56export function summarizeBunAudit(report: unknown): { Critical: number, High: number } {
57 if (!report || typeof report !== 'object' || Array.isArray(report)) throw new Error('Invalid Bun audit report')
58 const counts = { Critical: 0, High: 0 }
59 for (const advisories of Object.values(report)) {
60 if (!Array.isArray(advisories)) throw new Error('Invalid Bun audit advisories')
61 for (const advisory of advisories) {
62 const severity = advisory?.severity
63 if (!['critical', 'high', 'moderate', 'low', 'info'].includes(severity))
64 throw new Error('Invalid Bun audit severity')
65 if (severity === 'critical') counts.Critical++
66 if (severity === 'high') counts.High++
67 }
68 }
69 return counts
70}
71
72function scanNpm(directory: string, output: string, syft: string, grype: string) {
73 const archives = readArchiveManifest(directory)
74 const consumer = mkdtempSync(join(tmpdir(), 'craft-release-consumer-'))
75 try {
76 const dependencies = Object.fromEntries(archives.map(({ name, file }) => [name, `file:${join(resolve(directory), file)}`]))
77 writeFileSync(join(consumer, 'package.json'), `${JSON.stringify({ private: true, dependencies }, null, 2)}\n`)
78 run([process.execPath, 'install', '--ignore-scripts', '--save', '--save-text-lockfile'], consumer)
79 const audit = Bun.spawnSync([process.execPath, 'audit', '--production', '--json'], { cwd: consumer, stdout: 'pipe', stderr: 'pipe' })
80 const auditText = audit.stdout.toString()
81 let auditCounts: ReturnType<typeof summarizeBunAudit>
82 try {
83 const report = JSON.parse(auditText)
84 auditCounts = summarizeBunAudit(report)
85 if (audit.exitCode !== 0 && Object.keys(report).length === 0)
86 throw new Error('Bun audit failed without reporting any advisories')
87 }
88 catch { throw new Error(`Bun production audit did not return a valid report: ${audit.stderr.toString()}`) }
89 mkdirSync(output, { recursive: true })
90 writeFileSync(join(output, 'bun-audit.json'), auditText)
91
92 const sbom = join(output, 'npm.cyclonedx.json')
93 run([syft, join(consumer, 'node_modules'), '--select-catalogers', '+javascript-package-cataloger', '-o', `cyclonedx-json=${sbom}`])
94 const inventory = JSON.parse(readFileSync(sbom, 'utf8')) as Inventory
95 if (inventory.bomFormat !== 'CycloneDX' || !Array.isArray(inventory.components))
96 throw new Error('Invalid npm release inventory')
97 for (const archive of archives) {
98 if (!inventory.components.some(component => component.name === archive.name && component.version === archive.version))
99 throw new Error(`${archive.name}: package missing from installed release inventory`)
100 }
101 const summary = scanSbom(sbom, join(output, 'grype'), grype)
102 enforceScanPolicy(summary.counts, true)
103 if (auditCounts.Critical || auditCounts.High)
104 throw new Error('High or Critical findings in npm production dependencies block this release')
105 readArchiveManifest(directory)
106 console.log(`Scanned ${archives.length} packed npm packages and their resolved production dependencies`)
107 }
108 finally { rmSync(consumer, { recursive: true, force: true }) }
109}
110
111export function nativeInventory(binaryRoot: string, platform: 'macos' | 'linux', version: string, zigVersion: string, discovered: Component[][] = []): Inventory {
112 const entries = platform === 'macos'
113 ? [{ path: 'bin/craft', name: 'craft-darwin-arm64', magic: ['cffaedfe', 'feedfacf'] }, { path: 'cross/darwin-x64/craft', name: 'craft-darwin-x64', magic: ['cffaedfe', 'feedfacf'] }]
114 : [{ path: 'bin/craft', name: 'craft-linux-x64', magic: ['7f454c46'] }, { path: 'cross/windows-x64/craft.exe', name: 'craft-windows-x64', magic: ['4d5a'] }]
115 const binaries: Component[] = entries.map(({ path, name, magic }) => {
116 const absolute = join(binaryRoot, path)
117 if (!existsSync(absolute) || !statSync(absolute).isFile() || statSync(absolute).size < 1024)
118 throw new Error(`${name}: missing or empty release binary`)
119 const bytes = readFileSync(absolute)
120 if (!magic.some(prefix => bytes.subarray(0, prefix.length / 2).toString('hex') === prefix))
121 throw new Error(`${name}: unexpected executable format`)
122 return { 'bom-ref': `urn:craft:binary:${name}`, type: 'application', name, version,
123 hashes: [{ alg: 'SHA-256', content: createHash('sha256').update(bytes).digest('hex') }],
124 properties: [{ name: 'craft:artifact-path', value: path }],
125 }
126 })
127 const zig = { 'bom-ref': 'urn:craft:zig-std', type: 'library', name: 'zig-std', version: zigVersion }
128 return {
129 bomFormat: 'CycloneDX', specVersion: '1.6', version: 1,
130 metadata: { component: { type: 'application', name: 'craft-native-release', version } },
131 components: [...binaries, zig, ...discovered.flat()],
132 dependencies: binaries.map(binary => ({ ref: binary['bom-ref'], dependsOn: [zig['bom-ref']] })),
133 }
134}
135
136function scanNative(binaryRoot: string, platform: 'macos' | 'linux', output: string, syft: string, grype: string) {
137 mkdirSync(output, { recursive: true })
138 const { version } = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')) as { version: string }
139 const zigVersion = Bun.JSONC.parse(readFileSync(join(root, 'pantry.jsonc'), 'utf8')).dependencies['ziglang.org'] as string
140 const paths = platform === 'macos' ? ['bin/craft', 'cross/darwin-x64/craft'] : ['bin/craft', 'cross/windows-x64/craft.exe']
141 const discovered = paths.map((path, index) => {
142 const report = join(output, `native-${index}.cyclonedx.json`)
143 run([syft, join(binaryRoot, path), '-o', `cyclonedx-json=${report}`])
144 const inventory = JSON.parse(readFileSync(report, 'utf8')) as Inventory
145 if (inventory.bomFormat !== 'CycloneDX' || (inventory.components !== undefined && !Array.isArray(inventory.components)))
146 throw new Error(`${path}: invalid binary scanner inventory`)
147 return inventory.components ?? []
148 })
149 const inventory = nativeInventory(binaryRoot, platform, version, zigVersion, discovered)
150 const sbom = join(output, 'native.cyclonedx.json')
151 writeFileSync(sbom, `${JSON.stringify(inventory, null, 2)}\n`)
152 const summary = scanSbom(sbom, join(output, 'grype'), grype)
153 enforceScanPolicy(summary.counts, true)
154 const after = nativeInventory(binaryRoot, platform, version, zigVersion)
155 for (const [index, binary] of after.components.slice(0, 2).entries()) {
156 if (JSON.stringify(binary.hashes) !== JSON.stringify(inventory.components[index]!.hashes))
157 throw new Error(`${binary.name}: release binary changed after scanning`)
158 }
159 console.log(`Scanned ${platform} release binaries and declared Zig runtime dependencies`)
160}
161
162if (import.meta.main) {
163 const args = Bun.argv.slice(2)
164 if (args.length !== 3 || args.some(arg => !arg))
165 throw new Error('Usage: scan-release-artifacts.ts <npm|macos|linux> <input> <output>')
166 const [mode, input, output] = args as [string, string, string]
167 const syft = process.env.CRAFT_SYFT ?? 'syft'
168 const grype = process.env.CRAFT_GRYPE ?? 'grype'
169 if (mode === 'npm') scanNpm(resolve(input), resolve(output), syft, grype)
170 else if (mode === 'macos' || mode === 'linux') scanNative(resolve(input), mode, resolve(output), syft, grype)
171 else throw new Error(`Unknown release artifact mode: ${mode}`)
172}