Skip to content

Commit c3d7328

Browse files
opencodeclaude
andcommitted
fix(config): fail closed on unparseable documents, per lead ruling
Supersedes 78be611's lenient-parse fix for the same Copilot-flagged hole. A lenient recovery parse can itself silently DROP the secret-bearing branch when a malformed region swallows it -- the redactor would then see no secrets while the raw text still carries them byte-for-byte. A guarantee conditional on how badly the file is broken is not a guarantee. readTarget now withholds `text` entirely when the document has any parse diagnostic, returning a named RedactionWithheld({reason: "could-not-parse"}) citizen instead of a best-effort redaction -- same pattern as McpRuntime's truthful unavailable default (TKT-323 chunk 2), never an implicit fallback. The escape hatch for a broken config is editing it on disk directly, already the rule for every other read path; the diagnostics already returned are the fix-it information. Both regression tests flipped from asserting a redacted `text` to asserting a withheld one, and the pre-existing malformed-jsonc fixture gains a real secret so this leak class can never return silently again. Confirmed red against the reverted code, green against the fix. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JJQrqYMv43CrdbjpTT7884
1 parent 5879394 commit c3d7328

3 files changed

Lines changed: 57 additions & 21 deletions

File tree

‎packages/core/src/config/document.ts‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export type Diagnostic = ConfigDocumentSchema.Diagnostic
2929
export const ReadResult = ConfigDocumentSchema.ReadResult
3030
export type ReadResult = ConfigDocumentSchema.ReadResult
3131

32+
export const RedactionWithheld = ConfigDocumentSchema.RedactionWithheld
33+
export type RedactionWithheld = ConfigDocumentSchema.RedactionWithheld
34+
3235
export const RestartImpact = ConfigDocumentSchema.RestartImpact
3336
export type RestartImpact = ConfigDocumentSchema.RestartImpact
3437

@@ -344,15 +347,23 @@ const layer = Layer.effect(
344347
// it against the actual on-disk file, so it must never be computed from the redacted display
345348
// copy a client echoes back.
346349
const hash = Hash.sha256(text)
347-
// Redaction must not depend on the document parsing cleanly: `parseAndDiagnose` discards its
348-
// `parsed` value on ANY diagnostic, even one unrelated to mcp, which would otherwise leave a
349-
// secret in `text` unredacted whenever the file has an unrelated syntax error elsewhere
350-
// (Copilot review, PR #32) -- exactly the leak this change exists to close, reachable through
351-
// a different path. Use jsonc-parser's own lenient parse directly, ignoring its error list,
352-
// purely to locate secret paths -- the same forgiving parse it always performs internally.
353-
const lenientParsed: unknown = text.trim() === "" ? undefined : parse(text, [], { allowTrailingComma: true })
354-
const mcp =
355-
lenientParsed && typeof lenientParsed === "object" ? (lenientParsed as Record<string, unknown>).mcp : undefined
350+
// Fail closed rather than redact best-effort (lead ruling, TKT-323 feedback #191 follow-up,
351+
// superseding an earlier lenient-parse fix reviewed on PR #32): a lenient parse can recover a
352+
// tree that silently DROPS the secret-bearing branch when a malformed region swallows it, so
353+
// the redactor would see no secrets while the raw text still carries them byte-for-byte. A
354+
// guarantee conditional on how badly the file is broken is not a guarantee. The escape hatch
355+
// for a broken config is editing it on disk directly (already the rule for every other read
356+
// path); these diagnostics are the fix-it information.
357+
if (diagnostics.length > 0) {
358+
return new ReadResult({
359+
target,
360+
text: new RedactionWithheld({ reason: "could-not-parse" }),
361+
hash,
362+
parsed: redactParsed(parsed),
363+
diagnostics,
364+
})
365+
}
366+
const mcp = parsed && typeof parsed === "object" ? (parsed as Record<string, unknown>).mcp : undefined
356367
const redactedText = mcp === undefined ? text : redactSecretsInText(text, mcp)
357368
return new ReadResult({ target, text: redactedText, hash, parsed: redactParsed(parsed), diagnostics })
358369
})

‎packages/core/test/config/document.test.ts‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,18 @@ describe("ConfigDocument", () => {
8686
),
8787
)
8888

89-
it.live("readTarget surfaces a diagnostic for malformed jsonc instead of throwing", () =>
89+
it.live("readTarget surfaces a diagnostic for malformed jsonc instead of throwing, and never leaks a secret in it", () =>
9090
withTmp((tmp) =>
9191
Effect.gen(function* () {
92-
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "opencode.json"), "{ invalid"))
92+
// Malformed AND secret-bearing (Henry, TKT-323 feedback #191 follow-up): a fixture that is
93+
// merely malformed can't distinguish "fails closed" from "happens not to touch mcp" -- the
94+
// secret is what proves this leak class can never return silently.
95+
yield* Effect.promise(() =>
96+
fs.writeFile(
97+
path.join(tmp.path, "opencode.json"),
98+
'{ "mcp": { "servers": { "local": { "environment": { "API_KEY": "sk-real-secret" } } } } invalid',
99+
),
100+
)
93101

94102
return yield* Effect.gen(function* () {
95103
const doc = yield* ConfigDocument.Service
@@ -98,6 +106,8 @@ describe("ConfigDocument", () => {
98106

99107
expect(read.diagnostics.length).toBeGreaterThan(0)
100108
expect(read.diagnostics[0]?.severity).toBe("error")
109+
expect(read.text).toBeInstanceOf(ConfigDocument.RedactionWithheld)
110+
expect(JSON.stringify(read)).not.toContain("sk-real-secret")
101111
}).pipe(Effect.provide(testLayer(tmp.path))).pipe(Effect.orDie)
102112
}),
103113
),
@@ -222,13 +232,15 @@ describe("ConfigDocument", () => {
222232
),
223233
)
224234

225-
// Regression for a Copilot review finding on PR #32: `parseAndDiagnose` discards its whole
226-
// `parsed` value on ANY diagnostic, even one unrelated to mcp -- readTarget's redaction used to
227-
// key off that same discarded value, so a file with an unrelated syntax error but a real,
228-
// otherwise-valid mcp secret came back over the wire completely unredacted. mcp is valid and
229-
// appears before a genuine, unrelated syntax error (a missing colon) later in the same document
230-
// -- jsonc-parser recovers from it and still needs to redact.
231-
it.live("readTarget still redacts secrets in text when the rest of the document has a syntax error", () =>
235+
// Regression for a Copilot review finding on PR #32, flipped by a lead ruling that superseded
236+
// the fix Copilot itself suggested (lenient parse): `parseAndDiagnose` discards its whole
237+
// `parsed` value on ANY diagnostic, even one unrelated to mcp -- a lenient recovery parse can
238+
// itself silently DROP the secret-bearing branch when a malformed region swallows it, so a
239+
// best-effort redaction is only a guarantee in the common case. The ruling: fail closed instead
240+
// -- withhold `text` entirely rather than return a redaction that isn't reliably complete. mcp is
241+
// valid and appears before a genuine, unrelated syntax error (a missing colon) later in the same
242+
// document -- text must still be withheld, not redacted, once ANY diagnostic exists.
243+
it.live("readTarget withholds text, rather than redact best-effort, when the document has a syntax error", () =>
232244
withTmp((tmp) =>
233245
Effect.gen(function* () {
234246
const filepath = path.join(tmp.path, "opencode.json")
@@ -244,8 +256,9 @@ describe("ConfigDocument", () => {
244256
const read = yield* doc.readTarget(project.id)
245257

246258
expect(read.diagnostics.length).toBeGreaterThan(0)
247-
expect(read.text).not.toContain("sk-real-secret")
248-
expect(read.text).toContain("[redacted]")
259+
expect(read.text).toBeInstanceOf(ConfigDocument.RedactionWithheld)
260+
expect((read.text as ConfigDocument.RedactionWithheld).reason).toBe("could-not-parse")
261+
expect(JSON.stringify(read)).not.toContain("sk-real-secret")
249262
}).pipe(Effect.provide(testLayer(tmp.path))).pipe(Effect.orDie)
250263
}),
251264
),

‎packages/schema/src/config-document.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,21 @@ export class Diagnostic extends Schema.Class<Diagnostic>("Config.Document.Diagno
2323
length: Schema.optional(Schema.Int),
2424
}) {}
2525

26+
// A named citizen for the fail-closed state (same rule as McpRuntime's truthful unavailable
27+
// default, TKT-323 chunk 2): a document that does not parse cleanly cannot be redacted with any
28+
// guarantee -- a lenient best-effort parse can recover a tree that silently DROPS the
29+
// secret-bearing branch when a malformed region swallows it, so the redactor would see no secrets
30+
// while the raw text still carries them byte-for-byte. `text` is withheld rather than returning a
31+
// best-effort redaction that is only a guarantee in the common case. `diagnostics` on the
32+
// surrounding ReadResult carries the parse errors -- the fix-it information for the escape hatch,
33+
// which is editing the file on disk directly.
34+
export class RedactionWithheld extends Schema.Class<RedactionWithheld>("Config.Document.RedactionWithheld")({
35+
reason: Schema.Literal("could-not-parse"),
36+
}) {}
37+
2638
export class ReadResult extends Schema.Class<ReadResult>("Config.Document.ReadResult")({
2739
target: TargetSummary,
28-
text: Schema.String,
40+
text: Schema.Union([Schema.String, RedactionWithheld]),
2941
hash: Schema.String,
3042
parsed: Schema.Unknown,
3143
diagnostics: Schema.Array(Diagnostic),

0 commit comments

Comments
 (0)