File tree 2 files changed +17
-0
lines changed
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) {
478
478
const keys = Object . keys ( obj ) ;
479
479
480
480
for ( const key of keys ) {
481
+ if ( utils . specialProperties . has ( key ) ) {
482
+ continue ;
483
+ }
484
+
481
485
const fullPath = prefix + key ;
482
486
483
487
if ( obj [ key ] == null ) {
@@ -663,6 +667,9 @@ Schema.prototype.path = function(path, obj) {
663
667
let fullPath = '' ;
664
668
665
669
for ( const sub of subpaths ) {
670
+ if ( utils . specialProperties . has ( sub ) ) {
671
+ throw new Error ( 'Cannot set special property `' + sub + '` on a schema' ) ;
672
+ }
666
673
fullPath = fullPath += ( fullPath . length > 0 ? '.' : '' ) + sub ;
667
674
if ( ! branch [ sub ] ) {
668
675
this . nested [ fullPath ] = true ;
Original file line number Diff line number Diff line change @@ -2682,4 +2682,14 @@ describe('schema', function() {
2682
2682
assert . equal ( TestSchema . path ( 'testprop.$*' ) . instance , 'Number' ) ;
2683
2683
assert . equal ( TestSchema . path ( 'testprop.$*' ) . options . ref , 'OtherModel' ) ;
2684
2684
} ) ;
2685
+
2686
+ it ( 'disallows setting special properties with `add()` or constructor (gh-12085)' , 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
+ } ) ;
2685
2695
} ) ;
You can’t perform that action at this time.
0 commit comments