@@ -173,20 +173,12 @@ function deserializeObject(
173
173
stringSize <= 0 ||
174
174
stringSize > buffer . length - index ||
175
175
buffer [ index + stringSize - 1 ] !== 0
176
- )
176
+ ) {
177
177
throw new Error ( 'bad string length in bson' ) ;
178
-
179
- value = buffer . toString ( 'utf8' , index , index + stringSize - 1 ) ;
180
-
181
- for ( let i = 0 ; i < value . length ; i ++ ) {
182
- if ( value . charCodeAt ( i ) === 0xfffd ) {
183
- if ( ! validateUtf8 ( buffer , index , index + stringSize - 1 ) ) {
184
- throw new Error ( 'Invalid UTF-8 string in BSON document' ) ;
185
- }
186
- break ;
187
- }
188
178
}
189
179
180
+ value = getValidatedString ( buffer , index , index + stringSize - 1 ) ;
181
+
190
182
index = index + stringSize ;
191
183
} else if ( elementType === constants . BSON_DATA_OID ) {
192
184
const oid = Buffer . alloc ( 12 ) ;
@@ -464,9 +456,10 @@ function deserializeObject(
464
456
stringSize <= 0 ||
465
457
stringSize > buffer . length - index ||
466
458
buffer [ index + stringSize - 1 ] !== 0
467
- )
459
+ ) {
468
460
throw new Error ( 'bad string length in bson' ) ;
469
- const symbol = buffer . toString ( 'utf8' , index , index + stringSize - 1 ) ;
461
+ }
462
+ const symbol = getValidatedString ( buffer , index , index + stringSize - 1 ) ;
470
463
value = promoteValues ? symbol : new BSONSymbol ( symbol ) ;
471
464
index = index + stringSize ;
472
465
} else if ( elementType === constants . BSON_DATA_TIMESTAMP ) {
@@ -496,9 +489,10 @@ function deserializeObject(
496
489
stringSize <= 0 ||
497
490
stringSize > buffer . length - index ||
498
491
buffer [ index + stringSize - 1 ] !== 0
499
- )
492
+ ) {
500
493
throw new Error ( 'bad string length in bson' ) ;
501
- const functionString = buffer . toString ( 'utf8' , index , index + stringSize - 1 ) ;
494
+ }
495
+ const functionString = getValidatedString ( buffer , index , index + stringSize - 1 ) ;
502
496
503
497
// If we are evaluating the functions
504
498
if ( evalFunctions ) {
@@ -538,11 +532,12 @@ function deserializeObject(
538
532
stringSize <= 0 ||
539
533
stringSize > buffer . length - index ||
540
534
buffer [ index + stringSize - 1 ] !== 0
541
- )
535
+ ) {
542
536
throw new Error ( 'bad string length in bson' ) ;
537
+ }
543
538
544
539
// Javascript function
545
- const functionString = buffer . toString ( 'utf8' , index , index + stringSize - 1 ) ;
540
+ const functionString = getValidatedString ( buffer , index , index + stringSize - 1 ) ;
546
541
// Update parse index position
547
542
index = index + stringSize ;
548
543
// Parse the element
@@ -670,3 +665,16 @@ function isolateEval(
670
665
// Set the object
671
666
return functionCache [ functionString ] . bind ( object ) ;
672
667
}
668
+
669
+ function getValidatedString ( buffer : Buffer , start : number , end : number ) {
670
+ const value = buffer . toString ( 'utf8' , start , end ) ;
671
+ for ( let i = 0 ; i < value . length ; i ++ ) {
672
+ if ( value . charCodeAt ( i ) === 0xfffd ) {
673
+ if ( ! validateUtf8 ( buffer , start , end ) ) {
674
+ throw new Error ( 'Invalid UTF-8 string in BSON document' ) ;
675
+ }
676
+ break ;
677
+ }
678
+ }
679
+ return value ;
680
+ }
0 commit comments