mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
feat(opencode-config): redact secrets and add MCP listing to the internal config API
This commit is contained in:
1 parent
5bd688de4b
commit
4b294fbcef
31 files changed
+1238
-181
No files matched your search
@@ -92,7 +92,8 @@ describe('buildSettingsSkill', () => {
|
||||
const skill = buildSettingsSkill()
|
||||
expect(skill).toContain('## OpenCode Configuration')
|
||||
expect(skill).toContain('GET /opencode-config')
|
||||
expect(skill).toContain('PUT /opencode-config')
|
||||
expect(skill).toContain('PATCH /opencode-config')
|
||||
expect(skill).toContain('GET /opencode-config/mcp')
|
||||
expect(skill).toContain('restartRequired')
|
||||
expect(skill).toContain('Never attempt the restart yourself')
|
||||
})
|
||||
|
||||
@@ -65,7 +65,14 @@ function parseSnapshot(snapshot: string): { version: number; sources: Array<{ na
|
||||
}
|
||||
|
||||
const locationReloadMock = vi.hoisted(() => vi.fn<() => Promise<void>>())
|
||||
const openCodeClient = { api: { location: { reload: locationReloadMock } } } as unknown as OpenCodeClient
|
||||
const mcpAddMock = vi.hoisted(() => vi.fn<() => Promise<void>>())
|
||||
const mcpRemoveMock = vi.hoisted(() => vi.fn<() => Promise<void>>())
|
||||
const openCodeClient = {
|
||||
api: {
|
||||
location: { reload: locationReloadMock },
|
||||
mcp: { add: mcpAddMock, remove: mcpRemoveMock },
|
||||
},
|
||||
} as unknown as OpenCodeClient
|
||||
|
||||
describe('opencode-config-apply', () => {
|
||||
let workDir: string
|
||||
@@ -79,6 +86,8 @@ describe('opencode-config-apply', () => {
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks()
|
||||
locationReloadMock.mockReset().mockResolvedValue(undefined)
|
||||
mcpAddMock.mockReset().mockResolvedValue(undefined)
|
||||
mcpRemoveMock.mockReset().mockResolvedValue(undefined)
|
||||
workDir = await mkdtemp(path.join(tmpdir(), 'opencode-config-apply-'))
|
||||
paths.config = path.join(workDir, 'opencode.json')
|
||||
paths.configDir = workDir
|
||||
@@ -323,18 +332,50 @@ describe('opencode-config-apply', () => {
|
||||
expect(clearStartupErrorMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('applies an mcp-only change without reloading or marking a restart pending', async () => {
|
||||
it('reloads an mcp-only change without touching the MCP API', async () => {
|
||||
await writeFile(sourcePath('opencode.json'), '{"theme":"dark"}', 'utf8')
|
||||
const server = { type: 'local' as const, command: ['npx', 'local'] }
|
||||
|
||||
const result = expectStatus(await applyOpenCodeConfigUpdate({
|
||||
content: { theme: 'dark', mcp: { local: { type: 'local' } } },
|
||||
content: { theme: 'dark', mcp: { servers: { local: server } } },
|
||||
settingsService,
|
||||
openCodeClient,
|
||||
}), 'applied')
|
||||
}), 'reloaded')
|
||||
|
||||
expect(result.config.content).toEqual({ theme: 'dark', mcp: { local: { type: 'local' } } })
|
||||
expect(result.config.content).toEqual({ theme: 'dark', mcp: { servers: { local: server } } })
|
||||
expect(locationReloadMock).toHaveBeenCalledOnce()
|
||||
expect(mcpAddMock).not.toHaveBeenCalled()
|
||||
expect(mcpRemoveMock).not.toHaveBeenCalled()
|
||||
expect(markRestartPendingMock).not.toHaveBeenCalled()
|
||||
expect(locationReloadMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reloads an mcp-only removal without touching the MCP API', async () => {
|
||||
const server = { type: 'local' as const, command: ['npx', 'local'] }
|
||||
await writeFile(sourcePath('opencode.json'), JSON.stringify({ theme: 'dark', mcp: { servers: { local: server } } }), 'utf8')
|
||||
|
||||
expectStatus(await applyOpenCodeConfigUpdate({
|
||||
content: { theme: 'dark', mcp: { servers: {} } },
|
||||
settingsService,
|
||||
openCodeClient,
|
||||
}), 'reloaded')
|
||||
|
||||
expect(locationReloadMock).toHaveBeenCalledOnce()
|
||||
expect(mcpRemoveMock).not.toHaveBeenCalled()
|
||||
expect(mcpAddMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reloads an mcp-only change that touches a legacy flat entry', async () => {
|
||||
await writeFile(sourcePath('opencode.json'), '{"theme":"dark"}', 'utf8')
|
||||
|
||||
expectStatus(await applyOpenCodeConfigUpdate({
|
||||
content: { theme: 'dark', mcp: { legacy: { type: 'local', command: ['npx', 'legacy'] } } },
|
||||
settingsService,
|
||||
openCodeClient,
|
||||
}), 'reloaded')
|
||||
|
||||
expect(locationReloadMock).toHaveBeenCalledOnce()
|
||||
expect(mcpAddMock).not.toHaveBeenCalled()
|
||||
expect(mcpRemoveMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reloads when mcp changes alongside another key', async () => {
|
||||
|
||||
@@ -63,8 +63,10 @@ vi.mock('../../src/utils/fs-safe', async (importOriginal) => {
|
||||
import { LEGACY_OPENCODE_CONFIG_SOURCE_NAME } from '@opencode-manager/shared'
|
||||
import {
|
||||
HEALTH_WATCH_MAX_ENTRIES,
|
||||
OPENCODE_CONFIG_REDACTED_VALUE,
|
||||
OPENCODE_CONFIG_SEED,
|
||||
OpenCodeConfigConflictError,
|
||||
OpenCodeConfigRedactedValueError,
|
||||
OpenCodeConfigShadowedRemovalError,
|
||||
OpenCodeConfigSnapshotError,
|
||||
archiveBrokenOpenCodeConfigFile,
|
||||
@@ -249,6 +251,162 @@ describe('opencode-config-file', () => {
|
||||
await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe(lower)
|
||||
})
|
||||
|
||||
it('merges only the patched paths and leaves every other field untouched', async () => {
|
||||
const lower = '{\n // lower config\n "model": "lower/model"\n}\n'
|
||||
const target = '{\n // target config\n "theme": "dark",\n "small_model": "small"\n}\n'
|
||||
await writeFile(sourcePath('opencode.json'), lower, 'utf8')
|
||||
await writeFile(sourcePath('opencode.jsonc'), target, 'utf8')
|
||||
|
||||
const updated = await updateOpenCodeConfigFile({ theme: 'light' }, { mode: 'merge' })
|
||||
|
||||
expect(updated.content).toEqual({ model: 'lower/model', theme: 'light', small_model: 'small' })
|
||||
const targetContent = await readFile(sourcePath('opencode.jsonc'), 'utf8')
|
||||
expect(targetContent).toContain('// target config')
|
||||
expect(targetContent).toContain('"theme": "light"')
|
||||
expect(targetContent).toContain('"small_model": "small"')
|
||||
await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe(lower)
|
||||
})
|
||||
|
||||
it('merges a nested patch, creating paths that do not exist yet', async () => {
|
||||
const raw = JSON.stringify({
|
||||
mcp: {
|
||||
servers: {
|
||||
linear: { type: 'remote', url: 'https://linear.example.com', headers: { Authorization: 'Bearer old' } },
|
||||
},
|
||||
},
|
||||
})
|
||||
await writeFile(sourcePath('opencode.json'), raw, 'utf8')
|
||||
|
||||
const updated = await updateOpenCodeConfigFile(
|
||||
{
|
||||
mcp: {
|
||||
servers: {
|
||||
linear: { headers: { Authorization: 'Bearer new' } },
|
||||
github: { type: 'local', command: ['npx', 'github'] },
|
||||
},
|
||||
},
|
||||
},
|
||||
{ mode: 'merge' },
|
||||
)
|
||||
|
||||
expect(updated.content).toEqual({
|
||||
mcp: {
|
||||
servers: {
|
||||
linear: { type: 'remote', url: 'https://linear.example.com', headers: { Authorization: 'Bearer new' } },
|
||||
github: { type: 'local', command: ['npx', 'github'] },
|
||||
},
|
||||
},
|
||||
})
|
||||
const onDisk = JSON.parse(await readFile(sourcePath('opencode.json'), 'utf8')) as Record<string, unknown>
|
||||
expect(onDisk).toEqual(updated.content)
|
||||
})
|
||||
|
||||
it('removes a patched path with a null value', async () => {
|
||||
await writeFile(sourcePath('opencode.json'), JSON.stringify({ theme: 'dark', small_model: 's' }), 'utf8')
|
||||
|
||||
const updated = await updateOpenCodeConfigFile({ small_model: null }, { mode: 'merge' })
|
||||
|
||||
expect(updated.content).toEqual({ theme: 'dark' })
|
||||
const onDisk = JSON.parse(await readFile(sourcePath('opencode.json'), 'utf8')) as Record<string, unknown>
|
||||
expect(onDisk).toEqual({ theme: 'dark' })
|
||||
})
|
||||
|
||||
it('treats a merge patch with unchanged values as no change', async () => {
|
||||
const raw = '{\n // keep this comment\n "theme": "dark"\n}\n'
|
||||
await writeFile(sourcePath('opencode.json'), raw, 'utf8')
|
||||
|
||||
const updated = await updateOpenCodeConfigFile({ theme: 'dark' }, { mode: 'merge' })
|
||||
|
||||
expect(updated.rawContent).toBe(raw)
|
||||
})
|
||||
|
||||
it('rejects a merge patch whose merged result is schema-invalid and writes nothing', async () => {
|
||||
const raw = '{"theme":"dark"}'
|
||||
await writeFile(sourcePath('opencode.json'), raw, 'utf8')
|
||||
|
||||
await expect(updateOpenCodeConfigFile({ model: 5 }, { mode: 'merge' })).rejects.toBeInstanceOf(ZodError)
|
||||
|
||||
await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe(raw)
|
||||
})
|
||||
|
||||
it('rejects a merge patch that removes a value only a lower-priority source defines', async () => {
|
||||
const lower = '{"theme":"light"}'
|
||||
const target = '{"model":"b"}'
|
||||
await writeFile(sourcePath('opencode.json'), lower, 'utf8')
|
||||
await writeFile(sourcePath('opencode.jsonc'), target, 'utf8')
|
||||
|
||||
const error = await updateOpenCodeConfigFile({ theme: null }, { mode: 'merge' }).catch((caught: unknown) => caught)
|
||||
|
||||
expect(error).toBeInstanceOf(OpenCodeConfigShadowedRemovalError)
|
||||
expect((error as OpenCodeConfigShadowedRemovalError).paths).toEqual(['theme'])
|
||||
expect((error as OpenCodeConfigShadowedRemovalError).sources).toEqual(['opencode.json'])
|
||||
await expect(readFile(sourcePath('opencode.jsonc'), 'utf8')).resolves.toBe(target)
|
||||
await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe(lower)
|
||||
})
|
||||
|
||||
it('merges into an existing nested object while deleting sibling keys with null', async () => {
|
||||
const raw = JSON.stringify({
|
||||
mcp: { servers: { linear: { url: 'https://linear.example.com', headers: { Authorization: 'Bearer old', 'X-Keep': '1' } } } },
|
||||
})
|
||||
await writeFile(sourcePath('opencode.json'), raw, 'utf8')
|
||||
|
||||
const updated = await updateOpenCodeConfigFile(
|
||||
{ mcp: { servers: { linear: { headers: { Authorization: null } } } } },
|
||||
{ mode: 'merge' },
|
||||
)
|
||||
|
||||
expect(updated.content).toEqual({
|
||||
mcp: { servers: { linear: { url: 'https://linear.example.com', headers: { 'X-Keep': '1' } } } },
|
||||
})
|
||||
const onDisk = JSON.parse(await readFile(sourcePath('opencode.json'), 'utf8')) as Record<string, unknown>
|
||||
expect(onDisk).toEqual(updated.content)
|
||||
})
|
||||
|
||||
it('strips nested nulls when a merge patch creates a new path', async () => {
|
||||
await writeFile(sourcePath('opencode.json'), '{"theme":"dark"}', 'utf8')
|
||||
|
||||
const updated = await updateOpenCodeConfigFile(
|
||||
{ mcp: { servers: { linear: { url: 'https://linear.example.com', headers: { Authorization: null, 'X-Trace': 'on' } } } } },
|
||||
{ mode: 'merge' },
|
||||
)
|
||||
|
||||
expect(updated.content).toEqual({
|
||||
theme: 'dark',
|
||||
mcp: { servers: { linear: { url: 'https://linear.example.com', headers: { 'X-Trace': 'on' } } } },
|
||||
})
|
||||
const onDisk = await readFile(sourcePath('opencode.json'), 'utf8')
|
||||
expect(onDisk).not.toContain('null')
|
||||
expect(JSON.parse(onDisk)).toEqual(updated.content)
|
||||
})
|
||||
|
||||
it('rejects a redacted placeholder in a merge patch and writes nothing', async () => {
|
||||
const raw = '{"theme":"dark"}'
|
||||
await writeFile(sourcePath('opencode.json'), raw, 'utf8')
|
||||
|
||||
const error = await updateOpenCodeConfigFile(
|
||||
{ mcp: { servers: { linear: { headers: { Authorization: OPENCODE_CONFIG_REDACTED_VALUE } } } } },
|
||||
{ mode: 'merge' },
|
||||
).catch((caught: unknown) => caught)
|
||||
|
||||
expect(error).toBeInstanceOf(OpenCodeConfigRedactedValueError)
|
||||
expect((error as OpenCodeConfigRedactedValueError).paths).toEqual(['mcp.servers.linear.headers.Authorization'])
|
||||
await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe(raw)
|
||||
})
|
||||
|
||||
it('rejects a redacted placeholder in replace content including array indexes and writes nothing', async () => {
|
||||
const raw = '{"theme":"dark"}'
|
||||
await writeFile(sourcePath('opencode.json'), raw, 'utf8')
|
||||
|
||||
const error = await updateOpenCodeConfigFile({
|
||||
theme: 'light',
|
||||
instructions: ['keep', OPENCODE_CONFIG_REDACTED_VALUE],
|
||||
}).catch((caught: unknown) => caught)
|
||||
|
||||
expect(error).toBeInstanceOf(OpenCodeConfigRedactedValueError)
|
||||
expect((error as OpenCodeConfigRedactedValueError).paths).toEqual(['instructions.1'])
|
||||
await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe(raw)
|
||||
})
|
||||
|
||||
it('removes an override from the target source only and reveals inherited values', async () => {
|
||||
const lower = JSON.stringify({ theme: 'light', model: 'a' })
|
||||
await writeFile(sourcePath('opencode.json'), lower, 'utf8')
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { redactOpenCodeConfigFile } from '../../src/services/opencode-config-redact'
|
||||
import type { OpenCodeConfigFile } from '../../src/types/settings'
|
||||
|
||||
function buildConfig(content: Record<string, unknown>): OpenCodeConfigFile {
|
||||
return {
|
||||
path: '/config/opencode.jsonc',
|
||||
content,
|
||||
rawContent: JSON.stringify(content),
|
||||
isValid: true,
|
||||
updatedAt: 1,
|
||||
sources: [
|
||||
{
|
||||
name: 'opencode.jsonc',
|
||||
path: '/config/opencode.jsonc',
|
||||
content,
|
||||
rawContent: JSON.stringify(content),
|
||||
isValid: true,
|
||||
updatedAt: 1,
|
||||
},
|
||||
],
|
||||
revision: 'revision',
|
||||
}
|
||||
}
|
||||
|
||||
describe('redactOpenCodeConfigFile', () => {
|
||||
it('redacts secret-keyed values and every header and environment value while keeping keys', () => {
|
||||
const redacted = redactOpenCodeConfigFile(
|
||||
buildConfig({
|
||||
theme: 'dark',
|
||||
providers: { example: { apiKey: 'secret-key', label: 'Example' } },
|
||||
mcp: {
|
||||
servers: {
|
||||
linear: {
|
||||
type: 'remote',
|
||||
url: 'https://linear.example.com',
|
||||
headers: { Authorization: 'Bearer secret', 'X-Trace': 'trace' },
|
||||
},
|
||||
local: { type: 'local', command: ['npx', 'server'], environment: { GITHUB_TOKEN: 'token', NODE_ENV: 'production' } },
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
expect(redacted.content).toEqual({
|
||||
theme: 'dark',
|
||||
providers: { example: { apiKey: '<redacted>', label: 'Example' } },
|
||||
mcp: {
|
||||
servers: {
|
||||
linear: {
|
||||
type: 'remote',
|
||||
url: 'https://linear.example.com',
|
||||
headers: { Authorization: '<redacted>', 'X-Trace': '<redacted>' },
|
||||
},
|
||||
local: {
|
||||
type: 'local',
|
||||
command: ['npx', 'server'],
|
||||
environment: { GITHUB_TOKEN: '<redacted>', NODE_ENV: '<redacted>' },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
expect(redacted.redactedPaths).toEqual([
|
||||
'mcp.servers.linear.headers.Authorization',
|
||||
'mcp.servers.linear.headers.X-Trace',
|
||||
'mcp.servers.local.environment.GITHUB_TOKEN',
|
||||
'mcp.servers.local.environment.NODE_ENV',
|
||||
'providers.example.apiKey',
|
||||
])
|
||||
})
|
||||
|
||||
it('drops the raw source text and redacts each source content', () => {
|
||||
const redacted = redactOpenCodeConfigFile(buildConfig({ providers: { example: { apiKey: 'secret' } } }))
|
||||
const [source] = redacted.sources
|
||||
|
||||
expect('rawContent' in redacted).toBe(false)
|
||||
expect(redacted.content).toEqual({ providers: { example: { apiKey: '<redacted>' } } })
|
||||
expect(redacted.redactedPaths).toEqual(['providers.example.apiKey'])
|
||||
expect(source?.content).toEqual({ providers: { example: { apiKey: '<redacted>' } } })
|
||||
expect(source && 'rawContent' in source).toBe(false)
|
||||
})
|
||||
|
||||
it('leaves a config without secrets unchanged', () => {
|
||||
const redacted = redactOpenCodeConfigFile(buildConfig({ theme: 'dark', plugin: ['/plugins/probe'] }))
|
||||
|
||||
expect(redacted.content).toEqual({ theme: 'dark', plugin: ['/plugins/probe'] })
|
||||
expect(redacted.redactedPaths).toEqual([])
|
||||
})
|
||||
|
||||
it('leaves numeric token settings untouched', () => {
|
||||
const redacted = redactOpenCodeConfigFile(
|
||||
buildConfig({ maxTokens: 8192, budgetTokens: 4096, max_tokens: 2048 }),
|
||||
)
|
||||
|
||||
expect(redacted.content).toEqual({ maxTokens: 8192, budgetTokens: 4096, max_tokens: 2048 })
|
||||
expect(redacted.redactedPaths).toEqual([])
|
||||
})
|
||||
|
||||
it('does not mask server, provider, or model names that look like secret keys', () => {
|
||||
const redacted = redactOpenCodeConfigFile(
|
||||
buildConfig({
|
||||
mcp: {
|
||||
servers: {
|
||||
'github-token': {
|
||||
type: 'remote',
|
||||
url: 'https://github.example.com',
|
||||
headers: { Authorization: 'Bearer secret' },
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: {
|
||||
'secret-provider': {
|
||||
apiKey: 'provider-secret',
|
||||
models: { 'token-model': { name: 'gpt' } },
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
expect(redacted.content).toEqual({
|
||||
mcp: {
|
||||
servers: {
|
||||
'github-token': {
|
||||
type: 'remote',
|
||||
url: 'https://github.example.com',
|
||||
headers: { Authorization: '<redacted>' },
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: {
|
||||
'secret-provider': {
|
||||
apiKey: '<redacted>',
|
||||
models: { 'token-model': { name: 'gpt' } },
|
||||
},
|
||||
},
|
||||
})
|
||||
expect(redacted.redactedPaths).toEqual([
|
||||
'mcp.servers.github-token.headers.Authorization',
|
||||
'provider.secret-provider.apiKey',
|
||||
])
|
||||
})
|
||||
|
||||
it('leaves non-string secret-keyed values untouched', () => {
|
||||
const redacted = redactOpenCodeConfigFile(
|
||||
buildConfig({ password: 1234, token: true, secret: null }),
|
||||
)
|
||||
|
||||
expect(redacted.content).toEqual({ password: 1234, token: true, secret: null })
|
||||
expect(redacted.redactedPaths).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -224,20 +224,20 @@ describe('ocm-manager plugin', () => {
|
||||
expect(JSON.parse(init.body)).toEqual({ theme: 'dark' })
|
||||
})
|
||||
|
||||
it('sends a PUT request with a JSON body', async () => {
|
||||
it('sends a PATCH request to the config route with a JSON body', async () => {
|
||||
const fetchMock = jsonResponse({})
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
const tool = await loadTool(configHome)
|
||||
|
||||
await runTool(tool, { action: 'request', params: { method: 'PUT', path: '/opencode-config', body: { content: { theme: 'dark' } } } })
|
||||
await runTool(tool, { action: 'request', params: { method: 'PATCH', path: '/opencode-config', body: { patch: { theme: 'dark' } } } })
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||
const [url, init] = fetchMock.mock.calls[0] ?? []
|
||||
expect(url).toBe('http://localhost:5003/api/internal/opencode-config')
|
||||
expect(init.method).toBe('PUT')
|
||||
expect(init.method).toBe('PATCH')
|
||||
expect(init.headers.Authorization).toBe('Bearer secret-token')
|
||||
expect(init.headers['content-type']).toBe('application/json')
|
||||
expect(JSON.parse(init.body)).toEqual({ content: { theme: 'dark' } })
|
||||
expect(JSON.parse(init.body)).toEqual({ patch: { theme: 'dark' } })
|
||||
})
|
||||
|
||||
it('allows every route in the exported allow list', async () => {
|
||||
@@ -263,6 +263,7 @@ describe('ocm-manager plugin', () => {
|
||||
['GET', '/repos/0/mirror/head'],
|
||||
['POST', '/notifications/send'],
|
||||
['DELETE', '/settings'],
|
||||
['PUT', '/opencode-config'],
|
||||
] as const
|
||||
|
||||
for (const [method, path] of deniedRoutes) {
|
||||
|
||||
Reference in new issue
Block a user