Commit 9f9d664
authored
fix(shared): prevent clone() from re-parenting the cloned object (#1790)
`clone()` copied properties with plain assignment, so a source object
carrying an own `__proto__` key went through `Object.prototype`'s
`__proto__` setter instead of getting a property. The clone came back
re-parented onto the source's payload, or silently lost the key when its
value was not an object. Copying through `Object.defineProperty` makes
every key a real own property.
This is reachable with ordinary input: `JSON.parse` and `NullProtoObj`
both produce own `__proto__` keys, and `isPlainObject` treats both as
plain objects.
## Fixes
- Cloning an object with an own `__proto__` key now yields a clone whose
prototype is still `Object.prototype`, with `__proto__` present as a
normal own property.
- Non-object `__proto__` values (e.g. `2`) are preserved instead of
being dropped by the setter.
- Scope note: `Object.prototype` itself was never mutated, so this was
clone corruption rather than global prototype pollution.
## Testing
New `clone with __proto__ property` case covers both the `JSON.parse`
and `NullProtoObj` sources and fails on the previous code (the clone's
prototype had become the payload). Full `packages/shared` suite passes
at 302 tests, lint clean.1 parent e0b4947 commit 9f9d664
2 files changed
Lines changed: 25 additions & 2 deletions
File tree
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
318 | 318 | | |
319 | 319 | | |
320 | 320 | | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
321 | 343 | | |
322 | 344 | | |
323 | 345 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
143 | 144 | | |
144 | | - | |
| 145 | + | |
145 | 146 | | |
146 | 147 | | |
147 | 148 | | |
148 | | - | |
| 149 | + | |
149 | 150 | | |
150 | 151 | | |
151 | 152 | | |
| |||
0 commit comments