@@ -6,11 +6,19 @@ import { isPlainObject } from "../../helpers/isPlainObject";
66import { tryDecodeAsJWT } from "../../helpers/tryDecodeAsJWT" ;
77import { detectDbJsInjection } from "../js-injection/detectDbJsInjection" ;
88
9+ // Matches the depth limit used by extractStringsFromUserInput
10+ const MAX_DEPTH = 1024 ;
11+
912function matchFilterPartInUser (
1013 userInput : unknown ,
1114 filterPart : Record < string , unknown > ,
12- pathToPayload : PathPart [ ] = [ ]
15+ pathToPayload : PathPart [ ] = [ ] ,
16+ depth = 0
1317) : { match : false } | { match : true ; pathToPayload : string } {
18+ if ( depth > MAX_DEPTH ) {
19+ return { match : false } ;
20+ }
21+
1422 if ( typeof userInput === "string" ) {
1523 // Check for js injection in $where
1624 if ( detectDbJsInjection ( userInput , filterPart ) ) {
@@ -25,7 +33,8 @@ function matchFilterPartInUser(
2533 return matchFilterPartInUser (
2634 jwt . object ,
2735 filterPart ,
28- pathToPayload . concat ( [ { type : "jwt" } ] )
36+ pathToPayload . concat ( [ { type : "jwt" } ] ) ,
37+ depth + 1
2938 ) ;
3039 }
3140 }
@@ -40,7 +49,8 @@ function matchFilterPartInUser(
4049 const match = matchFilterPartInUser (
4150 userInput [ key ] ,
4251 filterPart ,
43- pathToPayload . concat ( [ { type : "object" , key : key } ] )
52+ pathToPayload . concat ( [ { type : "object" , key : key } ] ) ,
53+ depth + 1
4454 ) ;
4555
4656 if ( match . match ) {
@@ -54,15 +64,25 @@ function matchFilterPartInUser(
5464 const match = matchFilterPartInUser (
5565 userInput [ index ] ,
5666 filterPart ,
57- pathToPayload . concat ( [ { type : "array" , index : index } ] )
67+ pathToPayload . concat ( [ { type : "array" , index : index } ] ) ,
68+ depth + 1
5869 ) ;
5970
6071 if ( match . match ) {
6172 return match ;
6273 }
6374 }
6475
65- return matchFilterPartInUser ( userInput . join ( ) , filterPart , pathToPayload ) ;
76+ try {
77+ return matchFilterPartInUser (
78+ userInput . join ( ) ,
79+ filterPart ,
80+ pathToPayload ,
81+ depth + 1
82+ ) ;
83+ } catch {
84+ // Ignore deeply nested arrays that overflow during native join recursion.
85+ }
6686 }
6787
6888 return {
0 commit comments