The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
// Copyright 2026 BoxLite AI
// SPDX-License-Identifier: AGPL-3.0
import {
BadRequestException,
ConflictException,
RequestTimeoutException,
ServiceUnavailableException,
UnauthorizedException,
type INestApplication,
} from '@nestjs/common'
import { AllExceptionsFilter } from '../filters/all-exceptions.filter'
import { getRedisConnectionToken } from '@nestjs-modules/ioredis'
import { PassportModule } from '@nestjs/passport'
import { Test } from '@nestjs/testing'
import type { AddressInfo } from 'node:net'
import { ApiKeyStrategy } from '../auth/api-key.strategy'
import { JwtStrategy } from '../auth/jwt.strategy'
import { CheckpointService } from '../box/services/checkpoint.service'
import { CheckpointRecoverService } from '../box/services/checkpoint-recover.service'
import { BoxStateWaiterService } from '../box/services/box-state-waiter.service'
import { BoxState } from '../box/enums/box-state.enum'
import { OrganizationMemberRole } from '../organization/enums/organization-member-role.enum'
import { OrganizationResourcePermission as Permission } from '../organization/enums/organization-resource-permission.enum'
import { OrganizationService } from '../organization/services/organization.service'
import { OrganizationUserService } from '../organization/services/organization-user.service'
import { SystemRole } from '../user/enums/system-role.enum'
import { BoxliteCheckpointController } from './boxlite-checkpoint.controller'
import { AuthenticatedRateLimitGuard } from '../common/guards/authenticated-rate-limit.guard'
import { BoxUsageService } from '../usage/services/box-usage.service'
import { CommerceBoxLimitService } from './commerce-box-limit.service'
import { BoxliteBoxController } from './boxlite-box.controller'
import { BoxService } from '../box/services/box.service'
import { MetricsInterceptor } from '../interceptors/metrics.interceptor'
import { McpDelegation, MCP_DELEGATION_HEADER } from '../auth/mcp-delegation'
// Real HTTP, Passport API-key validation and organization permission guards;
// credential storage, membership, Redis and checkpoint persistence are fixtures.
describe('Checkpoint REST authentication and request admission', () => {
let app: INestApplication
const organizationId = 'checkpoint-org'
const capture = jest.fn()
const checkpoints = { create: jest.fn(), list: jest.fn(), get: jest.fn(), remove: jest.fn() }
const recovery = { createBox: jest.fn() }
const boxes = { create: jest.fn() }
const waiter = { waitForStarted: jest.fn() }
const limits = { resolveMaxCreatedBoxes: jest.fn() }
const created = { id: 'target-id', name: 'target', state: BoxState.STARTED, cpu: 4, memory: 8 }
const record = {
id: 'checkpoint-id',
name: 'base',
sourceBoxId: 'source-id',
state: 'creating',
createdAt: new Date('2026-11-06T00:00:00Z'),
arcPath: 'gs://private/artifact',
environment: { env: { INTERNAL: 'not-public' } },
archive: null,
}
beforeAll(async () => {
const entries = new Map<string, string>()
const redis = {
get: async (key: string) => entries.get(key) ?? null,
set: async (key: string, value: string) => {
entries.set(key, value)
return 'OK'
},
setex: async (key: string, _ttl: number, value: string) => {
entries.set(key, value)
return 'OK'
},
}
const users = {
findOne: async (id: string) => ({
id,
role: SystemRole.USER,
email: '[email protected]',
emailVerified: true,
}),
}
const config = { get: (key: string) => (key === 'proxy.apiKey' ? 'checkpoint-proxy-key' : 30) }
const apiKeys = {
getApiKeyByValue: async (token: string) => {
if (
![
'checkpoint-write-key',
'checkpoint-read-key',
'checkpoint-write-only-key',
'checkpoint-expired-key',
].includes(token)
)
throw new UnauthorizedException()
const permissions =
token === 'checkpoint-read-key'
? [Permission.READ_IMAGES]
: token === 'checkpoint-write-only-key'
? [Permission.WRITE_BOXES]
: [Permission.WRITE_BOXES]
return {
organizationId,
userId: 'owner',
name: token,
permissions,
expiresAt: token === 'checkpoint-expired-key' ? new Date(0) : undefined,
}
},
updateLastUsedAt: jest.fn().mockResolvedValue(undefined),
}
const module = await Test.createTestingModule({
imports: [PassportModule.register({ session: false })],
controllers: [BoxliteCheckpointController, BoxliteBoxController],
providers: [
{ provide: CheckpointService, useValue: checkpoints },
{ provide: CheckpointRecoverService, useValue: recovery },
{ provide: BoxService, useValue: boxes },
{ provide: BoxStateWaiterService, useValue: waiter },
{ provide: CommerceBoxLimitService, useValue: limits },
{ provide: BoxUsageService, useValue: {} },
{ provide: getRedisConnectionToken(), useValue: redis },
{ provide: OrganizationService, useValue: { findOne: async (id: string) => ({ id }) } },
{
provide: OrganizationUserService,
useValue: {
findOneCached: async (org: string, userId: string) =>
org === organizationId ? { organizationId: org, userId, role: OrganizationMemberRole.OWNER } : null,
},
},
{
provide: ApiKeyStrategy,
useFactory: () =>
new ApiKeyStrategy(
redis as never,
apiKeys as never,
users as never,
config as never,
{ findByApiKey: async () => null } as never,
{ findOneByProxyApiKey: async () => null } as never,
),
},
{
provide: JwtStrategy,
useFactory: () =>
new JwtStrategy(
{ issuer: 'http://127.0.0.1:1', audience: 'checkpoint-tests', jwksUri: 'http://127.0.0.1:1/jwks' },
users as never,
config as never,
{ recordFirstUse: async () => undefined } as never,
),
},
],
})
.overrideGuard(AuthenticatedRateLimitGuard)
.useValue({ canActivate: () => true })
.compile()
app = module.createNestApplication()
app.setGlobalPrefix('api')
app.useGlobalInterceptors(
new MetricsInterceptor(
{ getOrThrow: () => 'test', get: () => undefined } as never,
{ enabled: true, capture } as never,
),
)
app.useGlobalFilters(new AllExceptionsFilter({ incrementFailedAuth: jest.fn() } as never))
await app.listen(0, '127.0.0.1')
})
afterAll(async () => {
await app?.close()
})
beforeEach(() => {
jest.clearAllMocks()
checkpoints.create.mockResolvedValue(record)
checkpoints.get.mockResolvedValue(record)
checkpoints.list.mockResolvedValue({ checkpoints: [record], nextPageToken: undefined })
recovery.createBox.mockReset().mockResolvedValue({ id: 'target-id' })
waiter.waitForStarted.mockReset().mockResolvedValue(created)
limits.resolveMaxCreatedBoxes.mockReset().mockResolvedValue(2)
})
function request(path: string, token?: string, body?: unknown, method?: string, headers?: Record<string, string>) {
return fetch(`http://127.0.0.1:${(app.getHttpServer().address() as AddressInfo).port}/api${path}`, {
method: method ?? (body === undefined ? 'GET' : 'POST'),
headers: {
'content-type': 'application/json',
...headers,
...(token ? { authorization: `Bearer ${token}` } : {}),
},
body: body === undefined ? undefined : JSON.stringify(body),
})
}
it.each([
['/v1/checkpoints', 'sdk', 'sdk'],
['/v1/default/checkpoints', 'dashboard', 'dashboard'],
['/v1/checkpoint-org/checkpoints', 'mcp', 'unattributed'],
['/v1/checkpoints', undefined, 'unattributed'],
])('records a finished checkpoint request at %s from %s', async (path, label, source) => {
const headers = label ? { 'X-BoxLite-Source': label } : {}
capture.mockImplementationOnce(() => new Promise(() => {}))
const response = await request(path, 'checkpoint-write-key', { source_box_id: 'private-box' }, 'POST', headers)
expect(response.status).toBe(201)
expect(capture.mock.calls).toEqual([
[
{
event: 'cloud.api.operation',
distinctId: 'owner',
timestamp: expect.any(Date),
groups: { organization: organizationId },
properties: { 'api.operation': 'checkpoint.create', 'client.source': source, 'http.status_class': '2xx' },
},
{ users: ['owner'], organizations: [organizationId] },
],
])
})
it('attributes a consumed MCP delegation and drops denied or polling requests', async () => {
const nonce = new McpDelegation().issue({
role: SystemRole.USER,
userId: 'owner',
email: '[email protected]',
organizationId,
})
const response = await request('/v1/checkpoints', undefined, { source_box_id: 'private-box' }, 'POST', {
[MCP_DELEGATION_HEADER]: nonce,
})
expect(response.status).toBe(201)
expect(capture.mock.calls[0][0].properties['client.source']).toBe('mcp')
capture.mockClear()
expect((await request('/v1/checkpoints', 'checkpoint-read-key', { source_box_id: 'private-box' })).status).toBe(403)
expect((await request('/v1/checkpoints', 'checkpoint-read-key')).status).toBe(200)
expect(capture).not.toHaveBeenCalled()
})
it('records a rejected restore intent without exporting its selector or validation error', async () => {
await request('/v1/boxes', 'checkpoint-write-key', { checkpoint_id: 'private', image: 'private-image' })
const events = capture.mock.calls.filter(([row]) => row.event === 'cloud.api.operation')
expect(events).toHaveLength(1)
expect(events[0][0].properties).toEqual({
'api.operation': 'checkpoint.restore',
'client.source': 'unattributed',
'http.status_class': '4xx',
})
})
it.each(['pageToken', 'page_token'])('passes the %s pagination cursor to checkpoint storage', async (parameter) => {
const response = await request(`/v1/checkpoints?${parameter}=next-page`, 'checkpoint-write-key')
expect(response.status).toBe(200)
expect(checkpoints.list).toHaveBeenCalledWith(organizationId, 'next-page')
})
it('admits scoped capture on canonical, explicit-org and legacy default paths', async () => {
for (const prefix of ['', `/${organizationId}`, '/default']) {
const result = await request(`/v1${prefix}/checkpoints`, 'checkpoint-write-key', {
source_box_id: 'source-id',
name: 'base',
})
expect(result.status).toBe(201)
expect(await result.json()).toEqual({
id: record.id,
name: 'base',
source_box_id: 'source-id',
state: 'creating',
kind: 'disk',
created_at: record.createdAt.toISOString(),
size_bytes: 0,
})
}
expect(checkpoints.create.mock.calls).toEqual(
Array.from({ length: 3 }, () => [organizationId, 'source-id', 'base', 'disk']),
)
})
it('admits explicit disk capture', async () => {
const result = await request('/v1/checkpoints', 'checkpoint-write-key', {
source_box_id: 'source-id',
kind: 'disk',
})
expect(result.status).toBe(201)
expect(await result.json()).toMatchObject({ id: record.id, kind: 'disk' })
expect(checkpoints.create).toHaveBeenCalledWith(organizationId, 'source-id', undefined, 'disk')
})
it('preserves the invalid_state code from a checkpoint source state conflict', async () => {
const message = 'Checkpoint source must be idle and stopped; stop the Box before creating a checkpoint'
checkpoints.create.mockRejectedValueOnce(new ConflictException({ message, code: 'invalid_state' }))
const result = await request('/v1/checkpoints', 'checkpoint-write-key', {
source_box_id: 'source-id',
kind: 'disk',
})
expect(result.status).toBe(409)
expect(await result.json()).toMatchObject({ code: 'invalid_state', message })
})
it('preserves a duplicate checkpoint name conflict without adding a code', async () => {
const message = 'Checkpoint name already exists'
checkpoints.create.mockRejectedValueOnce(new ConflictException(message))
const result = await request('/v1/checkpoints', 'checkpoint-write-key', {
source_box_id: 'source-id',
name: 'base',
})
expect(result.status).toBe(409)
const body = await result.json()
expect(body).toMatchObject({ message })
expect(body).not.toHaveProperty('code')
})
it('passes full capture to the service and preserves its unsupported error', async () => {
checkpoints.create.mockRejectedValueOnce(
new BadRequestException({ message: 'Full checkpoints are not supported', code: 'unsupported' }),
)
const result = await request('/v1/checkpoints', 'checkpoint-write-key', {
source_box_id: 'source-id',
kind: 'full',
})
expect(result.status).toBe(400)
expect(await result.json()).toMatchObject({ code: 'unsupported' })
expect(checkpoints.create).toHaveBeenCalledWith(organizationId, 'source-id', undefined, 'full')
})
it.each([0, 42])(
'omits a known memory size of %i bytes while creating a full checkpoint',
async (memorySizeBytes) => {
checkpoints.create.mockResolvedValueOnce({ ...record, kind: 'full', memorySizeBytes })
const result = await request('/v1/checkpoints', 'checkpoint-write-key', {
source_box_id: 'source-id',
kind: 'full',
})
expect(result.status).toBe(201)
const body = await result.json()
expect(body).toMatchObject({ kind: 'full', state: 'creating' })
expect(body).not.toHaveProperty('memory_size_bytes')
},
)
it.each([0, 42])('returns a known memory size of %i bytes for a ready full checkpoint', async (memorySizeBytes) => {
checkpoints.get.mockResolvedValueOnce({ ...record, kind: 'full', state: 'ready', memorySizeBytes })
const result = await request('/v1/checkpoints/base', 'checkpoint-read-key')
expect(result.status).toBe(200)
expect(await result.json()).toMatchObject({ kind: 'full', state: 'ready', memory_size_bytes: memorySizeBytes })
})
it.each([
['disk', 42],
['full', null],
['full', undefined],
])('omits memory_size_bytes for a ready %s checkpoint with memorySizeBytes=%p', async (kind, memorySizeBytes) => {
checkpoints.get.mockResolvedValueOnce({
...record,
kind,
state: 'ready',
memorySizeBytes,
archive: { sizeBytes: 100, memorySizeBytes: 42 },
})
const result = await request('/v1/checkpoints/base', 'checkpoint-read-key')
expect(result.status).toBe(200)
const body = await result.json()
expect(body).toMatchObject({ kind, state: 'ready', size_bytes: 100 })
expect(body).not.toHaveProperty('memory_size_bytes')
})
it.each([null, 'bogus', 123])('rejects invalid checkpoint kind %j before calling the service', async (kind) => {
const result = await request('/v1/checkpoints', 'checkpoint-write-key', { source_box_id: 'source-id', kind })
expect(result.status).toBe(400)
expect(await result.json()).not.toHaveProperty('code', 'unsupported')
expect(checkpoints.create).not.toHaveBeenCalled()
})
it('allows reads without exposing the stored environment or object URI', async () => {
const list = await request('/v1/checkpoints', 'checkpoint-read-key')
expect(list.status).toBe(200)
const body = await list.json()
expect(body.checkpoints[0]).toMatchObject({ id: record.id, source_box_id: 'source-id' })
expect(JSON.stringify(body)).not.toMatch(/not-public|gs:\/\/private/)
expect((await request('/v1/checkpoints/base', 'checkpoint-read-key')).status).toBe(200)
expect(checkpoints.get).toHaveBeenCalledWith(organizationId, 'base')
})
it('rejects missing, unknown and expired credentials before persistence', async () => {
for (const token of [undefined, 'unknown-key', 'checkpoint-expired-key']) {
expect((await request('/v1/checkpoints', token, { source_box_id: 'source-id' })).status).toBe(401)
}
expect(checkpoints.create).not.toHaveBeenCalled()
})
it('requires box write permission for capture, even for an owner key', async () => {
for (const token of ['checkpoint-read-key', 'checkpoint-proxy-key']) {
expect((await request('/v1/checkpoints', token, { source_box_id: 'source-id' })).status).toBe(403)
}
expect(checkpoints.create).not.toHaveBeenCalled()
})
it('reuses the existing box write scope without requiring a new read scope', async () => {
expect((await request('/v1/checkpoints', 'checkpoint-write-only-key', { source_box_id: 'source-id' })).status).toBe(
201,
)
})
it('rejects a foreign organization before any checkpoint lookup or capture', async () => {
expect(
(await request('/v1/other-org/checkpoints', 'checkpoint-write-key', { source_box_id: 'source-id' })).status,
).toBe(403)
expect((await request('/v1/other-org/checkpoints/base', 'checkpoint-read-key')).status).toBe(403)
expect(checkpoints.create).not.toHaveBeenCalled()
expect(checkpoints.get).not.toHaveBeenCalled()
})
it('rejects unknown fields and invalid capture arguments before persistence', async () => {
for (const body of [
{},
{ source_box_id: '' },
{ source_box_id: 'source', cpus: 4 },
{ source_box_id: 'source', name: '../escape' },
{ source_box_id: 'source', name: 42 },
]) {
expect((await request('/v1/checkpoints', 'checkpoint-write-key', body)).status).toBe(400)
}
expect(checkpoints.create).not.toHaveBeenCalled()
})
it('creates from a checkpoint on all organization paths and waits for startup', async () => {
for (const prefix of ['', `/${organizationId}`, '/default']) {
const result = await request(`/v1${prefix}/boxes`, 'checkpoint-write-key', {
checkpoint_id: 'base',
name: 'new-target',
})
expect(result.status).toBe(201)
expect(await result.json()).toMatchObject({ box_id: 'target-id', status: 'running', cpus: 4, memory_mib: 8192 })
}
expect(recovery.createBox.mock.calls).toEqual(
Array.from({ length: 3 }, () => [{ id: organizationId }, 'base', 'new-target', 2]),
)
expect(waiter.waitForStarted).toHaveBeenCalledWith('target-id', organizationId, 30, expect.any(Function))
expect(boxes.create).not.toHaveBeenCalled()
})
it('lets the checkpoint path assign a name when none is given', async () => {
const result = await request('/v1/boxes', 'checkpoint-write-key', { checkpoint_id: 'base' })
expect(result.status).toBe(201)
expect(recovery.createBox).toHaveBeenCalledWith({ id: organizationId }, 'base', undefined, 2)
})
it('keeps ordinary creation on the image path', async () => {
boxes.create.mockResolvedValue(created)
const result = await request('/v1/boxes', 'checkpoint-write-key', { image: 'alpine:latest', cpus: 2 })
expect(result.status).toBe(201)
expect(boxes.create).toHaveBeenCalledTimes(1)
expect(recovery.createBox).not.toHaveBeenCalled()
})
it.each([
[{ image: 'other:latest' }, 'checkpoint_id and image are mutually exclusive'],
[{ rootfs_path: '/srv/rootfs' }, 'rootfs_path is local-only; use image instead'],
[{ cpus: 2 }, 'remove: cpus'],
[{ env: { OVERRIDE: 'denied' } }, 'remove: env'],
[{ advanced: { privileged: false } }, 'remove: advanced'],
[{ detach: true }, 'remove: detach'],
[{ cpus: 2, memory_mib: 512 }, 'remove: cpus, memory_mib'],
[{ box_id: 'target' }, 'property box_id should not exist'],
])('rejects checkpoint creation combined with %j without creating anything', async (extra, message) => {
const result = await request('/v1/boxes', 'checkpoint-write-key', { checkpoint_id: 'base', ...extra })
expect(result.status).toBe(400)
expect(JSON.stringify((await result.json()).message)).toContain(message)
expect(limits.resolveMaxCreatedBoxes).not.toHaveBeenCalled()
expect(recovery.createBox).not.toHaveBeenCalled()
expect(boxes.create).not.toHaveBeenCalled()
})
it('rejects invalid checkpoint selectors and names before admission', async () => {
for (const body of [
{ checkpoint_id: '' },
{ checkpoint_id: null },
{ checkpoint_id: 42 },
{ checkpoint_id: 'x'.repeat(129) },
{ checkpoint_id: 'base', name: '../outside' },
{ checkpoint_id: 'base', name: '' },
{ checkpoint_id: 'base', name: 'x'.repeat(129) },
{ checkpoint_id: 'base', name: null },
]) {
expect((await request('/v1/boxes', 'checkpoint-write-key', body)).status).toBe(400)
}
expect(recovery.createBox).not.toHaveBeenCalled()
expect(boxes.create).not.toHaveBeenCalled()
})
it('enforces credentials, Box write scope and tenant isolation before checkpoint admission', async () => {
for (const [token, status] of [
[undefined, 401],
['unknown-key', 401],
['checkpoint-expired-key', 401],
['checkpoint-read-key', 403],
['checkpoint-proxy-key', 403],
] as const) {
expect((await request('/v1/boxes', token, { checkpoint_id: 'base' })).status).toBe(status)
}
expect((await request('/v1/other-org/boxes', 'checkpoint-write-key', { checkpoint_id: 'base' })).status).toBe(403)
expect(limits.resolveMaxCreatedBoxes).not.toHaveBeenCalled()
expect(recovery.createBox).not.toHaveBeenCalled()
})
it('no longer routes the import and recovery paths', async () => {
for (const prefix of ['', `/${organizationId}`]) {
const result = await request(`/v1${prefix}/boxes/import`, 'checkpoint-write-key', { checkpoint_id: 'base' })
expect(result.status).toBe(404)
}
expect((await request('/v1/boxes/target/recover', 'checkpoint-write-key', { checkpoint_id: 'base' })).status).toBe(
404,
)
expect(recovery.createBox).not.toHaveBeenCalled()
expect(boxes.create).not.toHaveBeenCalled()
})
it.each([
new RequestTimeoutException({ message: 'Still importing', code: 'import_pending' }),
new BadRequestException({ message: 'Archive import failed', code: 'checkpoint_import_failed' }),
])('preserves a newly admitted Box identity on wait failure: %s', async (error) => {
waiter.waitForStarted.mockRejectedValue(error)
const result = await request('/v1/boxes', 'checkpoint-write-key', { checkpoint_id: 'base' })
expect(result.status).toBe(error.getStatus())
expect(await result.json()).toMatchObject({ ...(error.getResponse() as object), boxId: 'target-id' })
expect(recovery.createBox).toHaveBeenCalledTimes(1)
})
it('does not attach an identity when admission itself fails', async () => {
recovery.createBox.mockRejectedValue(new BadRequestException('Checkpoint is not ready'))
const result = await request('/v1/boxes', 'checkpoint-write-key', { checkpoint_id: 'base' })
expect(result.status).toBe(400)
expect(await result.json()).not.toHaveProperty('boxId')
expect(waiter.waitForStarted).not.toHaveBeenCalled()
})
it('surfaces the checkpoint capability gate from admission', async () => {
recovery.createBox.mockRejectedValue(new ServiceUnavailableException('Checkpoints are not enabled'))
const result = await request('/v1/boxes', 'checkpoint-write-key', { checkpoint_id: 'base' })
expect(result.status).toBe(503)
expect(boxes.create).not.toHaveBeenCalled()
})
it('requires Box write scope for deletion and validates force without bypassing tenant guards', async () => {
expect((await request('/v1/checkpoints/base', 'checkpoint-write-key', undefined, 'DELETE')).status).toBe(204)
expect(checkpoints.remove).toHaveBeenLastCalledWith(organizationId, 'base', false)
expect((await request('/v1/checkpoints/base?force=true', 'checkpoint-write-key', undefined, 'DELETE')).status).toBe(
204,
)
expect(checkpoints.remove).toHaveBeenLastCalledWith(organizationId, 'base', true)
checkpoints.remove.mockClear()
expect((await request('/v1/checkpoints/base?force=yes', 'checkpoint-write-key', undefined, 'DELETE')).status).toBe(
400,
)
expect((await request('/v1/checkpoints/base', 'checkpoint-read-key', undefined, 'DELETE')).status).toBe(403)
expect((await request('/v1/other-org/checkpoints/base', 'checkpoint-write-key', undefined, 'DELETE')).status).toBe(
403,
)
expect(checkpoints.remove).not.toHaveBeenCalled()
})
})