fix: preserve __proto__ object keys #20
<a href="https://stackblitz.com/~/github.com/promplate/partial-json-parser-js/pull/20"><img src="https://developer.stackblitz.com/img/review_pr_small.svg" alt="Review PR in StackBlitz Codeflow" align="left" width="103" height="20" target="_blank"></a> _Run & review this pull request in StackBlitz Codeflow._
@rupayon123 is attempting to deploy a commit to the promplate Team on Vercel.
A member of the Team first needs to authorize it.
!high
Using Object.defineProperty for every single key in every parsed object introduces a significant performance overhead compared to direct property assignment (obj[key] = value). Since "__proto__" is the only standard accessor property on Object.prototype that behaves as a setter, we can optimize this by only using Object.defineProperty when the key is exactly "__proto__", and using direct assignment for all other keys.
if (key === "__proto__") {
Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true });
} else {
obj[key] = value;
}
Code Review
This pull request updates the JSON parser to use Object.defineProperty instead of direct assignment when parsing object keys, ensuring that special keys like __proto__ are preserved as own enumerable properties. A test case was added to verify this behavior. The reviewer pointed out that using Object.defineProperty for every key introduces a significant performance overhead and suggested optimizing this by only using it when the key is exactly "__proto__".
[!IMPORTANT]
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
issue (bug_risk): Using Object.defineProperty here changes behavior vs simple assignment, especially with prototype accessors, and adds overhead in a hot path.
With inherited accessors this is observable: if obj’s prototype defines a setter for key, obj[key] = value would call that setter, but defineProperty instead creates an own data property and completely bypasses it. Unless you explicitly intend to ignore prototype setters or customize descriptors, the direct assignment is safer and more appropriate here.
✅ Addressed in 12f1eb32a704d3d463ea2422f95e80b5f4f14bbf: The general case now uses direct assignment again, with Object.defineProperty limited to the "__proto__" key, so normal prototype setter behavior and performance characteristics are restored for other properties.
Hey - I've found 1 issue, and left some high level feedback:
Object.defineProperty for every parsed key is a behavior and performance change compared to obj[key] = value; consider special-casing only the "__proto__" key (e.g., if (key === '__proto__') { defineProperty... } else { obj[key] = value; }) to minimize impact on the common path.<details>
<summary>Prompt for AI Agents</summary>
~~~markdown
Please address the comments from this code review:
Overall Comments
Object.defineProperty for every parsed key is a behavior and performance change compared to obj[key] = value; consider special-casing only the "__proto__" key (e.g., if (key === '__proto__') { defineProperty... } else { obj[key] = value; }) to minimize impact on the common path.Individual Comments
Comment 1
<location path="src/index.ts" line_range="109" />
<code_context>
try {
const value = parseAny();
+ Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true });
} catch (e) {
if (Allow.OBJ & allow) return obj;
</code_context>
<issue_to_address>
issue (bug_risk): Using
Object.defineProperty here changes behavior vs simple assignment, especially with prototype accessors, and adds overhead in a hot path.With inherited accessors this is observable: if obj’s prototype defines a setter for key, obj[key] = value would call that setter, but defineProperty instead creates an own data property and completely bypasses it. Unless you explicitly intend to ignore prototype setters or customize descriptors, the direct assignment is safer and more appropriate here.
</issue_to_address>
~~~
</details>
***
<details>
<summary>Sourcery is free for open source - if you like our reviews please consider sharing them ✨</summary>
</details>
<sub>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
</sub>
<!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- review_stack_entry_start -->

<!-- review_stack_entry_end -->
No actionable comments were generated in the recent review. 🎉
<details>
<summary>ℹ️ Recent review info</summary>
<details>
<summary>⚙️ Run configuration</summary>
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 26534675-9a96-4375-ad70-643ceaec33eb
</details>
<details>
<summary>📥 Commits</summary>
Reviewing files that changed from the base of the PR and between 419b5a421240a18c6c9181d0fe9917c72ad39c1d and 12f1eb32a704d3d463ea2422f95e80b5f4f14bbf.
</details>
<details>
<summary>📒 Files selected for processing (1)</summary>
src/index.ts
</details>
<details>
<summary>🚧 Files skipped from review as they are similar to previous changes (1)</summary>
src/index.ts
</details>
</details>
---
<!-- walkthrough_start -->
<details>
<summary>📝 Walkthrough</summary>
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
Changes
src/index.ts
parseObj() to special-case the __proto__ key: use Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true }) instead of obj[key] = value, ensuring __proto__ is preserved as an own enumerable property.src/proto.test.ts
parse('{"__proto__":0}') produces an object where __proto__ is an own enumerable property with value: 0 and writable/enumerable/configurable set to true.<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Walkthrough
In src/index.ts, object member assignment inside the partial JSON parser is changed from obj[key] = value to Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true }). This prevents the JavaScript engine from intercepting assignment to __proto__ (which mutates the prototype chain rather than creating an own property), ensuring keys like __proto__ are stored as regular own enumerable properties. A new test file src/proto.test.ts is added to verify that parsing {"__proto__":0} produces an object with __proto__ as an own enumerable property with value: 0.
Estimated code review effort
🎯 2 (Simple) | ⏱️ ~5 minutes
</details>
<!-- walkthrough_end -->
<!-- pre_merge_checks_walkthrough_start -->
<details>
<summary>🚥 Pre-merge checks | ✅ 5</summary>
<details>
<summary>✅ Passed checks (5 passed)</summary>
| Check name | Status | Explanation |
| :------------------------: | :------- | :------------------------------------------------------------------------------------------------------------------------------------ |
| Title check | ✅ Passed | The title directly describes the main fix: preserving __proto__ as an object key during parsing. |
| Description check | ✅ Passed | The description clearly explains the fix and references the linked issue, relating directly to the changeset. |
| Linked Issues check | ✅ Passed | The code changes fully satisfy issue #8 requirements: __proto__ key handling now uses Object.defineProperty with correct descriptors. |
| Out of Scope Changes check | ✅ Passed | All changes are directly scoped to fixing the __proto__ parsing issue and adding a regression test. |
| Docstring Coverage | ✅ Passed | No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. |
</details>
<sub>✏️ Tip: You can configure your own custom pre-merge checks in the settings.</sub>
</details>
<!-- pre_merge_checks_walkthrough_end -->
<!-- tips_start -->
---
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
<details>
<summary>❤️ Share</summary>
</details>
<sub>Comment @coderabbitai help to get the list of available commands and usage tips.</sub>
<!-- tips_end -->
Approved these changes.
Pushed the small follow-up from the review: only __proto__ uses Object.defineProperty now, and normal keys are back to direct assignment.
I couldn’t run the suite from here, so CI is still the check.
Local validation update:
vitest run passes on this branch: 2 files, 7 tests.Same build caveat as #19: the local tsc build is blocked by existing package/dev dependency typing setup around Vite/Vitest Node typings, so CI should remain the build source of truth.
Good point. I changed this in 12f1eb3: the current PR uses Object.defineProperty only when the key is __proto__; ordinary keys still use direct assignment. The regression keeps checking that __proto__ is an own enumerable property. The current PR diff confirms the change; the Vercel preview remains gated on team authorization.
rupayon123
Fixes #8
This keeps
"__proto__"as an own enumerable property instead of assigning through the prototype setter. I added a focused Vitest regression forparse('{"__proto__":0}').I couldn’t run the suite locally here, so CI is the check.