Skip to content

Commit 7c4fee5

Browse files
committed
Reduce code duplication by adding modsecurity_request_body_enable_partial_processing_for_no_files_length
1 parent 1634a2a commit 7c4fee5

File tree

2 files changed

+41
-72
lines changed

2 files changed

+41
-72
lines changed

apache2/msc_multipart.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,23 @@ static int multipart_parse_content_disposition(modsec_rec *msr, char *c_d_value)
251251
return 1;
252252
}
253253

254+
/* Enable partial processing if no_files_len exceeds limit and action is ProcessPartial */
255+
static void modsecurity_request_body_enable_partial_processing_for_no_files_length(modsec_rec *msr,
256+
int *length)
257+
{
258+
/* Enable partial processing if no_files_len exceeds limit and action is ProcessPartial */
259+
if ( (msr->msc_reqbody_no_files_length > (unsigned long)msr->txcfg->reqbody_no_files_limit)
260+
&& (msr->txcfg->if_limit_action == REQUEST_BODY_LIMIT_ACTION_PARTIAL))
261+
{
262+
*length -= msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit;
263+
if (msr->txcfg->debuglog_level >= 9) {
264+
msr_log(msr, 9, "MULTIPART: length shortened by %" APR_SIZE_T_FMT " bytes because of no_files_len limit.",
265+
msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit);
266+
}
267+
modsecurity_request_body_enable_partial_processing(msr);
268+
}
269+
}
270+
254271
/**
255272
*
256273
*/
@@ -272,18 +289,7 @@ static int multipart_process_part_header(modsec_rec *msr, char **error_msg) {
272289

273290
/* The buffer is data so increase the data length counter. */
274291
msr->msc_reqbody_no_files_length += (MULTIPART_BUF_SIZE - msr->mpd->bufleft);
275-
276-
/* Enable partial processing if the no_files_length exceeds the limit and the limit action is ProcessPartial. */
277-
if ((msr->msc_reqbody_no_files_length > (unsigned long)msr->txcfg->reqbody_no_files_limit)
278-
&& (msr->txcfg->if_limit_action == REQUEST_BODY_LIMIT_ACTION_PARTIAL))
279-
{
280-
msr->mpd->bufleft -= msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit;
281-
if (msr->txcfg->debuglog_level >= 9) {
282-
msr_log(msr, 9, "MULTIPART: length shortend by %" APR_SIZE_T_FMT " bytes because of no_files_len limit.",
283-
msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit);
284-
}
285-
modsecurity_request_body_enable_partial_processing(msr);
286-
}
292+
modsecurity_request_body_enable_partial_processing_for_no_files_length(msr, &msr->mpd->bufleft);
287293

288294
if (len > 1) {
289295
if (msr->mpd->buf[len - 2] == '\r') {
@@ -598,18 +604,7 @@ static int multipart_process_part_data(modsec_rec *msr, char **error_msg) {
598604

599605
/* The buffer contains data so increase the data length counter. */
600606
msr->msc_reqbody_no_files_length += (MULTIPART_BUF_SIZE - msr->mpd->bufleft) + msr->mpd->reserve[0];
601-
602-
/* Enable partial processing if the no_files_length exceeds the limit and the limit action is ProcessPartial. */
603-
if ((msr->msc_reqbody_no_files_length > (unsigned long)msr->txcfg->reqbody_no_files_limit)
604-
&& (msr->txcfg->if_limit_action == REQUEST_BODY_LIMIT_ACTION_PARTIAL))
605-
{
606-
msr->mpd->bufleft -= msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit;
607-
if (msr->txcfg->debuglog_level >= 9) {
608-
msr_log(msr, 9, "MULTIPART: length shortend by %" APR_SIZE_T_FMT " bytes because of no_files_len limit.",
609-
msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit);
610-
}
611-
modsecurity_request_body_enable_partial_processing(msr);
612-
}
607+
modsecurity_request_body_enable_partial_processing_for_no_files_length(msr, &msr->mpd->bufleft);
613608

614609
/* add this part to the list of parts */
615610

apache2/msc_reqbody.c

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,24 @@ static apr_status_t modsecurity_request_body_store_memory(modsec_rec *msr,
312312
return 1;
313313
}
314314

315+
/* Enable partial processing if no_files_len exceeds limit and action is ProcessPartial */
316+
static void modsecurity_request_body_enable_partial_processing_for_no_files_length(modsec_rec *msr,
317+
apr_size_t *length, const char *reqbody_processor)
318+
{
319+
/* Enable partial processing if no_files_len exceeds limit and action is ProcessPartial */
320+
if ( (msr->msc_reqbody_no_files_length > (unsigned long)msr->txcfg->reqbody_no_files_limit)
321+
&& (msr->txcfg->if_limit_action == REQUEST_BODY_LIMIT_ACTION_PARTIAL))
322+
{
323+
*length -= msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit;
324+
if (msr->txcfg->debuglog_level >= 9) {
325+
msr_log(msr, 9, "%s: length shortened by %" APR_SIZE_T_FMT " bytes because of no_files_len limit.",
326+
reqbody_processor,
327+
msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit);
328+
}
329+
modsecurity_request_body_enable_partial_processing(msr);
330+
}
331+
}
332+
315333
/**
316334
* Stores one chunk of request body data. Returns -1 on error.
317335
*/
@@ -361,18 +379,7 @@ apr_status_t modsecurity_request_body_store(modsec_rec *msr,
361379
else if (strcmp(msr->msc_reqbody_processor, "XML") == 0) {
362380
/* Increase per-request data length counter. */
363381
msr->msc_reqbody_no_files_length += length;
364-
365-
/* Enable partial processing if no_files_len exceeds limit and action is ProcessPartial */
366-
if ( (msr->msc_reqbody_no_files_length > (unsigned long)msr->txcfg->reqbody_no_files_limit)
367-
&& (msr->txcfg->if_limit_action == REQUEST_BODY_LIMIT_ACTION_PARTIAL))
368-
{
369-
length -= msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit;
370-
if (msr->txcfg->debuglog_level >= 9) {
371-
msr_log(msr, 9, "XML: length shortened by %" APR_SIZE_T_FMT " bytes because of no_files_len limit.",
372-
msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit);
373-
}
374-
modsecurity_request_body_enable_partial_processing(msr);
375-
}
382+
modsecurity_request_body_enable_partial_processing_for_no_files_length(msr, &length, "XML");
376383

377384
/* Process data as XML. */
378385
if (xml_process_chunk(msr, data, length, &my_error_msg) < 0) {
@@ -385,18 +392,7 @@ apr_status_t modsecurity_request_body_store(modsec_rec *msr,
385392
else if (strcmp(msr->msc_reqbody_processor, "JSON") == 0) {
386393
/* Increase per-request data length counter. */
387394
msr->msc_reqbody_no_files_length += length;
388-
389-
/* Enable partial processing if no_files_len exceeds limit and action is ProcessPartial */
390-
if ( (msr->msc_reqbody_no_files_length > (unsigned long)msr->txcfg->reqbody_no_files_limit)
391-
&& (msr->txcfg->if_limit_action == REQUEST_BODY_LIMIT_ACTION_PARTIAL))
392-
{
393-
length -= msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit;
394-
if (msr->txcfg->debuglog_level >= 9) {
395-
msr_log(msr, 9, "JSON: length shortened by %" APR_SIZE_T_FMT " bytes because of no_files_len limit.",
396-
msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit);
397-
}
398-
modsecurity_request_body_enable_partial_processing(msr);
399-
}
395+
modsecurity_request_body_enable_partial_processing_for_no_files_length(msr, &length, "JSON");
400396

401397
/* Process data as JSON. */
402398
#ifdef WITH_YAJL
@@ -416,18 +412,7 @@ apr_status_t modsecurity_request_body_store(modsec_rec *msr,
416412
else if (strcmp(msr->msc_reqbody_processor, "URLENCODED") == 0) {
417413
/* Increase per-request data length counter. */
418414
msr->msc_reqbody_no_files_length += length;
419-
420-
/* Enable partial processing if no_files_len exceeds limit and action is ProcessPartial */
421-
if ( (msr->msc_reqbody_no_files_length > (unsigned long)msr->txcfg->reqbody_no_files_limit)
422-
&& (msr->txcfg->if_limit_action == REQUEST_BODY_LIMIT_ACTION_PARTIAL))
423-
{
424-
length -= msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit;
425-
if (msr->txcfg->debuglog_level >= 9) {
426-
msr_log(msr, 9, "URLENCODED: length shortened by %" APR_SIZE_T_FMT " bytes because of no_files_len limit.",
427-
msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit);
428-
}
429-
modsecurity_request_body_enable_partial_processing(msr);
430-
}
415+
modsecurity_request_body_enable_partial_processing_for_no_files_length(msr, &length, "URLENCODED");
431416

432417
/* Do nothing else, URLENCODED processor does not support streaming. */
433418
}
@@ -439,18 +424,7 @@ apr_status_t modsecurity_request_body_store(modsec_rec *msr,
439424
} else if (msr->txcfg->reqbody_buffering != REQUEST_BODY_FORCEBUF_OFF) {
440425
/* Increase per-request data length counter if forcing buffering. */
441426
msr->msc_reqbody_no_files_length += length;
442-
443-
/* Enable partial processing if no_files_len exceeds limit and action is ProcessPartial */
444-
if ( (msr->msc_reqbody_no_files_length > (unsigned long)msr->txcfg->reqbody_no_files_limit)
445-
&& (msr->txcfg->if_limit_action == REQUEST_BODY_LIMIT_ACTION_PARTIAL))
446-
{
447-
length -= msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit;
448-
if (msr->txcfg->debuglog_level >= 9) {
449-
msr_log(msr, 9, "forceBuf: length shortened by %" APR_SIZE_T_FMT " bytes because of no_files_len limit.",
450-
msr->msc_reqbody_no_files_length - (unsigned long)msr->txcfg->reqbody_no_files_limit);
451-
}
452-
modsecurity_request_body_enable_partial_processing(msr);
453-
}
427+
modsecurity_request_body_enable_partial_processing_for_no_files_length(msr, &length, "forceBuf");
454428
}
455429

456430
/* Check that we are not over the request body no files limit. */

0 commit comments

Comments
 (0)