fix: keep the object branch's __proto__ handling when filtering per key

A regression from the previous commit. The object branch was a spread, which
*defines* properties and is therefore safe; rewriting it into a per-key loop to
filter unserializable values turned it into `out[key] = value`, which *sets* —
so a persisted `"__proto__"` key vanished on reload and, with an object value,
gave the returned metadata a caller-controlled prototype. The legacy branch had
already been fixed for exactly this.

Fixed at the root rather than the site: both branches now go through one
`defineMetadataKey`, so a change to how one stores a key cannot leave the other
behind. That split is what let the earlier fix fail to protect this path.

The test uses `JSON.parse('{"__proto__":…}')`, not an object literal — a
literal `__proto__:` key sets the prototype at construction and creates no own
property, so a test written that way passes against the broken code. Confirmed
by reverting the line: only the parsed case fails.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0177mPHAdECD18nLLCTwR5rh
Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-23 20:18:38 -04:00
commit bc433bbf72

View file

@ -1432,6 +1432,23 @@ describe("normalizeStoredMetadata (#1910)", () => {
});
});
it.each([
["the object form", { __proto__: { polluted: true } } as unknown],
[
"a hand-edited file's object form",
JSON.parse('{"__proto__":{"polluted":true}}'),
],
["the legacy pair form", [{ key: "__proto__", value: { polluted: true } }]],
])("stores a `__proto__` key from %s as an own property", (_label, input) => {
// Plain assignment hits the prototype setter: the entry vanishes and, for
// an object value, the result gets a caller-controlled prototype. Both
// branches must define rather than set — the object branch regressed once
// when it changed from a spread to a per-key loop.
const out = normalizeStoredMetadata(input);
expect(Object.getPrototypeOf(out)).toBe(Object.prototype);
expect(({} as Record<string, unknown>).polluted).toBeUndefined();
});
it("stores a legacy `__proto__` key as an own property", () => {
// Plain assignment would hit the prototype setter: the entry would vanish
// (and an object value would mutate the prototype) instead of being stored.