Skip to content

Commit 3bf6615

Browse files
author
Felipe Zimmerle
committed
nginx: better dealing with chunked request body
1 parent 39c7414 commit 3bf6615

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

nginx/modsecurity/ngx_http_modsecurity.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,23 @@ ngx_http_modsecurity_save_request_body(ngx_http_request_t *r)
537537
buf->last += content_length;
538538
r->header_in = buf;
539539

540+
if (r->headers_in.content_length) {
541+
ngx_str_t *str = NULL;
542+
543+
str = &r->headers_in.content_length->value;
544+
str->data = ngx_palloc(r->pool, NGX_OFF_T_LEN);
545+
if (str->data == NULL) {
546+
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
547+
return NGX_OK;
548+
}
549+
str->len = ngx_snprintf(str->data, NGX_OFF_T_LEN, "%O", content_length) - str->data;
550+
551+
}
552+
553+
540554
r->headers_in.content_length_n = content_length;
541555

542-
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ModSec: Content length: %O, Content length n: %O", content_length, r->headers_in.content_length_n);
556+
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ModSec: Content length: %O, Content length n: %O", content_length, r->headers_in.content_length_n);
543557
return NGX_OK;
544558
}
545559

@@ -635,7 +649,7 @@ ngx_http_modsecurity_load_headers_out(ngx_http_request_t *r)
635649
*(const char **)apr_array_push(ctx->req->content_languages) = data;
636650
}
637651

638-
/* req->chunked = r->chunked; may be useless */
652+
req->chunked = r->chunked;
639653
req->clength = r->headers_out.content_length_n;
640654
req->mtime = apr_time_make(r->headers_out.last_modified_time, 0);
641655

@@ -654,7 +668,7 @@ ngx_http_modsecurity_save_headers_out(ngx_http_request_t *r)
654668

655669
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity);
656670

657-
/* r->chunked = ctx->req->chunked; */
671+
r->chunked = ctx->req->chunked;
658672

659673
ngx_http_clean_header(r);
660674

@@ -1055,7 +1069,6 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
10551069

10561070
return NGX_DECLINED;
10571071
}
1058-
10591072
if (ctx->waiting_more_body == 1) {
10601073
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
10611074
"ModSec: waiting for more data before proceed. / count: %d", r->main->count);
@@ -1068,6 +1081,8 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
10681081
"ModSec: asking for the request body, if any.");
10691082

10701083
ctx->body_requested = 1;
1084+
r->request_body_in_single_buf = 1;
1085+
10711086
rc = ngx_http_read_client_request_body(r,
10721087
ngx_http_modsecurity_request_read);
10731088

@@ -1084,8 +1099,11 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
10841099
}
10851100

10861101
if (ctx->waiting_more_body == 0 && ctx->request_processed == 0) {
1102+
10871103
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
10881104
"ModSec: request is ready to be processed.");
1105+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1106+
"ModSec: chuncked? %d", r->chunked);
10891107
ngx_http_modsecurity_process_request(r);
10901108
ctx->request_processed = 1;
10911109
}
@@ -1287,6 +1305,8 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
12871305
}
12881306

12891307
return ngx_http_next_body_filter(r, out);
1308+
1309+
return NGX_OK;
12901310
}
12911311
#endif
12921312

tests/regression/nginx/conf/nginx.conf.template

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ http {
1818
client_max_body_size 30M;
1919
listen [% listen %];
2020
server_name localhost;
21-
21+
client_body_in_single_buffer on;
22+
client_body_in_file_only on;
23+
proxy_buffering off;
2224

2325

2426
location /no-proxy/test.txt {

0 commit comments

Comments
 (0)