跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

agent-bootstrap.test.mjs

65 行 · 4.2 KB历史下载
1import assert from 'node:assert/strict';
2import { createHash } from 'node:crypto';
3import { mkdtemp, readFile, rm, writeFile, readdir } from 'node:fs/promises';
4import { tmpdir } from 'node:os';
5import { join } from 'node:path';
6import { test } from 'node:test';
7import { parseArgs, selectAsset, checksumFor, makePlan, downloadPlan, httpsResponse } from '../www/public/agent-bootstrap.mjs';
8
9const tag = 'v0.1.0-test';
10const prefix = `https://github.com/Lee-xeo/Claude-Cage/releases/download/${tag}/`;
11const data = Buffer.from('fake package fixture');
12const hash = createHash('sha256').update(data).digest('hex');
13function 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
19test('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
33test('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
39test('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
62test('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});