Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a36fc14

Browse files
committed
Added test for custom error page
1 parent 798396f commit a36fc14

File tree

1 file changed

+241
-0
lines changed

1 file changed

+241
-0
lines changed
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
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 localhost;
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 AB
71+
SecAuditLog %%TESTDIR%%/auditlog-local.txt
72+
SecAuditLogType Serial
73+
SecAuditLogStorageDir %%TESTDIR%%/
74+
';
75+
}
76+
}
77+
78+
server {
79+
listen 127.0.0.1:8081;
80+
server_name localhost;
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 AB
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(8);
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('/index.html?what=root');
130+
$t2 = http_get('/index.html?what=other');
131+
132+
# Performing requests to a server with ModSecurity enabled at server context
133+
$t3 = http_get2('/index.html?what=root');
134+
$t4 = http_get2('/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+
###############################################################################
161+
162+
sub http_get2($;%) {
163+
my ($url, %extra) = @_;
164+
return http2(<<EOF, %extra);
165+
GET $url HTTP/1.0
166+
Host: localhost
167+
168+
EOF
169+
}
170+
171+
sub http2($;%) {
172+
my ($request, %extra) = @_;
173+
174+
my $s = http_start2($request, %extra);
175+
176+
return $s if $extra{start} or !defined $s;
177+
return http_end2($s);
178+
}
179+
180+
sub http_start2($;%) {
181+
my ($request, %extra) = @_;
182+
my $s;
183+
184+
eval {
185+
local $SIG{ALRM} = sub { die "timeout\n" };
186+
local $SIG{PIPE} = sub { die "sigpipe\n" };
187+
alarm(8);
188+
189+
$s = $extra{socket} || IO::Socket::INET->new(
190+
Proto => 'tcp',
191+
PeerAddr => '127.0.0.1:' . port(8081)
192+
)
193+
or die "Can't connect to nginx: $!\n";
194+
195+
log_out($request);
196+
$s->print($request);
197+
198+
select undef, undef, undef, $extra{sleep} if $extra{sleep};
199+
return '' if $extra{aborted};
200+
201+
if ($extra{body}) {
202+
log_out($extra{body});
203+
$s->print($extra{body});
204+
}
205+
206+
alarm(0);
207+
};
208+
alarm(0);
209+
if ($@) {
210+
log_in("died: $@");
211+
return undef;
212+
}
213+
214+
return $s;
215+
}
216+
217+
sub http_end2($;%) {
218+
my ($s) = @_;
219+
my $reply;
220+
221+
eval {
222+
local $SIG{ALRM} = sub { die "timeout\n" };
223+
local $SIG{PIPE} = sub { die "sigpipe\n" };
224+
alarm(8);
225+
226+
local $/;
227+
$reply = $s->getline();
228+
229+
alarm(0);
230+
};
231+
alarm(0);
232+
if ($@) {
233+
log_in("died: $@");
234+
return undef;
235+
}
236+
237+
log_in($reply);
238+
return $reply;
239+
}
240+
241+
###############################################################################

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /