Skip to content

Commit 980561c

Browse files
committed
Change handling of invalid request body
### modsecurity.h * Standardize body parser return codes ### msc_json.c, msc_xml.c * Return special error code on body parsing failure ### msc_reqbody.c, apache2_io.c, mod_security2.c * Change body parsing error codes to refer to constants defined in header.
1 parent a6ad396 commit 980561c

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

apache2/apache2_io.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ apr_status_t read_request_body(modsec_rec *msr, char **error_msg) {
193193
msr_log(msr, 4, "Input filter: Reading request body.");
194194
}
195195
if (modsecurity_request_body_start(msr, error_msg) < 0) {
196-
return -1;
196+
return BODY_PARSER_ERR_GENERIC;
197197
}
198198

199199
finished_reading = 0;
@@ -226,7 +226,7 @@ apr_status_t read_request_body(modsec_rec *msr, char **error_msg) {
226226
return -2;
227227
default :
228228
*error_msg = apr_psprintf(msr->mp, "Error reading request body: %s", get_apr_error(msr->mp, rc));
229-
return -1;
229+
return BODY_PARSER_ERR_GENERIC;
230230
}
231231
}
232232

@@ -321,7 +321,7 @@ apr_status_t read_request_body(modsec_rec *msr, char **error_msg) {
321321
}
322322

323323
if((msr->txcfg->is_enabled == MODSEC_ENABLED) && (msr->txcfg->if_limit_action == REQUEST_BODY_LIMIT_ACTION_REJECT))
324-
return -1;
324+
return BODY_PARSER_ERR_GENERIC;
325325
}
326326

327327
}

apache2/mod_security2.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,19 +1071,14 @@ static int hook_request_late(request_rec *r) {
10711071
}
10721072
break;
10731073
case -6 : /* EOF when reading request body. */
1074-
if (my_error_msg != NULL) {
1075-
msr_log(msr, 4, "%s", my_error_msg);
1076-
}
1077-
r->connection->keepalive = AP_CONN_CLOSE;
1078-
return HTTP_BAD_REQUEST;
1079-
break;
10801074
case -7 : /* Partial recieved */
10811075
if (my_error_msg != NULL) {
10821076
msr_log(msr, 4, "%s", my_error_msg);
10831077
}
10841078
r->connection->keepalive = AP_CONN_CLOSE;
10851079
return HTTP_BAD_REQUEST;
10861080
break;
1081+
case BODY_PARSER_ERR_INVALID_BODY : /* Error Parsing Body. To be handled by modsec rules */
10871082
default :
10881083
/* allow through */
10891084
break;

apache2/modsecurity.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ extern DSOLOCAL char *msc_waf_lock_group;
273273

274274
#define NBSP 160
275275

276+
277+
#define BODY_PARSER_ERR_GENERIC -1
278+
#define BODY_PARSER_ERR_INVALID_BODY -8
279+
#define BODY_PARSER_OK_SUCCESS 1
280+
276281
struct rule_exception {
277282
int type;
278283
const char *param;

apache2/msc_json.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ int json_process_chunk(modsec_rec *msr, const char *buf, unsigned int size, char
292292
int json_complete(modsec_rec *msr, char **error_msg) {
293293
char *json_data = (char *) NULL;
294294

295-
if (error_msg == NULL) return -1;
295+
if (error_msg == NULL) return BODY_PARSER_ERR_GENERIC;
296296
*error_msg = NULL;
297297

298298
/* Wrap up the parsing process */
@@ -301,10 +301,10 @@ int json_complete(modsec_rec *msr, char **error_msg) {
301301
char *yajl_err = yajl_get_error(msr->json->handle, 0, NULL, 0);
302302
*error_msg = apr_pstrdup(msr->mp, yajl_err);
303303
yajl_free_error(msr->json->handle, yajl_err);
304-
return -1;
304+
return BODY_PARSER_ERR_INVALID_BODY;
305305
}
306306

307-
return 1;
307+
return BODY_PARSER_OK_SUCCESS;
308308
}
309309

310310
/**

apache2/msc_reqbody.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -697,45 +697,47 @@ apr_status_t modsecurity_request_body_end(modsec_rec *msr, char **error_msg) {
697697
if (msr->txcfg->debuglog_level >= 4) {
698698
msr_log(msr, 4, "%s", *error_msg);
699699
}
700-
return -1;
700+
return BODY_PARSER_ERR_INVALID_BODY;
701701
}
702702

703703
if (multipart_get_arguments(msr, "BODY", msr->arguments) < 0) {
704704
*error_msg = "Multipart parsing error: Failed to retrieve arguments.";
705705
msr->msc_reqbody_error = 1;
706706
msr->msc_reqbody_error_msg = *error_msg;
707707
msr_log(msr, 2, "%s", *error_msg);
708-
return -1;
708+
return BODY_PARSER_ERR_INVALID_BODY;
709709
}
710710
}
711711
else if (strcmp(msr->msc_reqbody_processor, "JSON") == 0) {
712712
#ifdef WITH_YAJL
713-
if (json_complete(msr, &my_error_msg) < 0 && msr->msc_reqbody_length > 0) {
713+
int json_parser_response = json_complete(msr, &my_error_msg);
714+
if (json_parser_response < 0 && msr->msc_reqbody_length > 0) {
714715
*error_msg = apr_psprintf(msr->mp, "JSON parser error: %s", my_error_msg);
715716
msr->msc_reqbody_error = 1;
716717
msr->msc_reqbody_error_msg = *error_msg;
717718
msr_log(msr, 2, "%s", *error_msg);
718-
return -1;
719+
return json_parser_response;
719720
}
720721
#else
721722
*error_msg = apr_psprintf(msr->mp, "JSON support was not enabled");
722723
msr->msc_reqbody_error = 1;
723724
msr->msc_reqbody_error_msg = *error_msg;
724725
msr_log(msr, 2, "%s", *error_msg);
725-
return -1;
726+
return BODY_PARSER_ERR_GENERIC;
726727
#endif
727728

728729
}
729730
else if (strcmp(msr->msc_reqbody_processor, "URLENCODED") == 0) {
730731
return modsecurity_request_body_end_urlencoded(msr, error_msg);
731732
}
732733
else if (strcmp(msr->msc_reqbody_processor, "XML") == 0) {
733-
if (xml_complete(msr, &my_error_msg) < 0) {
734+
int xml_parser_response = xml_complete(msr, &my_error_msg);
735+
if (xml_parser_response < 0) {
734736
*error_msg = apr_psprintf(msr->mp, "XML parser error: %s", my_error_msg);
735737
msr->msc_reqbody_error = 1;
736738
msr->msc_reqbody_error_msg = *error_msg;
737739
msr_log(msr, 2, "%s", *error_msg);
738-
return -1;
740+
return xml_parser_response;
739741
}
740742
}
741743
} else if (msr->txcfg->reqbody_buffering != REQUEST_BODY_FORCEBUF_OFF) {
@@ -746,7 +748,7 @@ apr_status_t modsecurity_request_body_end(modsec_rec *msr, char **error_msg) {
746748
/* Note the request body no files length. */
747749
msr_log(msr, 4, "Request body no files length: %" APR_SIZE_T_FMT, msr->msc_reqbody_no_files_length);
748750

749-
return 1;
751+
return BODY_PARSER_OK_SUCCESS;
750752
}
751753

752754
/**

apache2/msc_xml.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int xml_init(modsec_rec *msr, char **error_msg) {
3636
entity = xmlParserInputBufferCreateFilenameDefault(xml_unload_external_entity);
3737
}
3838

39-
return 1;
39+
return BODY_PARSER_OK_SUCCESS;
4040
}
4141

4242
#if 0
@@ -59,7 +59,7 @@ static void xml_receive_sax_error(void *data, const char *msg, ...) {
5959
* Feed one chunk of data to the XML parser.
6060
*/
6161
int xml_process_chunk(modsec_rec *msr, const char *buf, unsigned int size, char **error_msg) {
62-
if (error_msg == NULL) return -1;
62+
if (error_msg == NULL) return BODY_PARSER_ERR_GENERIC;
6363
*error_msg = NULL;
6464

6565
/* We want to initialise our parsing context here, to
@@ -87,7 +87,7 @@ int xml_process_chunk(modsec_rec *msr, const char *buf, unsigned int size, char
8787
msr->xml->parsing_ctx = xmlCreatePushParserCtxt(NULL, NULL, buf, size, "body.xml");
8888
if (msr->xml->parsing_ctx == NULL) {
8989
*error_msg = apr_psprintf(msr->mp, "XML: Failed to create parsing context.");
90-
return -1;
90+
return BODY_PARSER_ERR_GENERIC;
9191
}
9292
} else {
9393

@@ -96,18 +96,18 @@ int xml_process_chunk(modsec_rec *msr, const char *buf, unsigned int size, char
9696
xmlParseChunk(msr->xml->parsing_ctx, buf, size, 0);
9797
if (msr->xml->parsing_ctx->wellFormed != 1) {
9898
*error_msg = apr_psprintf(msr->mp, "XML: Failed parsing document.");
99-
return -1;
99+
return BODY_PARSER_ERR_INVALID_BODY;
100100
}
101101
}
102102

103-
return 1;
103+
return BODY_PARSER_OK_SUCCESS;
104104
}
105105

106106
/**
107107
* Finalise XML parsing.
108108
*/
109109
int xml_complete(modsec_rec *msr, char **error_msg) {
110-
if (error_msg == NULL) return -1;
110+
if (error_msg == NULL) return BODY_PARSER_ERR_GENERIC;
111111
*error_msg = NULL;
112112

113113
/* Only if we have a context, meaning we've done some work. */
@@ -126,11 +126,11 @@ int xml_complete(modsec_rec *msr, char **error_msg) {
126126

127127
if (msr->xml->well_formed != 1) {
128128
*error_msg = apr_psprintf(msr->mp, "XML: Failed parsing document.");
129-
return -1;
129+
return BODY_PARSER_ERR_INVALID_BODY;
130130
}
131131
}
132132

133-
return 1;
133+
return BODY_PARSER_OK_SUCCESS;
134134
}
135135

136136
/**
@@ -149,5 +149,5 @@ apr_status_t xml_cleanup(modsec_rec *msr) {
149149
msr->xml->doc = NULL;
150150
}
151151

152-
return 1;
152+
return BODY_PARSER_OK_SUCCESS;
153153
}

0 commit comments

Comments
 (0)