@@ -132,6 +132,41 @@ describe("syncDiff", () => {
132132 } )
133133} )
134134
135+ describe ( "keys colliding with Object.prototype properties" , ( ) => {
136+ // built via JSON.parse because a "__proto__" key in an object literal would
137+ // set the prototype instead of creating an own property
138+ const protoState = JSON . parse ( `{
139+ "__proto__": {"metas": [{"id": 1, "phx_ref": "1"}]},
140+ "constructor": {"metas": [{"id": 2, "phx_ref": "2"}]},
141+ "hasOwnProperty": {"metas": [{"id": 3, "phx_ref": "3"}]}
142+ }` )
143+
144+ it ( "syncState joins and leaves them without touching the prototype" , ( ) => {
145+ let joined = [ ]
146+ let state = Presence . syncState ( { } , protoState , key => joined . push ( key ) )
147+
148+ expect ( Object . getPrototypeOf ( state ) ) . toBe ( null )
149+ expect ( joined ) . toEqual ( [ "__proto__" , "constructor" , "hasOwnProperty" ] )
150+ expect ( Presence . list ( state , ( key , { metas} ) => metas [ 0 ] . id ) ) . toEqual ( [ 1 , 2 , 3 ] )
151+
152+ let left = [ ]
153+ state = Presence . syncState ( state , { } , null , key => left . push ( key ) )
154+ expect ( left ) . toEqual ( [ "__proto__" , "constructor" , "hasOwnProperty" ] )
155+ expect ( Presence . list ( state ) ) . toEqual ( [ ] )
156+ expect ( Object . prototype . metas ) . toBe ( undefined )
157+ } )
158+
159+ it ( "syncDiff joins and leaves them without touching the prototype" , ( ) => {
160+ let diff = JSON . parse ( `{"joins": {"__proto__": {"metas": [{"id": 1, "phx_ref": "1"}]}}, "leaves": {}}` )
161+ let state = Presence . syncDiff ( { } , diff )
162+ expect ( Presence . list ( state , ( key , { metas} ) => [ key , metas [ 0 ] . id ] ) ) . toEqual ( [ [ "__proto__" , 1 ] ] )
163+
164+ state = Presence . syncDiff ( state , JSON . parse ( `{"joins": {}, "leaves": {"__proto__": {"metas": [{"id": 1, "phx_ref": "1"}]}}}` ) )
165+ expect ( Presence . list ( state ) ) . toEqual ( [ ] )
166+ expect ( Object . prototype . metas ) . toBe ( undefined )
167+ } )
168+ } )
169+
135170describe ( "list" , ( ) => {
136171 it ( "lists full presence by default" , ( ) => {
137172 let state = fixtures . state ( )
0 commit comments