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.test.tsadded+98-0
Changes to scripts/scan-release-artifacts.test.ts
@@ -0,0 +1,98 @@
1import { afterEach, expect, test } from 'bun:test'
2import { createHash } from 'node:crypto'
3import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
4import { tmpdir } from 'node:os'
5import { join } from 'node:path'
6import { publishNpmArchives } from './publish-npm-archives'
7import { expectedNpmPackages, nativeInventory, readArchiveManifest, summarizeBunAudit } from './scan-release-artifacts'
8
9const roots: string[] = []
10afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }) })
11const fixture = () => {
12 const root = mkdtempSync(join(tmpdir(), 'craft-release-artifacts-test-'))
13 roots.push(root)
14 return root
15}
16
17function archives(root: string) {
18 const packages = expectedNpmPackages().map((name, index) => {
19 const file = `${index}.tgz`
20 const bytes = `${name}@1.2.3`
21 writeFileSync(join(root, file), bytes)
22 return { name, version: '1.2.3', file, sha256: createHash('sha256').update(bytes).digest('hex') }
23 })
24 writeFileSync(join(root, 'manifest.json'), JSON.stringify({ packages }))
25 return packages
26}
27
28test('release manifest requires every public archive and unchanged bytes', () => {
29 const root = fixture()
30 const packages = archives(root)
31 expect(readArchiveManifest(root)).toHaveLength(packages.length)
32 writeFileSync(join(root, packages[0]!.file), 'changed')
33 expect(() => readArchiveManifest(root)).toThrow('hash mismatch')
34 writeFileSync(join(root, packages[0]!.file), `${packages[0]!.name}@1.2.3`)
35 packages[0]!.file = '../escape.tgz'
36 writeFileSync(join(root, 'manifest.json'), JSON.stringify({ packages }))
37 expect(() => readArchiveManifest(root)).toThrow('Malformed')
38 packages.shift()
39 writeFileSync(join(root, 'manifest.json'), JSON.stringify({ packages }))
40 expect(() => readArchiveManifest(root)).toThrow('does not cover every')
41})
42
43test('Bun audit parser fails closed and only High or Critical blocks', () => {
44 expect(summarizeBunAudit({})).toEqual({ Critical: 0, High: 0 })
45 expect(summarizeBunAudit({ svelte: [{ severity: 'moderate' }], unsafe: [{ severity: 'high' }, { severity: 'critical' }] }))
46 .toEqual({ Critical: 1, High: 1 })
47 for (const invalid of [null, [], { package: null }, { package: [{}] }, { package: [{ severity: 'unknown' }] }])
48 expect(() => summarizeBunAudit(invalid)).toThrow()
49})
50
51test('release scanner rejects extra arguments before scanning', () => {
52 const result = Bun.spawnSync([
53 process.execPath,
54 join(import.meta.dir, 'scan-release-artifacts.ts'),
55 'linux', '/missing', '/missing', 'done',
56 ], { stdout: 'pipe', stderr: 'pipe' })
57 expect(result.exitCode).not.toBe(0)
58 expect(result.stderr.toString()).toContain('Usage: scan-release-artifacts.ts')
59})
60
61function binary(root: string, path: string, magic: string) {
62 const absolute = join(root, path)
63 mkdirSync(join(absolute, '..'), { recursive: true })
64 const bytes = Buffer.alloc(2048)
65 Buffer.from(magic, 'hex').copy(bytes)
66 writeFileSync(absolute, bytes)
67}
68
69test('native inventory binds exact binaries and Zig runtime version', () => {
70 const root = fixture()
71 binary(root, 'bin/craft', '7f454c46')
72 binary(root, 'cross/windows-x64/craft.exe', '4d5a')
73 const inventory = nativeInventory(root, 'linux', '1.2.3', '0.17.0-dev.1963')
74 expect(inventory.components.map(component => component.name)).toEqual(['craft-linux-x64', 'craft-windows-x64', 'zig-std'])
75 expect(inventory.components[0]?.hashes).toEqual([{ alg: 'SHA-256', content: createHash('sha256').update(readFileSync(join(root, 'bin/craft'))).digest('hex') }])
76 writeFileSync(join(root, 'cross/windows-x64/craft.exe'), 'not a binary')
77 expect(() => nativeInventory(root, 'linux', '1.2.3', '0.17.0-dev.1963')).toThrow('missing or empty')
78 binary(root, 'cross/windows-x64/craft.exe', '7f454c46')
79 expect(() => nativeInventory(root, 'linux', '1.2.3', '0.17.0-dev.1963')).toThrow('unexpected executable format')
80})
81
82test('publisher uses only hash-verified archives and skips an existing version', () => {
83 const root = fixture()
84 const packages = archives(root)
85 const calls: string[][] = []
86 publishNpmArchives(root, (args) => {
87 calls.push(args)
88 if (args[0] === 'view')
89 return args[1] === `${packages[0]!.name}@1.2.3`
90 ? { exitCode: 0, stdout: '"1.2.3"', stderr: '' }
91 : { exitCode: 1, stdout: '', stderr: 'E404' }
92 return { exitCode: 0, stdout: '', stderr: '' }
93 })
94 expect(calls.filter(args => args[0] === 'publish')).toHaveLength(packages.length - 1)
95 expect(calls.find(args => args[0] === 'publish')).toContain('--ignore-scripts')
96 expect(calls.find(args => args[0] === 'publish')).toContain('--provenance')
97 expect(calls.some(args => args[0] === 'publish' && args[1] === join(root, packages[0]!.file))).toBe(false)
98})