diff --git a/src/index.ts b/src/index.ts index ea6977d..38e0f9a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -106,7 +106,11 @@ const _parseJSON = (jsonString: string, allow: number) => { index++; // skip colon try { const value = parseAny(); - obj[key] = value; + if (key === "__proto__") { + Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true }); + } else { + obj[key] = value; + } } catch (e) { if (Allow.OBJ & allow) return obj; else throw e; diff --git a/src/proto.test.ts b/src/proto.test.ts new file mode 100644 index 0000000..e33855c --- /dev/null +++ b/src/proto.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vitest"; +import { parse } from "./index"; + +describe("object key parsing", () => { + it("preserves __proto__ as an own enumerable key", () => { + const value = parse('{"__proto__":0}'); + const descriptor = Object.getOwnPropertyDescriptor(value, "__proto__"); + + expect(descriptor).toMatchObject({ + value: 0, + writable: true, + enumerable: true, + configurable: true, + }); + }); +});