|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# |
| 4 | +# ModSecurity, http://www.modsecurity.org/ |
| 5 | +# Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/) |
| 6 | +# |
| 7 | +# You may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# If any of the files related to licensing are missing or if you have any |
| 13 | +# other questions related to licensing please contact Trustwave Holdings, Inc. |
| 14 | +# directly using the email address security@modsecurity.org. |
| 15 | +# |
| 16 | + |
| 17 | + |
| 18 | +# Tests for ModSecurity module. |
| 19 | + |
| 20 | +############################################################################### |
| 21 | + |
| 22 | +use warnings; |
| 23 | +use strict; |
| 24 | + |
| 25 | +use Test::More; |
| 26 | + |
| 27 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 28 | + |
| 29 | +use lib 'lib'; |
| 30 | +use Test::Nginx; |
| 31 | + |
| 32 | +############################################################################### |
| 33 | + |
| 34 | +select STDERR; $| = 1; |
| 35 | +select STDOUT; $| = 1; |
| 36 | + |
| 37 | +my $t = Test::Nginx->new()->has(qw/http/); |
| 38 | + |
| 39 | +$t->write_file_expand('nginx.conf', <<'EOF'); |
| 40 | +
|
| 41 | +%%TEST_GLOBALS%% |
| 42 | +
|
| 43 | +daemon off; |
| 44 | +
|
| 45 | +events { |
| 46 | +} |
| 47 | +
|
| 48 | +http { |
| 49 | + %%TEST_GLOBALS_HTTP%% |
| 50 | +
|
| 51 | + server { |
| 52 | + listen 127.0.0.1:8080; |
| 53 | + server_name s1; |
| 54 | +
|
| 55 | + error_page 403 /403.html; |
| 56 | +
|
| 57 | + location /403.html { |
| 58 | + root %%TESTDIR%%/http; |
| 59 | + internal; |
| 60 | + } |
| 61 | +
|
| 62 | + location / { |
| 63 | + modsecurity on; |
| 64 | + modsecurity_rules ' |
| 65 | + SecRuleEngine On |
| 66 | + SecRule ARGS "@streq root" "id:10,phase:1,auditlog,status:403,deny" |
| 67 | + SecDebugLog %%TESTDIR%%/auditlog-debug-local.txt |
| 68 | + SecDebugLogLevel 9 |
| 69 | + SecAuditEngine RelevantOnly |
| 70 | + SecAuditLogParts ABIJDEFHZ |
| 71 | + SecAuditLog %%TESTDIR%%/auditlog-local.txt |
| 72 | + SecAuditLogType Serial |
| 73 | + SecAuditLogStorageDir %%TESTDIR%%/ |
| 74 | + '; |
| 75 | + } |
| 76 | + } |
| 77 | +
|
| 78 | + server { |
| 79 | + listen 127.0.0.1:8080; |
| 80 | + server_name s2; |
| 81 | +
|
| 82 | + modsecurity on; |
| 83 | + modsecurity_rules ' |
| 84 | + SecRuleEngine On |
| 85 | + SecRule ARGS "@streq root" "id:10,phase:1,auditlog,status:403,deny" |
| 86 | + SecDebugLog %%TESTDIR%%/auditlog-debug-global.txt |
| 87 | + SecDebugLogLevel 9 |
| 88 | + SecAuditEngine RelevantOnly |
| 89 | + SecAuditLogParts ABIJDEFHZ |
| 90 | + SecAuditLog %%TESTDIR%%/auditlog-global.txt |
| 91 | + SecAuditLogType Serial |
| 92 | + SecAuditLogStorageDir %%TESTDIR%%/ |
| 93 | + '; |
| 94 | +
|
| 95 | + error_page 403 /403.html; |
| 96 | +
|
| 97 | + location /403.html { |
| 98 | + modsecurity off; |
| 99 | + root %%TESTDIR%%/http; |
| 100 | + internal; |
| 101 | + } |
| 102 | +
|
| 103 | + location / { |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | +EOF |
| 108 | + |
| 109 | +my $index_txt = "This is the index page."; |
| 110 | +my $custom_txt = "This is a custom error page."; |
| 111 | + |
| 112 | +$t->write_file("/index.html", $index_txt); |
| 113 | +mkdir($t->testdir() . '/http'); |
| 114 | +$t->write_file("/http/403.html", $custom_txt); |
| 115 | + |
| 116 | +$t->run(); |
| 117 | +$t->plan(10); |
| 118 | + |
| 119 | +############################################################################### |
| 120 | + |
| 121 | +my $d = $t->testdir(); |
| 122 | + |
| 123 | +my $t1; |
| 124 | +my $t2; |
| 125 | +my $t3; |
| 126 | +my $t4; |
| 127 | + |
| 128 | +# Performing requests to a server with ModSecurity enabled at location context |
| 129 | +$t1 = http_get_host('s1', '/index.html?what=root'); |
| 130 | +$t2 = http_get_host('s1', '/index.html?what=other'); |
| 131 | + |
| 132 | +# Performing requests to a server with ModSecurity enabled at server context |
| 133 | +$t3 = http_get_host('s2', '/index.html?what=root'); |
| 134 | +$t4 = http_get_host('s2', '/index.html?what=other'); |
| 135 | + |
| 136 | +my $local = do { |
| 137 | + local $/ = undef; |
| 138 | + open my $fh, "<", "$d/auditlog-local.txt" |
| 139 | + or die "could not open: $!"; |
| 140 | + <$fh>; |
| 141 | +}; |
| 142 | + |
| 143 | +my $global = do { |
| 144 | + local $/ = undef; |
| 145 | + open my $fh, "<", "$d/auditlog-global.txt" |
| 146 | + or die "could not open: $!"; |
| 147 | + <$fh>; |
| 148 | +}; |
| 149 | + |
| 150 | +like($t1, qr/$custom_txt/, 'ModSecurity at location / root'); |
| 151 | +like($t2, qr/$index_txt/, 'ModSecurity at location / other'); |
| 152 | +like($local, qr/what=root/, 'ModSecurity at location / root present in auditlog'); |
| 153 | +unlike($local, qr/what=other/, 'ModSecurity at location / other not present in auditlog'); |
| 154 | + |
| 155 | +like($t3, qr/$custom_txt/, 'ModSecurity at server / root'); |
| 156 | +like($t4, qr/$index_txt/, 'ModSecurity at server / other'); |
| 157 | +like($global, qr/what=root/, 'ModSecurity at server / root present in auditlog'); |
| 158 | +unlike($global, qr/what=other/, 'ModSecurity at server / other not present in auditlog'); |
| 159 | + |
| 160 | +like($local, qr/Access denied with code 403/, 'ModSecurity at location / 403 in auditlog'); |
| 161 | +like($global, qr/Access denied with code 403/, 'ModSecurity at server / 403 in auditlog'); |
| 162 | + |
| 163 | +############################################################################### |
| 164 | + |
| 165 | +sub http_get_host { |
| 166 | +my ($host, $url) = @_; |
| 167 | +return http(<<EOF); |
| 168 | +GET $url HTTP/1.0 |
| 169 | +Host: $host |
| 170 | +
|
| 171 | +EOF |
| 172 | +} |
| 173 | + |
| 174 | +############################################################################### |
0 commit comments