mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
* feat(sandbox): add KVM-backed agent sandboxing via microsandbox * feat(sandbox): route host-shell surfaces into microVM via shell wrapper * feat(sandbox): route host-shell surfaces into microVM via shell wrapper * fix(sandbox): harden enforcement validation and backup handling * fix(sandbox): bound lifecycle queue and harden install/backup paths Bound the OpenCode supervisor lifecycle queue to one running plus one pending operation so repeated restarts cannot stack unbounded HTTP requests, while exempting stop() so shutdown is never dropped. Reject enforcement backups that carry no recoverable removed sections, matching the existing fail-closed handling of unparseable backups. Install OpenCode through a private mktemp staging directory instead of the predictable /tmp/opencode path. Use named path/os imports in the files added by this branch. * refactor(sandbox): remove OpenCode version allowlist gating under enforcement * fix(settings): use resolved opencode binary and verify installed version * refactor(sandbox): scope enforcement to agent bash tool only * fix(sandbox): plan bash against factory directory and attest runtime tmpfs; align permission reply API * fix(sandbox): pin neutral entrypoint and persist opencode upgrades across rebuilds * fix(sandbox): align canonical spec with msb 0.6.8 runtime defaults * feat(sandbox): show unwrapped sandboxed bash commands in message UI * refactor(sandbox): pin shell shim to sandbox agent bash without rewriting commands * feat(sandbox): route notifications through a host-side manager tool * feat(sandbox): route all assistant capabilities through the ocm tool Add an allow-listed request action to the generated ocm plugin so agents reach settings, assistant reload, repos, git-info, workspaces, and schedules through the tool instead of curling the internal API with a bearer token, which is impossible under sandboxing. Rewrite the curl-based skills to use the ocm request action and drop the apiBaseUrl plumbing. Remove the now-unread .opencode/internal-token file and the sandbox tmpfs mask that hid it; attestation now rejects any tmpfs other than the runtime /tmp mount. * fix(sidebar): wire new-session and new-schedule actions to every page AssistantRedirect never registered a listener for the sidebar's new-session event, so the desktop sidebar CTA on /assistant was a no-op; the Schedules pages had the same gap for new-schedule. Make useSidebarAction the single source of truth for action keys and event dispatch, and warn when an action is dispatched with no mounted handler so an unhandled CTA fails loudly instead of silently. Wire the missing listeners to the existing per-page handlers. * feat(sandbox): forward git credentials into the microVM and add sandbox guest image * feat(sandbox): add git credential forwarding toggle to settings
146 lines
5.4 KiB
TypeScript
146 lines
5.4 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
import { promises as fs } from 'fs'
|
|
import path from 'path'
|
|
import os from 'os'
|
|
import { pathToFileURL } from 'url'
|
|
import { installManagedPlugins, getOpenCodePluginDir } from '../../src/services/opencode/plugin-registry'
|
|
|
|
type ShellEnvHook = (
|
|
input: { cwd: string },
|
|
output: { env: Record<string, string> },
|
|
) => Promise<void>
|
|
type PluginFactory = () => Promise<{ 'shell.env': ShellEnvHook }>
|
|
|
|
async function loadPlugin(configHome: string): Promise<PluginFactory> {
|
|
const file = path.join(getOpenCodePluginDir(configHome), 'ocm-gh-env.js')
|
|
const mod = await import(pathToFileURL(file).href)
|
|
return mod.default as PluginFactory
|
|
}
|
|
|
|
describe('ocm-gh-env plugin', () => {
|
|
let configHome: string
|
|
|
|
beforeEach(async () => {
|
|
configHome = await fs.mkdtemp(path.join(os.tmpdir(), 'ocm-ghenv-'))
|
|
await installManagedPlugins(configHome)
|
|
process.env.OCM_INTERNAL_API_URL = 'http://localhost:5003/api/internal'
|
|
process.env.OCM_INTERNAL_TOKEN = 'secret-token'
|
|
})
|
|
|
|
afterEach(async () => {
|
|
vi.unstubAllGlobals()
|
|
delete process.env.OCM_INTERNAL_API_URL
|
|
delete process.env.OCM_INTERNAL_TOKEN
|
|
await fs.rm(configHome, { recursive: true, force: true })
|
|
})
|
|
|
|
it('writes the plugin file into the auto-discovery dir', async () => {
|
|
const file = path.join(getOpenCodePluginDir(configHome), 'ocm-gh-env.js')
|
|
await expect(fs.access(file)).resolves.toBeUndefined()
|
|
})
|
|
|
|
it('atomically replaces a symlink at the plugin path with a regular file', async () => {
|
|
const pluginDir = getOpenCodePluginDir(configHome)
|
|
const pluginPath = path.join(pluginDir, 'ocm-gh-env.js')
|
|
const symlinkTarget = path.join(pluginDir, 'attacker-hook.js')
|
|
await fs.mkdir(pluginDir, { recursive: true })
|
|
await fs.rm(pluginPath, { force: true })
|
|
await fs.writeFile(symlinkTarget, 'export default async function () {}')
|
|
await fs.symlink(symlinkTarget, pluginPath)
|
|
|
|
await installManagedPlugins(configHome)
|
|
|
|
const stat = await fs.lstat(pluginPath)
|
|
expect(stat.isFile()).toBe(true)
|
|
expect(stat.isSymbolicLink()).toBe(false)
|
|
expect(await fs.readFile(pluginPath, 'utf-8')).toContain('shell.env')
|
|
expect(await fs.readFile(symlinkTarget, 'utf-8')).toBe('export default async function () {}')
|
|
})
|
|
|
|
it('throws when the plugin file cannot be written instead of swallowing the failure', async () => {
|
|
const blockedHome = path.join(configHome, 'blocked')
|
|
await fs.mkdir(blockedHome, { recursive: true })
|
|
await fs.writeFile(path.join(blockedHome, 'opencode'), 'not a directory')
|
|
|
|
await expect(installManagedPlugins(blockedHome)).rejects.toThrow()
|
|
})
|
|
|
|
it('injects fetched GH env into output.env', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' }),
|
|
})
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const factory = await loadPlugin(configHome)
|
|
const hooks = await factory()
|
|
const output = { env: {} as Record<string, string> }
|
|
await hooks['shell.env']({ cwd: '/repo' }, output)
|
|
|
|
expect(output.env).toEqual({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' })
|
|
const [url, options] = fetchMock.mock.calls[0]!
|
|
expect(url.toString()).toBe('http://localhost:5003/api/internal/git-credentials/gh-env?cwd=%2Frepo')
|
|
expect(options).toEqual({ headers: { Authorization: 'Bearer secret-token' } })
|
|
})
|
|
|
|
it('does not fetch when the internal env vars are missing', async () => {
|
|
delete process.env.OCM_INTERNAL_API_URL
|
|
delete process.env.OCM_INTERNAL_TOKEN
|
|
const fetchMock = vi.fn()
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const factory = await loadPlugin(configHome)
|
|
const hooks = await factory()
|
|
const output = { env: {} as Record<string, string> }
|
|
await hooks['shell.env']({ cwd: '/repo' }, output)
|
|
|
|
expect(output.env).toEqual({})
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('never throws when the fetch fails', async () => {
|
|
const fetchMock = vi.fn().mockRejectedValue(new Error('network down'))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const factory = await loadPlugin(configHome)
|
|
const hooks = await factory()
|
|
const output = { env: {} as Record<string, string> }
|
|
|
|
await expect(hooks['shell.env']({ cwd: '/repo' }, output)).resolves.toBeUndefined()
|
|
expect(output.env).toEqual({})
|
|
})
|
|
|
|
it('caches results within the TTL so rapid calls fetch once', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' }),
|
|
})
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const factory = await loadPlugin(configHome)
|
|
const hooks = await factory()
|
|
const out1 = { env: {} as Record<string, string> }
|
|
const out2 = { env: {} as Record<string, string> }
|
|
await hooks['shell.env']({ cwd: '/repo' }, out1)
|
|
await hooks['shell.env']({ cwd: '/repo' }, out2)
|
|
|
|
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
expect(out2.env).toEqual({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' })
|
|
})
|
|
|
|
it('caches results per cwd', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' }),
|
|
})
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const factory = await loadPlugin(configHome)
|
|
const hooks = await factory()
|
|
await hooks['shell.env']({ cwd: '/repo-a' }, { env: {} })
|
|
await hooks['shell.env']({ cwd: '/repo-b' }, { env: {} })
|
|
|
|
expect(fetchMock).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|