Skip to content

Restore r->write_event_handler after reading request body #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ addons:
- liblmdb-dev

env:
- VER_NGINX=1.13.10
- VER_NGINX=1.12.2
- VER_NGINX=1.15.5
- VER_NGINX=1.14.0

before_script:
- cd ..
Expand All @@ -33,7 +33,7 @@ before_script:
- cd ..
- wget http://nginx.org/download/nginx-${VER_NGINX}.tar.gz && tar -xf nginx-${VER_NGINX}.tar.gz
- cd nginx-${VER_NGINX}
- ./configure --add-module=../ModSecurity-nginx
- ./configure --with-http_auth_request_module --add-module=../ModSecurity-nginx
- make
- sudo make install
- cd ..
Expand Down
3 changes: 3 additions & 0 deletions src/ngx_http_modsecurity_pre_access.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ngx_http_modsecurity_request_read(ngx_http_request_t *r)
if (ctx->waiting_more_body)
{
ctx->waiting_more_body = 0;
r->write_event_handler = ngx_http_core_run_phases;
ngx_http_core_run_phases(r);
}
}
Expand Down Expand Up @@ -131,6 +132,8 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r)

dd("request body is ready to be processed");

r->write_event_handler = ngx_http_core_run_phases;

ngx_chain_t *chain = r->request_body->bufs;

/**
Expand Down
46 changes: 44 additions & 2 deletions tests/modsecurity-request-body.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use Test::Nginx;
select STDERR; $| = 1;
select STDOUT; $| = 1;

my $t = Test::Nginx->new()->has(qw/http/);
my $t = Test::Nginx->new()->has(qw/http auth_request/);

$t->write_file_expand('nginx.conf', <<'EOF');

Expand All @@ -41,6 +41,7 @@ http {
server_name localhost;

modsecurity on;
client_header_buffer_size 1024;

location /bodyaccess {
modsecurity_rules '
Expand Down Expand Up @@ -82,14 +83,28 @@ http {
';
proxy_pass http://127.0.0.1:8081;
}

location = /auth {
return 200;
}

location = /useauth {
modsecurity on;
modsecurity_rules '
SecRuleEngine On
SecRequestBodyAccess On
';
auth_request /auth;
proxy_pass http://127.0.0.1:8081;
}
}
}
EOF

$t->run_daemon(\&http_daemon);
$t->run()->waitforsocket('127.0.0.1:' . port(8081));

$t->plan(32);
$t->plan(36);

###############################################################################

Expand All @@ -104,6 +119,33 @@ like(http_req_body($method, '/bodylimitprocesspartial', 'BODY' x 32 . 'BAD BODY'
like(http_req_body($method, '/bodylimitprocesspartial', 'BODY' x 30 . 'BAD BODY' x 32), qr/403 Forbidden/, "$method request body limit process partial, block");
}

like(http_req_body('POST', '/useauth', 'BODY' x 16), qr/TEST-OK-IF-YOU-SEE-THIS/, "POST with auth_request (request size < client_header_buffer_size)");
like(http_req_body('POST', '/useauth', 'BODY' x 257), qr/TEST-OK-IF-YOU-SEE-THIS/, "POST with auth_request (request size > client_header_buffer_size)");

like(
http(
'POST /useauth HTTP/1.0' . CRLF
. 'Content-Length: 1028' . CRLF . CRLF
. 'BODY' x 256,
sleep => 0.1,
body => 'BODY'
),
qr/TEST-OK-IF-YOU-SEE-THIS/,
'POST with auth_request (request size > client_header_buffer_size), no preread'
);

like(
http(
'POST /useauth HTTP/1.0' . CRLF
. 'Content-Length: 64' . CRLF . CRLF
. 'BODY' x 15,
sleep => 0.1,
body => 'BODY'
),
qr/TEST-OK-IF-YOU-SEE-THIS/,
'POST with auth_request (request size < client_header_buffer_size), no preread'
);

###############################################################################

sub http_daemon {
Expand Down