diff --git a/README.md b/README.md index 36b859c2..a2bd2664 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_response_body +------------------------- +**syntax:** *modsecurity_response_body on | off* + +**context:** *http, server, location* + +**default:** *on* + +Controls whether the connector buffers response bodies and hands them to +ModSecurity. It does not enable inspection on its own: whether the body is +inspected at all is still decided by `SecResponseBodyAccess` and +`SecResponseBodyMimeType`. + +When turned off the connector no longer asks nginx to keep the response body +in memory, so with `sendfile` enabled file buffers are sent straight from the +file instead of being read into memory first, and it no longer copies the +response buffers into ModSecurity. The setting of the main request's location at +the time ModSecurity processes the response headers applies to the whole +response, including subrequests (SSI includes, `add_after_body`, +`auth_request`) and an `error_page` served after a ModSecurity intervention. + +ModSecurity still runs phase 4 on the last buffer, but against an empty body +(ModSecurity itself skips phase 4 altogether when `SecResponseBodyAccess` is +`Off` or the response Content-Type is not listed in +`SecResponseBodyMimeType`): + +* `RESPONSE_BODY` is empty and `RESPONSE_CONTENT_LENGTH` is `0`. +* Rules that look for content in the response body silently stop matching. + This includes the OWASP CRS outbound data leakage rules + (`RESPONSE-95x-*.conf`) that inspect `RESPONSE_BODY`. +* Rules that match on the absence of content now match on every response, + for instance `RESPONSE_BODY "!@contains "`, `RESPONSE_BODY "@rx ^$"` + or `RESPONSE_CONTENT_LENGTH "@lt 100"`. If such a rule is disruptive, it + fires after the response header has already been passed on, so the client + gets an empty or truncated response instead of the error page. +* The audit log has no part E (response body). +* Phase 4 rules that do not look at the body, for instance rules on + `RESPONSE_STATUS` or on variables set in earlier phases, keep working. + +Turning it off is recommended for locations serving static files or large +downloads, and wherever `SecResponseBodyAccess Off` is in use: ModSecurity +still copies the response bodies of the configured MIME types and the +connector has no way to query that setting, so this directive is the only +way to avoid the cost. + +```nginx +server { + sendfile on; + modsecurity on; + modsecurity_rules_file /etc/my_modsecurity_rules.conf; + location /static/ { + modsecurity_response_body off; + } +} +``` + # 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_body_filter.c b/src/ngx_http_modsecurity_body_filter.c index 0c28e3c1..21ddea3c 100644 --- a/src/ngx_http_modsecurity_body_filter.c +++ b/src/ngx_http_modsecurity_body_filter.c @@ -143,14 +143,17 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in) int is_request_processed = 0; for (; chain != NULL; chain = chain->next) { - u_char *data = chain->buf->pos; int ret; - msc_append_response_body(ctx->modsec_transaction, data, chain->buf->last - data); - ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0); - if (ret > 0) { - return ngx_http_filter_finalize_request(r, - &ngx_http_modsecurity_module, ret); + if (ctx->response_body) { + u_char *data = chain->buf->pos; + + msc_append_response_body(ctx->modsec_transaction, data, chain->buf->last - data); + ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0); + if (ret > 0) { + return ngx_http_filter_finalize_request(r, + &ngx_http_modsecurity_module, ret); + } } /* XXX: chain->buf->last_buf || chain->buf->last_in_chain */ diff --git a/src/ngx_http_modsecurity_common.h b/src/ngx_http_modsecurity_common.h index 9f79b5ba..003433c0 100644 --- a/src/ngx_http_modsecurity_common.h +++ b/src/ngx_http_modsecurity_common.h @@ -100,6 +100,7 @@ typedef struct { unsigned logged:1; unsigned intervention_triggered:1; unsigned request_body_processed:1; + unsigned response_body:1; } ngx_http_modsecurity_ctx_t; @@ -119,6 +120,7 @@ typedef struct { ngx_flag_t enable; ngx_flag_t use_error_log; + ngx_flag_t response_body; #if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS) ngx_flag_t sanity_checks_enabled; #endif diff --git a/src/ngx_http_modsecurity_header_filter.c b/src/ngx_http_modsecurity_header_filter.c index 03b8764d..1a315828 100644 --- a/src/ngx_http_modsecurity_header_filter.c +++ b/src/ngx_http_modsecurity_header_filter.c @@ -411,6 +411,7 @@ ngx_int_t ngx_http_modsecurity_header_filter(ngx_http_request_t *r) { ngx_http_modsecurity_ctx_t *ctx; + ngx_http_modsecurity_conf_t *mcf; ngx_list_part_t *part = &r->headers_out.headers.part; ngx_table_elt_t *data = part->elts; ngx_uint_t i = 0; @@ -448,12 +449,16 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r) return ngx_http_next_header_filter(r); } - /* - * Lets ask nginx to keep the response body in memory - * - * FIXME: I don't see a reason to keep it `1' when SecResponseBody is disabled. - */ - r->filter_need_in_memory = 1; + mcf = ngx_http_get_module_loc_conf(r->main, ngx_http_modsecurity_module); + ctx->response_body = mcf->response_body; + + if (ctx->response_body) { + /* + * Ask nginx to keep the response body in memory so that the body + * filter can feed it to ModSecurity. + */ + r->filter_need_in_memory = 1; + } ctx->processed = 1; /* diff --git a/src/ngx_http_modsecurity_module.c b/src/ngx_http_modsecurity_module.c index 1e55ade0..a1e06892 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_response_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, response_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->response_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->response_body, p->response_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-response-body-directive.t b/tests/modsecurity-response-body-directive.t new file mode 100644 index 00000000..cb167dbd --- /dev/null +++ b/tests/modsecurity-response-body-directive.t @@ -0,0 +1,293 @@ +#!/usr/bin/perl + +# +# ModSecurity, http://www.modsecurity.org/ +# Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/) +# +# You may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# If any of the files related to licensing are missing or if you have any +# other questions related to licensing please contact Trustwave Holdings, Inc. +# directly using the email address security@modsecurity.org. +# + + +# Tests for ModSecurity module (modsecurity_response_body directive). + +############################################################################### + +use warnings; +use strict; + +use Test::More; + +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 ssi auth_request/); + +$t->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + sendfile on; + default_type text/plain; + + modsecurity on; + modsecurity_rules ' + SecRuleEngine On + SecResponseBodyAccess On + SecResponseBodyMimeType text/plain + SecRule RESPONSE_BODY "@contains MARKER" "id:41,phase:4,log,auditlog,pass" + SecRule RESPONSE_STATUS "@rx ^[0-9]+$" "id:42,phase:4,log,auditlog,pass" + SecAuditEngine RelevantOnly + SecAuditLogParts ABH + SecAuditLogType Serial + SecAuditLogStorageDir %%TESTDIR%%/ + '; + + server { + listen 127.0.0.1:8080; + server_name localhost; + + location /inspect/ { + modsecurity_rules ' + SecAuditLog %%TESTDIR%%/audit-inspect.txt + '; + error_log %%TESTDIR%%/error-inspect.log debug; + } + + location /skip/ { + modsecurity_response_body off; + modsecurity_rules ' + SecAuditLog %%TESTDIR%%/audit-skip.txt + '; + error_log %%TESTDIR%%/error-skip.log debug; + } + + location /inherit/ { + modsecurity_response_body off; + + location /inherit/nested/ { + modsecurity_rules ' + SecAuditLog %%TESTDIR%%/audit-nested.txt + '; + error_log %%TESTDIR%%/error-nested.log debug; + } + } + + location /ssi/ { + ssi on; + ssi_types text/plain; + modsecurity_response_body off; + modsecurity_rules ' + SecAuditLog %%TESTDIR%%/audit-ssi.txt + '; + } + + location = /include { + return 200 "included MARKER"; + } + + location /auth/ { + sendfile off; + auth_request /auth-off; + modsecurity_rules ' + SecAuditLog %%TESTDIR%%/audit-auth.txt + '; + } + + location = /auth-off { + sendfile off; + modsecurity_response_body off; + return 204; + } + + location /deny/ { + modsecurity_response_body off; + modsecurity_rules ' + SecRule REQUEST_URI "@beginsWith /deny/" "id:43,phase:3,deny,status:403,log,auditlog" + SecAuditLog %%TESTDIR%%/audit-deny.txt + '; + error_page 403 /error; + } + + location = /error { + internal; + return 403 "error page MARKER"; + } + } + + server { + listen 127.0.0.1:8080; + server_name srvoff; + + modsecurity_response_body off; + + location /on/ { + modsecurity_response_body on; + modsecurity_rules ' + SecAuditLog %%TESTDIR%%/audit-srv-on.txt + '; + error_log %%TESTDIR%%/error-srv-on.log debug; + } + + location /off/ { + modsecurity_rules ' + SecAuditLog %%TESTDIR%%/audit-srv-off.txt + '; + error_log %%TESTDIR%%/error-srv-off.log debug; + } + } +} + +EOF + +for my $d (qw{inspect skip inherit inherit/nested ssi auth deny on off}) { + mkdir($t->testdir() . '/' . $d); +} +for my $d (qw{inspect skip inherit/nested auth on off}) { + $t->write_file("/$d/page", 'the body has a MARKER in it'); +} +$t->write_file('/ssi/page', 'main end'); +$t->write_file('/deny/page', 'not sent'); + +$t->run(); +$t->plan(39); + +############################################################################### + +like(http_get('/inspect/page'), qr/MARKER in it/, 'default: body delivered'); +like(http_get('/skip/page'), qr/MARKER in it/, 'off: body delivered intact'); +like(http_get('/inherit/nested/page'), qr/MARKER in it/, + 'inherited off: body delivered intact'); +like(get_srvoff('/on/page'), qr/MARKER in it/, + 'server off, location on: body delivered'); +like(get_srvoff('/off/page'), qr/MARKER in it/, + 'server off: body delivered intact'); +like(http_get('/ssi/page'), qr/main included MARKER end/, + 'off with SSI include: body delivered'); +like(http_get('/auth/page'), qr/MARKER in it/, + 'auth_request into off: body delivered'); +like(http_get('/deny/page'), qr/403 Forbidden.*error page MARKER/s, + 'off with error_page: error page delivered'); + +my $inspect = read_file($t->testdir() . '/audit-inspect.txt'); +my $skip = read_file($t->testdir() . '/audit-skip.txt'); +my $nested = read_file($t->testdir() . '/audit-nested.txt'); +my $srvon = read_file($t->testdir() . '/audit-srv-on.txt'); +my $srvoff = read_file($t->testdir() . '/audit-srv-off.txt'); +my $ssi = read_file($t->testdir() . '/audit-ssi.txt'); +my $auth = read_file($t->testdir() . '/audit-auth.txt'); +my $deny = read_file($t->testdir() . '/audit-deny.txt'); + +like($inspect, qr/\[id "41"\]/, 'default: RESPONSE_BODY rule matched'); +like($inspect, qr/\[id "42"\]/, 'default: phase 4 ran'); +unlike($skip, qr/\[id "41"\]/, 'off: RESPONSE_BODY not fed to ModSecurity'); +like($skip, qr/\[id "42"\]/, 'off: phase 4 still ran'); +unlike($nested, qr/\[id "41"\]/, + 'inherited off: RESPONSE_BODY not fed to ModSecurity'); +like($nested, qr/\[id "42"\]/, 'inherited off: phase 4 still ran'); +like($srvon, qr/\[id "41"\]/, + 'server off, location on: RESPONSE_BODY rule matched'); +like($srvon, qr/\[id "42"\]/, 'server off, location on: phase 4 ran'); +unlike($srvoff, qr/\[id "41"\]/, + 'server off: RESPONSE_BODY not fed to ModSecurity'); +like($srvoff, qr/\[id "42"\]/, 'server off: phase 4 still ran'); + +# the include is a subrequest in a location left at the default "on"; the +# main request's setting applies to the whole response. +unlike($ssi, qr/\[id "41"\]/, + 'off with SSI include: included body not fed to ModSecurity'); +like($ssi, qr/\[id "42"\]/, 'off with SSI include: phase 4 ran'); + +# the auth subrequest reaches the header filter before the main request does. +like($auth, qr/\[id "41"\]/, + 'auth_request into off: RESPONSE_BODY rule matched'); +like($auth, qr/\[id "42"\]/, 'auth_request into off: phase 4 ran'); + +# the error page is served from a location left at the default "on" after the +# header filter already ran for the "off" location. +like($deny, qr/\[id "43"\]/, 'off with error_page: phase 3 rule denied'); +unlike($deny, qr/\[id "41"\]/, + 'off with error_page: error page body not fed to ModSecurity'); +like($deny, qr/\[id "42"\]/, 'off with error_page: phase 4 ran'); + +my %elog; +for my $n (qw/inspect skip nested srv-on srv-off/) { + $elog{$n} = read_file($t->testdir() . "/error-$n.log"); +} + +# the per location error_log takes these requests out of the error.log that +# Test::Nginx scans for alerts, so check them here as well. +for my $n (qw/inspect skip nested srv-on srv-off/) { + unlike($elog{$n}, qr/\[alert\]|\[crit\]|\[emerg\]/, "$n: no alerts"); +} + +SKIP: { +skip 'needs --with-debug', 9 unless $t->has_module('--with-debug'); +skip 'no sendfile on win32', 9 if $^O eq 'MSWin32'; + +# nginx keeps the buffer flagged as in-file even when it has to read it into +# memory, so sendfile() is used either way here; what "t:1 f:1" tells apart is +# whether the copy filter had to read the file into memory beforehand, while +# "t:0 f:1" is a buffer sent straight from the file. The copy filter only +# leaves a buffer in the file when sendfile is available, so without it both +# locations read the body into memory and there is nothing to tell apart. +like($elog{'inspect'}, qr/write new buf t:1 f:1 /, + 'default: body read into memory'); +like($elog{'skip'}, qr/write new buf t:0 f:1 /, + 'off: body sent straight from file'); +unlike($elog{'skip'}, qr/write new buf t:1 f:1 /, + 'off: body not read into memory'); +like($elog{'nested'}, qr/write new buf t:0 f:1 /, + 'inherited off: body sent straight from file'); +unlike($elog{'nested'}, qr/write new buf t:1 f:1 /, + 'inherited off: body not read into memory'); +like($elog{'srv-on'}, qr/write new buf t:1 f:1 /, + 'server off, location on: body read into memory'); +unlike($elog{'srv-on'}, qr/write new buf t:0 f:1 /, + 'server off, location on: body not sent straight from file'); +like($elog{'srv-off'}, qr/write new buf t:0 f:1 /, + 'server off: body sent straight from file'); +unlike($elog{'srv-off'}, qr/write new buf t:1 f:1 /, + 'server off: body not read into memory'); +} + +############################################################################### + +sub get_srvoff { + my ($uri) = @_; + return http(<; + close $fh; + return $data; +}