also looking at this
Gate releases on shipped artifact findings
#316
11 files
+454
-25
| @@ -0,0 +1,98 @@ | ||
| 1 | import { afterEach, expect, test } from 'bun:test' | |
| 2 | import { createHash } from 'node:crypto' | |
| 3 | import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' | |
| 4 | import { tmpdir } from 'node:os' | |
| 5 | import { join } from 'node:path' | |
| 6 | import { publishNpmArchives } from './publish-npm-archives' | |
| 7 | import { expectedNpmPackages, nativeInventory, readArchiveManifest, summarizeBunAudit } from './scan-release-artifacts' | |
| 8 | ||
| 9 | const roots: string[] = [] | |
| 10 | afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }) }) | |
| 11 | const fixture = () => { | |
| 12 | const root = mkdtempSync(join(tmpdir(), 'craft-release-artifacts-test-')) | |
| 13 | roots.push(root) | |
| 14 | return root | |
| 15 | } | |
| 16 | ||
| 17 | function 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 | ||
| 28 | test('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 | ||
| 43 | test('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 | ||
| 51 | test('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 | ||
| 61 | function 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 | ||
| 69 | test('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 | ||
| 82 | test('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 | }) | |