Files
opencode-manager/backend/test/routes/internal/shell-env.test.ts
T
Chris Scott e0331b43bf feat(secrets): repository secrets and variables injected into agent shells and terminals
Add a GitHub Actions-style store for secrets and variables so tests can read
credentials from the environment without .env files in the repo.

- secrets table keyed by kind, repo (NULL for global) and environment;
  secrets are encrypted with encryptSecret and write-only, variables are plain
  text. Names follow GitHub's rules and reject Manager-reserved names.
- Global (/api/secrets, /api/variables) and repo
  (/api/repos/:id/secrets|variables) routes.
- The trusted .ocm/project.json gains environment and env fields that map
  names to ${{ secrets.X }} / ${{ vars.X }} expressions. Both are part of the
  trust hash; files without them hash as before, so existing trust survives.
- RepoEnvService.resolveForDirectory resolves the repo and approved mapping
  for any directory, including worktrees outside the repo folder, with
  environment > repo > global precedence. It never throws.
- /api/internal/shell-env replaces /git-credentials/gh-env and feeds the
  ocm-gh-env plugin, the sandbox shell plan and TerminalService.create.
- The plugin masks injected secret values as *** in completed tool results.
- The sandbox shell plan is now the only source of the guest environment: the
  plugin pins OCM_SANDBOX_FORWARD_ENV and the shim forwards only those names.
2026-10-23 22:59:57 -04:00

77 lines
3.0 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest'
import { Database } from 'bun:sqlite'
import { migrate } from '../../../src/db/migration-runner'
import { allMigrations } from '../../../src/db/migrations'
import { SettingsService } from '../../../src/services/settings'
import { createInternalShellEnvRoutes } from '../../../src/routes/internal/shell-env'
import type { RepoEnvService, ResolvedRepoEnv } from '../../../src/services/repo-env'
import type { GitCredential } from '@opencode-manager/shared'
function createRepoEnvService(resolve: (cwd: string) => Promise<ResolvedRepoEnv>): RepoEnvService {
return { resolveForDirectory: vi.fn(resolve) } as unknown as RepoEnvService
}
describe('internal shell-env routes', () => {
let db: Database
let settingsService: SettingsService
function appWith(repoEnvService: RepoEnvService) {
return createInternalShellEnvRoutes(db, repoEnvService)
}
beforeEach(() => {
db = new Database(':memory:')
migrate(db, allMigrations)
settingsService = new SettingsService(db)
})
it('returns GH_TOKEN and GITHUB_TOKEN for a GitHub PAT when no cwd is given', async () => {
settingsService.updateSettings({
gitCredentials: [
{ name: 'github', host: 'github.com', type: 'pat', token: 'ghp_test_token' } as GitCredential,
],
})
const repoEnvService = createRepoEnvService(async () => ({ env: {}, maskValues: [], missing: [] }))
const app = appWith(repoEnvService)
const res = await app.request('/')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ env: { GH_TOKEN: 'ghp_test_token', GITHUB_TOKEN: 'ghp_test_token' }, mask: [] })
expect(repoEnvService.resolveForDirectory).not.toHaveBeenCalled()
})
it('returns an empty GH env when no GitHub credential exists', async () => {
const app = appWith(createRepoEnvService(async () => ({ env: {}, maskValues: [], missing: [] })))
const res = await app.request('/')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ env: {}, mask: [] })
})
it('merges the mapped repository env and mask after the GitHub env for a cwd', async () => {
settingsService.updateSettings({
gitCredentials: [
{ name: 'github', host: 'github.com', type: 'pat', token: 'ghp_repo_token' } as GitCredential,
],
gitIdentities: [{ id: 'work', name: 'Work User', email: 'work@example.com' }],
})
const repoEnvService = createRepoEnvService(async () => ({
env: { API_URL: 'https://internal.example', GITHUB_TOKEN: 'mapped' },
maskValues: ['super-secret'],
missing: [],
}))
const app = appWith(repoEnvService)
const res = await app.request('/?cwd=/tmp/any-repo')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
env: { GH_TOKEN: 'ghp_repo_token', GITHUB_TOKEN: 'mapped', API_URL: 'https://internal.example' },
mask: ['super-secret'],
})
expect(repoEnvService.resolveForDirectory).toHaveBeenCalledWith('/tmp/any-repo')
})
})