diff --git a/src/ngx_http_modsecurity_module.c b/src/ngx_http_modsecurity_module.c index 1e55ade0..01f697e9 100644 --- a/src/ngx_http_modsecurity_module.c +++ b/src/ngx_http_modsecurity_module.c @@ -334,14 +334,14 @@ ngx_http_modsecurity_get_module_ctx(ngx_http_request_t *r) ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module); 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 */ 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 new file mode 100644 index 00000000..3fec27c7 --- /dev/null +++ b/tests/modsecurity-internal-redirect.t @@ -0,0 +1,150 @@ +#!/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 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" + SecAuditEngine RelevantOnly + SecAuditLogParts ABH + SecAuditLog %%TESTDIR%%/audit.txt + SecAuditLogType Serial + '; + + error_log %%TESTDIR%%/msc.log info; + + error_page 403 /denied.html; + + location / { + try_files $uri /fallback; + } + + location = /fallback { + internal; + add_header X-Fallback yes; + } + + location = /denied.html { + internal; + } + + location /named { + try_files $uri @named; + } + + location @named { + add_header X-Fallback yes; + return 200 "NAMED"; + } + + location /static { + } + } +} + +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(); +$t->plan(8); + +############################################################################### + +like(http_get('/missing?what=ok'), qr/FALLBACK/, 'try_files fallback served'); +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 +# without setting r->internal + +like(http(<testdir(); + +my $audit = do { + local $/ = undef; + open my $fh, "<", "$d/audit.txt" + or die "could not open: $!"; + <$fh>; +}; + +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 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'); + +###############################################################################