| 1 | import assert from 'node:assert/strict'; |
| 2 | import { createHash } from 'node:crypto'; |
| 3 | import { mkdtemp, readFile, rm, writeFile, readdir } from 'node:fs/promises'; |
| 4 | import { tmpdir } from 'node:os'; |
| 5 | import { join } from 'node:path'; |
| 6 | import { test } from 'node:test'; |
| 7 | import { parseArgs, selectAsset, checksumFor, makePlan, downloadPlan, httpsResponse } from '../www/public/agent-bootstrap.mjs'; |
| 8 | |
| 9 | const tag = 'v0.1.0-test'; |
| 10 | const prefix = `https://github.com/Lee-xeo/Claude-Cage/releases/download/${tag}/`; |
| 11 | const data = Buffer.from('fake package fixture'); |
| 12 | const hash = createHash('sha256').update(data).digest('hex'); |
| 13 | function release(platform) { |
| 14 | const suffix = { 'darwin-arm64': 'macos-arm64.dmg', 'linux-amd64': 'linux-amd64.deb', 'win32-amd64': 'windows-amd64-setup-unsigned.exe' }[platform]; |
| 15 | const name = `Cage-${tag.slice(1)}-${suffix}`; |
| 16 | return { tag_name: tag, assets: [{ name, size: data.length, browser_download_url: prefix + name }, { name: 'SHA256SUMS', browser_download_url: prefix + 'SHA256SUMS' }] }; |
| 17 | } |
| 18 | |
| 19 | test('select only exact platform artifacts and explicit unsigned consent', () => { |
| 20 | for (const platform of ['darwin-arm64', 'linux-amd64', 'win32-amd64']) { |
| 21 | const options = parseArgs(['plan', '--platform', platform, '--version', tag, '--allow-unsigned']); |
| 22 | const selected = selectAsset(release(platform), options); |
| 23 | assert.equal(selected.unsigned, platform === 'win32-amd64'); |
| 24 | assert.equal(selected.tag, tag); |
| 25 | } |
| 26 | assert.throws(() => selectAsset(release('win32-amd64'), { platform: 'win32-amd64' }), { code: 'unsigned_package_requires_consent' }); |
| 27 | assert.throws(() => parseArgs(['download', '--destination', 'relative']), { code: 'invalid_input' }); |
| 28 | assert.throws(() => parseArgs(['--version', '../../bad']), { code: 'invalid_input' }); |
| 29 | const wrong = release('linux-amd64'); wrong.assets[0].browser_download_url = 'https://example.org/package'; |
| 30 | assert.throws(() => selectAsset(wrong, { platform: 'linux-amd64' }), { code: 'invalid_release' }); |
| 31 | }); |
| 32 | |
| 33 | test('checksum manifest must identify exactly one package', () => { |
| 34 | assert.equal(checksumFor(`${hash} Cage.deb\r\n`, 'Cage.deb'), hash); |
| 35 | assert.throws(() => checksumFor(`${hash} other.deb`, 'Cage.deb'), { code: 'invalid_checksum_manifest' }); |
| 36 | assert.throws(() => checksumFor(`${hash} Cage.deb\n${hash} *Cage.deb`, 'Cage.deb'), { code: 'invalid_checksum_manifest' }); |
| 37 | }); |
| 38 | |
| 39 | test('plan pins metadata and sums to one release; downloads are verified and exclusive', async t => { |
| 40 | const options = parseArgs(['plan', '--platform', 'linux-amd64', '--version', tag]); |
| 41 | const metadata = release(options.platform); |
| 42 | const fetchFixture = async url => { |
| 43 | if (String(url).startsWith('https://api.github.com/')) return new Response(JSON.stringify(metadata)); |
| 44 | if (String(url).endsWith('SHA256SUMS')) return new Response(`${hash} ${metadata.assets[0].name}\n`); |
| 45 | return new Response(data); |
| 46 | }; |
| 47 | const plan = await makePlan(options, fetchFixture); |
| 48 | assert.equal(plan.sha256, hash); |
| 49 | assert.equal(plan.cli, '/usr/bin/claude-cage'); |
| 50 | const directory = await mkdtemp(join(tmpdir(), 'cage-bootstrap-')); |
| 51 | t.after(() => rm(directory, { recursive: true, force: true })); |
| 52 | const result = await downloadPlan(plan, directory, fetchFixture); |
| 53 | assert.deepEqual(await readFile(result.path), data); |
| 54 | assert.ok(result.installation.argv.includes(result.path)); |
| 55 | await writeFile(result.path, 'existing user file'); |
| 56 | await assert.rejects(downloadPlan(plan, directory, fetchFixture), { code: 'EEXIST' }); |
| 57 | assert.equal(await readFile(result.path, 'utf8'), 'existing user file'); |
| 58 | await assert.rejects(downloadPlan({ ...plan, asset: 'bad.deb', sha256: '0'.repeat(64) }, directory, fetchFixture), { code: 'checksum_mismatch' }); |
| 59 | assert.deepEqual(await readdir(directory), [plan.asset]); |
| 60 | }); |
| 61 | |
| 62 | test('refuse HTTP redirects and redact transport failures at the CLI boundary', async () => { |
| 63 | await assert.rejects(httpsResponse('https://example.org/', async () => new Response(null, { status: 302, headers: { location: 'http://example.org/' } })), { code: 'unsafe_download' }); |
| 64 | await assert.rejects(httpsResponse('https://example.org/', async () => { throw new Error('https://secret.example/private?token=do-not-echo'); }), error => error.code === 'download_connection_failed' && !error.message.includes('do-not-echo')); |
| 65 | }); |