fix(opencode-config): reject redacted placeholder in raw-text config writes

This commit is contained in:
Chris Scott committed 2026-09-29 16:46:10 -04:00
1 parent fa2d794239
commit 24e2037b73
3 files changed
+43 -4

No files matched your search

+9 -4
View File
@@ -223,6 +223,13 @@ function collectOpenCodeConfigRedactedPaths(value: unknown, basePath: string[] =
return paths
}
function assertNoOpenCodeConfigRedactedValues(value: unknown): void {
const paths = collectOpenCodeConfigRedactedPaths(value)
if (paths.length > 0) {
throw new OpenCodeConfigRedactedValueError(paths)
}
}
interface MergeOpenCodeConfigValuesOptions {
deleteNullValues?: boolean
}
@@ -501,13 +508,11 @@ export async function updateOpenCodeConfigFile(
if (targetSource?.rawContent === content) {
return toOpenCodeConfigFile(snapshot)
}
assertNoOpenCodeConfigRedactedValues(parseJsonc(content))
return writeOpenCodeConfigFile(content, targetName, snapshot)
}
const redactedPaths = collectOpenCodeConfigRedactedPaths(content)
if (redactedPaths.length > 0) {
throw new OpenCodeConfigRedactedValueError(redactedPaths)
}
assertNoOpenCodeConfigRedactedValues(content)
const invalidSources = snapshot.sources.filter((source) => !source.isValid).map((source) => source.name)
if (invalidSources.length > 0) {
@@ -208,6 +208,26 @@ describe('internal/opencode-config routes', () => {
await expect(readFile(configPath('opencode.jsonc'), 'utf8')).resolves.toBe(before)
})
it('PUT /api/internal/opencode-config rejects a redacted placeholder in raw content without writing', async () => {
await writeOpenCodeConfigFile(JSON.stringify({ theme: 'dark' }), 'opencode.jsonc')
const before = await readFile(configPath('opencode.jsonc'), 'utf8')
const res = await app.request('/api/internal/opencode-config', {
method: 'PUT',
headers: { 'content-type': 'application/json', ...authHeaders() },
body: JSON.stringify({
content: '{\n // keep\n "provider": { "example": { "apiKey": "<redacted>" } }\n}\n',
source: 'opencode.jsonc',
}),
})
expect(res.status).toBe(400)
const body = await res.json() as { error: string; paths: string[] }
expect(body.paths).toEqual(['provider.example.apiKey'])
expect(body.error).toContain('redacted placeholder')
await expect(readFile(configPath('opencode.jsonc'), 'utf8')).resolves.toBe(before)
})
it('PATCH /api/internal/opencode-config reloads for a non-mcp change', async () => {
await writeOpenCodeConfigFile(JSON.stringify({ theme: 'dark' }), 'opencode.jsonc')
@@ -407,6 +407,20 @@ describe('opencode-config-file', () => {
await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe(raw)
})
it('rejects a redacted placeholder in raw content and writes nothing', async () => {
const raw = '{"theme":"dark"}'
await writeFile(sourcePath('opencode.json'), raw, 'utf8')
const error = await updateOpenCodeConfigFile(
`{\n // comment\n "instructions": ["keep", "${OPENCODE_CONFIG_REDACTED_VALUE}"],\n}\n`,
{ source: 'opencode.json' },
).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')