From 80d56d8f4b303c8be7d164307e537f493ea03cbf Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 00:59:31 -0600 Subject: [PATCH] fix(record): preserve loose __proto__ fields with define semantics (#2828) --- bin/record.mjs | 13 ++++++++++++- bin/record.test.mjs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/bin/record.mjs b/bin/record.mjs index 24b26d1075..2190d5be01 100644 --- a/bin/record.mjs +++ b/bin/record.mjs @@ -107,7 +107,18 @@ export const parseLoose = (raw) => { } catch { // Keep the verbatim string; normalisation happens later. } - fields[key] = value; + // Define semantics, not assignment (#2828): assigning a + // '__proto__' key would invoke the prototype setter — a string + // value is silently dropped and an object value would replace + // this object's prototype — while the JSON input path preserves + // the field. Defining makes every accepted key an own data + // property, so no supplied field is ever lost. + Object.defineProperty(fields, key, { + value, + enumerable: true, + writable: true, + configurable: true + }); keyed += 1; } else if (segment.trim() !== '') { unkeyed.push(segment.trim()); diff --git a/bin/record.test.mjs b/bin/record.test.mjs index 1af0497e73..225cce0889 100644 --- a/bin/record.test.mjs +++ b/bin/record.test.mjs @@ -222,3 +222,37 @@ test('concurrent CLI appends produce only whole lines', async () => { } assert.equal(seen.size, writers); }); + +test('loose __proto__ fields are preserved as own data properties (#2828)', () => { + const protoBefore = Object.getPrototypeOf({}); + // String value: assignment would have dropped the field entirely. + const looseString = parseLoose('kind: note | __proto__: polluted'); + assert.ok(Object.hasOwn(looseString, '__proto__')); + assert.equal(looseString['__proto__'], 'polluted'); + assert.equal(Object.getPrototypeOf(looseString), Object.prototype); + // Object value: assignment would have replaced the prototype. + const looseObject = parseLoose('kind: note | __proto__: {"injected": true}'); + assert.ok(Object.hasOwn(looseObject, '__proto__')); + assert.deepEqual(looseObject['__proto__'], { injected: true }); + assert.equal(Object.getPrototypeOf(looseObject), Object.prototype); + assert.equal({}.injected, undefined, 'Object.prototype must not be polluted'); + assert.equal(Object.getPrototypeOf({}), protoBefore); +}); + +test('loose __proto__ survives normalisation and record() append, like the JSON path (#2828)', () => { + const { entry } = normaliseInput('kind: note | __proto__: {"injected": true}', stamps); + assert.ok(Object.hasOwn(entry, '__proto__')); + assert.deepEqual(entry['__proto__'], { injected: true }); + // JSON-path control: an own __proto__ field is preserved there too. + const control = normaliseInput('{"kind":"note","__proto__":{"injected":true}}', stamps).entry; + assert.ok(Object.hasOwn(control, '__proto__')); + assert.deepEqual(control['__proto__'], { injected: true }); + // End to end: the appended JSONL line carries the field. + const target = tmpTarget(); + const outcome = record('kind: note | __proto__: polluted', { target, env: {} }); + assert.equal(outcome, 'recorded'); + const line = JSON.parse(readLines(target)[0]); + assert.ok(Object.hasOwn(line, '__proto__')); + assert.equal(line['__proto__'], 'polluted'); + assert.equal({}.polluted, undefined, 'Object.prototype must not be polluted'); +});