Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion bin/record.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
34 changes: 34 additions & 0 deletions bin/record.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});