|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Test for ModSecurity-nginx connector (limit_req ordering). |
| 4 | + |
| 5 | +############################################################################### |
| 6 | + |
| 7 | +use warnings; |
| 8 | +use strict; |
| 9 | + |
| 10 | +use Test::More; |
| 11 | + |
| 12 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 13 | + |
| 14 | +use lib 'lib'; |
| 15 | +use Test::Nginx; |
| 16 | + |
| 17 | +############################################################################### |
| 18 | + |
| 19 | +select STDERR; $| = 1; |
| 20 | +select STDOUT; $| = 1; |
| 21 | + |
| 22 | +my $t = Test::Nginx->new()->has(qw/http/); |
| 23 | + |
| 24 | +$t->write_file_expand('nginx.conf', <<'EOF'); |
| 25 | +
|
| 26 | +%%TEST_GLOBALS%% |
| 27 | +
|
| 28 | +daemon off; |
| 29 | +
|
| 30 | +events { |
| 31 | +} |
| 32 | +
|
| 33 | +http { |
| 34 | + %%TEST_GLOBALS_HTTP%% |
| 35 | +
|
| 36 | + limit_req_zone $binary_remote_addr zone=limitzone:10m rate=2r/s; |
| 37 | +
|
| 38 | + server { |
| 39 | + listen 127.0.0.1:8080; |
| 40 | + server_name localhost; |
| 41 | +
|
| 42 | + modsecurity on; |
| 43 | +
|
| 44 | + location /limit { |
| 45 | + limit_req zone=limitzone burst=2 nodelay; |
| 46 | + modsecurity_rules ' |
| 47 | + SecRuleEngine On |
| 48 | + SecRule REQUEST_URI "@rx .*" "id:1001,phase:1,log,deny,status:403,msg:\'Request reached ModSecurity\'" |
| 49 | + '; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | +EOF |
| 54 | + |
| 55 | +$t->write_file("/limit", "limit test endpoint"); |
| 56 | +$t->run(); |
| 57 | +$t->plan(4); |
| 58 | + |
| 59 | +############################################################################### |
| 60 | + |
| 61 | +# First two requests should pass (200 OK), next two should be limited (429 Too Many Requests) and not reach ModSecurity |
| 62 | +my $uri = '/limit'; |
| 63 | + |
| 64 | +my $res1 = http_get($uri); |
| 65 | +my $res2 = http_get($uri); |
| 66 | +my $res3 = http_get($uri); |
| 67 | +my $res4 = http_get($uri); |
| 68 | + |
| 69 | +like($res1, qr/limit test endpoint/, 'limitreq scoring 1 (pass)'); |
| 70 | +like($res2, qr/limit test endpoint/, 'limitreq scoring 2 (pass)'); |
| 71 | +like($res3, qr/^HTTP.*429/, 'limitreq scoring 3 (limited)'); |
| 72 | +like($res4, qr/^HTTP.*429/, 'limitreq scoring 4 (limited)'); |
0 commit comments