remotestorage / remotestorage.js

⬡ JavaScript client library for integrating remoteStorage in apps
https://remotestoragejs.readthedocs.io
MIT License
2.3k stars 141 forks source link

Fix getNodes does not clone object in inmemorystorage #1282

Closed darkdread closed 1 year ago

darkdread commented 1 year ago

Getting a node in memory storage should clone whatever it was holding to prevent data mutation.

fixes #1281

darkdread commented 1 year ago

Just FYI, the localStorage back-end also doesn't seem to do a clone there...

https://github.com/remotestorage/remotestorage.js/blob/a189529ec1b589094131d708c6442da0fad72c04/src/localstorage.ts#L39

https://github.com/remotestorage/remotestorage.js/blob/a189529ec1b589094131d708c6442da0fad72c04/src/localstorage.ts#L51

Nodes are objects, and objects are mutable in JavaScript.

When we store nodes in localStorage, we stringify the object. When we get nodes from localStorage, we parse the string back into an object. This makes the node immutable, which means modifying the node outside of getNodes will not mutate localStorage, because the object reference is not referencing the node in localStorage.

What we have initially in inmemorystorage however, was referencing the node stored in this_storage, which means modifying any node outside of getNodes will modify the node in this_storage.