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 d3d9624d..77551c29 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"); @@ -343,15 +351,108 @@ 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; +} + + +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 *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 *b, const char **error) +{ + (void) b; /* only the remote variant takes a second argument */ + + 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 = 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'", a, error); + return strdup(error); + } + + *counter += res; + + return NGX_CONF_OK; +} + + char * ngx_conf_set_rules(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { - 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; value = cf->args->elts; @@ -361,31 +462,18 @@ ngx_conf_set_rules(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 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); - - if (res < 0) { - dd("Failed to load the rules: '%s' - reason: '%s'", rules, error); - return strdup(error); - } - mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_modsecurity_module); - mmcf->rules_inline += res; - return NGX_CONF_OK; + return ngx_http_modsecurity_add_rules(cf, conf, + ngx_http_modsecurity_add_inline, rules, NULL, &mmcf->rules_inline); } char * ngx_conf_set_rules_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { - int res; char *rules_set; 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; @@ -395,31 +483,18 @@ ngx_conf_set_rules_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 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); - - if (res < 0) { - dd("Failed to load the rules from: '%s' - reason: '%s'", rules_set, error); - return strdup(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; @@ -434,19 +509,11 @@ ngx_conf_set_rules_remote(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 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); } @@ -670,6 +737,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 NULL; + } + + conf->empty_rules_set = msc_create_rules_set(); + if (conf->empty_rules_set == NULL) { + return NULL; + } + + 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; @@ -697,7 +778,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 +800,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 +808,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 +839,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 (ngx_http_modsecurity_own_rules_set(cf, p) != NGX_OK) { + return NGX_CONF_ERROR; + } + + 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); @@ -804,6 +892,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; + } +} + +############################################################################### diff --git a/tests/modsecurity-config-inherit.t b/tests/modsecurity-config-inherit.t new file mode 100644 index 00000000..7e34a235 --- /dev/null +++ b/tests/modsecurity-config-inherit.t @@ -0,0 +1,240 @@ +#!/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 /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%%; + } + + 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->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(26); + +############################################################################### + +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'); + +# 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'); + +# 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; + } +} + +###############################################################################