diff --git a/README.md b/README.md
index 36b859c2..633079eb 100644
--- a/README.md
+++ b/README.md
@@ -58,7 +58,7 @@ Further information about building nginx third-party modules is available [here]
# Usage
ModSecurity for nginx extends your nginx configuration directives.
-It adds six new directives and they are:
+It adds the following directives:
modsecurity
-----------
@@ -211,6 +211,62 @@ 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
+(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;
+ 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 47175e45..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,7 +321,10 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r)
return NGX_DONE;
}
- if (ctx->body_requested == 0)
+ if (mcf->request_body == 0) {
+ dd("request body inspection disabled by modsecurity_request_body off");
+
+ } else if (ctx->body_requested == 0)
{
ngx_int_t rc = NGX_OK;
@@ -370,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.
*
@@ -387,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) {
@@ -400,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;
@@ -427,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;
}
}
@@ -448,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_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 1e55ade0..ebc5cd11 100644
--- a/src/ngx_http_modsecurity_module.c
+++ b/src/ngx_http_modsecurity_module.c
@@ -535,6 +535,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
};
@@ -730,6 +738,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
@@ -770,6 +779,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..4631746c
--- /dev/null
+++ b/tests/modsecurity-request-body-directive.t
@@ -0,0 +1,306 @@
+#!/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"
+ 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 {
+ 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 /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;
+
+ 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(21);
+
+###############################################################################
+
+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');
+
+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
+
+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, $type) = @_;
+ my $len = length($body);
+ $type = 'application/x-www-form-urlencoded' unless defined $type;
+ 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;
+ }
+}
+
+###############################################################################