Skip to content

Commit 97011af

Browse files
opencodeclaude
andcommitted
fix(config): redactParsed must not reshape an array-rooted document
Copilot review finding on PR #32 (Suppressed comments section): the typeof/null guard alone lets an array through -- typeof [] === "object" and [] !== null -- so Object.fromEntries(Object.entries(...)) silently turned an array-rooted document into a plain object with numeric string keys, beyond the intended mcp redaction. Excludes arrays explicitly; `parsed` now mirrors the real document shape. Regression test confirmed red pre-fix, green post-fix. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JJQrqYMv43CrdbjpTT7884
1 parent c3d7328 commit 97011af

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,12 @@ function redactField(key: string, value: unknown): unknown {
183183
// Applied to a WHOLE parsed document (readTarget's `parsed`), as opposed to `redactField`, which
184184
// `computeEffective` calls per top-level key while building its field-by-field provenance map.
185185
function redactParsed(parsed: unknown): unknown {
186-
if (typeof parsed !== "object" || parsed === null) return parsed
186+
// Array.isArray is required, not just the typeof/null guard: `typeof [] === "object"` and
187+
// `[] !== null`, so an array-rooted document (e.g. a malformed config with an array at the top
188+
// level) would otherwise pass through Object.fromEntries(Object.entries(...)) and come back as a
189+
// plain object with numeric string keys -- silently reshaping the parsed value beyond the
190+
// intended mcp redaction (Copilot review, PR #32).
191+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return parsed
187192
return Object.fromEntries(Object.entries(parsed as Record<string, unknown>).map(([k, v]) => [k, redactField(k, v)]))
188193
}
189194

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,28 @@ describe("ConfigDocument", () => {
113113
),
114114
)
115115

116+
// Regression for a suppressed Copilot review finding on PR #32: `redactParsed`'s
117+
// typeof/null guard alone lets an array root through, and `Object.fromEntries(Object.entries(...))`
118+
// silently reshapes it into a plain object with numeric string keys. An array root is invalid
119+
// against the config schema (so this exercises the same fail-closed diagnostics path), but
120+
// `parsed` must still mirror the real document's shape, not a redaction side effect of it.
121+
it.live("readTarget's parsed value stays an array when the document root is an array", () =>
122+
withTmp((tmp) =>
123+
Effect.gen(function* () {
124+
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "opencode.json"), "[1, 2, 3]"))
125+
126+
return yield* Effect.gen(function* () {
127+
const doc = yield* ConfigDocument.Service
128+
const project = (yield* doc.listTargets()).find((t) => t.kind === "project")!
129+
const read = yield* doc.readTarget(project.id)
130+
131+
expect(Array.isArray(read.parsed)).toBe(true)
132+
expect(read.parsed).toEqual([1, 2, 3])
133+
}).pipe(Effect.provide(testLayer(tmp.path))).pipe(Effect.orDie)
134+
}),
135+
),
136+
)
137+
116138
it.live("readTarget fails with TargetNotFoundError for an id from a different discovery", () =>
117139
withTmp((tmp) =>
118140
Effect.gen(function* () {

0 commit comments

Comments
 (0)