@@ -149,4 +149,41 @@ describe('merge', () => {
149149 expect ( m2 . getIn ( [ 'a' , 'b' , 0 ] ) ) . toEqual ( { plain : 'obj' } ) ;
150150 } )
151151
152+ it ( 'is not sensible to prototype pollution' , ( ) => {
153+ var m1 = fromJS ( { user : 'Alice' } ) ;
154+ // Map().set('__proto__', ...) properly creates a __proto__ key in the Map
155+ // (unlike Map({ __proto__: ... }) which triggers JS prototype setter)
156+ var m2 = Map ( ) . set ( '__proto__' , Map ( { admin : true } ) ) ;
157+
158+ var r1 = m1 . mergeDeep ( m2 ) ;
159+ // @ts -ignore -- testing prototype pollution, ignoring typing errors for tests
160+ expect ( r1 . toJS ( ) . admin ) . toBeUndefined ( ) ;
161+
162+ var r2 = m1 . mergeDeepWith ( ( a , b ) => b , m2 ) ;
163+ // @ts -ignore -- testing prototype pollution, ignoring typing errors for tests
164+ expect ( r2 . toJS ( ) . admin ) . toBeUndefined ( ) ;
165+
166+ var r3 = m1 . merge ( m2 ) ;
167+ // @ts -ignore -- testing prototype pollution, ignoring typing errors for tests
168+ expect ( r3 . toJS ( ) . admin ) . toBeUndefined ( ) ;
169+
170+ // @ts -ignore -- testing prototype pollution, ignoring typing errors for tests
171+ expect ( ( ( { } ) as any ) . admin ) . toBeUndefined ( ) ;
172+ } )
173+
174+ it ( 'is not sensible to prototype pollution via fromJS + JSON.parse' , ( ) => {
175+ var userProfile = fromJS ( { user : 'Alice' } ) ;
176+ var requestBody = fromJS ( JSON . parse ( '{"user":"Eve","__proto__":{"admin":true}}' ) ) ;
177+
178+ var r1 = userProfile . mergeDeep ( requestBody ) ;
179+ expect ( r1 . get ( 'user' ) ) . toBe ( 'Eve' ) ;
180+ // @ts -ignore -- testing prototype pollution, ignoring typing errors for tests
181+ expect ( r1 . toJS ( ) . admin ) . toBeUndefined ( ) ;
182+ // @ts -ignore -- testing prototype pollution, ignoring typing errors for tests
183+ expect ( r1 . toObject ( ) . admin ) . toBeUndefined ( ) ;
184+
185+ // @ts -ignore -- testing prototype pollution, ignoring typing errors for tests
186+ expect ( ( ( { } ) as any ) . admin ) . toBeUndefined ( ) ;
187+ } )
188+
152189} )
0 commit comments