@@ -164,6 +164,11 @@ static int yajl_start_array(void *ctx) {
164
164
else {
165
165
msr -> json -> prefix = apr_pstrdup (msr -> mp , msr -> json -> current_key );
166
166
}
167
+ msr -> json -> current_depth ++ ;
168
+ if (msr -> json -> current_depth > msr -> txcfg -> reqbody_json_depth_limit ) {
169
+ msr -> json -> depth_limit_exceeded = 1 ;
170
+ return 0 ;
171
+ }
167
172
168
173
if (msr -> txcfg -> debuglog_level >= 9 ) {
169
174
msr_log (msr , 9 , "New JSON hash context (prefix '%s')" , msr -> json -> prefix );
@@ -200,6 +205,7 @@ static int yajl_end_array(void *ctx) {
200
205
*/
201
206
msr -> json -> prefix = (unsigned char * ) NULL ;
202
207
}
208
+ msr -> json -> current_depth -- ;
203
209
204
210
return 1 ;
205
211
}
@@ -229,6 +235,11 @@ static int yajl_start_map(void *ctx)
229
235
else {
230
236
msr -> json -> prefix = apr_pstrdup (msr -> mp , msr -> json -> current_key );
231
237
}
238
+ msr -> json -> current_depth ++ ;
239
+ if (msr -> json -> current_depth > msr -> txcfg -> reqbody_json_depth_limit ) {
240
+ msr -> json -> depth_limit_exceeded = 1 ;
241
+ return 0 ;
242
+ }
232
243
233
244
if (msr -> txcfg -> debuglog_level >= 9 ) {
234
245
msr_log (msr , 9 , "New JSON hash context (prefix '%s')" , msr -> json -> prefix );
@@ -270,6 +281,7 @@ static int yajl_end_map(void *ctx)
270
281
msr -> json -> current_key = msr -> json -> prefix ;
271
282
msr -> json -> prefix = (unsigned char * ) NULL ;
272
283
}
284
+ msr -> json -> current_depth -- ;
273
285
274
286
return 1 ;
275
287
}
@@ -308,6 +320,9 @@ int json_init(modsec_rec *msr, char **error_msg) {
308
320
msr -> json -> prefix = (unsigned char * ) NULL ;
309
321
msr -> json -> current_key = (unsigned char * ) NULL ;
310
322
323
+ msr -> json -> current_depth = 0 ;
324
+ msr -> json -> depth_limit_exceeded = 0 ;
325
+
311
326
/**
312
327
* yajl initialization
313
328
*
@@ -337,7 +352,11 @@ int json_process_chunk(modsec_rec *msr, const char *buf, unsigned int size, char
337
352
msr -> json -> status = yajl_parse (msr -> json -> handle , buf , size );
338
353
if (msr -> json -> status != yajl_status_ok ) {
339
354
/* We need to free the yajl error message later, how to do this? */
340
- * error_msg = yajl_get_error (msr -> json -> handle , 0 , buf , size );
355
+ if (msr -> json -> depth_limit_exceeded ) {
356
+ * error_msg = "JSON depth limit exceeded" ;
357
+ } else {
358
+ * error_msg = yajl_get_error (msr -> json -> handle , 0 , NULL , 0 );
359
+ }
341
360
return -1 ;
342
361
}
343
362
@@ -357,7 +376,12 @@ int json_complete(modsec_rec *msr, char **error_msg) {
357
376
msr -> json -> status = yajl_complete_parse (msr -> json -> handle );
358
377
if (msr -> json -> status != yajl_status_ok ) {
359
378
/* We need to free the yajl error message later, how to do this? */
360
- * error_msg = yajl_get_error (msr -> json -> handle , 0 , NULL , 0 );
379
+ if (msr -> json -> depth_limit_exceeded ) {
380
+ * error_msg = "JSON depth limit exceeded" ;
381
+ } else {
382
+ * error_msg = yajl_get_error (msr -> json -> handle , 0 , NULL , 0 );
383
+ }
384
+
361
385
return -1 ;
362
386
}
363
387
0 commit comments