Skip to content

Commit 4cc4053

Browse files
fix: honor loose mode for closed record key schemas (#6157)
* fix: honor loose mode for closed record key schemas * fix(record): don't pass __proto__ through looseRecord's closed key path The closed-key pass-through added here writes into a plain {} from a for...in loop, so an own "__proto__" key (JSON.parse produces one) replaced the result's prototype. The open-key branch and z.object's catchall loop already skip it; mirror that. --------- Co-authored-by: pullfrog[bot] <226033991+pullfrog[bot]@users.noreply.github.com> Co-authored-by: Colin McDonnell <3084745+colinhacks@users.noreply.github.com>
1 parent 69be843 commit 4cc4053

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

β€Žpackages/zod/src/v4/classic/tests/record.test.tsβ€Ž

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,47 @@ test("looseRecord passes through non-matching keys", () => {
631631
expect(schema.parse({ other: "value" })).toEqual({ other: "value" });
632632
});
633633

634+
test("looseRecord with closed key schema passes through unrecognized keys", () => {
635+
const enumSchema = z.looseRecord(z.enum(["foo", "bar"]), z.any());
636+
expect(enumSchema.parse({ foo: 123, bar: {}, baz: null })).toEqual({
637+
foo: 123,
638+
bar: {},
639+
baz: null,
640+
});
641+
642+
const literalSchema = z.looseRecord(z.literal(["foo", "bar"]), z.any());
643+
expect(literalSchema.parse({ foo: 123, bar: {}, baz: null })).toEqual({
644+
foo: 123,
645+
bar: {},
646+
baz: null,
647+
});
648+
649+
// Recognized keys are still validated
650+
const validated = z.looseRecord(z.enum(["foo", "bar"]), z.string());
651+
expect(validated.parse({ foo: "ok", bar: "ok", baz: 123 })).toEqual({
652+
foo: "ok",
653+
bar: "ok",
654+
baz: 123,
655+
});
656+
expect(() => validated.parse({ foo: 123 })).toThrow();
657+
});
658+
659+
test("record with closed key schema still rejects unrecognized keys", () => {
660+
const schema = z.record(z.enum(["foo", "bar"]), z.any());
661+
expect(schema.safeParse({ foo: 123, bar: {}, baz: null }).success).toBe(false);
662+
});
663+
664+
// __proto__ in input must not replace the prototype of the parsed object via
665+
// the assignment setter on the result {}.
666+
// https://github.com/colinhacks/zod/security/advisories/GHSA-r34p-xfmx-58wv
667+
test("looseRecord with closed key schema drops __proto__", () => {
668+
const schema = z.looseRecord(z.enum(["foo", "bar"]), z.any());
669+
const parsed = schema.parse(JSON.parse('{"foo":1,"bar":2,"__proto__":{"isAdmin":true}}'));
670+
expect(Object.keys(parsed)).toEqual(["foo", "bar"]);
671+
expect((parsed as any).isAdmin).toBeUndefined();
672+
expect(Object.getPrototypeOf(parsed)).toBe(Object.prototype);
673+
});
674+
634675
test("intersection of loose records", () => {
635676
const schema = z.intersection(
636677
z.object({ name: z.string() }).passthrough(),

β€Žpackages/zod/src/v4/core/schemas.tsβ€Ž

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3014,8 +3014,15 @@ export const $ZodRecord: core.$constructor<$ZodRecord> = /*@__PURE__*/ core.$con
30143014
let unrecognized!: string[];
30153015
for (const key in input) {
30163016
if (!recordKeys.has(key)) {
3017-
unrecognized = unrecognized ?? [];
3018-
unrecognized.push(key);
3017+
if (def.mode === "loose") {
3018+
// skip __proto__ so it can't replace the result prototype via the
3019+
// assignment setter on the plain {} we build into
3020+
if (key === "__proto__") continue;
3021+
payload.value[key] = input[key];
3022+
} else {
3023+
unrecognized = unrecognized ?? [];
3024+
unrecognized.push(key);
3025+
}
30193026
}
30203027
}
30213028
if (unrecognized && unrecognized.length > 0) {

0 commit comments

Comments
Β (0)