feat(sandbox): route host-shell surfaces into microVM via shell wrapper

This commit is contained in:
Chris Scott committed 2026-08-14 18:26:21 -04:00
1 parent aa3fb48a62
commit ae117d6a78
57 files changed
+711 -784

No files matched your search

+2 -29
View File
@@ -1,6 +1,7 @@
import { logger } from '../../utils/logger'
import { ENV } from '@opencode-manager/shared/config/env'
import { getOpenCodeBasicAuthHeader, type OpenCodePasswordResolver } from './auth'
import { getOpenCodeUpstreamBaseUrl } from './upstream'
export interface ForwardRequest {
method: string
@@ -35,8 +36,6 @@ export interface OpenCodeClient {
postJson<T>(path: string, body: unknown, opts?: JsonRequestOptions): Promise<T>
setProviderAuth(providerId: string, apiKey: string): Promise<boolean>
deleteProviderAuth(providerId: string): Promise<boolean>
startMcpAuth(serverName: string, directory?: string): Promise<Response>
authenticateMcp(serverName: string, directory?: string): Promise<Response>
}
export type OpenCodeClientHost = string | (() => string)
@@ -234,28 +233,6 @@ export class FetchOpenCodeClient implements OpenCodeClient {
logger.error(`Failed to delete OpenCode auth: ${response.status} ${response.statusText}`)
return false
}
async startMcpAuth(serverName: string, directory?: string): Promise<Response> {
return this.request({
method: 'POST',
path: `/mcp/${encodeURIComponent(serverName)}/auth`,
headers: { 'Content-Type': 'application/json' },
directory,
})
}
async authenticateMcp(serverName: string, directory?: string): Promise<Response> {
return this.request({
method: 'POST',
path: `/mcp/${encodeURIComponent(serverName)}/auth/authenticate`,
headers: { 'Content-Type': 'application/json' },
directory,
})
}
}
function formatOpenCodeHostForUrl(host: string): string {
return host.includes(':') ? `[${host}]` : host
}
export function createOpenCodeClient(
@@ -263,11 +240,7 @@ export function createOpenCodeClient(
host?: OpenCodeClientHost,
): OpenCodeClient {
const resolveConfiguredHost = typeof host === 'function' ? host : () => (host ?? ENV.OPENCODE.HOST)
const baseUrl: OpenCodeClientHost = () => {
const configuredHost = resolveConfiguredHost()
const normalized = configuredHost === '0.0.0.0' ? '127.0.0.1' : configuredHost
return `http://${formatOpenCodeHostForUrl(normalized)}:${ENV.OPENCODE.PORT}`
}
const baseUrl: OpenCodeClientHost = () => getOpenCodeUpstreamBaseUrl(false, resolveConfiguredHost())
const passwordResolver = typeof passwordOverride === 'function' ? passwordOverride : undefined
const password = typeof passwordOverride === 'string' ? passwordOverride : ENV.OPENCODE.SERVER_PASSWORD
const basicAuth = getOpenCodeBasicAuthHeader(password)
@@ -0,0 +1,27 @@
import path from 'path'
import { writeFileAtomic } from '../../utils/fs-safe'
import { buildGhEnvPluginSource } from '../opencode-gh-env-plugin'
import { buildSandboxPluginSource } from '../opencode-sandbox-plugin'
export type ManagedOpenCodePlugin = {
filename: string
buildSource: () => string
}
export const MANAGED_OPENCODE_PLUGINS: readonly ManagedOpenCodePlugin[] = [
{ filename: 'ocm-gh-env.js', buildSource: buildGhEnvPluginSource },
{ filename: 'ocm-sandbox.js', buildSource: buildSandboxPluginSource },
]
export const TRUSTED_OPENCODE_PLUGIN_FILENAMES: readonly string[] = MANAGED_OPENCODE_PLUGINS.map((plugin) => plugin.filename)
export function getOpenCodePluginDir(configHome: string): string {
return path.join(configHome, 'opencode', 'plugin')
}
export async function installManagedPlugins(configHome: string): Promise<void> {
const dir = getOpenCodePluginDir(configHome)
for (const plugin of MANAGED_OPENCODE_PLUGINS) {
await writeFileAtomic(path.join(dir, plugin.filename), plugin.buildSource())
}
}
@@ -32,16 +32,6 @@ const BLOCKED_ENFORCED_ROUTES: ReadonlyArray<{
pathPattern: RegExp
reason: string
}> = [
{
methods: ['POST'],
pathPattern: /^\/session\/[^/]+\/shell$/,
reason: 'the session shell endpoint (`!command`) runs in the OpenCode host process',
},
{
methods: ['POST'],
pathPattern: /^\/session\/[^/]+\/command$/,
reason: 'custom slash commands can run shell templates in the OpenCode host process',
},
{
methods: ['POST'],
pathPattern: /^\/pty$/,
+32
View File
@@ -0,0 +1,32 @@
import { ENV } from '@opencode-manager/shared/config/env'
function isLoopbackBindHost(host: string): boolean {
const normalized = host.trim().toLowerCase().replace(/^\[|\]$/g, '')
return (
normalized === 'localhost' ||
normalized === '127.0.0.1' ||
normalized === '::1' ||
normalized === '::ffff:127.0.0.1' ||
normalized.startsWith('127.')
)
}
function resolveEffectiveHost(enforced: boolean, host: string): string {
return enforced && !isLoopbackBindHost(host) ? '127.0.0.1' : host
}
export function resolveEffectiveServerHost(enforced: boolean): string {
return resolveEffectiveHost(enforced, ENV.OPENCODE.HOST)
}
function formatOpenCodeHostForUrl(host: string): string {
if (host.startsWith('[') && host.endsWith(']')) return host
return host.includes(':') ? `[${host}]` : host
}
export function getOpenCodeUpstreamBaseUrl(enforced: boolean, hostOverride?: string | (() => string)): string {
const configuredHost = typeof hostOverride === 'function' ? hostOverride() : (hostOverride ?? ENV.OPENCODE.HOST)
const effectiveHost = resolveEffectiveHost(enforced, configuredHost)
const normalizedHost = effectiveHost === '0.0.0.0' ? '127.0.0.1' : effectiveHost
return `http://${formatOpenCodeHostForUrl(normalizedHost)}:${ENV.OPENCODE.PORT}`
}