Skip to content

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

22.7 KB
Raw Blame
/*
 * Copyright 2025 Daytona Platforms Inc.
 * Modified by BoxLite AI, 2025-2026
 * SPDX-License-Identifier: AGPL-3.0
 */

import { Audit, AUDIT_TARGET_ID } from '../audit/decorators/audit.decorator'
import { AuditAction } from '../audit/enums/audit-action.enum'
import { AuditTarget } from '../audit/enums/audit-target.enum'
import {
  Controller,
  All,
  Get,
  Delete,
  Param,
  Req,
  Res,
  Next,
  UseGuards,
  Logger,
  NotFoundException,
  ConflictException,
  HttpCode,
  HttpStatus,
  Post,
  Query,
  Body,
  Header,
} from '@nestjs/common'
import { ApiTags, ApiBearerAuth, ApiExcludeController } from '@nestjs/swagger'
import { createProxyMiddleware, fixRequestBody, Options } from 'http-proxy-middleware'
import { Request, Response, NextFunction } from 'express'
import { CombinedAuthGuard } from '../auth/combined-auth.guard'
import { OrganizationResourceActionGuard } from '../organization/guards/organization-resource-action.guard'
import { RequiredOrganizationResourcePermissions } from '../organization/decorators/required-organization-resource-permissions.decorator'
import { OrganizationResourcePermission } from '../organization/enums/organization-resource-permission.enum'
import { AuthContext } from '../common/decorators/auth-context.decorator'
import { OrganizationAuthContext } from '../common/interfaces/auth-context.interface'
import { BoxService, ProxyAdmission } from '../box/services/box.service'
import { RunnerService } from '../box/services/runner.service'
import { AUTO_RESUMABLE_STATES, BoxAutoResumeService } from '../box/services/box-auto-resume.service'
import { BoxState } from '../box/enums/box-state.enum'
import { BoxDesiredState } from '../box/enums/box-desired-state.enum'
import { reportedBoxState } from '../box/utils/box-state.util'
import { Box } from '../box/entities/box.entity'
import { TunnelService } from '../box/services/tunnel.service'
import { PrepareTunnelDto } from '../box/dto/tunnel.dto'
import { requestTiming } from '../common/utils/request-timing.util'
import { requestContext, UserContext } from '../common/decorators/user-context.decorator'
import { Context } from '../common/context/context'
import { recordBusinessEvent, recordOutcomes } from '../common/utils/business-event.util'
import { noEffect } from '../common/replay-no-effect'
import { ReplayParseIntPipe, ReplayValidationPipe } from '../common/replay-validation.pipe'
import { trackReplayUpstream } from './request-replay-response'

type ProxyActivityPolicy = { activity: boolean; autoResume: boolean }
const USER_OPERATION: ProxyActivityPolicy = { activity: true, autoResume: true }
const OBSERVATION_ONLY: ProxyActivityPolicy = { activity: false, autoResume: false }

// The runner can implicitly boot a stopped box on exec, so disabled cloud
// auto-resume must be enforced before forwarding the request.
function requireExecReady(box: Box, boxId: string): void {
  if (!box.autoResume && (reportedBoxState(box) !== BoxState.STARTED || box.desiredState !== BoxDesiredState.STARTED)) {
    throw new ConflictException(`Box ${boxId} is not running and auto-resume is disabled; start it first`)
  }
}

// SSH configure may wake a box only the way auto-resume would, and only onto
// the image's default command: a box with an explicit cmd/entrypoint must be
// started first, as the SDKs require (openapi `configureSsh`). Joining a start
// already in flight is fine; submitting one is not.
function refuseImplicitSshStart(box: Box, boxId: string): void {
  const state = reportedBoxState(box)
  if (state === BoxState.STARTED && box.desiredState === BoxDesiredState.STARTED) {
    return
  }
  if (!box.autoResume) {
    throw new ConflictException(`Box ${boxId} is not running and auto-resume is disabled; start it first`)
  }
  if (state !== BoxState.STARTED && !AUTO_RESUMABLE_STATES.includes(state)) {
    throw new ConflictException(`Box ${boxId} is not running (state: ${state})`)
  }
  const submitsStart =
    state === BoxState.STOPPED || state === BoxState.STOPPING || box.desiredState === BoxDesiredState.STOPPED
  if (submitsStart && (box.cmd?.length || box.entrypoint?.length)) {
    throw new ConflictException(`Box ${boxId} has an explicit cmd or entrypoint; start it before configuring SSH`)
  }
}

// SSH status never starts a box. The runner answers a stopped box's
// disabled status itself; a box between states has no answer to give yet.
function requireSshObservable(box: Box, boxId: string): void {
  if (box.state !== BoxState.STARTED && box.state !== BoxState.STOPPED) {
    throw new ConflictException(`Box ${boxId} is ${box.state}; SSH can be read once it is started or stopped`)
  }
}
/** A proxied call that is a business operation: a command started, or files uploaded. */
type ProxiedOperation = 'box.exec' | 'box.file_upload'

function requireSshRunning(box: Box, boxId: string): void {
  const state = reportedBoxState(box)
  if (state !== BoxState.STARTED || box.desiredState !== BoxDesiredState.STARTED) {
    throw new ConflictException(`Box ${boxId} is not running (state: ${state}); start it before changing SSH`)
  }
}

// Spec-first surface (openapi/box.openapi.yaml). Must stay out of the product
// spec: @All() expands to the SEARCH verb, which OpenAPI 3.0 cannot express.
@ApiExcludeController()
@ApiTags('BoxLite REST')
@Controller(['v1/boxes', 'v1/:prefix/boxes'])
@UseGuards(CombinedAuthGuard, OrganizationResourceActionGuard)
@ApiBearerAuth()
export class BoxliteProxyController {
  private readonly logger = new Logger(BoxliteProxyController.name)

  constructor(
    private readonly boxService: BoxService,
    private readonly runnerService: RunnerService,
    private readonly autoResume: BoxAutoResumeService,
    private readonly tunnelService: TunnelService,
  ) {}

  @All(':boxId/exec')
  @RequiredOrganizationResourcePermissions([OrganizationResourcePermission.WRITE_BOXES])
  @Audit({
    action: AuditAction.EXECUTE,
    targetType: AuditTarget.BOX,
    targetIdFromRequest: (req) => req.params.boxId,
    methods: ['POST'],
    completion: 'response',
  })
  async proxyExec(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Req() req: Request,
    @Res() res: Response,
    @Next() next: NextFunction,
  ) {
    return this.proxyToRunner(
      authContext,
      boxId,
      (runnerBoxId) => `/v1/boxes/${runnerBoxId}/exec`,
      req,
      res,
      next,
      USER_OPERATION,
      {
        operation: req.method === 'POST' ? 'box.exec' : undefined,
        admit: req.method === 'POST' ? requireExecReady : undefined,
      },
    )
  }

  @All(':boxId/executions/:execId/signal')
  @RequiredOrganizationResourcePermissions([OrganizationResourcePermission.WRITE_BOXES])
  @Audit({
    action: AuditAction.SIGNAL_EXECUTION,
    targetType: AuditTarget.BOX,
    targetIdFromRequest: (req) => req.params.boxId,
    methods: ['POST'],
    completion: 'response',
    requestMetadata: { executionId: (req) => req.params.execId },
  })
  async proxyExecSignal(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Param('execId') execId: string,
    @Req() req: Request,
    @Res() res: Response,
    @Next() next: NextFunction,
  ) {
    return this.proxyToRunner(
      authContext,
      boxId,
      (runnerBoxId) => `/v1/boxes/${runnerBoxId}/executions/${execId}/signal`,
      req,
      res,
      next,
      USER_OPERATION,
    )
  }

  @All(':boxId/executions/:execId/resize')
  @RequiredOrganizationResourcePermissions([OrganizationResourcePermission.WRITE_BOXES])
  async proxyExecResize(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Param('execId') execId: string,
    @Req() req: Request,
    @Res() res: Response,
    @Next() next: NextFunction,
  ) {
    return this.proxyToRunner(
      authContext,
      boxId,
      (runnerBoxId) => `/v1/boxes/${runnerBoxId}/executions/${execId}/resize`,
      req,
      res,
      next,
      USER_OPERATION,
    )
  }

  @Get(':boxId/executions')
  async proxyExecList(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Req() req: Request,
    @Res() res: Response,
    @Next() next: NextFunction,
  ) {
    return this.proxyToRunner(
      authContext,
      boxId,
      (runnerBoxId) => `/v1/boxes/${runnerBoxId}/executions`,
      req,
      res,
      next,
      OBSERVATION_ONLY,
    )
  }

  @Get(':boxId/executions/:execId')
  async proxyExecStatus(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Param('execId') execId: string,
    @Req() req: Request,
    @Res() res: Response,
    @Next() next: NextFunction,
  ) {
    return this.proxyToRunner(
      authContext,
      boxId,
      (runnerBoxId) => `/v1/boxes/${runnerBoxId}/executions/${execId}`,
      req,
      res,
      next,
      USER_OPERATION,
    )
  }

  @Delete(':boxId/executions/:execId')
  @RequiredOrganizationResourcePermissions([OrganizationResourcePermission.WRITE_BOXES])
  @Audit({
    action: AuditAction.TERMINATE_EXECUTION,
    targetType: AuditTarget.BOX,
    targetIdFromRequest: (req) => req.params.boxId,
    methods: ['DELETE'],
    completion: 'response',
    requestMetadata: { executionId: (req) => req.params.execId },
  })
  async proxyExecKill(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Param('execId') execId: string,
    @Req() req: Request,
    @Res() res: Response,
    @Next() next: NextFunction,
  ) {
    return this.proxyToRunner(
      authContext,
      boxId,
      (runnerBoxId) => `/v1/boxes/${runnerBoxId}/executions/${execId}`,
      req,
      res,
      next,
      USER_OPERATION,
    )
  }

  // /executions/:execId/attach is a WebSocket-only route. Real WS upgrades
  // bypass Express entirely and are handled by BoxliteWsProxyService via the
  // `server.on('upgrade', ...)` hook registered in main.ts. Plain HTTP GETs
  // to this path (callers that forgot the Upgrade headers) fall through to
  // a NestJS 404, which is the correct answer.

  @All(':boxId/files')
  @RequiredOrganizationResourcePermissions([OrganizationResourcePermission.WRITE_BOXES])
  @Audit({
    action: AuditAction.UPLOAD_FILES,
    targetType: AuditTarget.BOX,
    targetIdFromRequest: (req) => req.params.boxId,
    methods: ['PUT'],
    completion: 'response',
  })
  async proxyFiles(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Req() req: Request,
    @Res() res: Response,
    @Next() next: NextFunction,
  ) {
    const query = req.url.includes('?') ? req.url.substring(req.url.indexOf('?')) : ''
    return this.proxyToRunner(
      authContext,
      boxId,
      (runnerBoxId) => `/v1/boxes/${runnerBoxId}/files${query}`,
      req,
      res,
      next,
      USER_OPERATION,
      { proxyTimeoutMs: 0, operation: req.method === 'PUT' ? 'box.file_upload' : undefined },
    )
  }

  @All(':boxId/metrics')
  async proxyMetrics(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Req() req: Request,
    @Res() res: Response,
    @Next() next: NextFunction,
  ) {
    return this.proxyToRunner(
      authContext,
      boxId,
      (runnerBoxId) => `/v1/boxes/${runnerBoxId}/metrics`,
      req,
      res,
      next,
      OBSERVATION_ONLY,
    )
  }

  // The runner applies the configuration once; nothing on either hop retries
  // it, because every accepted call restarts the guest listener.
  @Post(':boxId/ssh')
  @RequiredOrganizationResourcePermissions([OrganizationResourcePermission.WRITE_BOXES])
  @Audit({
    action: AuditAction.CONFIGURE_SSH,
    targetType: AuditTarget.BOX,
    targetIdFromRequest: (req) => req.params.boxId,
    methods: ['POST'],
    completion: 'response',
  })
  async configureSsh(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Req() req: Request,
    @Res() res: Response,
    @Next() next: NextFunction,
  ) {
    return this.proxyToRunner(
      authContext,
      boxId,
      (runnerBoxId) => `/v1/boxes/${runnerBoxId}/ssh`,
      req,
      res,
      next,
      USER_OPERATION,
      { admit: refuseImplicitSshStart },
    )
  }

  @Get(':boxId/ssh')
  async getSshStatus(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Req() req: Request,
    @Res() res: Response,
    @Next() next: NextFunction,
  ) {
    return this.proxyToRunner(
      authContext,
      boxId,
      (runnerBoxId) => `/v1/boxes/${runnerBoxId}/ssh`,
      req,
      res,
      next,
      OBSERVATION_ONLY,
      { admit: requireSshObservable },
    )
  }

  @Post(':boxId/ssh/enable')
  @RequiredOrganizationResourcePermissions([OrganizationResourcePermission.WRITE_BOXES])
  @Audit({
    action: AuditAction.ENABLE_SSH,
    targetType: AuditTarget.BOX,
    targetIdFromRequest: (req) => req.params.boxId,
    methods: ['POST'],
    completion: 'response',
  })
  async enableSsh(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Req() req: Request,
    @Res() res: Response,
    @Next() next: NextFunction,
  ) {
    return this.proxyToRunner(
      authContext,
      boxId,
      (runnerBoxId) => `/v1/boxes/${runnerBoxId}/ssh/enable`,
      req,
      res,
      next,
      OBSERVATION_ONLY,
      { admit: requireSshRunning },
    )
  }

  @Delete(':boxId/ssh')
  @RequiredOrganizationResourcePermissions([OrganizationResourcePermission.WRITE_BOXES])
  @Audit({
    action: AuditAction.DISABLE_SSH,
    targetType: AuditTarget.BOX,
    targetIdFromRequest: (req) => req.params.boxId,
    methods: ['DELETE'],
    completion: 'response',
  })
  async disableSsh(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Req() req: Request,
    @Res() res: Response,
    @Next() next: NextFunction,
  ) {
    return this.proxyToRunner(
      authContext,
      boxId,
      (runnerBoxId) => `/v1/boxes/${runnerBoxId}/ssh`,
      req,
      res,
      next,
      OBSERVATION_ONLY,
      { admit: requireSshRunning },
    )
  }

  @Post(':boxId/network/tunnel')
  @Header('Cache-Control', 'no-store')
  @RequiredOrganizationResourcePermissions([OrganizationResourcePermission.WRITE_BOXES])
  @HttpCode(HttpStatus.OK)
  @Audit({
    action: AuditAction.CREATE_TUNNEL,
    targetType: AuditTarget.BOX,
    targetIdFromRequest: (req) => req.params.boxId,
    requestMetadata: { port: (req) => req.query.port },
  })
  async proxyNetworkTunnel(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Query('port', ReplayParseIntPipe) port: number,
    @UserContext() ctx: Context,
    @Body(new ReplayValidationPipe({ whitelist: true, forbidNonWhitelisted: true })) options: PrepareTunnelDto = {},
    @Req() request?: Request,
  ) {
    // findOneByIdOrName already 404s for a missing/destroyed box.
    const box = await this.boxService.findOneByIdOrName(boxId, authContext.organizationId)
    if (request) request[AUDIT_TARGET_ID] = box.id
    return recordOutcomes(
      { name: 'tunnel.create', correlationId: box.id, organizationId: box.organizationId, ctx },
      () => this.openTunnel(box, boxId, port, authContext.organizationId, options),
      (error) => (error instanceof ConflictException ? 'conflict' : 'internal'),
    )
  }

  private async openTunnel(box: Box, boxId: string, port: number, organizationId: string, options: PrepareTunnelDto) {
    // Tunnel URLs expose the box to the public internet, so only a box with
    // inbound access enabled can use one. Any other box is reached through
    // the authenticated API.
    if (!box.inboundEnabled) {
      throw new ConflictException(
        `Box ${boxId} has inbound access disabled; enable inbound access before opening a tunnel`,
      )
    }

    // POL-326/POL-352: opening a tunnel is user activity, exactly like the
    // proxyToRunner routes. Persist it before the readiness gate so the
    // AutoStop sweeper cannot reap the box we are about to hand out; the
    // sweeper rechecks this Redis-buffered timestamp after taking its state
    // lock.
    await this.boxService
      .updateLastActivityAt(box.id, new Date())
      .catch((err) => this.logger.warn(`updateLastActivityAt failed for ${box.id}: ${err}`))

    // Refuse at once a box the CONNECT could never wake. A box it can wake
    // gets its URI as it is: the proxy wakes it on the CONNECT, and waits for
    // the service to listen, so waking it here would only hold the caller.
    if (box.state !== BoxState.STARTED && (!box.autoResume || !AUTO_RESUMABLE_STATES.includes(box.state))) {
      throw new ConflictException(`Box ${boxId} is not running (state: ${box.state})`)
    }

    const uri = await this.boxService.getNetworkTunnelUrl(boxId, organizationId, port)
    const access = await this.tunnelService.prepare(box.id, port, options.isPublic ?? true)
    return { uri, ...access }
  }

  // Revoking needs neither inbound access nor a running box: it only withdraws
  // the declaration, so a port stays revocable after either is turned off.
  @Delete(':boxId/network/tunnel')
  @RequiredOrganizationResourcePermissions([OrganizationResourcePermission.WRITE_BOXES])
  @HttpCode(HttpStatus.NO_CONTENT)
  @Audit({
    action: AuditAction.REVOKE_TUNNEL,
    targetType: AuditTarget.BOX,
    targetIdFromRequest: (req) => req.params.boxId,
    requestMetadata: { port: (req) => req.query.port },
  })
  async revokeNetworkTunnel(
    @AuthContext() authContext: OrganizationAuthContext,
    @Param('boxId') boxId: string,
    @Query('port', ReplayParseIntPipe) port: number,
    @Req() request?: Request,
  ): Promise<void> {
    const box = await this.boxService.findOneByIdOrName(boxId, authContext.organizationId)
    if (request) request[AUDIT_TARGET_ID] = box.id
    await this.tunnelService.revoke(box.id, port)
  }

  private async proxyToRunner(
    authContext: OrganizationAuthContext,
    boxId: string,
    targetPathForRunnerBox: (runnerBoxId: string) => string,
    req: Request,
    res: Response,
    next: NextFunction,
    policy: ProxyActivityPolicy,
    // admit throws to refuse the request after activity is recorded and before
    // any resume.
    opts?: { ws?: boolean; proxyTimeoutMs?: number; admit?: ProxyAdmission; operation?: ProxiedOperation },
  ) {
    const box = await this.boxService.findOneByIdOrName(boxId, authContext.organizationId)
    if (!box) {
      throw new NotFoundException(`Box ${boxId} not found`)
    }

    req[AUDIT_TARGET_ID] = box.id
    if (opts?.operation) {
      this.recordProxiedOperation(opts.operation, box, requestContext(req), res)
    }

    if (policy.activity) {
      // Persist activity before the readiness gate. The lifecycle sweeper rechecks
      // this Redis-buffered timestamp after taking its state lock, closing the
      // request-vs-AutoStop race without holding a lock through cold start.
      await this.boxService
        .updateLastActivityAt(box.id, new Date())
        .catch((err) => this.logger.warn(`updateLastActivityAt failed for ${box.id}: ${err}`))
    }

    if (policy.activity && typeof box.autoStop === 'number' && box.autoStop > 0) {
      // A long-running operation (a file upload/download, a streaming exec) is
      // still activity for its whole lifetime, not just its first request.
      // Refresh the idle timer periodically so the AutoStop sweeper does not
      // reap the box mid-transfer; stop once the response completes or errors.
      const stopHeartbeat = this.startActivityHeartbeat(box.id, box.autoStop)
      let stopped = false
      const stop = (): void => {
        if (stopped) return
        stopped = true
        stopHeartbeat()
      }
      res.once('close', stop)
      res.once('finish', stop)
      res.once('error', stop)
    }

    // Only this local admission check proves that neither auto-resume nor
    // runner dispatch has begun. An error after ensureReady may follow a start.
    try {
      opts?.admit?.(box, boxId)
    } catch (error) {
      if (error instanceof Error) throw noEffect(error)
      throw error
    }

    if (policy.autoResume && box.autoResume) {
      await this.autoResume.ensureReady(box, authContext.organization, opts?.admit, requestTiming(req))
    }

    const runner = await this.runnerService.findOne(box.runnerId)
    if (!runner) {
      throw new NotFoundException(`Runner for box ${boxId} not found`)
    }

    const targetUrl = runner.apiUrl || runner.proxyUrl
    if (!targetUrl) {
      throw new NotFoundException(`Runner endpoint for box ${boxId} not found`)
    }

    const proxyOptions: Options = {
      target: targetUrl,
      secure: false,
      changeOrigin: true,
      autoRewrite: true,
      ws: opts?.ws ?? false,
      pathRewrite: () => targetPathForRunnerBox(box.id),
      on: {
        proxyReq: (proxyReq, originalReq) => {
          proxyReq.setHeader('Authorization', `Bearer ${runner.apiKey}`)
          fixRequestBody(proxyReq, originalReq)
        },
        proxyRes: trackReplayUpstream,
      },
      proxyTimeout: opts?.proxyTimeoutMs ?? 5 * 60 * 1000,
    }

    return createProxyMiddleware(proxyOptions)(req, res, next)
  }

  /**
   * Records the operation as requested now and its outcome when the response
   * ends: the runner's (or a gateway error's) status decides success, and a
   * client that disconnects first leaves an exception.
   */
  private recordProxiedOperation(name: ProxiedOperation, box: Box, ctx: Context, res: Response): void {
    const event = {
      name,
      correlationId: box.id,
      organizationId: box.organizationId,
      ctx,
    }
    recordBusinessEvent({ ...event, outcome: 'requested' })
    let recorded = false
    const finish = (exceptionType?: string) => {
      if (recorded) return
      recorded = true
      res.off('finish', onFinish)
      res.off('close', onClose)
      if (exceptionType) recordBusinessEvent({ ...event, outcome: 'exception', exceptionType })
      else recordBusinessEvent({ ...event, outcome: 'success' })
    }
    const onFinish = () => finish(res.statusCode >= 400 ? `http_${res.statusCode}` : undefined)
    const onClose = () => finish('client_closed')
    res.once('finish', onFinish)
    res.once('close', onClose)
    // The asynchronous box lookup may have outlived the response's terminal event.
    if (res.writableFinished) onFinish()
    else if (res.destroyed || res.closed) onClose()
  }

  private startActivityHeartbeat(boxId: string, autoStopSeconds: number): () => void {
    // Half the autoStop window (in ms) so the idle deadline can never lapse
    // between two beats, however the sweeper samples it. Redis writes are
    // throttled by the activity service's own per-box lock.
    const intervalMs = autoStopSeconds * 500
    const timer = setInterval(() => {
      void this.boxService
        .updateLastActivityAt(boxId, new Date())
        .catch((err) => this.logger.warn(`updateLastActivityAt heartbeat failed for ${boxId}: ${err}`))
    }, intervalMs)
    timer.unref?.()
    return () => clearInterval(timer)
  }
}