File tree Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -478,6 +478,10 @@ Schema.prototype.add = function add(obj, prefix) {
478478 const keys = Object . keys ( obj ) ;
479479
480480 for ( const key of keys ) {
481+ if ( utils . specialProperties . has ( key ) ) {
482+ continue ;
483+ }
484+
481485 const fullPath = prefix + key ;
482486
483487 if ( obj [ key ] == null ) {
@@ -663,6 +667,9 @@ Schema.prototype.path = function(path, obj) {
663667 let fullPath = '' ;
664668
665669 for ( const sub of subpaths ) {
670+ if ( utils . specialProperties . has ( sub ) ) {
671+ throw new Error ( 'Cannot set special property `' + sub + '` on a schema' ) ;
672+ }
666673 fullPath = fullPath += ( fullPath . length > 0 ? '.' : '' ) + sub ;
667674 if ( ! branch [ sub ] ) {
668675 this . nested [ fullPath ] = true ;
Original file line number Diff line number Diff line change @@ -2682,4 +2682,14 @@ describe('schema', function() {
26822682 assert . equal ( TestSchema . path ( 'testprop.$*' ) . instance , 'Number' ) ;
26832683 assert . equal ( TestSchema . path ( 'testprop.$*' ) . options . ref , 'OtherModel' ) ;
26842684 } ) ;
2685+
2686+ it ( 'disallows setting special properties with `add()` or constructor (gh-12085)' , async function ( ) {
2687+ const maliciousPayload = '{"__proto__.toString": "Number"}' ;
2688+
2689+ assert . throws ( ( ) => {
2690+ mongoose . Schema ( JSON . parse ( maliciousPayload ) ) ;
2691+ } , / _ _ p r o t o _ _ / ) ;
2692+
2693+ assert . ok ( { } . toString ( ) ) ;
2694+ } ) ;
26852695} ) ;
You can’t perform that action at this time.
0 commit comments