Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

3.8 KB
Raw Blame
import axios from 'axios'
import { createHash } from 'node:crypto'
import { execFileSync } from 'node:child_process'
import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { setupServer } from 'msw/node'
import { afterAll, beforeAll, expect, it } from 'vitest'
import type { ApiClient } from '@/api/apiClient'
import { requestSsh } from '@/lib/ssh'
import { sshHandlers } from './ssh-handlers'

const server = setupServer(...sshHandlers)
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
afterAll(() => server.close())

function hostKey() {
  const directory = mkdtempSync(join(tmpdir(), 'boxlite-ssh-preview-'))
  try {
    const path = join(directory, 'host')
    execFileSync('ssh-keygen', ['-q', '-t', 'ed25519', '-N', '', '-f', path])
    return {
      hostPublicKey: readFileSync(`${path}.pub`, 'utf8').split(' ').slice(0, 2).join(' '),
      config: {
        listen_address: '0.0.0.0:22',
        host_private_key: readFileSync(path, 'utf8'),
        accounts: [{ login: 'box', authorized_keys: [readFileSync(`${path}.pub`, 'utf8').trim()] }],
      },
    }
  } finally {
    rmSync(directory, { recursive: true, force: true })
  }
}

it('mock SSH preserves the configured host identity across disable and enable', async () => {
  const api = { axiosInstance: axios.create({ baseURL: 'https://ssh-preview.test/' }) } as unknown as ApiClient
  const keys = hostKey()
  const { data: configured } = await api.axiosInstance.post('v1/mock-org/boxes/identity-box/ssh', keys.config)
  expect(configured.host_public_key).toBe(keys.hostPublicKey)
  const expectedFingerprint = createHash('sha256')
    .update(Buffer.from(keys.hostPublicKey.split(' ')[1], 'base64'))
    .digest('base64')
    .replace(/=+$/, '')
  expect(configured.host_key_fingerprint).toBe(`SHA256:${expectedFingerprint}`)
  const disabled = await requestSsh(api, 'mock-org', 'identity-box', 'disable')
  expect(disabled.host_public_key).toBe('')
  expect((await requestSsh(api, 'mock-org', 'identity-box', 'status')).host_public_key).toBe('')
  const enabled = await requestSsh(api, 'mock-org', 'identity-box', 'enable')
  expect(enabled.host_public_key).toBe(configured.host_public_key)
  expect(enabled.host_key_fingerprint).toBe(configured.host_key_fingerprint)
  expect(JSON.stringify(enabled)).not.toContain(keys.config.host_private_key)
})

it.each(['missing', 'empty', 'truncated'])(
  'mock SSH rejects a %s private-key block without saving configuration',
  async (kind) => {
    const api = { axiosInstance: axios.create({ baseURL: 'https://ssh-preview.test/' }) } as unknown as ApiClient
    const keys = hostKey()
    const envelope = Buffer.from(keys.config.host_private_key.replace(/-----[^\n]+-----/g, ''), 'base64')
    let offset = Buffer.byteLength('openssh-key-v1\0')
    for (let field = 0; field < 3; field++) offset += 4 + envelope.readUInt32BE(offset)
    offset += 4 // Key count.
    offset += 4 + envelope.readUInt32BE(offset) // Public-key block.
    const invalid =
      kind === 'missing'
        ? envelope.subarray(0, offset)
        : kind === 'empty'
          ? Buffer.concat([envelope.subarray(0, offset), Buffer.alloc(4)])
          : envelope.subarray(0, envelope.length - 1)
    const privateKey = `-----BEGIN OPENSSH PRIVATE KEY-----\n${invalid.toString('base64')}\n-----END OPENSSH PRIVATE KEY-----\n`
    const boxId = `invalid-${kind}-block`

    await expect(
      api.axiosInstance.post(`v1/mock-org/boxes/${boxId}/ssh`, { ...keys.config, host_private_key: privateKey }),
    ).rejects.toMatchObject({ response: { status: 400 } })
    expect(await requestSsh(api, 'mock-org', boxId, 'status')).toMatchObject({ enabled: false, generation: 0 })
    await expect(requestSsh(api, 'mock-org', boxId, 'enable')).rejects.toMatchObject({ response: { status: 409 } })
  },
)