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/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 new file mode 100644 index 00000000..295c8171 --- /dev/null +++ b/tests/modsecurity-subrequest.t @@ -0,0 +1,135 @@ +#!/usr/bin/perl + +# 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 ssi/); + +$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 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-auth.txt + SecAuditLogType Serial + SecAuditLogStorageDir %%TESTDIR%%/ + '; + + location = /auth { + return 204; + } + + location / { + auth_request /auth; + add_header X-Main-Marker main; + } + } + + server { + 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 + 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(7); + +############################################################################### + +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 = $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'); + +$r = http(<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 +# 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. + +my $records = () = $audit =~ /^---\w+---A--$/mg; + +is($records, 1, 'phase 5 ran once for the transaction'); + +###############################################################################