@@ -38,6 +38,7 @@ class HjsonParser {
3838 private StringBuilder captureBuffer , peek ;
3939 private boolean capture ;
4040 private boolean legacyRoot ;
41+ private static final int MAX_DEPTH =1000 ;
4142
4243 private IHjsonDsfProvider [] dsfProviders ;
4344
@@ -111,14 +112,28 @@ JsonValue checkTrailing(JsonValue v) throws ParseException, IOException {
111112 return v ;
112113 }
113114
114- private JsonValue readValue () throws IOException {
115+ private JsonValue readValue () throws IOException , ParseException {
116+ return readValue (0 );
117+ }
118+
119+ private JsonValue readValue (int depth ) throws IOException , ParseException {
120+ if (current ==123 ) {
121+ ++depth ;
122+ }
123+ /* The following has been refrenced for the resolution of the vulnerability:
124+ https://github.com/FasterXML/jackson-databind/commit/fcfc4998ec23f0b1f7f8a9521c2b317b6c25892b
125+ */
126+ if (depth >MAX_DEPTH ) {
127+ throw error ("The passed json has exhausted the depth supported of " +MAX_DEPTH +"." );
128+ }
115129 switch (current ) {
116130 case '\'' :
117131 case '"' : return readString ();
118- case '[' : return readArray ();
119- case '{' : return readObject (false );
132+ case '[' : return readArray (depth );
133+ case '{' : return readObject (false , depth );
120134 default : return readTfnns ();
121135 }
136+
122137 }
123138
124139 private JsonValue readTfnns () throws IOException {
@@ -161,7 +176,7 @@ private JsonValue readTfnns() throws IOException {
161176 }
162177 }
163178
164- private JsonArray readArray () throws IOException {
179+ private JsonArray readArray (int depth ) throws IOException {
165180 read ();
166181 JsonArray array =new JsonArray ();
167182 skipWhiteSpace ();
@@ -170,7 +185,7 @@ private JsonArray readArray() throws IOException {
170185 }
171186 while (true ) {
172187 skipWhiteSpace ();
173- array .add (readValue ());
188+ array .add (readValue (depth ));
174189 skipWhiteSpace ();
175190 if (readIf (',' )) skipWhiteSpace (); // , is optional
176191 if (readIf (']' )) break ;
@@ -180,6 +195,10 @@ private JsonArray readArray() throws IOException {
180195 }
181196
182197 private JsonObject readObject (boolean objectWithoutBraces ) throws IOException {
198+ return this .readObject (objectWithoutBraces , 0 );
199+ }
200+
201+ private JsonObject readObject (boolean objectWithoutBraces , int depth ) throws IOException , ParseException {
183202 if (!objectWithoutBraces ) read ();
184203 JsonObject object =new JsonObject ();
185204 skipWhiteSpace ();
@@ -196,7 +215,7 @@ private JsonObject readObject(boolean objectWithoutBraces) throws IOException {
196215 throw expected ("':'" );
197216 }
198217 skipWhiteSpace ();
199- object .add (name , readValue ());
218+ object .add (name , readValue (depth ));
200219 skipWhiteSpace ();
201220 if (readIf (',' )) skipWhiteSpace (); // , is optional
202221 }
0 commit comments