File tree 2 files changed +45
-1
lines changed
2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -54,7 +54,12 @@ function sanitize(target, options) {
54
54
delete obj [ key ] ;
55
55
if ( replaceWith ) {
56
56
key = key . replace ( REPLACE_REGEX , replaceWith ) ;
57
- obj [ key ] = val ;
57
+ // Avoid to set __proto__ and constructor.prototype
58
+ // https://portswigger.net/daily-swig/prototype-pollution-the-dangerous-and-underrated-vulnerability-impacting-javascript-applications
59
+ // https://snyk.io/vuln/SNYK-JS-LODASH-73638
60
+ if ( key !== "__proto__" && key !== "constructor" && key !== "prototype" ) {
61
+ obj [ key ] = val ;
62
+ }
58
63
} else {
59
64
shouldRecurse = false ;
60
65
}
Original file line number Diff line number Diff line change @@ -431,6 +431,45 @@ describe('Express Mongo Sanitize', function() {
431
431
} , done ) ;
432
432
} ) ;
433
433
} ) ;
434
+
435
+ describe ( '__proto__ key' , function ( ) {
436
+ it ( 'should not set __proto__ property' , function ( done ) {
437
+ const app = express ( ) ;
438
+ app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
439
+ app . use ( bodyParser . json ( ) ) ;
440
+ app . use ( sanitize ( {
441
+ replaceWith : '_'
442
+ } ) ) ;
443
+
444
+ app . post ( '/body' , function ( req , res ) {
445
+ // should not inject valued
446
+ expect ( req . body . injected ) . to . be . undefined ;
447
+ res . status ( 200 ) . json ( {
448
+ body : req . body
449
+ } ) ;
450
+ } ) ;
451
+ request ( app )
452
+ . post ( '/body' )
453
+ . send ( {
454
+ // replace $ with _
455
+ $_proto__ : {
456
+ injected : "injected value"
457
+ } ,
458
+ query : {
459
+ q : 'search'
460
+ }
461
+ } )
462
+ . set ( 'Content-Type' , 'application/json' )
463
+ . set ( 'Accept' , 'application/json' )
464
+ . expect ( 200 , {
465
+ body : {
466
+ query : {
467
+ q : 'search'
468
+ }
469
+ }
470
+ } , done ) ;
471
+ } ) ;
472
+ } ) ;
434
473
} ) ;
435
474
436
475
describe ( 'Preserve Data: prohibited characters' , function ( ) {
You can’t perform that action at this time.
0 commit comments