@@ -328,3 +328,46 @@ describe('objects are created from paths and their value is set correctly', () =
328328 } )
329329
330330} )
331+
332+ describe ( 'rejects prototype-pollution paths' , ( ) => {
333+ afterEach ( ( ) => {
334+ delete ( Object . prototype as any ) . polluted
335+ } )
336+
337+ it ( 'throws on __proto__ as the first segment' , ( ) => {
338+ const record : any = { }
339+ expect ( ( ) => jsonPath . setValue ( record , '__proto__.polluted' , true ) ) . to . throw ( / f o r b i d d e n / )
340+ expect ( ( { } as any ) . polluted ) . to . equal ( undefined )
341+ } )
342+
343+ it ( 'throws on __proto__ as a nested segment' , ( ) => {
344+ const record : any = { a : { } }
345+ expect ( ( ) => jsonPath . setValue ( record , 'a.__proto__.polluted' , true ) ) . to . throw ( / f o r b i d d e n / )
346+ expect ( ( { } as any ) . polluted ) . to . equal ( undefined )
347+ } )
348+
349+ it ( 'throws on constructor.prototype path' , ( ) => {
350+ const record : any = { }
351+ expect ( ( ) => jsonPath . setValue ( record , 'constructor.prototype.polluted' , true ) ) . to . throw ( / f o r b i d d e n / )
352+ expect ( ( { } as any ) . polluted ) . to . equal ( undefined )
353+ } )
354+
355+ it ( 'throws on a single __proto__ segment (record prototype switch)' , ( ) => {
356+ const record : any = { }
357+ expect ( ( ) => jsonPath . setValue ( record , '__proto__' , { polluted : true } ) ) . to . throw ( / f o r b i d d e n / )
358+ expect ( ( record as any ) . polluted ) . to . equal ( undefined )
359+ } )
360+
361+ it ( 'getValue refuses to traverse __proto__' , ( ) => {
362+ expect ( ( ) => jsonPath . getValue ( { } , '__proto__.toString' ) ) . to . throw ( / f o r b i d d e n / )
363+ } )
364+
365+ it ( 'isValidPath flags forbidden keys but accepts normal paths' , ( ) => {
366+ expect ( jsonPath . isValidPath ( '__proto__.x' ) ) . to . equal ( false )
367+ expect ( jsonPath . isValidPath ( 'constructor.prototype.x' ) ) . to . equal ( false )
368+ expect ( jsonPath . isValidPath ( 'a.__proto__.x' ) ) . to . equal ( false )
369+ expect ( jsonPath . isValidPath ( 'a.b.c' ) ) . to . equal ( true )
370+ expect ( jsonPath . isValidPath ( 'items[0].name' ) ) . to . equal ( true )
371+ expect ( jsonPath . isValidPath ( 'user_proto.foo' ) ) . to . equal ( true )
372+ } )
373+ } )
0 commit comments