From c30374b01d8d2d3302d611aa4ae71eeb2362c24d Mon Sep 17 00:00:00 2001 From: jaysee Date: Fri, 25 Sep 2026 19:43:51 +0200 Subject: [PATCH 1/5] Share the parent RulesSet with blocks that declare no rules Every server, location, if and limit_except block created its own RulesSet, and merge_conf copied the whole parent ruleset into it, even when the block has no ModSecurity directive. With the CRS loaded once at http level this costs tens of KB per block: hundreds of MB in the master and every worker on configs with a few hundred virtual hosts, twice that during a reload, and a slower configuration parse. A block now only gets its own RulesSet on its first modsecurity_rules* directive. merge_conf points a block without rules to its parent's RulesSet instead of copying it, and gives an empty one to a parent that has none. Only the block that created a RulesSet frees it. On a config with ~260 virtual hosts (14k blocks), nginx -t goes from 888 MB / 2.2 s to 325 MB / 1.1 s. See #92. Adds tests/modsecurity-config-inherit.t for rules inheritance across nested locations, if blocks, sibling locations and servers. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/ngx_http_modsecurity_module.c | 69 +++++++-- tests/modsecurity-config-inherit.t | 221 +++++++++++++++++++++++++++++ 2 files changed, 279 insertions(+), 11 deletions(-) create mode 100644 tests/modsecurity-config-inherit.t diff --git a/src/ngx_http_modsecurity_module.c b/src/ngx_http_modsecurity_module.c index d3d9624d..60c78b24 100644 --- a/src/ngx_http_modsecurity_module.c +++ b/src/ngx_http_modsecurity_module.c @@ -343,6 +343,38 @@ ngx_http_modsecurity_get_module_ctx(ngx_http_request_t *r) return ctx; } +/* + * A block only gets its own RulesSet once it declares rules itself; blocks + * without any share their parent's (see merge_conf). Creating one per block + * and merging the parent into it copied the whole ruleset into every server, + * location, if and limit_except block, tens of KB each with the CRS. + */ +static ngx_int_t +ngx_http_modsecurity_own_rules_set(ngx_conf_t *cf, ngx_http_modsecurity_conf_t *mcf) +{ + ngx_pool_cleanup_t *cln; + + if (mcf->rules_set != NULL) { + return NGX_OK; + } + + cln = ngx_pool_cleanup_add(cf->pool, 0); + if (cln == NULL) { + return NGX_ERROR; + } + + mcf->rules_set = msc_create_rules_set(); + if (mcf->rules_set == NULL) { + return NGX_ERROR; + } + + cln->handler = ngx_http_modsecurity_cleanup_rules; + cln->data = mcf; + + return NGX_OK; +} + + char * ngx_conf_set_rules(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { @@ -361,6 +393,10 @@ ngx_conf_set_rules(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } + if (ngx_http_modsecurity_own_rules_set(cf, mcf) != NGX_OK) { + return NGX_CONF_ERROR; + } + old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool); res = msc_rules_add(mcf->rules_set, rules, &error); ngx_http_modsecurity_pcre_malloc_done(old_pool); @@ -395,6 +431,10 @@ ngx_conf_set_rules_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } + if (ngx_http_modsecurity_own_rules_set(cf, mcf) != NGX_OK) { + return NGX_CONF_ERROR; + } + old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool); res = msc_rules_add_file(mcf->rules_set, rules_set, &error); ngx_http_modsecurity_pcre_malloc_done(old_pool); @@ -434,6 +474,10 @@ ngx_conf_set_rules_remote(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } + if (ngx_http_modsecurity_own_rules_set(cf, mcf) != NGX_OK) { + return NGX_CONF_ERROR; + } + old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool); res = msc_rules_add_remote(mcf->rules_set, rules_remote_key, rules_remote_server, &error); ngx_http_modsecurity_pcre_malloc_done(old_pool); @@ -697,7 +741,6 @@ ngx_http_modsecurity_init_main_conf(ngx_conf_t *cf, void *conf) static void * ngx_http_modsecurity_create_conf(ngx_conf_t *cf) { - ngx_pool_cleanup_t *cln; ngx_http_modsecurity_conf_t *conf; conf = (ngx_http_modsecurity_conf_t *) ngx_pcalloc(cf->pool, @@ -720,7 +763,7 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf) */ conf->enable = NGX_CONF_UNSET; - conf->rules_set = msc_create_rules_set(); + /* rules_set stays NULL until a rules directive, see own_rules_set() */ conf->pool = cf->pool; conf->transaction_id = NGX_CONF_UNSET_PTR; conf->use_error_log = NGX_CONF_UNSET; @@ -728,15 +771,6 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf) conf->sanity_checks_enabled = NGX_CONF_UNSET; #endif - cln = ngx_pool_cleanup_add(cf->pool, 0); - if (cln == NULL) { - dd("failed to create the ModSecurity configuration cleanup"); - return NGX_CONF_ERROR; - } - - cln->handler = ngx_http_modsecurity_cleanup_rules; - cln->data = conf; - dd ("conf created at: '%p'", conf); return conf; @@ -768,6 +802,19 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(c->sanity_checks_enabled, p->sanity_checks_enabled, 0); #endif + /* transactions need a RulesSet, even an empty one */ + if (p->rules_set == NULL + && ngx_http_modsecurity_own_rules_set(cf, p) != NGX_OK) + { + return NGX_CONF_ERROR; + } + + /* no rules of its own: share the parent's instead of copying it */ + if (c->rules_set == NULL) { + c->rules_set = p->rules_set; + return NGX_CONF_OK; + } + #if defined(MODSECURITY_DDEBUG) && (MODSECURITY_DDEBUG) dd("PARENT RULES"); msc_rules_dump(p->rules_set); diff --git a/tests/modsecurity-config-inherit.t b/tests/modsecurity-config-inherit.t new file mode 100644 index 00000000..70feebff --- /dev/null +++ b/tests/modsecurity-config-inherit.t @@ -0,0 +1,221 @@ +#!/usr/bin/perl + +# Tests for ModSecurity-nginx connector (rules inheritance across blocks +# that declare no rules of their own: nested locations, if blocks, siblings). +# Content is proxied, not "return"ed: return runs in the rewrite phase, before +# ModSecurity's access phase handler. + +############################################################################### + +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 proxy rewrite/); + +$t->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + modsecurity on; + modsecurity_rules ' + SecRuleEngine On + SecRule ARGS "@streq http" "id:1,phase:1,deny,status:403" + '; + + server { + listen 127.0.0.1:%%PORT_8080%%; + server_name localhost; + + location / { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + + location /outer { + location /outer/inner { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + + location /if { + if ($arg_x) { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + + location /own { + modsecurity_rules ' + SecRule ARGS "@streq own" "id:2,phase:1,deny,status:403" + '; + + location /own/nested { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + + location /sibling { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + + location /off { + modsecurity off; + + location /off/on { + modsecurity on; + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + } + + server { + listen 127.0.0.1:%%PORT_8081%%; + server_name localhost; + + modsecurity_rules ' + SecRule ARGS "@streq server" "id:3,phase:1,deny,status:403" + '; + + location / { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + + location /engine-off { + modsecurity_rules ' + SecRuleEngine Off + '; + + location /engine-off/nested { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + } + + server { + listen 127.0.0.1:%%PORT_8082%%; + server_name localhost; + + location / { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + } +} +EOF + +$t->run_daemon(\&http_daemon); +$t->run()->waitforsocket('127.0.0.1:' . port(8090)); + +$t->plan(22); + +############################################################################### + +my ($p1, $p2) = (port(8081), port(8082)); + +# http level rules, inherited by blocks without rules of their own +like(http_get('/?a=http'), qr/403/, 'http rules, location'); +like(http_get('/?a=clean'), qr/TEST-OK-IF-YOU-SEE-THIS/, 'http rules, clean'); +like(http_get('/outer?a=http'), qr/403/, 'http rules, outer location'); +like(http_get('/outer/inner?a=http'), qr/403/, 'http rules, nested location'); +like(http_get('/if?x=1&a=http'), qr/403/, 'http rules, if block'); +like(http_get('/if?a=http'), qr/403/, 'http rules, location with if'); + +# location rules add to the http ones, and are inherited by nested blocks +like(http_get('/own?a=own'), qr/403/, 'location rules'); +like(http_get('/own?a=http'), qr/403/, 'location rules, http rules kept'); +like(http_get('/own/nested?a=own'), qr/403/, 'location rules, nested'); +like(http_get('/own/nested?a=http'), qr/403/, 'location rules, nested, http'); + +# rules of a block don't leak into its siblings nor its parent +like(http_get('/sibling?a=own'), qr/TEST-OK-IF-YOU-SEE-THIS/, + 'no leak to sibling location'); +like(http_get('/?a=own'), qr/TEST-OK-IF-YOU-SEE-THIS/, 'no leak to parent'); +like(http_get('/?a=server'), qr/TEST-OK-IF-YOU-SEE-THIS/, + 'no leak from another server'); + +# modsecurity off/on is still per block +like(http_get('/off?a=http'), qr/TEST-OK-IF-YOU-SEE-THIS/, 'modsecurity off'); +like(http_get('/off/on?a=http'), qr/403/, 'modsecurity on again, nested'); + +# server level rules +like(http_get_port('/?a=server', $p1), qr/403/, 'server rules'); +like(http_get_port('/?a=http', $p1), qr/403/, 'server rules, http rules kept'); +like(http_get_port('/engine-off?a=server', $p1), qr/TEST-OK-IF-YOU-SEE-THIS/, + 'SecRuleEngine Off'); +like(http_get_port('/engine-off/nested?a=server', $p1), + qr/TEST-OK-IF-YOU-SEE-THIS/, 'SecRuleEngine Off, nested'); + +# server without rules of its own +like(http_get_port('/?a=http', $p2), qr/403/, 'server without rules, http'); +like(http_get_port('/?a=server', $p2), qr/TEST-OK-IF-YOU-SEE-THIS/, + 'server without rules, clean'); +like(http_get_port('/?a=own', $p2), qr/TEST-OK-IF-YOU-SEE-THIS/, + 'server without rules, no leak'); + +############################################################################### + +sub http_get_port { + my ($uri, $port) = @_; + return http(< '127.0.0.1:' . $port); +GET $uri HTTP/1.0 +Host: localhost + +EOF +} + +sub http_daemon { + my $server = IO::Socket::INET->new( + Proto => 'tcp', + LocalHost => '127.0.0.1:' . port(8090), + 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 $headers = ''; + + while (<$client>) { + $headers .= $_; + last if (/^\x0d?\x0a?$/); + } + + print $client <<'EOF'; +HTTP/1.1 200 OK +Connection: close + +EOF + print $client "TEST-OK-IF-YOU-SEE-THIS" + unless $headers =~ /^HEAD/i; + + close $client; + } +} + +############################################################################### From 52cde508ef4346858072751b71b827dd020d4b91 Mon Sep 17 00:00:00 2001 From: jaysee Date: Sat, 26 Sep 2026 12:29:52 +0200 Subject: [PATCH 2/5] tests: cover modsecurity_rules_file in the inheritance test The lazy RulesSet creation also runs from modsecurity_rules_file: check that rules loaded from a file in a location apply there and in nested locations, keep the http level rules, and don't leak to a sibling. Co-Authored-By: Claude Opus 5.5 (1M context) --- tests/modsecurity-config-inherit.t | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/modsecurity-config-inherit.t b/tests/modsecurity-config-inherit.t index 70feebff..7e34a235 100644 --- a/tests/modsecurity-config-inherit.t +++ b/tests/modsecurity-config-inherit.t @@ -75,6 +75,15 @@ http { proxy_pass http://127.0.0.1:%%PORT_8090%%; } + location /file { + modsecurity_rules_file %%TESTDIR%%/rules.conf; + + location /file/nested { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + location /sibling { proxy_pass http://127.0.0.1:%%PORT_8090%%; } @@ -125,10 +134,13 @@ http { } EOF +$t->write_file('rules.conf', + 'SecRule ARGS "@streq file" "id:4,phase:1,deny,status:403"'); + $t->run_daemon(\&http_daemon); $t->run()->waitforsocket('127.0.0.1:' . port(8090)); -$t->plan(22); +$t->plan(26); ############################################################################### @@ -148,10 +160,17 @@ like(http_get('/own?a=http'), qr/403/, 'location rules, http rules kept'); like(http_get('/own/nested?a=own'), qr/403/, 'location rules, nested'); like(http_get('/own/nested?a=http'), qr/403/, 'location rules, nested, http'); +# same with modsecurity_rules_file +like(http_get('/file?a=file'), qr/403/, 'location rules file'); +like(http_get('/file?a=http'), qr/403/, 'location rules file, http rules kept'); +like(http_get('/file/nested?a=file'), qr/403/, 'location rules file, nested'); + # rules of a block don't leak into its siblings nor its parent like(http_get('/sibling?a=own'), qr/TEST-OK-IF-YOU-SEE-THIS/, 'no leak to sibling location'); like(http_get('/?a=own'), qr/TEST-OK-IF-YOU-SEE-THIS/, 'no leak to parent'); +like(http_get('/sibling?a=file'), qr/TEST-OK-IF-YOU-SEE-THIS/, + 'no leak from rules file'); like(http_get('/?a=server'), qr/TEST-OK-IF-YOU-SEE-THIS/, 'no leak from another server'); From 532121579621d3808b1166f8c128c503fead4dca Mon Sep 17 00:00:00 2001 From: jC Date: Tue, 17 Nov 2026 10:03:20 +0100 Subject: [PATCH 3/5] Address review: empty RulesSet fallback, single rules helper, more tests - create_ctx: a loc conf that never went through merge_conf (created by another module) has no RulesSet any more; fall back to an empty one kept in the main conf instead of passing NULL to libmodsecurity. - merge_conf: drop the redundant NULL test, and document that the field merges are kept above the early return of the sharing path. - The three rules directives go through one helper (own the RulesSet, add the rules, count them), so a new directive can't forget to own one. - New test, http block without rules: server and location rules (inline and file) merged on an empty RulesSet, nested locations, limit_except (GET and POST) and isolation between blocks. --- src/ngx_http_modsecurity_common.h | 1 + src/ngx_http_modsecurity_module.c | 177 +++++++++++------ tests/modsecurity-config-inherit-norules.t | 218 +++++++++++++++++++++ 3 files changed, 333 insertions(+), 63 deletions(-) create mode 100644 tests/modsecurity-config-inherit-norules.t diff --git a/src/ngx_http_modsecurity_common.h b/src/ngx_http_modsecurity_common.h index 9f79b5ba..d5769d93 100644 --- a/src/ngx_http_modsecurity_common.h +++ b/src/ngx_http_modsecurity_common.h @@ -106,6 +106,7 @@ typedef struct { typedef struct { void *pool; ModSecurity *modsec; + void *empty_rules_set; ngx_uint_t rules_inline; ngx_uint_t rules_file; ngx_uint_t rules_remote; diff --git a/src/ngx_http_modsecurity_module.c b/src/ngx_http_modsecurity_module.c index 60c78b24..704a6555 100644 --- a/src/ngx_http_modsecurity_module.c +++ b/src/ngx_http_modsecurity_module.c @@ -36,6 +36,7 @@ static void *ngx_http_modsecurity_create_conf(ngx_conf_t *cf); static char *ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child); static void ngx_http_modsecurity_cleanup_instance(void *data); static void ngx_http_modsecurity_cleanup_rules(void *data); +static void ngx_http_modsecurity_cleanup_empty_rules(void *data); /* @@ -275,6 +276,7 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r) ngx_http_modsecurity_ctx_t *ctx; ngx_http_modsecurity_conf_t *mcf; ngx_http_modsecurity_main_conf_t *mmcf; + void *rules_set; ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_modsecurity_ctx_t)); if (ctx == NULL) @@ -286,16 +288,22 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r) mmcf = ngx_http_get_module_main_conf(r, ngx_http_modsecurity_module); mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module); - dd("creating transaction with the following rules: '%p' -- ms: '%p'", mcf->rules_set, mmcf->modsec); + /* + * A loc conf that never went through merge_conf (created by another + * module) has no RulesSet: use the empty one, as before it was lazy. + */ + rules_set = mcf->rules_set != NULL ? mcf->rules_set : mmcf->empty_rules_set; + + dd("creating transaction with the following rules: '%p' -- ms: '%p'", rules_set, mmcf->modsec); if (mcf->transaction_id) { if (ngx_http_complex_value(r, mcf->transaction_id, &s) != NGX_OK) { return NGX_CONF_ERROR; } - ctx->modsec_transaction = msc_new_transaction_with_id(mmcf->modsec, mcf->rules_set, (char *) s.data, r->connection->log); + ctx->modsec_transaction = msc_new_transaction_with_id(mmcf->modsec, rules_set, (char *) s.data, r->connection->log); } else { - ctx->modsec_transaction = msc_new_transaction(mmcf->modsec, mcf->rules_set, r->connection->log); + ctx->modsec_transaction = msc_new_transaction(mmcf->modsec, rules_set, r->connection->log); } dd("transaction created"); @@ -375,91 +383,114 @@ ngx_http_modsecurity_own_rules_set(ngx_conf_t *cf, ngx_http_modsecurity_conf_t * } -char * -ngx_conf_set_rules(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +typedef int (*ngx_http_modsecurity_add_pt)(void *rules_set, const char *a, + const char *b, const char **error); + + +static int +ngx_http_modsecurity_add_inline(void *rules_set, const char *rules, + const char *unused, const char **error) { - int res; - char *rules; - ngx_str_t *value; - const char *error; - ngx_pool_t *old_pool; - ngx_http_modsecurity_conf_t *mcf = conf; - ngx_http_modsecurity_main_conf_t *mmcf; + return msc_rules_add(rules_set, rules, error); +} - value = cf->args->elts; - rules = ngx_str_to_char(value[1], cf->pool); - if (rules == (char *)-1) { - return NGX_CONF_ERROR; - } +static int +ngx_http_modsecurity_add_file(void *rules_set, const char *file, + const char *unused, const char **error) +{ + return msc_rules_add_file(rules_set, file, error); +} + + +static int +ngx_http_modsecurity_add_remote(void *rules_set, const char *key, + const char *server, const char **error) +{ + return msc_rules_add_remote(rules_set, key, server, error); +} + + +/* + * Common part of the three rules directives: make sure the block owns its + * RulesSet (see own_rules_set), add the rules, and count them. Going through + * this single path means a new rules directive can't forget to own one. + */ +static char * +ngx_http_modsecurity_add_rules(ngx_conf_t *cf, ngx_http_modsecurity_conf_t *mcf, + ngx_http_modsecurity_add_pt add, const char *a, const char *b, + ngx_uint_t *counter) +{ + int res; + const char *error; + ngx_pool_t *old_pool; if (ngx_http_modsecurity_own_rules_set(cf, mcf) != NGX_OK) { return NGX_CONF_ERROR; } old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool); - res = msc_rules_add(mcf->rules_set, rules, &error); + res = add(mcf->rules_set, a, b, &error); ngx_http_modsecurity_pcre_malloc_done(old_pool); if (res < 0) { - dd("Failed to load the rules: '%s' - reason: '%s'", rules, error); + dd("Failed to load the rules: '%s' - reason: '%s'", a, error); return strdup(error); } - mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_modsecurity_module); - mmcf->rules_inline += res; + *counter += res; return NGX_CONF_OK; } char * -ngx_conf_set_rules_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +ngx_conf_set_rules(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { - int res; - char *rules_set; + char *rules; ngx_str_t *value; - const char *error; - ngx_pool_t *old_pool; - ngx_http_modsecurity_conf_t *mcf = conf; ngx_http_modsecurity_main_conf_t *mmcf; value = cf->args->elts; - rules_set = ngx_str_to_char(value[1], cf->pool); + rules = ngx_str_to_char(value[1], cf->pool); - if (rules_set == (char *)-1) { + if (rules == (char *)-1) { return NGX_CONF_ERROR; } - if (ngx_http_modsecurity_own_rules_set(cf, mcf) != NGX_OK) { - return NGX_CONF_ERROR; - } + mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_modsecurity_module); - old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool); - res = msc_rules_add_file(mcf->rules_set, rules_set, &error); - ngx_http_modsecurity_pcre_malloc_done(old_pool); + return ngx_http_modsecurity_add_rules(cf, conf, + ngx_http_modsecurity_add_inline, rules, NULL, &mmcf->rules_inline); +} - if (res < 0) { - dd("Failed to load the rules from: '%s' - reason: '%s'", rules_set, error); - return strdup(error); + +char * +ngx_conf_set_rules_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ + char *rules_set; + ngx_str_t *value; + ngx_http_modsecurity_main_conf_t *mmcf; + + value = cf->args->elts; + rules_set = ngx_str_to_char(value[1], cf->pool); + + if (rules_set == (char *)-1) { + return NGX_CONF_ERROR; } mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_modsecurity_module); - mmcf->rules_file += res; - return NGX_CONF_OK; + return ngx_http_modsecurity_add_rules(cf, conf, + ngx_http_modsecurity_add_file, rules_set, NULL, &mmcf->rules_file); } char * ngx_conf_set_rules_remote(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { - int res; ngx_str_t *value; - const char *error; const char *rules_remote_key, *rules_remote_server; - ngx_pool_t *old_pool; - ngx_http_modsecurity_conf_t *mcf = conf; ngx_http_modsecurity_main_conf_t *mmcf; value = cf->args->elts; @@ -474,23 +505,11 @@ ngx_conf_set_rules_remote(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } - if (ngx_http_modsecurity_own_rules_set(cf, mcf) != NGX_OK) { - return NGX_CONF_ERROR; - } - - old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool); - res = msc_rules_add_remote(mcf->rules_set, rules_remote_key, rules_remote_server, &error); - ngx_http_modsecurity_pcre_malloc_done(old_pool); - - if (res < 0) { - dd("Failed to load the rules from: '%s' - reason: '%s'", rules_remote_server, error); - return strdup(error); - } - mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_modsecurity_module); - mmcf->rules_remote += res; - return NGX_CONF_OK; + return ngx_http_modsecurity_add_rules(cf, conf, + ngx_http_modsecurity_add_remote, rules_remote_key, + rules_remote_server, &mmcf->rules_remote); } @@ -714,6 +733,20 @@ ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf) msc_set_connector_info(conf->modsec, MODSECURITY_NGINX_WHOAMI); msc_set_log_cb(conf->modsec, ngx_http_modsecurity_log); + /* fallback for loc confs without a RulesSet, see create_ctx */ + cln = ngx_pool_cleanup_add(cf->pool, 0); + if (cln == NULL) { + return NGX_CONF_ERROR; + } + + conf->empty_rules_set = msc_create_rules_set(); + if (conf->empty_rules_set == NULL) { + return NGX_CONF_ERROR; + } + + cln->handler = ngx_http_modsecurity_cleanup_empty_rules; + cln->data = conf; + dd ("main conf created at: '%p', instance is: '%p'", conf, conf->modsec); return conf; @@ -802,19 +835,23 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(c->sanity_checks_enabled, p->sanity_checks_enabled, 0); #endif + /* + * All the field merges are above on purpose: the sharing below returns + * early, so anything merged after it would be skipped for most blocks. + */ + /* transactions need a RulesSet, even an empty one */ - if (p->rules_set == NULL - && ngx_http_modsecurity_own_rules_set(cf, p) != NGX_OK) - { + if (ngx_http_modsecurity_own_rules_set(cf, p) != NGX_OK) { return NGX_CONF_ERROR; } - /* no rules of its own: share the parent's instead of copying it */ if (c->rules_set == NULL) { + /* no rules of its own: share the parent's instead of copying it */ c->rules_set = p->rules_set; return NGX_CONF_OK; } + /* the child declared rules: it gets the parent's merged into its own */ #if defined(MODSECURITY_DDEBUG) && (MODSECURITY_DDEBUG) dd("PARENT RULES"); msc_rules_dump(p->rules_set); @@ -851,6 +888,20 @@ ngx_http_modsecurity_cleanup_instance(void *data) } +static void +ngx_http_modsecurity_cleanup_empty_rules(void *data) +{ + ngx_pool_t *old_pool; + ngx_http_modsecurity_main_conf_t *mmcf; + + mmcf = (ngx_http_modsecurity_main_conf_t *) data; + + old_pool = ngx_http_modsecurity_pcre_malloc_init(mmcf->pool); + msc_rules_cleanup(mmcf->empty_rules_set); + ngx_http_modsecurity_pcre_malloc_done(old_pool); +} + + static void ngx_http_modsecurity_cleanup_rules(void *data) { diff --git a/tests/modsecurity-config-inherit-norules.t b/tests/modsecurity-config-inherit-norules.t new file mode 100644 index 00000000..e03788d1 --- /dev/null +++ b/tests/modsecurity-config-inherit-norules.t @@ -0,0 +1,218 @@ +#!/usr/bin/perl + +# Tests for ModSecurity-nginx connector, rules inheritance when the http block +# declares no rules: server and location blocks with rules get them merged on +# top of an empty RulesSet, blocks below them (nested, limit_except) share it. +# Content is proxied, not "return"ed: return runs in the rewrite phase, before +# ModSecurity's access phase handler. + +############################################################################### + +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 proxy rewrite/); + + +$t->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + modsecurity on; + + server { + listen 127.0.0.1:%%PORT_8080%%; + server_name localhost; + + location / { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + + location /own { + modsecurity_rules ' + SecRuleEngine On + SecRule ARGS "@streq own" "id:1,phase:1,deny,status:403" + '; + + location /own/nested { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + + location /own/limit { + limit_except GET { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + + location /file { + modsecurity_rules_file %%TESTDIR%%/rules.conf; + + location /file/nested { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + } + + server { + listen 127.0.0.1:%%PORT_8081%%; + server_name localhost; + + modsecurity_rules ' + SecRuleEngine On + SecRule ARGS "@streq server" "id:3,phase:1,deny,status:403" + '; + + location / { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + + location /nested { + location /nested/inner { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + + location /limit { + limit_except GET { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + } + + server { + listen 127.0.0.1:%%PORT_8082%%; + server_name localhost; + + location / { + proxy_pass http://127.0.0.1:%%PORT_8090%%; + } + } +} +EOF + +$t->write_file('rules.conf', + "SecRuleEngine On\n" + . 'SecRule ARGS "@streq file" "id:4,phase:1,deny,status:403"'); + +$t->run_daemon(\&http_daemon); +$t->run()->waitforsocket('127.0.0.1:' . port(8090)); + +$t->plan(18); + +############################################################################### + +my ($p1, $p2) = (port(8081), port(8082)); +my $ok = qr/TEST-OK-IF-YOU-SEE-THIS/; + +# no rules anywhere above: requests are served, with an empty RulesSet +like(http_get('/?a=own'), $ok, 'no rules, location'); +like(http_get_port('/?a=own', $p2), $ok, 'no rules, server'); + +# location rules, on top of an empty http block +like(http_get('/own?a=own'), qr/403/, 'location rules'); +like(http_get('/own?a=clean'), $ok, 'location rules, clean'); +like(http_get('/own/nested?a=own'), qr/403/, 'location rules, nested'); +like(http_get('/own/limit?a=own'), qr/403/, 'location rules, limit_except, GET'); +like(http_post('/own/limit?a=own'), qr/403/, 'location rules, limit_except, POST'); +like(http_get('/?a=file'), $ok, 'no leak to parent location'); + +like(http_get('/file?a=file'), qr/403/, 'location rules file'); +like(http_get('/file/nested?a=file'), qr/403/, 'location rules file, nested'); +like(http_get('/file?a=own'), $ok, 'location rules file, no leak from sibling'); + +# server rules, on top of an empty http block +like(http_get_port('/?a=server', $p1), qr/403/, 'server rules'); +like(http_get_port('/?a=clean', $p1), $ok, 'server rules, clean'); +like(http_get_port('/nested/inner?a=server', $p1), qr/403/, 'server rules, nested'); +like(http_get_port('/limit?a=server', $p1), qr/403/, 'server rules, limit_except, GET'); +like(http_post_port('/limit?a=server', $p1), qr/403/, 'server rules, limit_except, POST'); + +# the server without rules is not affected by the others +like(http_get_port('/?a=server', $p2), $ok, 'server without rules, no leak'); +like(http_get('/own?a=server'), $ok, 'location rules, no leak from other server'); + +############################################################################### + +sub http_get_port { + my ($uri, $port) = @_; + return http(< '127.0.0.1:' . $port); +GET $uri HTTP/1.0 +Host: localhost + +EOF +} + +sub http_post { + return http_post_port($_[0], port(8080)); +} + +sub http_post_port { + my ($uri, $port) = @_; + return http(< '127.0.0.1:' . $port); +POST $uri HTTP/1.0 +Host: localhost +Content-Length: 0 + +EOF +} + +sub http_daemon { + my $server = IO::Socket::INET->new( + Proto => 'tcp', + LocalHost => '127.0.0.1:' . port(8090), + 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 $headers = ''; + + while (<$client>) { + $headers .= $_; + last if (/^\x0d?\x0a?$/); + } + + print $client <<'EOF'; +HTTP/1.1 200 OK +Connection: close + +EOF + print $client "TEST-OK-IF-YOU-SEE-THIS" + unless $headers =~ /^HEAD/i; + + close $client; + } +} + +############################################################################### From e8e74abd79f7e24883a4955b0d17676e4e75be5a Mon Sep 17 00:00:00 2001 From: jC Date: Tue, 17 Nov 2026 10:17:06 +0100 Subject: [PATCH 4/5] create_main_conf: return NULL on allocation failure ngx_http_block() only tests the result against NULL, NGX_CONF_ERROR would be stored as the main conf. Fixes the two failure branches of the empty RulesSet fallback added in the previous commit. Co-Authored-By: Claude Sonnet 5.5 --- src/ngx_http_modsecurity_module.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ngx_http_modsecurity_module.c b/src/ngx_http_modsecurity_module.c index 704a6555..5ee0d6d1 100644 --- a/src/ngx_http_modsecurity_module.c +++ b/src/ngx_http_modsecurity_module.c @@ -736,12 +736,12 @@ ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf) /* fallback for loc confs without a RulesSet, see create_ctx */ cln = ngx_pool_cleanup_add(cf->pool, 0); if (cln == NULL) { - return NGX_CONF_ERROR; + return NULL; } conf->empty_rules_set = msc_create_rules_set(); if (conf->empty_rules_set == NULL) { - return NGX_CONF_ERROR; + return NULL; } cln->handler = ngx_http_modsecurity_cleanup_empty_rules; From 49ec78cd8bf93440516bc553df8026e280124243 Mon Sep 17 00:00:00 2001 From: jC Date: Tue, 17 Nov 2026 10:29:54 +0100 Subject: [PATCH 5/5] rules helper: reference the unused argument of the inline and file adapters Silences the SonarCloud S1172 findings on the PR (the adapters share a signature with the remote one, which takes two arguments). Co-Authored-By: Claude Sonnet 5.5 --- src/ngx_http_modsecurity_module.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ngx_http_modsecurity_module.c b/src/ngx_http_modsecurity_module.c index 5ee0d6d1..77551c29 100644 --- a/src/ngx_http_modsecurity_module.c +++ b/src/ngx_http_modsecurity_module.c @@ -389,16 +389,20 @@ typedef int (*ngx_http_modsecurity_add_pt)(void *rules_set, const char *a, static int ngx_http_modsecurity_add_inline(void *rules_set, const char *rules, - const char *unused, const char **error) + const char *b, const char **error) { + (void) b; /* only the remote variant takes a second argument */ + return msc_rules_add(rules_set, rules, error); } static int ngx_http_modsecurity_add_file(void *rules_set, const char *file, - const char *unused, const char **error) + const char *b, const char **error) { + (void) b; /* only the remote variant takes a second argument */ + return msc_rules_add_file(rules_set, file, error); }