File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import mix from '../../../src/lodash/mix' ;
2+
3+ describe ( 'mix' , ( ) => {
4+ it ( 'merges plain objects' , ( ) => {
5+ const result = mix ( { } as any , { a : 1 } , { b : 2 } ) ;
6+ expect ( result ) . toEqual ( { a : 1 , b : 2 } ) ;
7+ } ) ;
8+
9+ it ( 'does not pollute Object.prototype via __proto__' , ( ) => {
10+ const payload = JSON . parse ( '{"__proto__": {"polluted": true}}' ) ;
11+ mix ( { } , payload ) ;
12+ expect ( ( Object . prototype as any ) . polluted ) . toBeUndefined ( ) ;
13+ } ) ;
14+
15+ it ( 'does not pollute via constructor key' , ( ) => {
16+ const payload = JSON . parse ( '{"constructor": {"prototype": {"polluted": true}}}' ) ;
17+ mix ( { } , payload ) ;
18+ expect ( ( Object . prototype as any ) . polluted ) . toBeUndefined ( ) ;
19+ } ) ;
20+
21+ it ( 'does not pollute via prototype key' , ( ) => {
22+ mix ( { } as any , { prototype : { polluted : true } } as any ) ;
23+ expect ( ( Object . prototype as any ) . polluted ) . toBeUndefined ( ) ;
24+ } ) ;
25+ } ) ;
Original file line number Diff line number Diff line change 11// FIXME: Mutable param should be forbidden in static lang.
22function _mix < Base , Source > ( dist : Base & Source , obj : Source ) : void {
33 for ( const key in obj ) {
4- if ( obj . hasOwnProperty ( key ) && key !== 'constructor' && obj [ key ] !== undefined ) {
4+ // Prevent prototype pollution by skipping dangerous keys
5+ if (
6+ key === '__proto__' ||
7+ key === 'constructor' ||
8+ key === 'prototype'
9+ ) {
10+ continue ;
11+ }
12+ if ( obj . hasOwnProperty ( key ) && obj [ key ] !== undefined ) {
513 ( < any > dist ) [ key ] = obj [ key ] ;
614 }
715 }
You can’t perform that action at this time.
0 commit comments