|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Andrei Belov |
| 4 | + |
| 5 | +# Tests for ModSecurity-nginx connector (modsecurity_transaction_id). |
| 6 | + |
| 7 | +############################################################################### |
| 8 | + |
| 9 | +use warnings; |
| 10 | +use strict; |
| 11 | + |
| 12 | +use Test::More; |
| 13 | + |
| 14 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 15 | + |
| 16 | +use lib 'lib'; |
| 17 | +use Test::Nginx; |
| 18 | + |
| 19 | +############################################################################### |
| 20 | + |
| 21 | +select STDERR; $| = 1; |
| 22 | +select STDOUT; $| = 1; |
| 23 | + |
| 24 | +my $t = Test::Nginx->new()->plan(3)->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 | + modsecurity_transaction_id "tid-HTTP-DEFAULT-$request_id"; |
| 37 | +
|
| 38 | + server { |
| 39 | + listen 127.0.0.1:8080; |
| 40 | + server_name server1; |
| 41 | +
|
| 42 | + location / { |
| 43 | + error_log %%TESTDIR%%/e_s1l1.log info; |
| 44 | + modsecurity on; |
| 45 | + modsecurity_rules ' |
| 46 | + SecRuleEngine On |
| 47 | + SecDefaultAction "phase:1,log,deny,status:403" |
| 48 | + SecRule ARGS "@streq block403" "id:4,phase:1,status:403,block" |
| 49 | + '; |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + server { |
| 54 | + listen 127.0.0.1:8080; |
| 55 | + server_name server2; |
| 56 | +
|
| 57 | + modsecurity_transaction_id "tid-SERVER-DEFAULT-$request_id"; |
| 58 | +
|
| 59 | + location / { |
| 60 | + error_log %%TESTDIR%%/e_s2l1.log info; |
| 61 | + modsecurity on; |
| 62 | + modsecurity_rules ' |
| 63 | + SecRuleEngine On |
| 64 | + SecDefaultAction "phase:1,log,deny,status:403" |
| 65 | + SecRule ARGS "@streq block403" "id:4,phase:1,status:403,block" |
| 66 | + '; |
| 67 | + } |
| 68 | +
|
| 69 | + location /specific { |
| 70 | + error_log %%TESTDIR%%/e_s2l2.log info; |
| 71 | + modsecurity on; |
| 72 | + modsecurity_transaction_id "tid-LOCATION-SPECIFIC-$request_id"; |
| 73 | + modsecurity_rules ' |
| 74 | + SecRuleEngine On |
| 75 | + SecDefaultAction "phase:1,log,deny,status:403" |
| 76 | + SecRule ARGS "@streq block403" "id:4,phase:1,status:403,block" |
| 77 | + '; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | +EOF |
| 82 | + |
| 83 | +$t->run(); |
| 84 | + |
| 85 | +############################################################################### |
| 86 | + |
| 87 | +# charge limit_req |
| 88 | + |
| 89 | +http(<<EOF); |
| 90 | +GET /?what=block403 HTTP/1.0 |
| 91 | +Host: server1 |
| 92 | +
|
| 93 | +EOF |
| 94 | + |
| 95 | +is(lines($t, 'e_s1l1.log', 'unique_id "tid-HTTP-DEFAULT-'), 2, 'http default'); |
| 96 | + |
| 97 | +http(<<EOF); |
| 98 | +GET /?what=block403 HTTP/1.0 |
| 99 | +Host: server2 |
| 100 | +
|
| 101 | +EOF |
| 102 | + |
| 103 | +is(lines($t, 'e_s2l1.log', 'unique_id "tid-SERVER-DEFAULT-'), 2, 'server default'); |
| 104 | + |
| 105 | +http(<<EOF); |
| 106 | +GET /specific/?what=block403 HTTP/1.0 |
| 107 | +Host: server2 |
| 108 | +
|
| 109 | +EOF |
| 110 | + |
| 111 | +is(lines($t, 'e_s2l2.log', 'unique_id "tid-LOCATION-SPECIFIC-'), 2, 'location specific'); |
| 112 | + |
| 113 | +############################################################################### |
| 114 | + |
| 115 | +sub lines { |
| 116 | + my ($t, $file, $pattern) = @_; |
| 117 | + my $path = $t->testdir() . '/' . $file; |
| 118 | + open my $fh, '<', $path or return "$!"; |
| 119 | + my $value = map { $_ =~ /\Q$pattern\E/ } (<$fh>); |
| 120 | + close $fh; |
| 121 | + return $value; |
| 122 | +} |
| 123 | + |
| 124 | +############################################################################### |
0 commit comments