From 2b54ecdcde9e5ce51a1da41f9c5f6ea26a0b0eb8 Mon Sep 17 00:00:00 2001 From: Tom Sommer Date: Sat, 19 Sep 2026 15:34:19 +0200 Subject: [PATCH 1/2] Skip the pool cleanup walk in get_module_ctx for ordinary requests The header and body filters run for every response on the server, including locations where modsecurity is off, and each call without a module context walked r->pool->cleanup looking for one. r->ctx is only discarded by ngx_http_internal_redirect(), which zeroes it in ngx_http_core_module.c:2652 and sets r->internal at :2663, by ngx_http_named_location() (:2726, r->internal at :2720) and by ngx_http_filter_finalize_request(), which zeroes it in ngx_http_special_response.c:551 and sets r->filter_finalize at :557. Subrequests get a fresh context array (ngx_http_core_module.c:2471) and have internal set too (:2577). The walk is now limited to those requests, matching ngx_http_realip_get_module_ctx() (ngx_http_realip_module.c:552), the function this fallback is modelled on. The filter finalize case is reachable from an ordinary, non internal request: the not modified filter calls ngx_http_filter_finalize_request() with a NULL module on a failed If-Match or If-Unmodified-Since precondition (ngx_http_not_modified_filter_module.c:67 and :74), which discards every module context. tests/modsecurity-internal-redirect.t covers it along with try_files and named location redirects. --- src/ngx_http_modsecurity_module.c | 10 +- tests/modsecurity-internal-redirect.t | 133 ++++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 tests/modsecurity-internal-redirect.t diff --git a/src/ngx_http_modsecurity_module.c b/src/ngx_http_modsecurity_module.c index d3d9624d..a257448f 100644 --- a/src/ngx_http_modsecurity_module.c +++ b/src/ngx_http_modsecurity_module.c @@ -326,11 +326,19 @@ ngx_http_modsecurity_get_module_ctx(ngx_http_request_t *r) { ngx_http_modsecurity_ctx_t *ctx; ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module); - if (ctx == NULL) { + if (ctx == NULL && (r->internal || r->filter_finalize)) { /* * refer /src/http/modules/ngx_http_realip_module.c * if module context was reset, the original address * can still be found in the cleanup handler + * + * r->ctx is only discarded by ngx_http_internal_redirect() and + * ngx_http_named_location(), which set r->internal, and by + * ngx_http_filter_finalize_request(), which sets r->filter_finalize; + * subrequests start with a fresh context array and have internal + * set as well. Every other request still has its context, so the + * cleanup list is not walked: the filters run for every response on + * the server, including locations where modsecurity is off. */ ngx_pool_cleanup_t *cln; for (cln = r->pool->cleanup; cln; cln = cln->next) { diff --git a/tests/modsecurity-internal-redirect.t b/tests/modsecurity-internal-redirect.t new file mode 100644 index 00000000..75287728 --- /dev/null +++ b/tests/modsecurity-internal-redirect.t @@ -0,0 +1,133 @@ +#!/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 (module context recovery after the request +# context has been discarded: internal redirects, named locations and +# filter finalization). + +############################################################################### + +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 rewrite/); + +$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 + SecRule ARGS:what "@streq bad" "id:91,phase:1,deny,status:403,log,auditlog" + SecRule RESPONSE_HEADERS:X-Fallback "@streq yes" "id:92,phase:3,log,auditlog,pass" + SecRule ARGS:what "@streq precond" "id:93,phase:1,log,auditlog,pass" + SecAuditEngine RelevantOnly + SecAuditLogParts ABH + SecAuditLog %%TESTDIR%%/audit.txt + SecAuditLogType Serial + SecAuditLogStorageDir %%TESTDIR%%/ + '; + + location / { + try_files $uri /fallback; + } + + location = /fallback { + internal; + add_header X-Fallback yes; + } + + location /named { + try_files $uri @named; + } + + location @named { + add_header X-Fallback yes; + return 200 "NAMED"; + } + + location /static { + } + } +} + +EOF + +$t->write_file('fallback', 'FALLBACK'); +mkdir($t->testdir() . '/static'); +$t->write_file('static/precond.html', 'PRECOND'); +$t->run(); +$t->plan(8); + +############################################################################### + +like(http_get('/missing?what=ok'), qr/FALLBACK/, 'try_files fallback served'); +like(http_get('/missing?what=bad'), qr/^HTTP.*403/, 'phase 1 deny before try_files'); +like(http_get('/named/missing?what=ok'), qr/NAMED/, 'named location served'); + +# a failed precondition makes the not modified filter call +# ngx_http_filter_finalize_request(), which discards the module contexts +# without setting r->internal + +like(http(<testdir(); + +my $audit = do { + local $/ = undef; + open my $fh, "<", "$d/audit.txt" + or die "could not open: $!"; + <$fh>; +}; + +like($audit, qr/\[id "92"\]/, 'phase 3 ran after internal redirect (ctx recovered)'); +like($audit, qr/\[id "91"\]/, 'deny audited'); +my @p3 = ($audit =~ /\[id "92"\]/g); +is(scalar @p3, 2, 'phase 3 logged once per redirected request'); +like($audit, qr/\[id "93"\]/, 'logging phase ran after filter finalization'); + +############################################################################### From b85a200f07078b88afdcfea5dcc716e2daf11af2 Mon Sep 17 00:00:00 2001 From: Tom Sommer Date: Fri, 13 Nov 2026 07:36:24 +0100 Subject: [PATCH 2/2] Walk the cleanup list unconditionally and cache the recovered context Review follow-up. The r->internal / r->filter_finalize guard assumed only nginx core clears r->ctx. lua-nginx-module's ngx.exec("@name") zeroes r->ctx itself (ngx_http_lua_handle_exec) and then calls ngx_http_named_location(), which sets r->internal only once it has found the location. On "could not find named location" or the redirect-cycle 500 the request is finalized with the context gone, internal = 0 and filter_finalize = 0, so the guard skipped the walk, phase 3 and msc_process_logging() never ran and the audit record was lost. Drop the guard: walk whenever the slot is empty, as before this branch. When the walk finds the transaction, store it back with ngx_http_set_ctx(), so the following calls for the same request (header filter, every body filter call, log handler, and the access handler of a redirect target) are a plain array read again until the next reset. A request without a transaction still walks on every call, as on master. Replace the stale realip comment and drop the claim that subrequests recovering the main transaction is correct; that is fixed separately in #394. Test changes: - prove recovery by counting a logged phase-1 rule in the error log: a request that lost its context gets a second transaction at the try_files target, which runs phase 1 again. Request data cannot tell the two apart: the new transaction is fed r->unparsed_uri, which an internal redirect does not change, so ARGS (and any TX state derived from them) are the same in both. - replace the deny-before-try_files case with a phase-1 deny followed by an error_page redirect; without recovery the error page request gets a second transaction and the deny is audited twice. - drop SecAuditLogStorageDir, which only applies to Concurrent. With get_module_ctx() reduced to the r->ctx lookup, tests 5-8 fail (phase-1 count 3/2, phase-3 count 1/2, deny records 2/1, no record for the 412). --- src/ngx_http_modsecurity_module.c | 14 +++----------- tests/modsecurity-internal-redirect.t | 27 ++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/ngx_http_modsecurity_module.c b/src/ngx_http_modsecurity_module.c index afa22954..01f697e9 100644 --- a/src/ngx_http_modsecurity_module.c +++ b/src/ngx_http_modsecurity_module.c @@ -332,24 +332,16 @@ ngx_http_modsecurity_get_module_ctx(ngx_http_request_t *r) { ngx_http_modsecurity_ctx_t *ctx; ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module); - if (ctx == NULL && (r->internal || r->filter_finalize)) { + if (ctx == NULL) { /* - * refer /src/http/modules/ngx_http_realip_module.c - * if module context was reset, the original address + * if module context was reset, the transaction * can still be found in the cleanup handler - * - * r->ctx is only discarded by ngx_http_internal_redirect() and - * ngx_http_named_location(), which set r->internal, and by - * ngx_http_filter_finalize_request(), which sets r->filter_finalize; - * subrequests start with a fresh context array and have internal - * set as well. Every other request still has its context, so the - * cleanup list is not walked: the filters run for every response on - * the server, including locations where modsecurity is off. */ ngx_pool_cleanup_t *cln; for (cln = r->pool->cleanup; cln; cln = cln->next) { if (cln->handler == ngx_http_modsecurity_cleanup) { ctx = cln->data; + ngx_http_set_ctx(r, ctx, ngx_http_modsecurity_module); break; } } diff --git a/tests/modsecurity-internal-redirect.t b/tests/modsecurity-internal-redirect.t index 75287728..3fec27c7 100644 --- a/tests/modsecurity-internal-redirect.t +++ b/tests/modsecurity-internal-redirect.t @@ -57,6 +57,7 @@ http { modsecurity on; modsecurity_rules ' SecRuleEngine On + SecRule ARGS:what "@streq ok" "id:90,phase:1,pass,log" SecRule ARGS:what "@streq bad" "id:91,phase:1,deny,status:403,log,auditlog" SecRule RESPONSE_HEADERS:X-Fallback "@streq yes" "id:92,phase:3,log,auditlog,pass" SecRule ARGS:what "@streq precond" "id:93,phase:1,log,auditlog,pass" @@ -64,9 +65,12 @@ http { SecAuditLogParts ABH SecAuditLog %%TESTDIR%%/audit.txt SecAuditLogType Serial - SecAuditLogStorageDir %%TESTDIR%%/ '; + error_log %%TESTDIR%%/msc.log info; + + error_page 403 /denied.html; + location / { try_files $uri /fallback; } @@ -76,6 +80,10 @@ http { add_header X-Fallback yes; } + location = /denied.html { + internal; + } + location /named { try_files $uri @named; } @@ -93,6 +101,7 @@ http { EOF $t->write_file('fallback', 'FALLBACK'); +$t->write_file('denied.html', 'DENIED'); mkdir($t->testdir() . '/static'); $t->write_file('static/precond.html', 'PRECOND'); $t->run(); @@ -101,8 +110,9 @@ $t->plan(8); ############################################################################### like(http_get('/missing?what=ok'), qr/FALLBACK/, 'try_files fallback served'); -like(http_get('/missing?what=bad'), qr/^HTTP.*403/, 'phase 1 deny before try_files'); like(http_get('/named/missing?what=ok'), qr/NAMED/, 'named location served'); +like(http_get('/missing?what=bad'), qr/^HTTP.*403.*DENIED/s, + 'phase 1 deny, error_page served'); # a failed precondition makes the not modified filter call # ngx_http_filter_finalize_request(), which discards the module contexts @@ -124,10 +134,17 @@ my $audit = do { <$fh>; }; -like($audit, qr/\[id "92"\]/, 'phase 3 ran after internal redirect (ctx recovered)'); -like($audit, qr/\[id "91"\]/, 'deny audited'); +my $log = $t->read_file('msc.log'); + +# a request whose context is lost after the redirect gets a second +# transaction, which runs phase 1 again + +my @p1 = ($log =~ /\[id "90"\]/g); +is(scalar @p1, 2, 'phase 1 ran once per redirected request (ctx recovered)'); my @p3 = ($audit =~ /\[id "92"\]/g); -is(scalar @p3, 2, 'phase 3 logged once per redirected request'); +is(scalar @p3, 2, 'phase 3 ran once per redirected request'); +my @deny = ($audit =~ /\[id "91"\]/g); +is(scalar @deny, 1, 'deny audited once across error_page redirect'); like($audit, qr/\[id "93"\]/, 'logging phase ran after filter finalization'); ###############################################################################