From 2256e6ec02317fc4dddd2d11e595f1f2fa764eb1 Mon Sep 17 00:00:00 2001 From: Tom Sommer Date: Sat, 19 Sep 2026 15:36:52 +0200 Subject: [PATCH 1/4] Add modsecurity_response_body directive to skip response body buffering When set to off the header filter no longer forces filter_need_in_memory (so with sendfile enabled file buffers are no longer read into memory before being sent) and the body filter no longer copies response buffers into libmodsecurity. The RESPONSE_BODY phase still runs on the last buffer so phase 4 rules that do not need the body keep working. libmodsecurity copies response bodies of the configured MIME types even with SecResponseBodyAccess Off, and the connector cannot query that setting, so an explicit directive is the only way to avoid the cost. --- README.md | 43 +++++- src/ngx_http_modsecurity_body_filter.c | 20 ++- src/ngx_http_modsecurity_common.h | 1 + src/ngx_http_modsecurity_header_filter.c | 19 ++- src/ngx_http_modsecurity_module.c | 10 ++ tests/modsecurity-response-body-directive.t | 161 ++++++++++++++++++++ 6 files changed, 239 insertions(+), 15 deletions(-) create mode 100644 tests/modsecurity-response-body-directive.t diff --git a/README.md b/README.md index a93e5df0..de0e0063 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,47 @@ 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. `RESPONSE_BODY` is therefore empty and +`RESPONSE_CONTENT_LENGTH` is `0`, as both are derived from the bytes the +connector appended. The connector still runs the response body phase on the +last buffer, so phase 4 rules that do not need the body (for instance rules on +`RESPONSE_STATUS`) keep working. Note that ModSecurity itself skips phase 4 +altogether when `SecResponseBodyAccess` is `Off` or the response Content-Type +is not listed in `SecResponseBodyMimeType`. + +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..494bb25b 100644 --- a/src/ngx_http_modsecurity_body_filter.c +++ b/src/ngx_http_modsecurity_body_filter.c @@ -39,8 +39,8 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in) { ngx_chain_t *chain = in; ngx_http_modsecurity_ctx_t *ctx = NULL; -#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS) ngx_http_modsecurity_conf_t *mcf; +#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS) ngx_list_part_t *part = &r->headers_out.headers.part; ngx_table_elt_t *data = part->elts; ngx_uint_t i = 0; @@ -62,8 +62,9 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in) return ngx_http_next_body_filter(r, in); } -#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS) mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module); + +#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS) if (mcf != NULL && mcf->sanity_checks_enabled != NGX_CONF_UNSET) { #if 0 @@ -143,14 +144,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 (mcf == NULL || mcf->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..0a7b8348 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 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..d928b942 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,18 @@ 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, ngx_http_modsecurity_module); + if (mcf == NULL || mcf->response_body) { + /* + * Ask nginx to keep the response body in memory so that the body + * filter can feed it to ModSecurity. With sendfile enabled this + * makes the copy filter read file buffers (static files, upstream + * responses spooled to disk) into memory instead of sending them + * straight from the file, so skip it when the operator turned + * response body inspection off. + */ + 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 d3d9624d..c8e4b517 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_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 }; @@ -724,6 +732,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 @@ -764,6 +773,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..181a8914 --- /dev/null +++ b/tests/modsecurity-response-body-directive.t @@ -0,0 +1,161 @@ +#!/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/); + +$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; + + 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 "@streq 200" "id:42,phase:4,log,auditlog,pass" + SecAuditEngine RelevantOnly + SecAuditLogParts ABH + SecAuditLogType Serial + SecAuditLogStorageDir %%TESTDIR%%/ + '; + + 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 + '; + } + } + } +} + +EOF + +mkdir($t->testdir() . '/inspect'); +mkdir($t->testdir() . '/skip'); +mkdir($t->testdir() . '/inherit'); +mkdir($t->testdir() . '/inherit/nested'); +$t->write_file('/inspect/page', 'the body has a MARKER in it'); +$t->write_file('/skip/page', 'the body has a MARKER in it'); +$t->write_file('/inherit/nested/page', 'the body has a MARKER in it'); + +$t->run(); +$t->plan(13); + +############################################################################### + +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'); + +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'); + +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'); + +my $einspect = read_file($t->testdir() . '/error-inspect.log'); +my $eskip = read_file($t->testdir() . '/error-skip.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. +unlike($einspect, qr/\[alert\]|\[crit\]|\[emerg\]/, 'default: no alerts'); +unlike($eskip, qr/\[alert\]|\[crit\]|\[emerg\]/, 'off: no alerts'); + +SKIP: { +skip 'needs --with-debug', 2 unless $t->has_module('--with-debug'); +skip 'no sendfile on win32', 2 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. 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($einspect, qr/write new buf t:1 f:1 /, 'default: body read into memory'); +unlike($eskip, qr/write new buf t:1 f:1 /, 'off: body sent straight from file'); +} + +############################################################################### + +sub read_file { + my ($path) = @_; + local $/; + open my $fh, '<', $path or return ''; + my $data = <$fh>; + close $fh; + return $data; +} From 6ae13e1c08184135aeb3bcfaf86907e411dcaa39 Mon Sep 17 00:00:00 2001 From: Tom Sommer Date: Fri, 13 Nov 2026 07:35:53 +0100 Subject: [PATCH 2/4] Take the modsecurity_response_body decision once, in the header filter The header filter decided whether to set r->filter_need_in_memory from the location conf, and the body filter looked the conf up again to decide whether to append buffers. The two lookups can see different locations: when a phase 3 intervention in the header filter sends an error_page, the request is internally redirected after the header filter ran, so the body filter read the error page location's setting. With "off" on the denied location and the default "on" on the error page location, the error page body was appended to RESPONSE_BODY although nothing asked for it to be in memory. Store the decision on the ctx when the response headers are processed and test only that in the body filter. The body filter no longer needs the loc conf outside MODSECURITY_SANITY_CHECKS, so its lookup goes back under that #if as on master, and the impossible mcf == NULL arms are gone. SSI includes and add_after_body do not hit this: the postpone filter runs before this module and passes subrequest output down as r->main, so the body filter already saw the main request's conf for them. A regression test for an SSI include into a default location is added anyway. Tests: server level "off" with "on" in a child location, debug log checks that the inherited and server level "off" cases send the file buffer straight from the file ("t:0 f:1") and never read it into memory ("t:1 f:1"), and the error_page case, which fails without this change. --- src/ngx_http_modsecurity_body_filter.c | 7 +- src/ngx_http_modsecurity_common.h | 1 + src/ngx_http_modsecurity_header_filter.c | 10 +- tests/modsecurity-response-body-directive.t | 186 ++++++++++++++++---- 4 files changed, 156 insertions(+), 48 deletions(-) diff --git a/src/ngx_http_modsecurity_body_filter.c b/src/ngx_http_modsecurity_body_filter.c index 494bb25b..21ddea3c 100644 --- a/src/ngx_http_modsecurity_body_filter.c +++ b/src/ngx_http_modsecurity_body_filter.c @@ -39,8 +39,8 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in) { ngx_chain_t *chain = in; ngx_http_modsecurity_ctx_t *ctx = NULL; - ngx_http_modsecurity_conf_t *mcf; #if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS) + 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; @@ -62,9 +62,8 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in) return ngx_http_next_body_filter(r, in); } - mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module); - #if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS) + mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module); if (mcf != NULL && mcf->sanity_checks_enabled != NGX_CONF_UNSET) { #if 0 @@ -146,7 +145,7 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in) { int ret; - if (mcf == NULL || mcf->response_body) { + if (ctx->response_body) { u_char *data = chain->buf->pos; msc_append_response_body(ctx->modsec_transaction, data, chain->buf->last - data); diff --git a/src/ngx_http_modsecurity_common.h b/src/ngx_http_modsecurity_common.h index 0a7b8348..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; diff --git a/src/ngx_http_modsecurity_header_filter.c b/src/ngx_http_modsecurity_header_filter.c index d928b942..060d63ae 100644 --- a/src/ngx_http_modsecurity_header_filter.c +++ b/src/ngx_http_modsecurity_header_filter.c @@ -450,14 +450,12 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r) } mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module); - if (mcf == NULL || mcf->response_body) { + 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. With sendfile enabled this - * makes the copy filter read file buffers (static files, upstream - * responses spooled to disk) into memory instead of sending them - * straight from the file, so skip it when the operator turned - * response body inspection off. + * filter can feed it to ModSecurity. */ r->filter_need_in_memory = 1; } diff --git a/tests/modsecurity-response-body-directive.t b/tests/modsecurity-response-body-directive.t index 181a8914..b4d0ca6f 100644 --- a/tests/modsecurity-response-body-directive.t +++ b/tests/modsecurity-response-body-directive.t @@ -34,7 +34,7 @@ use Test::Nginx; select STDERR; $| = 1; select STDOUT; $| = 1; -my $t = Test::Nginx->new()->has(qw/http/); +my $t = Test::Nginx->new()->has(qw/http ssi/); $t->write_file_expand('nginx.conf', <<'EOF'); @@ -48,26 +48,26 @@ 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; - 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 "@streq 200" "id:42,phase:4,log,auditlog,pass" - SecAuditEngine RelevantOnly - SecAuditLogParts ABH - SecAuditLogType Serial - SecAuditLogStorageDir %%TESTDIR%%/ - '; - location /inspect/ { modsecurity_rules ' SecAuditLog %%TESTDIR%%/audit-inspect.txt @@ -90,23 +90,74 @@ http { 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 /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 -mkdir($t->testdir() . '/inspect'); -mkdir($t->testdir() . '/skip'); -mkdir($t->testdir() . '/inherit'); -mkdir($t->testdir() . '/inherit/nested'); -$t->write_file('/inspect/page', 'the body has a MARKER in it'); -$t->write_file('/skip/page', 'the body has a MARKER in it'); -$t->write_file('/inherit/nested/page', 'the body has a MARKER in it'); +for my $d (qw{inspect skip inherit inherit/nested ssi deny on off}) { + mkdir($t->testdir() . '/' . $d); +} +for my $d (qw{inspect skip inherit/nested 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(13); +$t->plan(36); ############################################################################### @@ -114,10 +165,22 @@ 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('/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 $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'); @@ -126,31 +189,78 @@ 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'); - -my $einspect = read_file($t->testdir() . '/error-inspect.log'); -my $eskip = read_file($t->testdir() . '/error-skip.log'); +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 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. -unlike($einspect, qr/\[alert\]|\[crit\]|\[emerg\]/, 'default: no alerts'); -unlike($eskip, qr/\[alert\]|\[crit\]|\[emerg\]/, 'off: no alerts'); +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', 2 unless $t->has_module('--with-debug'); -skip 'no sendfile on win32', 2 if $^O eq 'MSWin32'; +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. 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($einspect, qr/write new buf t:1 f:1 /, 'default: body read into memory'); -unlike($eskip, qr/write new buf t:1 f:1 /, 'off: body sent straight from file'); +# 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(< Date: Fri, 13 Nov 2026 07:35:53 +0100 Subject: [PATCH 3/4] README: document what modsecurity_response_body off does to phase 4 Phase 4 still runs with "off", against an empty RESPONSE_BODY and RESPONSE_CONTENT_LENGTH 0, because Transaction::processResponseBody() is the only place libmodsecurity evaluates phase 4 rules: skipping the call would also drop phase 4 rules that do not look at the body. Spell out the consequences: body content rules (CRS outbound leakage rules included) stop matching, rules on the absence of content match on every response and, if disruptive, fire after the header was passed on, and the audit log has no part E. &RESPONSE_BODY stays 1, so it is not listed. Also say that the setting of the location whose response headers were processed applies to subrequests and error pages. --- README.md | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index de0e0063..2f7200d4 100644 --- a/README.md +++ b/README.md @@ -201,13 +201,28 @@ inspected at all is still decided by `SecResponseBodyAccess` and 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. `RESPONSE_BODY` is therefore empty and -`RESPONSE_CONTENT_LENGTH` is `0`, as both are derived from the bytes the -connector appended. The connector still runs the response body phase on the -last buffer, so phase 4 rules that do not need the body (for instance rules on -`RESPONSE_STATUS`) keep working. Note that ModSecurity itself skips phase 4 -altogether when `SecResponseBodyAccess` is `Off` or the response Content-Type -is not listed in `SecResponseBodyMimeType`. +response buffers into ModSecurity. The setting of the location whose response +headers were passed to ModSecurity applies to the whole response, including +subrequests (SSI includes, `add_after_body`) 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 From 13cda85f86fe2cf70099b971c5b026ae607e13b8 Mon Sep 17 00:00:00 2001 From: Tom Sommer Date: Fri, 13 Nov 2026 07:40:22 +0100 Subject: [PATCH 4/4] Read modsecurity_response_body from the main request's location An auth_request subrequest reaches the header filter before the main request does: the access phase handler has already created the ctx, the subrequest finds it through the shared pool cleanup, and ctx->processed is still 0. Since 6ae13e1 the decision stored on the ctx was then taken from the auth location, so "off" there silently disabled response body inspection for the protected location. Before 6ae13e1 the body filter read the main request's conf, so this was a regression of that commit. Look the setting up on r->main. For every response produced by the main request itself r->main == r, so nothing else changes. The test gains an auth_request into an "off" location, which fails at 6ae13e1, and the README says which location's setting applies. --- README.md | 8 +++--- src/ngx_http_modsecurity_header_filter.c | 2 +- tests/modsecurity-response-body-directive.t | 30 ++++++++++++++++++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2f7200d4..b0f3bb45 100644 --- a/README.md +++ b/README.md @@ -201,10 +201,10 @@ inspected at all is still decided by `SecResponseBodyAccess` and 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 location whose response -headers were passed to ModSecurity applies to the whole response, including -subrequests (SSI includes, `add_after_body`) and an `error_page` served after -a ModSecurity intervention. +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 diff --git a/src/ngx_http_modsecurity_header_filter.c b/src/ngx_http_modsecurity_header_filter.c index 060d63ae..1a315828 100644 --- a/src/ngx_http_modsecurity_header_filter.c +++ b/src/ngx_http_modsecurity_header_filter.c @@ -449,7 +449,7 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r) return ngx_http_next_header_filter(r); } - mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module); + mcf = ngx_http_get_module_loc_conf(r->main, ngx_http_modsecurity_module); ctx->response_body = mcf->response_body; if (ctx->response_body) { diff --git a/tests/modsecurity-response-body-directive.t b/tests/modsecurity-response-body-directive.t index b4d0ca6f..cb167dbd 100644 --- a/tests/modsecurity-response-body-directive.t +++ b/tests/modsecurity-response-body-directive.t @@ -34,7 +34,7 @@ use Test::Nginx; select STDERR; $| = 1; select STDOUT; $| = 1; -my $t = Test::Nginx->new()->has(qw/http ssi/); +my $t = Test::Nginx->new()->has(qw/http ssi auth_request/); $t->write_file_expand('nginx.conf', <<'EOF'); @@ -107,6 +107,20 @@ http { 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 ' @@ -147,17 +161,17 @@ http { EOF -for my $d (qw{inspect skip inherit inherit/nested ssi deny on off}) { +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 on off}) { +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(36); +$t->plan(39); ############################################################################### @@ -171,6 +185,8 @@ 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'); @@ -180,6 +196,7 @@ 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'); @@ -202,6 +219,11 @@ 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');