immer icon indicating copy to clipboard operation
immer copied to clipboard

`produce` leaks proxy objects when symbols are present

Open dhmk083 opened this issue 1 year ago • 5 comments

🐛 Bug Report

When you use produce on an object which contains a symbol property which is an object, it leaks Proxy object into returned value.

Link to repro

https://codesandbox.io/p/sandbox/suspicious-sanne-2ypqwt

To Reproduce

  1. Get an object with symbol:
const sym = Symbol()

let state = {
  id: 1,
  [sym]: {} // must be an object
}
  1. Use it with produce and read state[sym] value inside draft function. In returned value state[sym] object will be a revoked proxy instead of plain object.

  2. Use it with produce again. Now an error will be thrown because state[sym] is corrupted.

TypeError
Cannot perform 'getPrototypeOf' on a proxy that has been revoked

Observed behavior

Proxy object is leaking out of produce function.

Expected behavior

Plain object should be returned instead.

Environment

The bug appears since 10.0.0 version. It works fine in 9.0.21 version.

dhmk083 avatar Jan 06 '24 08:01 dhmk083

Is it fixed when enabling useStrictShallowCopy(true)?

On Sat, 6 Jan 2024, 09:26 Dmitriy Verenich, @.***> wrote:

🐛 Bug Report

When you use produce on an object which contains a symbol property which is an object, it leaks Proxy object into returned value. Link to repro

https://codesandbox.io/p/sandbox/suspicious-sanne-2ypqwt https://codesandbox.io/p/sandbox/suspicious-sanne-2ypqwt?file=%2Fsrc%2Findex.ts To Reproduce

  1. Get an object with symbol:

const sym = Symbol() let state = { id: 1, [sym]: {} // must be an object}

Use it with produce and read state[sym] value inside draft function. In returned value state[sym] object will be a revoked proxy instead of plain object. 2.

Use it with produce again. Now an error will be thrown because state[sym] is corrupted.

TypeError Cannot perform 'getPrototypeOf' on a proxy that has been revoked

Observed behavior

Proxy object is leaking out of produce function. Expected behavior

Plain object should be returned instead. Environment

The bug appears since 10.0.0 version. It works fine in 9.0.21 version.

— Reply to this email directly, view it on GitHub https://github.com/immerjs/immer/issues/1087, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN4NBHVYMU5YYHK2OBD35DYNEDCXAVCNFSM6AAAAABBPMVYWSVHI2DSMVQWIX3LMV43ASLTON2WKOZSGA3DQNBWHEZTQNQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

mweststrate avatar Jan 06 '24 12:01 mweststrate

No, still error.

dhmk083 avatar Jan 06 '24 12:01 dhmk083

I encountered the same issue. Reverting to version 9.0.21 resolved the problem for me as well.

lsby avatar Jan 08 '24 11:01 lsby

This was changed here: https://github.com/immerjs/immer/commit/452a5085c0f84c3277ee16ab9b072ad99127ce90#diff-4ade25325a6cd7d1cca619364c84d19bfa47e6939a2c1b38d065767255b8f86eR66

However, based on the commit messages, I'm not sure why the change was made.

joshkel avatar Feb 07 '24 19:02 joshkel

Hello! Had the same issue, objects nested on symbols are automatically revoked.

Fyi: Sets seem to work, it seems to only affect objects.

Is this a necessary change for 10.0.x or can it be reverted? @mweststrate

lveillard avatar Feb 17 '24 22:02 lveillard

Likely same issue as #1096. Will investigate when I can carve out a sliver of time. If someone is willing to submit a PR with unit test(s), that will already be quite helpful!

mweststrate avatar Mar 09 '24 09:03 mweststrate

#1105 should fix the handling of Proxy properties. When drafted, they are now finalized correctly. Let me know if it isn't solved by [email protected]

mweststrate avatar Mar 09 '24 21:03 mweststrate

10.0.4 fixes this issue for us, although it introduced another issue; see #1106.

joshkel avatar Mar 11 '24 13:03 joshkel