From 6cc53e385b1b8276f4f8ecc0e81ecd546d3ea560 Mon Sep 17 00:00:00 2001 From: Tom Sommer Date: Sat, 19 Sep 2026 15:32:05 +0200 Subject: [PATCH 1/2] Add modsecurity_request_body directive to skip request body buffering When set to off the connector no longer reads and buffers the whole request body in the ACCESS phase and no longer feeds it to libmodsecurity. The REQUEST_BODY phase still runs, so rules on ARGS and REQUEST_HEADERS keep working, and the content handler reads the body with nginx's own settings (proxy_request_buffering off works again). libmodsecurity copies the body on msc_append_request_body regardless of SecRequestBodyAccess, so this is the only way to avoid that cost. --- README.md | 38 +++- src/ngx_http_modsecurity_access.c | 29 +++ src/ngx_http_modsecurity_common.h | 1 + src/ngx_http_modsecurity_module.c | 11 + tests/modsecurity-request-body-directive.t | 250 +++++++++++++++++++++ 5 files changed, 328 insertions(+), 1 deletion(-) create mode 100644 tests/modsecurity-request-body-directive.t diff --git a/README.md b/README.md index a93e5df0..0627e026 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Further information about nginx third-party add-ons support are available [here] # Usage ModSecurity for nginx extends your nginx configuration directives. -It adds four new directives and they are: +It adds the following directives: modsecurity ----------- @@ -185,6 +185,42 @@ modsecurity_use_error_log Turns on or off ModSecurity error log functionality. +modsecurity_request_body +------------------------ +**syntax:** *modsecurity_request_body on | off* + +**context:** *http, server, location* + +**default:** *on* + +Turns on or off the request body inspection. + +When turned off, the connector does not read and buffer the request body and +does not feed it to ModSecurity, so `REQUEST_BODY`, `ARGS_POST`, `FILES` and +the other body related variables are empty. Phase 2 rules are still executed, +hence rules matching on `ARGS`, `REQUEST_HEADERS` and friends keep working. +The request body is then read by the content handler with nginx's own +settings, which means that directives such as `proxy_request_buffering off` +are effective again. + +This is useful for locations handling large uploads, encrypted payloads, or +for locations where `SecRequestBodyAccess Off` is already set: libmodsecurity +copies the request body regardless of `SecRequestBodyAccess`, so turning this +directive off is the only way to avoid that cost. + +```nginx +server { + modsecurity on; + modsecurity_rules_file /etc/my_modsecurity_rules.conf; + + location /upload { + modsecurity_request_body off; + proxy_request_buffering off; + proxy_pass http://backend; + } +} +``` + # Contributing As an open source project we invite (and encourage) anyone from the community to contribute to our project. This may take the form of: new diff --git a/src/ngx_http_modsecurity_access.c b/src/ngx_http_modsecurity_access.c index effa8a91..f7567c5b 100644 --- a/src/ngx_http_modsecurity_access.c +++ b/src/ngx_http_modsecurity_access.c @@ -326,6 +326,35 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r) return NGX_DONE; } + if (mcf->request_body == 0) + { + int ret = 0; + + /* + * Request body inspection is disabled for this location. Do not + * buffer the body here (that lets the content handler stream it, + * e.g. proxy_request_buffering off), but still run the + * REQUEST_BODY phase so rules that do not depend on the body + * (ARGS, REQUEST_HEADERS, ...) execute in their usual phase. + */ + dd("request body inspection disabled by modsecurity_request_body off"); + + old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool); + msc_process_request_body(ctx->modsec_transaction); + ctx->request_body_processed = 1; + ngx_http_modsecurity_pcre_malloc_done(old_pool); + + ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0); + if (r->error_page) { + return NGX_DECLINED; + } + if (ret > 0) { + return ret; + } + + return NGX_DECLINED; + } + if (ctx->body_requested == 0) { ngx_int_t rc = NGX_OK; diff --git a/src/ngx_http_modsecurity_common.h b/src/ngx_http_modsecurity_common.h index 9f79b5ba..149d35a8 100644 --- a/src/ngx_http_modsecurity_common.h +++ b/src/ngx_http_modsecurity_common.h @@ -119,6 +119,7 @@ typedef struct { ngx_flag_t enable; ngx_flag_t use_error_log; + ngx_flag_t request_body; #if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS) ngx_flag_t sanity_checks_enabled; #endif diff --git a/src/ngx_http_modsecurity_module.c b/src/ngx_http_modsecurity_module.c index d3d9624d..380d36e5 100644 --- a/src/ngx_http_modsecurity_module.c +++ b/src/ngx_http_modsecurity_module.c @@ -529,6 +529,14 @@ static ngx_command_t ngx_http_modsecurity_commands[] = { offsetof(ngx_http_modsecurity_conf_t, use_error_log), NULL }, + { + ngx_string("modsecurity_request_body"), + NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG, + ngx_conf_set_flag_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_modsecurity_conf_t, request_body), + NULL + }, ngx_null_command }; @@ -713,6 +721,7 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf) * set by ngx_pcalloc(): * * conf->enable = 0; + * conf->request_body = 0; * conf->sanity_checks_enabled = 0; * conf->rules_set = NULL; * conf->pool = NULL; @@ -724,6 +733,7 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf) conf->pool = cf->pool; conf->transaction_id = NGX_CONF_UNSET_PTR; conf->use_error_log = NGX_CONF_UNSET; + conf->request_body = NGX_CONF_UNSET; #if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS) conf->sanity_checks_enabled = NGX_CONF_UNSET; #endif @@ -764,6 +774,7 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(c->enable, p->enable, 0); ngx_conf_merge_ptr_value(c->transaction_id, p->transaction_id, NULL); ngx_conf_merge_value(c->use_error_log, p->use_error_log, 1); + ngx_conf_merge_value(c->request_body, p->request_body, 1); #if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS) ngx_conf_merge_value(c->sanity_checks_enabled, p->sanity_checks_enabled, 0); #endif diff --git a/tests/modsecurity-request-body-directive.t b/tests/modsecurity-request-body-directive.t new file mode 100644 index 00000000..b425eeca --- /dev/null +++ b/tests/modsecurity-request-body-directive.t @@ -0,0 +1,250 @@ +#!/usr/bin/perl + +# Tests for ModSecurity-nginx connector (modsecurity_request_body directive). + +############################################################################### + +use warnings; +use strict; + +use Test::More; +use IO::Select; + +BEGIN { use FindBin; chdir($FindBin::Bin); } + +use lib 'lib'; +use Test::Nginx; + +############################################################################### + +select STDERR; $| = 1; +select STDOUT; $| = 1; + +my $t = Test::Nginx->new()->has(qw/http proxy/); + +$t->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + server { + listen 127.0.0.1:8080; + server_name localhost; + + modsecurity on; + modsecurity_rules ' + SecRuleEngine On + SecRequestBodyAccess On + SecRule REQUEST_BODY "@rx BAD BODY" "id:11,phase:2,deny,log,status:403" + SecRule ARGS:what "@streq badarg" "id:12,phase:2,deny,log,status:403" + '; + + location /inspect { + proxy_pass http://127.0.0.1:%%PORT_8081%%; + } + + location /skip { + modsecurity_request_body off; + proxy_pass http://127.0.0.1:%%PORT_8081%%; + } + + location /nobuffer { + modsecurity_request_body off; + proxy_request_buffering off; + proxy_pass http://127.0.0.1:%%PORT_8081%%; + } + + location /early { + modsecurity_request_body off; + proxy_request_buffering off; + proxy_pass http://127.0.0.1:%%PORT_8081%%; + } + + location /earlybuf { + proxy_request_buffering off; + proxy_pass http://127.0.0.1:%%PORT_8081%%; + } + + location /parent { + modsecurity_request_body off; + + location /parent/child { + proxy_pass http://127.0.0.1:%%PORT_8081%%; + } + + location /parent/on { + modsecurity_request_body on; + proxy_pass http://127.0.0.1:%%PORT_8081%%; + } + } + } +} + +EOF + +$t->run_daemon(\&http_daemon); +$t->run()->waitforsocket('127.0.0.1:' . port(8081)); +$t->plan(12); + +############################################################################### + +like(http_post('/inspect', 'GOOD BODY'), qr/LEN=9/, 'default: good body passes'); +like(http_post('/inspect', 'VERY BAD BODY'), qr/^HTTP.*403/, 'default: bad body blocked'); +like(http_post('/skip', 'VERY BAD BODY'), qr/LEN=13/, 'off: body not inspected, upstream gets it'); +like(http_post('/skip?what=badarg', 'GOOD BODY'), qr/^HTTP.*403/, 'off: phase 2 still runs on ARGS'); +like(http_get('/skip'), qr/LEN=0/, 'off: GET without body works'); +like(http_post('/nobuffer', 'VERY BAD BODY'), qr/LEN=13/, 'off + proxy_request_buffering off: full body reaches upstream'); +like(http_post('/parent/child', 'VERY BAD BODY'), qr/LEN=13/, 'off inherited by nested location'); +like(http_post('/parent/on', 'VERY BAD BODY'), qr/^HTTP.*403/, 'on overrides inherited off'); +like(http_post('/parent/on', 'GOOD BODY'), qr/LEN=9/, 'on overrides inherited off, good body passes'); + +# the upstream must see the beginning of the request body before the client +# has sent all of it, otherwise the body is still being buffered by nginx + +my $s = http_post_delayed('/early', 'VERY BAD BODY', 6); +like(http_read($s, 5), qr/EARLY LEN=6/, + 'off + proxy_request_buffering off: partial body streamed to upstream'); +http_post_finish($s, 'VERY BAD BODY', 6); + +$s = http_post_delayed('/earlybuf', 'VERY BAD BODY', 6); +is(http_read($s, 3), '', + 'on + proxy_request_buffering off: nothing sent upstream before the body is complete'); +http_post_finish($s, 'VERY BAD BODY', 6); +like(http_read($s, 5), qr/^HTTP.*403/, + 'on + proxy_request_buffering off: complete body still inspected'); + +############################################################################### + +sub http_post { + my ($url, $body) = @_; + my $len = length($body); + return http(< 1); +POST $url HTTP/1.1 +Host: localhost +Connection: close +Content-Type: application/x-www-form-urlencoded +Content-Length: $len + +EOF +} + +sub http_post_finish { + my ($s, $body, $sent) = @_; + + local $SIG{PIPE} = 'IGNORE'; + + eval { + log_out(substr($body, $sent)); + $s->print(substr($body, $sent)); + }; +} + +sub http_read { + my ($s, $timeout) = @_; + my $reply = ''; + my $select = IO::Select->new($s); + + while ($select->can_read($timeout)) { + my $buf = ''; + my $n = $s->sysread($buf, 1024); + last if (!defined $n || $n == 0); + $reply .= $buf; + } + + log_in($reply); + return $reply; +} + +sub http_daemon { + my $server = IO::Socket::INET->new( + Proto => 'tcp', + LocalHost => '127.0.0.1:' . port(8081), + Listen => 5, + Reuse => 1 + ) + or die "Can't create listening socket: $!\n"; + + local $SIG{PIPE} = 'IGNORE'; + + while (my $client = $server->accept()) { + $client->autoflush(1); + + my $select = IO::Select->new($client); + my $buf = ''; + + # the whole request is read with sysread(), so that no part of + # it is left in a buffer where select() cannot see it + + while ($buf !~ /\x0d?\x0a\x0d?\x0a/) { + last unless $select->can_read(5); + my $chunk = ''; + my $n = $client->sysread($chunk, 1024); + last if (!defined $n || $n == 0); + $buf .= $chunk; + } + + my ($headers, $body) = split(/\x0d?\x0a\x0d?\x0a/, $buf, 2); + $headers = '' unless defined $headers; + $body = '' unless defined $body; + + my $uri = ''; + my $len = 0; + + $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; + $len = $1 if $headers =~ /Content-Length:\s*(\d+)/i; + + # /early answers with the part of the request body that nginx + # has already forwarded, the other locations wait for all of it + + my $timeout = ($uri =~ m!^/early!) ? 1 : 5; + + while (length($body) < $len && $select->can_read($timeout)) { + my $chunk = ''; + my $n = $client->sysread($chunk, $len - length($body)); + last if (!defined $n || $n == 0); + $body .= $chunk; + } + + my $got = length($body); + + if ($uri =~ m!^/early!) { + print $client <<"EOF"; +HTTP/1.1 200 OK +Connection: close + +EARLY LEN=$got +EOF + } else { + print $client <<"EOF"; +HTTP/1.1 200 OK +Connection: close + +TEST-OK LEN=$got +EOF + } + + close $client; + } +} + +############################################################################### From a079a09a8ced67630fdd92865f7c7f2425024aff Mon Sep 17 00:00:00 2001 From: Tom Sommer Date: Fri, 13 Nov 2026 07:51:24 +0100 Subject: [PATCH 2/2] Address review of modsecurity_request_body - Route the off path through the shared msc_process_request_body() block, which skips only the read and the append when the body is not inspected (or r->request_body is NULL), instead of duplicating it. - Set ctx->intervention_triggered on request body interventions, in the shared block and at the per-chunk check, so the header and body filters skip response processing after a phase 2 deny. - Keep the setting of the location where the access phase first runs for the whole request. Re-checking it after an internal redirect would need a second msc_process_request_body() call: phase 2 rules then run twice and the JSON processor, whose yajl handle is not reset, rejects valid bodies. - README: ModSecurity body limits are not enforced in off locations (only client_max_body_size); phase 2 needs SecRequestBodyAccess On; the XML processor rejects the empty body (remove 200000 there); redirect semantics; this is a per-location opt-out, not streaming inspection. - Drop the misleading request_body line from the create_conf comment. - tests: JSON, multipart and XML bodies against rules modelled on the recommended 200000-200002, phase 2 denies in off and on locations, and the try_files redirect from an off location. --- README.md | 28 ++++++++-- src/ngx_http_modsecurity_access.c | 53 +++++------------- src/ngx_http_modsecurity_module.c | 1 - tests/modsecurity-request-body-directive.t | 62 ++++++++++++++++++++-- 4 files changed, 97 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 0627e026..1faabcfa 100644 --- a/README.md +++ b/README.md @@ -197,17 +197,37 @@ Turns on or off the request body inspection. When turned off, the connector does not read and buffer the request body and does not feed it to ModSecurity, so `REQUEST_BODY`, `ARGS_POST`, `FILES` and -the other body related variables are empty. Phase 2 rules are still executed, -hence rules matching on `ARGS`, `REQUEST_HEADERS` and friends keep working. -The request body is then read by the content handler with nginx's own -settings, which means that directives such as `proxy_request_buffering off` +the other body related variables are empty. Phase 2 rules are still executed +(with `SecRequestBodyAccess On`; libmodsecurity skips phase 2 entirely when it +is `Off`), hence rules matching on `ARGS`, `REQUEST_HEADERS` and friends keep +working. The request body is then read by the content handler with nginx's +own settings, which means that directives such as `proxy_request_buffering off` are effective again. +As no body reaches ModSecurity, `SecRequestBodyLimit`, +`SecRequestBodyNoFilesLimit` and `SecRequestBodyLimitAction` are not enforced +in such a location; only nginx's `client_max_body_size` limits the body size. + +The body processors still run, on an empty body. The URLENCODED, MULTIPART +and JSON processors accept it, but the XML processor fails to parse an empty +document and sets `REQBODY_ERROR`, so with the recommended rules 200000 and +200002 every XML request to such a location is rejected with 400. Remove rule +200000 there: `modsecurity_rules 'SecRuleRemoveById 200000';`. + +The setting of the location where the access phase first runs applies to the +whole request: an internal redirect (for example `try_files`, `index` or a +named location) to a location with `on` does not re-enable body inspection, +so set `off` only where every redirect target may also skip it. + This is useful for locations handling large uploads, encrypted payloads, or for locations where `SecRequestBodyAccess Off` is already set: libmodsecurity copies the request body regardless of `SecRequestBodyAccess`, so turning this directive off is the only way to avoid that cost. +This directive is a per-location opt-out. It does not make the connector +inspect the body while it is streamed to the backend: with `on`, the whole +body is still buffered before inspection. + ```nginx server { modsecurity on; diff --git a/src/ngx_http_modsecurity_access.c b/src/ngx_http_modsecurity_access.c index 5308b5b6..1159e862 100644 --- a/src/ngx_http_modsecurity_access.c +++ b/src/ngx_http_modsecurity_access.c @@ -305,12 +305,11 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r) return NGX_HTTP_INTERNAL_SERVER_ERROR; } - if (ctx->request_body_processed) { - // should we use r->internal or r->filter_finalize? + if (ctx->intervention_triggered) { return NGX_DECLINED; } - if (ctx->intervention_triggered) { + if (ctx->request_body_processed) { return NGX_DECLINED; } @@ -322,36 +321,10 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r) return NGX_DONE; } - if (mcf->request_body == 0) - { - int ret = 0; - - /* - * Request body inspection is disabled for this location. Do not - * buffer the body here (that lets the content handler stream it, - * e.g. proxy_request_buffering off), but still run the - * REQUEST_BODY phase so rules that do not depend on the body - * (ARGS, REQUEST_HEADERS, ...) execute in their usual phase. - */ + if (mcf->request_body == 0) { dd("request body inspection disabled by modsecurity_request_body off"); - old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool); - msc_process_request_body(ctx->modsec_transaction); - ctx->request_body_processed = 1; - ngx_http_modsecurity_pcre_malloc_done(old_pool); - - ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0); - if (r->error_page) { - return NGX_DECLINED; - } - if (ret > 0) { - return ret; - } - - return NGX_DECLINED; - } - - if (ctx->body_requested == 0) + } else if (ctx->body_requested == 0) { ngx_int_t rc = NGX_OK; @@ -399,14 +372,12 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r) if (ctx->waiting_more_body == 0) { int ret = 0; - int already_inspected = 0; + ngx_chain_t *chain = NULL; dd("request body is ready to be processed"); r->write_event_handler = ngx_http_core_run_phases; - ngx_chain_t *chain = r->request_body->bufs; - /** * TODO: Speed up the analysis by sending chunk while they arrive. * @@ -416,7 +387,10 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r) * function. */ - if (r->request_body->temp_file != NULL) { + if (mcf->request_body == 0 || r->request_body == NULL) { + dd("request body is not inspected"); + + } else if (r->request_body->temp_file != NULL) { ngx_str_t file_path = r->request_body->temp_file->file.name; const char *file_name = ngx_str_to_char(file_path, r->pool); if (file_name == (char*)-1) { @@ -429,13 +403,12 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r) dd("request body inspection: file -- %s", file_name); msc_request_body_from_file(ctx->modsec_transaction, file_name); - - already_inspected = 1; } else { dd("inspection request body in memory."); + chain = r->request_body->bufs; } - while (chain && !already_inspected) + while (chain) { u_char *data = chain->buf->pos; @@ -456,6 +429,7 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r) */ ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0); if (ret > 0) { + ctx->intervention_triggered = 1; return ret; } } @@ -477,8 +451,9 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r) ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0); if (r->error_page) { return NGX_DECLINED; - } + } if (ret > 0) { + ctx->intervention_triggered = 1; return ret; } } diff --git a/src/ngx_http_modsecurity_module.c b/src/ngx_http_modsecurity_module.c index 834f1106..ebc5cd11 100644 --- a/src/ngx_http_modsecurity_module.c +++ b/src/ngx_http_modsecurity_module.c @@ -727,7 +727,6 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf) * set by ngx_pcalloc(): * * conf->enable = 0; - * conf->request_body = 0; * conf->sanity_checks_enabled = 0; * conf->rules_set = NULL; * conf->pool = NULL; diff --git a/tests/modsecurity-request-body-directive.t b/tests/modsecurity-request-body-directive.t index b425eeca..4631746c 100644 --- a/tests/modsecurity-request-body-directive.t +++ b/tests/modsecurity-request-body-directive.t @@ -44,6 +44,12 @@ http { SecRequestBodyAccess On SecRule REQUEST_BODY "@rx BAD BODY" "id:11,phase:2,deny,log,status:403" SecRule ARGS:what "@streq badarg" "id:12,phase:2,deny,log,status:403" + SecRule ARGS:what "@streq badarg" "id:13,phase:3,deny,log,status:418" + SecRule REQUEST_HEADERS:Content-Type "^(?:application(?:/soap\\+|/)|text/)xml" "id:200000,phase:1,t:none,t:lowercase,pass,nolog,ctl:requestBodyProcessor=XML" + SecRule REQUEST_HEADERS:Content-Type "^application/json" "id:200001,phase:1,t:none,t:lowercase,pass,nolog,ctl:requestBodyProcessor=JSON" + SecRule REQBODY_ERROR "!@eq 0" "id:200002,phase:2,t:none,log,deny,status:400" + SecRule MULTIPART_STRICT_ERROR "!@eq 0" "id:200003,phase:2,t:none,log,deny,status:400" + SecRule MULTIPART_UNMATCHED_BOUNDARY "@eq 1" "id:200004,phase:2,t:none,log,deny,status:400" '; location /inspect { @@ -72,6 +78,31 @@ http { proxy_pass http://127.0.0.1:%%PORT_8081%%; } + location /redirect { + modsecurity_request_body off; + try_files /nonexistent @inspect; + } + + location @inspect { + proxy_pass http://127.0.0.1:%%PORT_8081%%; + } + + location /errorpage { + modsecurity_request_body off; + error_page 403 /denied; + proxy_pass http://127.0.0.1:%%PORT_8081%%; + } + + location /denied { + proxy_pass http://127.0.0.1:%%PORT_8081%%; + } + + location /skipxml { + modsecurity_request_body off; + modsecurity_rules 'SecRuleRemoveById 200000'; + proxy_pass http://127.0.0.1:%%PORT_8081%%; + } + location /parent { modsecurity_request_body off; @@ -91,7 +122,7 @@ EOF $t->run_daemon(\&http_daemon); $t->run()->waitforsocket('127.0.0.1:' . port(8081)); -$t->plan(12); +$t->plan(21); ############################################################################### @@ -105,6 +136,30 @@ like(http_post('/parent/child', 'VERY BAD BODY'), qr/LEN=13/, 'off inherited by like(http_post('/parent/on', 'VERY BAD BODY'), qr/^HTTP.*403/, 'on overrides inherited off'); like(http_post('/parent/on', 'GOOD BODY'), qr/LEN=9/, 'on overrides inherited off, good body passes'); +like(http_post('/inspect?what=badarg', 'GOOD BODY'), qr/^HTTP.*403/, + 'on: phase 2 deny skips response phase'); +like(http_post('/redirect', 'VERY BAD BODY'), qr/LEN=13/, + 'off redirected to on: first location applies, body not inspected'); +like(http_post('/errorpage?what=badarg', 'VERY BAD BODY'), + qr/^HTTP.*403.*TEST-OK/s, 'off: phase 2 deny with error_page'); + +my $mp = "--XyZ\r\nContent-Disposition: form-data; name=\"a\"\r\n\r\n" + . "VERY BAD BODY\r\n--XyZ--\r\n"; +my $mplen = length($mp); + +like(http_post('/inspect', '{"a":', 'application/json'), + qr/^HTTP.*400/, 'on: invalid JSON rejected'); +like(http_post('/skip', '{"a":"VERY BAD BODY"}', 'application/json'), + qr/LEN=21/, 'off: JSON body passes'); +like(http_post('/inspect', $mp, 'multipart/form-data; boundary=XyZ'), + qr/^HTTP.*403/, 'on: multipart body inspected'); +like(http_post('/skip', $mp, 'multipart/form-data; boundary=XyZ'), + qr/LEN=$mplen/, 'off: multipart body passes'); +like(http_post('/inspect', '', 'text/xml'), + qr/^HTTP.*400/, 'on: invalid XML rejected'); +like(http_post('/skipxml', 'VERY BAD BODY', 'text/xml'), + qr/LEN=20/, 'off: XML body passes without XML processor'); + # the upstream must see the beginning of the request body before the client # has sent all of it, otherwise the body is still being buffered by nginx @@ -123,12 +178,13 @@ like(http_read($s, 5), qr/^HTTP.*403/, ############################################################################### sub http_post { - my ($url, $body) = @_; + my ($url, $body, $type) = @_; my $len = length($body); + $type = 'application/x-www-form-urlencoded' unless defined $type; return http(<