|
| 1 | +import { describe, it } from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import { mkdtemp, readFile, rm, symlink, writeFile } from 'node:fs/promises' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import { dirname, join } from 'node:path' |
| 6 | +import { createServer } from 'node:net' |
| 7 | +import { createRequire } from 'node:module' |
| 8 | +import { SandboxManager } from '@anthropic-ai/sandbox-runtime' |
| 9 | +import { |
| 10 | + gitCommitSigningSandboxOverlay, |
| 11 | + parseSshPublicKey, |
| 12 | + resolveInlineSshPublicSigningKey, |
| 13 | + resolveSshAgentSocketAllowList, |
| 14 | + sshAgentSocketAllowList, |
| 15 | +} from './git-commit-signing.ts' |
| 16 | + |
| 17 | +const LAUNCHD_SOCKET = '/private/tmp/com.apple.launchd.example/Listeners' |
| 18 | +const require = createRequire(import.meta.url) |
| 19 | + |
| 20 | +describe('sandbox-runtime unix-socket patch', () => { |
| 21 | + it('honours the per-spawn socket list instead of the process-global list', async () => { |
| 22 | + const entry = require.resolve('@anthropic-ai/sandbox-runtime') |
| 23 | + const manager = join(dirname(entry), 'sandbox', 'sandbox-manager.js') |
| 24 | + const source = await readFile(manager, 'utf8') |
| 25 | + assert.match( |
| 26 | + source, |
| 27 | + /allowUnixSockets: customConfig\?\.network\?\.allowUnixSockets \?\? getAllowUnixSockets\(\)/, |
| 28 | + ) |
| 29 | + }) |
| 30 | + |
| 31 | + it( |
| 32 | + 'emits the per-spawn socket in the generated macOS seatbelt profile', |
| 33 | + { skip: process.platform !== 'darwin' }, |
| 34 | + async () => { |
| 35 | + await SandboxManager.initialize( |
| 36 | + { |
| 37 | + network: { allowedDomains: [], deniedDomains: [] }, |
| 38 | + filesystem: { denyRead: [], allowWrite: [], denyWrite: [], allowGitConfig: true }, |
| 39 | + }, |
| 40 | + () => Promise.resolve(false), |
| 41 | + false, |
| 42 | + ) |
| 43 | + try { |
| 44 | + const wrapped = await SandboxManager.wrapWithSandboxArgv('true', '/bin/zsh', { |
| 45 | + network: { |
| 46 | + allowedDomains: [], |
| 47 | + deniedDomains: [], |
| 48 | + allowUnixSockets: [LAUNCHD_SOCKET], |
| 49 | + }, |
| 50 | + }) |
| 51 | + assert.ok( |
| 52 | + wrapped.argv.some((arg) => |
| 53 | + arg.includes(`network-outbound (remote unix-socket (subpath "${LAUNCHD_SOCKET}"))`), |
| 54 | + ), |
| 55 | + ) |
| 56 | + } finally { |
| 57 | + await SandboxManager.reset() |
| 58 | + } |
| 59 | + }, |
| 60 | + ) |
| 61 | +}) |
| 62 | + |
| 63 | +describe('sshAgentSocketAllowList', () => { |
| 64 | + it('admits exactly the named socket on macOS after opt-in', () => { |
| 65 | + assert.deepEqual( |
| 66 | + sshAgentSocketAllowList({ |
| 67 | + enabled: true, |
| 68 | + authSock: LAUNCHD_SOCKET, |
| 69 | + platform: 'darwin', |
| 70 | + isSocket: true, |
| 71 | + }), |
| 72 | + [LAUNCHD_SOCKET], |
| 73 | + ) |
| 74 | + }) |
| 75 | + |
| 76 | + it('fails closed for opt-out, unsupported platforms, and unsafe paths', () => { |
| 77 | + const base = { enabled: true, platform: 'darwin' as const, isSocket: true } |
| 78 | + assert.deepEqual( |
| 79 | + sshAgentSocketAllowList({ ...base, enabled: false, authSock: LAUNCHD_SOCKET }), |
| 80 | + [], |
| 81 | + ) |
| 82 | + assert.deepEqual( |
| 83 | + sshAgentSocketAllowList({ ...base, platform: 'linux', authSock: LAUNCHD_SOCKET }), |
| 84 | + [], |
| 85 | + ) |
| 86 | + assert.deepEqual(sshAgentSocketAllowList({ ...base, authSock: 'relative.sock' }), []) |
| 87 | + assert.deepEqual( |
| 88 | + sshAgentSocketAllowList({ ...base, authSock: '/private/tmp/../tmp/agent.sock' }), |
| 89 | + [], |
| 90 | + ) |
| 91 | + assert.deepEqual(sshAgentSocketAllowList({ ...base, authSock: '/', isSocket: false }), []) |
| 92 | + }) |
| 93 | + |
| 94 | + it('checks the path is a real unix socket', async () => { |
| 95 | + const dir = await mkdtemp(join(tmpdir(), 'copse-signing-socket-')) |
| 96 | + const socketPath = join(dir, 'agent.sock') |
| 97 | + const server = createServer() |
| 98 | + await new Promise<void>((resolve) => server.listen(socketPath, resolve)) |
| 99 | + try { |
| 100 | + assert.deepEqual( |
| 101 | + await resolveSshAgentSocketAllowList({ |
| 102 | + enabled: true, |
| 103 | + authSock: socketPath, |
| 104 | + platform: 'darwin', |
| 105 | + }), |
| 106 | + [socketPath], |
| 107 | + ) |
| 108 | + assert.deepEqual( |
| 109 | + await resolveSshAgentSocketAllowList({ |
| 110 | + enabled: true, |
| 111 | + authSock: dir, |
| 112 | + platform: 'darwin', |
| 113 | + }), |
| 114 | + [], |
| 115 | + ) |
| 116 | + } finally { |
| 117 | + await new Promise<void>((resolve) => { |
| 118 | + server.close(() => { |
| 119 | + resolve() |
| 120 | + }) |
| 121 | + }) |
| 122 | + await rm(dir, { recursive: true, force: true }) |
| 123 | + } |
| 124 | + }) |
| 125 | + |
| 126 | + it('adds only the socket capability to the normal commit sandbox', () => { |
| 127 | + const overlay = gitCommitSigningSandboxOverlay('/tmp/project', [LAUNCHD_SOCKET]) |
| 128 | + assert.deepEqual(overlay.network, { |
| 129 | + allowedDomains: [], |
| 130 | + deniedDomains: [], |
| 131 | + allowLocalBinding: false, |
| 132 | + allowUnixSockets: [LAUNCHD_SOCKET], |
| 133 | + }) |
| 134 | + assert.ok(overlay.filesystem) |
| 135 | + }) |
| 136 | +}) |
| 137 | + |
| 138 | +describe('SSH public signing identity', () => { |
| 139 | + const publicLine = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAITestKey comment@example.test' |
| 140 | + const privateKeyMarker = ['-----BEGIN OPENSSH', 'PRIVATE KEY-----'].join(' ') |
| 141 | + |
| 142 | + it('normalizes one public key and drops its comment', () => { |
| 143 | + assert.equal( |
| 144 | + parseSshPublicKey(`${publicLine}\n`), |
| 145 | + 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAITestKey', |
| 146 | + ) |
| 147 | + assert.equal(parseSshPublicKey(privateKeyMarker), null) |
| 148 | + assert.equal(parseSshPublicKey(`${publicLine}\n${publicLine}`), null) |
| 149 | + }) |
| 150 | + |
| 151 | + it('uses a public sibling instead of exposing the configured private-key path', async () => { |
| 152 | + const dir = await mkdtemp(join(tmpdir(), 'copse-signing-key-')) |
| 153 | + const privatePath = join(dir, 'id_ed25519') |
| 154 | + await writeFile(`${privatePath}.pub`, `${publicLine}\n`) |
| 155 | + try { |
| 156 | + assert.equal( |
| 157 | + await resolveInlineSshPublicSigningKey(privatePath), |
| 158 | + 'key::ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAITestKey', |
| 159 | + ) |
| 160 | + } finally { |
| 161 | + await rm(dir, { recursive: true, force: true }) |
| 162 | + } |
| 163 | + }) |
| 164 | + |
| 165 | + it('accepts an explicitly configured public-key path', async () => { |
| 166 | + const dir = await mkdtemp(join(tmpdir(), 'copse-signing-public-key-')) |
| 167 | + const publicPath = join(dir, 'signing.pub') |
| 168 | + await writeFile(publicPath, publicLine) |
| 169 | + try { |
| 170 | + assert.equal( |
| 171 | + await resolveInlineSshPublicSigningKey(publicPath), |
| 172 | + 'key::ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAITestKey', |
| 173 | + ) |
| 174 | + } finally { |
| 175 | + await rm(dir, { recursive: true, force: true }) |
| 176 | + } |
| 177 | + }) |
| 178 | + |
| 179 | + it('refuses symlinks and files that are not public keys', async () => { |
| 180 | + const dir = await mkdtemp(join(tmpdir(), 'copse-signing-refusal-')) |
| 181 | + const target = join(dir, 'target.pub') |
| 182 | + const link = join(dir, 'link.pub') |
| 183 | + const invalid = join(dir, 'invalid.pub') |
| 184 | + await writeFile(target, publicLine) |
| 185 | + await symlink(target, link) |
| 186 | + await writeFile(invalid, privateKeyMarker) |
| 187 | + try { |
| 188 | + assert.equal(await resolveInlineSshPublicSigningKey(link), null) |
| 189 | + assert.equal(await resolveInlineSshPublicSigningKey(invalid), null) |
| 190 | + assert.equal(await resolveInlineSshPublicSigningKey(join(dir, 'missing')), null) |
| 191 | + } finally { |
| 192 | + await rm(dir, { recursive: true, force: true }) |
| 193 | + } |
| 194 | + }) |
| 195 | +}) |
0 commit comments