|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Andrei Belov |
| 4 | + |
| 5 | +# Tests for ModSecurity-nginx connector (request body operations). |
| 6 | + |
| 7 | +############################################################################### |
| 8 | + |
| 9 | +use warnings; |
| 10 | +use strict; |
| 11 | + |
| 12 | +use Test::More; |
| 13 | +use Socket qw/ CRLF /; |
| 14 | + |
| 15 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 16 | + |
| 17 | +use lib 'lib'; |
| 18 | +use Test::Nginx; |
| 19 | + |
| 20 | +############################################################################### |
| 21 | + |
| 22 | +select STDERR; $| = 1; |
| 23 | +select STDOUT; $| = 1; |
| 24 | + |
| 25 | +my $t = Test::Nginx->new()->has(qw/http/); |
| 26 | + |
| 27 | +$t->write_file_expand('nginx.conf', <<'EOF'); |
| 28 | +
|
| 29 | +%%TEST_GLOBALS%% |
| 30 | +
|
| 31 | +daemon off; |
| 32 | +
|
| 33 | +events { |
| 34 | +} |
| 35 | +
|
| 36 | +http { |
| 37 | + %%TEST_GLOBALS_HTTP%% |
| 38 | +
|
| 39 | + server { |
| 40 | + listen 127.0.0.1:8080; |
| 41 | + server_name localhost; |
| 42 | +
|
| 43 | + modsecurity on; |
| 44 | +
|
| 45 | + location /bodyaccess { |
| 46 | + modsecurity_rules ' |
| 47 | + SecRuleEngine On |
| 48 | + SecRequestBodyAccess On |
| 49 | + SecRule REQUEST_BODY "@rx BAD BODY" "id:11,phase:request,deny,log,status:403" |
| 50 | + '; |
| 51 | + proxy_pass http://127.0.0.1:8081; |
| 52 | + } |
| 53 | +
|
| 54 | + location /nobodyaccess { |
| 55 | + modsecurity_rules ' |
| 56 | + SecRuleEngine On |
| 57 | + SecRequestBodyAccess Off |
| 58 | + SecRule REQUEST_BODY "@rx BAD BODY" "id:21,phase:request,deny,log,status:403" |
| 59 | + '; |
| 60 | + proxy_pass http://127.0.0.1:8081; |
| 61 | + } |
| 62 | +
|
| 63 | + location /bodylimitreject { |
| 64 | + modsecurity_rules ' |
| 65 | + SecRuleEngine On |
| 66 | + SecRequestBodyAccess On |
| 67 | + SecRequestBodyInMemoryLimit 128 |
| 68 | + SecRequestBodyLimit 128 |
| 69 | + SecRequestBodyLimitAction Reject |
| 70 | + SecRule REQUEST_BODY "@rx BAD BODY" "id:31,phase:request,deny,log,status:403" |
| 71 | + '; |
| 72 | + proxy_pass http://127.0.0.1:8081; |
| 73 | + } |
| 74 | +
|
| 75 | + location /bodylimitprocesspartial { |
| 76 | + modsecurity_rules ' |
| 77 | + SecRuleEngine On |
| 78 | + SecRequestBodyAccess On |
| 79 | + SecRequestBodyInMemoryLimit 128 |
| 80 | + SecRequestBodyLimit 128 |
| 81 | + SecRequestBodyLimitAction ProcessPartial |
| 82 | + SecRule REQUEST_BODY "@rx BAD BODY" "id:41,phase:request,deny,log,status:403" |
| 83 | + '; |
| 84 | + proxy_pass http://127.0.0.1:8081; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +EOF |
| 89 | + |
| 90 | +$t->run_daemon(\&http_daemon); |
| 91 | +$t->run()->waitforsocket('127.0.0.1:' . port(8081)); |
| 92 | + |
| 93 | +$t->plan(7); |
| 94 | + |
| 95 | +############################################################################### |
| 96 | + |
| 97 | +like(http_get_body('/bodyaccess', 'GOOD BODY'), qr/TEST-OK-IF-YOU-SEE-THIS/, 'request body access on, pass'); |
| 98 | +like(http_get_body('/bodyaccess', 'VERY BAD BODY'), qr/403 Forbidden/, 'request body access on, block'); |
| 99 | +like(http_get_body('/nobodyaccess', 'VERY BAD BODY'), qr/TEST-OK-IF-YOU-SEE-THIS/, 'request body access off, pass'); |
| 100 | +like(http_get_body('/bodylimitreject', 'BODY' x 32), qr/TEST-OK-IF-YOU-SEE-THIS/, 'request body limit reject, pass'); |
| 101 | +like(http_get_body('/bodylimitreject', 'BODY' x 33), qr/403 Forbidden/, 'request body limit reject, block'); |
| 102 | +like(http_get_body('/bodylimitprocesspartial', 'BODY' x 32 . 'BAD BODY'), qr/TEST-OK-IF-YOU-SEE-THIS/, 'request body limit process partial, pass'); |
| 103 | +like(http_get_body('/bodylimitprocesspartial', 'BODY' x 30 . 'BAD BODY' x 32), qr/403 Forbidden/, 'request body limit process partial, block'); |
| 104 | + |
| 105 | +############################################################################### |
| 106 | + |
| 107 | +sub http_daemon { |
| 108 | + my $server = IO::Socket::INET->new( |
| 109 | + Proto => 'tcp', |
| 110 | + LocalHost => '127.0.0.1:' . port(8081), |
| 111 | + Listen => 5, |
| 112 | + Reuse => 1 |
| 113 | + ) |
| 114 | + or die "Can't create listening socket: $!\n"; |
| 115 | + |
| 116 | + local $SIG{PIPE} = 'IGNORE'; |
| 117 | + |
| 118 | + while (my $client = $server->accept()) { |
| 119 | + $client->autoflush(1); |
| 120 | + |
| 121 | + my $headers = ''; |
| 122 | + my $uri = ''; |
| 123 | + |
| 124 | + while (<$client>) { |
| 125 | + $headers .= $_; |
| 126 | + last if (/^\x0d?\x0a?$/); |
| 127 | + } |
| 128 | + |
| 129 | + $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; |
| 130 | + |
| 131 | + print $client <<'EOF'; |
| 132 | +HTTP/1.1 200 OK |
| 133 | +Connection: close |
| 134 | +
|
| 135 | +EOF |
| 136 | + print $client "TEST-OK-IF-YOU-SEE-THIS" |
| 137 | + unless $headers =~ /^HEAD/i; |
| 138 | + |
| 139 | + close $client; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +sub http_get_body { |
| 144 | + my $uri = shift; |
| 145 | + my $last = pop; |
| 146 | + return http( join '', (map { |
| 147 | + my $body = $_; |
| 148 | + "GET $uri HTTP/1.1" . CRLF |
| 149 | + . "Host: localhost" . CRLF |
| 150 | + . "Content-Length: " . (length $body) . CRLF . CRLF |
| 151 | + . $body |
| 152 | + } @_), |
| 153 | + "GET $uri HTTP/1.1" . CRLF |
| 154 | + . "Host: localhost" . CRLF |
| 155 | + . "Connection: close" . CRLF |
| 156 | + . "Content-Length: " . (length $last) . CRLF . CRLF |
| 157 | + . $last |
| 158 | + ); |
| 159 | +} |
| 160 | + |
| 161 | +############################################################################### |
0 commit comments