fix: preserve __proto__ object keys#20
Conversation
|
|
|
@rupayon123 is attempting to deploy a commit to the promplate Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
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.
| try { | ||
| const value = parseAny(); | ||
| obj[key] = value; | ||
| Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true }); |
There was a problem hiding this comment.
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;
}There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Using
Object.definePropertyfor every parsed key is a behavior and performance change compared toobj[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.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Using `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();
- obj[key] = value;
+ 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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughChangessrc/index.ts
src/proto.test.ts
WalkthroughIn Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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. Comment |
|
Pushed the small follow-up from the review: only I couldn’t run the suite from here, so CI is still the check. |
|
Local validation update:
Same build caveat as #19: the local |
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.