From 883d6ffc4ada41155994d1ff8b797a41027396e4 Mon Sep 17 00:00:00 2001 From: Tom Sommer Date: Sat, 19 Sep 2026 15:34:26 +0200 Subject: [PATCH 1/3] Do not run the connector on subrequests Subrequests share the parent's pool, so the cleanup-list fallback in ngx_http_modsecurity_get_module_ctx() returns the main request's transaction for them. With auth_request the auth subrequest's response headers were fed to phase 3 as the "response headers" of the main request and ctx->processed was set, so the real response headers were never inspected. With log_subrequest on, the subrequest also runs the LOG phase, so msc_process_logging() was called on the main request's transaction once for the subrequest and once for the main request: phase 5 and the audit log ran twice for a single transaction. Skip subrequests in the header filter, in the body filter and in the log handler; the main request's own headers, body and logging phase are still processed. The body filter cannot currently see a subrequest, because the module is ordered after the postpone filter and that one forwards subrequest output as r->main, but the check is kept so the module stays correct if the filter order ever changes. No check is needed in the access handler: ngx_http_core_access_phase() already skips the whole ACCESS phase for subrequests. Behaviour change: subrequests are no longer inspected at all, even when the parent location has modsecurity off and the subrequest's location has it on. A subrequest never produces a response of its own, so there is nothing for the WAF to protect there. --- src/ngx_http_modsecurity_body_filter.c | 4 + src/ngx_http_modsecurity_header_filter.c | 11 +++ src/ngx_http_modsecurity_log.c | 9 ++ tests/modsecurity-subrequest.t | 109 +++++++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 tests/modsecurity-subrequest.t diff --git a/src/ngx_http_modsecurity_body_filter.c b/src/ngx_http_modsecurity_body_filter.c index 0c28e3c1..9eb0b083 100644 --- a/src/ngx_http_modsecurity_body_filter.c +++ b/src/ngx_http_modsecurity_body_filter.c @@ -50,6 +50,10 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in) return ngx_http_next_body_filter(r, in); } + if (r != r->main) { + return ngx_http_next_body_filter(r, in); + } + ctx = ngx_http_modsecurity_get_module_ctx(r); dd("body filter, recovering ctx: %p", ctx); diff --git a/src/ngx_http_modsecurity_header_filter.c b/src/ngx_http_modsecurity_header_filter.c index 03b8764d..7ff3c6d9 100644 --- a/src/ngx_http_modsecurity_header_filter.c +++ b/src/ngx_http_modsecurity_header_filter.c @@ -422,6 +422,17 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r) /* XXX: if NOT_MODIFIED, do we need to process it at all? see xslt_header_filter() */ + /* + * Subrequests share the parent request's pool, so the cleanup-list + * fallback in ngx_http_modsecurity_get_module_ctx() would hand us the + * main request's transaction. Their headers belong to the parent's + * content, not to the response the client sees; only inspect the main + * request. + */ + if (r != r->main) { + return ngx_http_next_header_filter(r); + } + ctx = ngx_http_modsecurity_get_module_ctx(r); dd("header filter, recovering ctx: %p", ctx); diff --git a/src/ngx_http_modsecurity_log.c b/src/ngx_http_modsecurity_log.c index 094685c5..0fdfc470 100644 --- a/src/ngx_http_modsecurity_log.c +++ b/src/ngx_http_modsecurity_log.c @@ -44,6 +44,15 @@ ngx_http_modsecurity_log_handler(ngx_http_request_t *r) dd("catching a new _log_ phase handler"); + /* + * With log_subrequest on, this handler also runs for subrequests, which + * share the parent's pool and therefore its transaction; processing the + * logging phase there would run phase 5 and the audit log twice. + */ + if (r != r->main) { + return NGX_OK; + } + /* if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_POST && r->method != NGX_HTTP_HEAD) { diff --git a/tests/modsecurity-subrequest.t b/tests/modsecurity-subrequest.t new file mode 100644 index 00000000..0ce83168 --- /dev/null +++ b/tests/modsecurity-subrequest.t @@ -0,0 +1,109 @@ +#!/usr/bin/perl + +# (C) Andrei Belov + +# Tests for ModSecurity-nginx connector (subrequests). + +############################################################################### + +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 auth_request/); + +$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; + log_subrequest on; + modsecurity_rules ' + SecRuleEngine On + SecRule REQUEST_URI "@streq /index.html" "id:500,phase:2,log,auditlog,pass" + SecRule RESPONSE_STATUS "@streq 204" "id:501,phase:3,log,auditlog,pass" + SecRule RESPONSE_HEADERS:X-Main-Marker "@streq main" "id:502,phase:3,log,auditlog,pass" + SecAuditEngine RelevantOnly + SecAuditLogParts ABH + SecAuditLog %%TESTDIR%%/audit.txt + SecAuditLogType Serial + SecAuditLogStorageDir %%TESTDIR%%/ + '; + + location = /auth { + return 204; + } + + location / { + auth_request /auth; + add_header X-Main-Marker main; + } + } +} + +EOF + +$t->write_file('index.html', 'MAIN PAGE'); +$t->run(); +$t->plan(5); + +############################################################################### + +my $r = http_get('/index.html'); + +like($r, qr/^HTTP.*200/, 'request with auth_request passes'); +like($r, qr/MAIN PAGE/, 'main body delivered'); + +my $audit = read_file($t->testdir() . '/audit.txt'); + +like($audit, qr/\[id "502"\]/, 'main response headers inspected'); +unlike($audit, qr/\[id "501"\]/, 'auth subrequest response not inspected as main response'); + +# Each serial audit log record starts with a part-A section header of the +# form "------A--" on a line of its own, so counting those lines +# counts the records. The rule with id 500 makes the transaction relevant +# before the auth subrequest is even created, so a second, premature run of +# phase 5 (one per logged subrequest) shows up as an extra record here. + +my $records = () = $audit =~ /^---\w+---A--$/mg; + +is($records, 1, 'phase 5 ran once for the transaction'); + +############################################################################### + +sub read_file { + my ($file) = @_; + + local $/ = undef; + open my $fh, '<', $file + or return ''; + my $content = <$fh>; + close $fh; + + return $content; +} + +############################################################################### From 6efc199cec02315f67466ab21af3f5d37d37b714 Mon Sep 17 00:00:00 2001 From: Tom Sommer Date: Fri, 13 Nov 2026 07:36:15 +0100 Subject: [PATCH 2/3] Skip subrequests in get_module_ctx() instead of in each hook The three r != r->main guards added to the log handler, the header filter and the body filter all worked around one thing: subrequests share the parent's pool (ngx_http_subrequest() sets sr->pool = r->pool), so the pool-cleanup fallback in ngx_http_modsecurity_get_module_ctx() handed them the main request's transaction. The connector never creates a context for a subrequest (ngx_http_core_access_phase() skips the ACCESS phase for them), so that fallback was the only way a subrequest got one. Return NULL from ngx_http_modsecurity_get_module_ctx() for subrequests before the cleanup walk and drop the per-hook guards. Every caller already treats a NULL ctx as "not ours" and passes through, and ngx_http_modsecurity_process_intervention() and the header resolvers are only reached from the main-request path. The body-filter guard is dropped rather than turned into a lookup via r->main: subrequest output reaches this filter only through the postpone filter, which forwards it as r->main (ngx_http_postpone_filter()), so the included content is inspected against the main transaction. postpone is always compiled in (auto/modules) and config places this module below it for both static and dynamic builds, the same ordering the body inspection already relies on to see the body before gzip. Test: the phase-5 double-run check now uses an SSI include, which is created while the main body is sent, after every ACCESS-phase handler. The auth_request variant depended on this module's ACCESS handler running before auth_request's; a phase:1 rule would not have changed that, since phases 1 and 2 both run inside that same handler. The record count is 2 on master and 1 with this change. Also use Test::Nginx's read_file() and drop the copied copyright line. --- src/ngx_http_modsecurity_body_filter.c | 4 -- src/ngx_http_modsecurity_header_filter.c | 11 ---- src/ngx_http_modsecurity_log.c | 9 --- src/ngx_http_modsecurity_module.c | 6 ++ tests/modsecurity-subrequest.t | 70 +++++++++++++++--------- 5 files changed, 51 insertions(+), 49 deletions(-) diff --git a/src/ngx_http_modsecurity_body_filter.c b/src/ngx_http_modsecurity_body_filter.c index 9eb0b083..0c28e3c1 100644 --- a/src/ngx_http_modsecurity_body_filter.c +++ b/src/ngx_http_modsecurity_body_filter.c @@ -50,10 +50,6 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in) return ngx_http_next_body_filter(r, in); } - if (r != r->main) { - return ngx_http_next_body_filter(r, in); - } - ctx = ngx_http_modsecurity_get_module_ctx(r); dd("body filter, recovering ctx: %p", ctx); diff --git a/src/ngx_http_modsecurity_header_filter.c b/src/ngx_http_modsecurity_header_filter.c index 7ff3c6d9..03b8764d 100644 --- a/src/ngx_http_modsecurity_header_filter.c +++ b/src/ngx_http_modsecurity_header_filter.c @@ -422,17 +422,6 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r) /* XXX: if NOT_MODIFIED, do we need to process it at all? see xslt_header_filter() */ - /* - * Subrequests share the parent request's pool, so the cleanup-list - * fallback in ngx_http_modsecurity_get_module_ctx() would hand us the - * main request's transaction. Their headers belong to the parent's - * content, not to the response the client sees; only inspect the main - * request. - */ - if (r != r->main) { - return ngx_http_next_header_filter(r); - } - ctx = ngx_http_modsecurity_get_module_ctx(r); dd("header filter, recovering ctx: %p", ctx); diff --git a/src/ngx_http_modsecurity_log.c b/src/ngx_http_modsecurity_log.c index 0fdfc470..094685c5 100644 --- a/src/ngx_http_modsecurity_log.c +++ b/src/ngx_http_modsecurity_log.c @@ -44,15 +44,6 @@ ngx_http_modsecurity_log_handler(ngx_http_request_t *r) dd("catching a new _log_ phase handler"); - /* - * With log_subrequest on, this handler also runs for subrequests, which - * share the parent's pool and therefore its transaction; processing the - * logging phase there would run phase 5 and the audit log twice. - */ - if (r != r->main) { - return NGX_OK; - } - /* if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_POST && r->method != NGX_HTTP_HEAD) { diff --git a/src/ngx_http_modsecurity_module.c b/src/ngx_http_modsecurity_module.c index 1e55ade0..778af722 100644 --- a/src/ngx_http_modsecurity_module.c +++ b/src/ngx_http_modsecurity_module.c @@ -331,6 +331,12 @@ ngx_inline ngx_http_modsecurity_ctx_t * ngx_http_modsecurity_get_module_ctx(ngx_http_request_t *r) { ngx_http_modsecurity_ctx_t *ctx; + + /* subrequests share r->pool; the cleanup walk would find the parent's ctx */ + if (r != r->main) { + return NULL; + } + ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module); if (ctx == NULL) { /* diff --git a/tests/modsecurity-subrequest.t b/tests/modsecurity-subrequest.t index 0ce83168..44592cf4 100644 --- a/tests/modsecurity-subrequest.t +++ b/tests/modsecurity-subrequest.t @@ -1,7 +1,5 @@ #!/usr/bin/perl -# (C) Andrei Belov - # Tests for ModSecurity-nginx connector (subrequests). ############################################################################### @@ -21,7 +19,7 @@ use Test::Nginx; select STDERR; $| = 1; select STDOUT; $| = 1; -my $t = Test::Nginx->new()->has(qw/http auth_request/); +my $t = Test::Nginx->new()->has(qw/http auth_request ssi/); $t->write_file_expand('nginx.conf', <<'EOF'); @@ -40,15 +38,13 @@ http { server_name localhost; modsecurity on; - log_subrequest on; modsecurity_rules ' SecRuleEngine On - SecRule REQUEST_URI "@streq /index.html" "id:500,phase:2,log,auditlog,pass" SecRule RESPONSE_STATUS "@streq 204" "id:501,phase:3,log,auditlog,pass" SecRule RESPONSE_HEADERS:X-Main-Marker "@streq main" "id:502,phase:3,log,auditlog,pass" SecAuditEngine RelevantOnly SecAuditLogParts ABH - SecAuditLog %%TESTDIR%%/audit.txt + SecAuditLog %%TESTDIR%%/audit-auth.txt SecAuditLogType Serial SecAuditLogStorageDir %%TESTDIR%%/ '; @@ -62,13 +58,40 @@ http { add_header X-Main-Marker main; } } + + server { + listen 127.0.0.1:8080; + server_name ssi; + + log_subrequest on; + default_type text/plain; + + modsecurity on; + modsecurity_rules ' + SecRuleEngine On + SecRule REQUEST_HEADERS:X-Test "@streq ssi" "id:500,phase:1,log,auditlog,pass" + SecAuditEngine RelevantOnly + SecAuditLogParts ABH + SecAuditLog %%TESTDIR%%/audit-ssi.txt + SecAuditLogType Serial + SecAuditLogStorageDir %%TESTDIR%%/ + '; + + location = /ssi.html { + default_type text/html; + ssi on; + } + } } EOF $t->write_file('index.html', 'MAIN PAGE'); +$t->write_file('ssi.html', + 'BEFORE AFTER'); +$t->write_file('static.txt', 'INCLUDED-MARKER'); $t->run(); -$t->plan(5); +$t->plan(6); ############################################################################### @@ -77,33 +100,30 @@ my $r = http_get('/index.html'); like($r, qr/^HTTP.*200/, 'request with auth_request passes'); like($r, qr/MAIN PAGE/, 'main body delivered'); -my $audit = read_file($t->testdir() . '/audit.txt'); +my $audit = $t->read_file('audit-auth.txt'); like($audit, qr/\[id "502"\]/, 'main response headers inspected'); unlike($audit, qr/\[id "501"\]/, 'auth subrequest response not inspected as main response'); -# Each serial audit log record starts with a part-A section header of the -# form "------A--" on a line of its own, so counting those lines -# counts the records. The rule with id 500 makes the transaction relevant -# before the auth subrequest is even created, so a second, premature run of -# phase 5 (one per logged subrequest) shows up as an extra record here. +$r = http(<read_file('audit-ssi.txt'); -sub read_file { - my ($file) = @_; +# Each serial audit log record starts with a part-A section header of the +# form "------A--" on a line of its own. The SSI subrequest is +# created while the main response body is sent, after every ACCESS-phase +# handler has run, so rule 500 has made the transaction relevant by then and +# a phase 5 run for the logged subrequest would show up as an extra record. - local $/ = undef; - open my $fh, '<', $file - or return ''; - my $content = <$fh>; - close $fh; +my $records = () = $audit =~ /^---\w+---A--$/mg; - return $content; -} +is($records, 1, 'phase 5 ran once for the transaction'); ############################################################################### From 3ab4c22dfc93ced208807b858efdefefa0a18505 Mon Sep 17 00:00:00 2001 From: Tom Sommer Date: Fri, 13 Nov 2026 07:36:37 +0100 Subject: [PATCH 3/3] Keep subrequest output in memory for response body inspection SSI and addition subrequest output reaches the body filter through the postpone filter as part of the main response, but the copy filter decides whether to read file buffers into memory per request (ngx_http_copy_filter(): r->main_filter_need_in_memory || r->filter_need_in_memory, both of the subrequest). The connector only set filter_need_in_memory on the main request, so with sendfile on an included static file arrived as an in_file buffer with an empty pos..last, msc_append_response_body() got 0 bytes and the included content reached the client without phase-4 inspection. This predates the subrequest changes: on master the subrequest returned at ctx->processed before the flag was set. In the header filter, set filter_need_in_memory on a subrequest whose main request has a modsecurity context, then pass it through. This is done per subrequest rather than via main_filter_need_in_memory on the main request, because that flag is only copied into subrequests created afterwards. Test: SSI include of a static text file with sendfile on and a phase-4 rule matching content only present in the included file; it fails without this change and passes with it. --- src/ngx_http_modsecurity_header_filter.c | 12 ++++++++++++ tests/modsecurity-subrequest.t | 8 +++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/ngx_http_modsecurity_header_filter.c b/src/ngx_http_modsecurity_header_filter.c index 03b8764d..5a3349d0 100644 --- a/src/ngx_http_modsecurity_header_filter.c +++ b/src/ngx_http_modsecurity_header_filter.c @@ -422,6 +422,18 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r) /* XXX: if NOT_MODIFIED, do we need to process it at all? see xslt_header_filter() */ + /* + * subrequest output reaches the body filter as part of the main + * response; keep it in memory so that file buffers are inspected too + */ + if (r != r->main) { + if (ngx_http_modsecurity_get_module_ctx(r->main) != NULL) { + r->filter_need_in_memory = 1; + } + + return ngx_http_next_header_filter(r); + } + ctx = ngx_http_modsecurity_get_module_ctx(r); dd("header filter, recovering ctx: %p", ctx); diff --git a/tests/modsecurity-subrequest.t b/tests/modsecurity-subrequest.t index 44592cf4..295c8171 100644 --- a/tests/modsecurity-subrequest.t +++ b/tests/modsecurity-subrequest.t @@ -63,13 +63,17 @@ http { listen 127.0.0.1:8080; server_name ssi; + sendfile on; log_subrequest on; default_type text/plain; modsecurity on; modsecurity_rules ' SecRuleEngine On + SecResponseBodyAccess On + SecResponseBodyMimeType text/html SecRule REQUEST_HEADERS:X-Test "@streq ssi" "id:500,phase:1,log,auditlog,pass" + SecRule RESPONSE_BODY "@contains INCLUDED-MARKER" "id:503,phase:4,log,auditlog,pass" SecAuditEngine RelevantOnly SecAuditLogParts ABH SecAuditLog %%TESTDIR%%/audit-ssi.txt @@ -91,7 +95,7 @@ $t->write_file('ssi.html', 'BEFORE AFTER'); $t->write_file('static.txt', 'INCLUDED-MARKER'); $t->run(); -$t->plan(6); +$t->plan(7); ############################################################################### @@ -116,6 +120,8 @@ like($r, qr/BEFORE INCLUDED-MARKER AFTER/, 'ssi include delivered'); $audit = $t->read_file('audit-ssi.txt'); +like($audit, qr/\[id "503"\]/, 'included file inspected in phase 4'); + # Each serial audit log record starts with a part-A section header of the # form "------A--" on a line of its own. The SSI subrequest is # created while the main response body is sent, after every ACCESS-phase