From 96026522b6b59846cb23f38c6f0415bb5df00733 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 3 Nov 2026 10:09:46 -0600 Subject: [PATCH] fix(catalog-sync): define __proto__ keys in deepMergeProviders (#4957) --- bin/provider-catalog-sync.mjs | 17 ++++++++++++- bin/provider-catalog-sync.test.mjs | 40 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/bin/provider-catalog-sync.mjs b/bin/provider-catalog-sync.mjs index c10fc4e01..91c00024a 100644 --- a/bin/provider-catalog-sync.mjs +++ b/bin/provider-catalog-sync.mjs @@ -560,7 +560,22 @@ export function deepMergeProviders(base, over) { const isObj = (v) => v !== null && typeof v === 'object' && !Array.isArray(v); if (!isObj(base) || !isObj(over)) return over; const out = { ...base }; - for (const k of Object.keys(over)) out[k] = deepMergeProviders(base[k], over[k]); + const hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key); + for (const k of Object.keys(over)) { + // #4957 (design #2953): the overlay is an external file, so a key it + // spells must become an own data property at every depth. A plain + // `out[k] = ...` is a [[Set]] write: for `__proto__` that invokes the + // inherited accessor and moves the prototype instead of storing the + // value. Define the leaf explicitly. The own-property check also keeps + // a missing base key from resolving through the prototype chain + // (base["__proto__"] would otherwise be Object.prototype itself). + Object.defineProperty(out, k, { + value: deepMergeProviders(hasOwn(base, k) ? base[k] : undefined, over[k]), + enumerable: true, + writable: true, + configurable: true, + }); + } return out; } diff --git a/bin/provider-catalog-sync.test.mjs b/bin/provider-catalog-sync.test.mjs index 838338742..cadcb6c64 100644 --- a/bin/provider-catalog-sync.test.mjs +++ b/bin/provider-catalog-sync.test.mjs @@ -450,6 +450,46 @@ test('deepMergeProviders merges catalogs per model, overlay wins on conflict (#4 assert.deepEqual(Object.keys(merged.x.models), ['m']); }); +test('deepMergeProviders defines external keys at depth >= 2 instead of assigning (#4957)', () => { + // Design #2953 corollary 4 probe matrix, one level deeper than the merge + // root: the overlay is an external file, so every key it spells must be + // an own data property rather than a [[Set]] write that can reach a + // prototype. JSON.parse (not object-literal syntax) creates the own + // __proto__ key exactly as the config parser does. + const base = { p: { models: { m: { limit: { context: 1 } } } } }; + const over = JSON.parse( + '{"p":{"models":{"m":{"limit":{' + + '"__proto__":{"polluted":true},"constructor":"c","toString":"t","ordinary":42}}}}}', + ); + const merged = deepMergeProviders(base, over); + const leaf = merged.p.models.m.limit; + assert.equal(Object.getPrototypeOf(leaf), Object.prototype); + assert.ok(Object.prototype.hasOwnProperty.call(leaf, '__proto__')); + assert.deepEqual(leaf.__proto__, { polluted: true }); + assert.equal(leaf.constructor, 'c'); + assert.equal(leaf.toString, 't'); + assert.equal(leaf.ordinary, 42); + assert.equal(leaf.context, 1); + assert.equal(Object.prototype.polluted, undefined); + // JSON round-trip: the own key survives serialization and the prototype + // stays put. + const roundTrip = JSON.parse(JSON.stringify(merged)); + assert.equal(Object.getPrototypeOf(roundTrip.p.models.m.limit), Object.prototype); + assert.ok(Object.prototype.hasOwnProperty.call(roundTrip.p.models.m.limit, '__proto__')); + assert.deepEqual(roundTrip.p.models.m.limit.__proto__, { polluted: true }); +}); + +test('deepMergeProviders defines a string __proto__ at depth 3 without moving prototypes (#4957)', () => { + const base = { p: { models: { m: { variants: { low: { options: {} } } } } } }; + const over = JSON.parse('{"p":{"models":{"m":{"variants":{"low":{"options":{"__proto__":"text"}}}}}}}'); + const merged = deepMergeProviders(base, over); + const options = merged.p.models.m.variants.low.options; + assert.equal(Object.getPrototypeOf(options), Object.prototype); + assert.ok(Object.prototype.hasOwnProperty.call(options, '__proto__')); + assert.equal(options.__proto__, 'text'); + assert.equal(Object.getPrototypeOf(merged.p.models.m.variants.low), Object.prototype); +}); + test('runSync diffs the merged effective config when a runtime overlay exists (#4544)', async () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'catalog-sync-overlay-')); const configPath = path.join(dir, 'opencode.jsonc');