Skip to content

Commit 3e26840

Browse files
committed
Tests: Add test cases for all phases in custom error page test
1 parent 162a3bd commit 3e26840

File tree

1 file changed

+47
-26
lines changed

1 file changed

+47
-26
lines changed

tests/modsecurity-config-custom-error-page.t

+47-26
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ http {
6363
modsecurity on;
6464
modsecurity_rules '
6565
SecRuleEngine On
66-
SecRule ARGS "@streq root" "id:10,phase:1,auditlog,status:403,deny"
66+
SecResponseBodyAccess On
67+
SecRule ARGS:phase1 "@streq BAD" "id:10,phase:1,auditlog,status:403,deny"
68+
SecRule ARGS:phase2 "@streq BAD" "id:11,phase:2,auditlog,status:403,deny"
69+
SecRule ARGS:phase3 "@streq BAD" "id:12,phase:3,auditlog,status:403,deny"
70+
SecRule ARGS:phase4 "@streq BAD" "id:13,phase:4,auditlog,status:403,deny"
6771
SecDebugLog %%TESTDIR%%/auditlog-debug-location.txt
6872
SecDebugLogLevel 9
6973
SecAuditEngine RelevantOnly
@@ -82,7 +86,11 @@ http {
8286
modsecurity on;
8387
modsecurity_rules '
8488
SecRuleEngine On
85-
SecRule ARGS "@streq root" "id:10,phase:1,auditlog,status:403,deny"
89+
SecResponseBodyAccess On
90+
SecRule ARGS:phase1 "@streq BAD" "id:10,phase:1,auditlog,status:403,deny"
91+
SecRule ARGS:phase2 "@streq BAD" "id:11,phase:2,auditlog,status:403,deny"
92+
SecRule ARGS:phase3 "@streq BAD" "id:12,phase:3,auditlog,status:403,deny"
93+
SecRule ARGS:phase4 "@streq BAD" "id:13,phase:4,auditlog,status:403,deny"
8694
SecDebugLog %%TESTDIR%%/auditlog-debug-server.txt
8795
SecDebugLogLevel 9
8896
SecAuditEngine RelevantOnly
@@ -112,25 +120,23 @@ $t->write_file("/index.html", $index_txt);
112120
my $error_txt = "This is a custom error page.";
113121
$t->write_file("/error.html", $error_txt);
114122

123+
$t->todo_alerts();
115124
$t->run();
116-
$t->plan(10);
125+
$t->plan(32);
117126

118127
###############################################################################
119128

120129
my $d = $t->testdir();
121130

122-
my $t1;
123-
my $t2;
124-
my $t3;
125-
my $t4;
126-
127131
# Performing requests to a server with ModSecurity enabled at location context
128-
$t1 = http_get_host('s1', '/?what=root');
129-
$t2 = http_get_host('s1', '/?what=other');
130-
131-
# Performing requests to a server with ModSecurity enabled at server context
132-
$t3 = http_get_host('s2', '/?what=root');
133-
$t4 = http_get_host('s2', '/?what=other');
132+
like(http_get_host('s1', '/?phase1=BAD'), qr/$error_txt/, 'location context, phase 1, error page');
133+
like(http_get_host('s1', '/?phase1=GOOD'), qr/$index_txt/, 'location context, phase 1, index page');
134+
like(http_get_host('s1', '/?phase2=BAD'), qr/$error_txt/, 'location context, phase 2, error page');
135+
like(http_get_host('s1', '/?phase2=GOOD'), qr/$index_txt/, 'location context, phase 2, index page');
136+
like(http_get_host('s1', '/?phase3=BAD'), qr/$error_txt/, 'location context, phase 3, error page');
137+
like(http_get_host('s1', '/?phase3=GOOD'), qr/$index_txt/, 'location context, phase 3, index page');
138+
is(http_get_host('s1', '/?phase4=BAD'), '', 'location context, phase 4, drop');
139+
like(http_get_host('s1', '/?phase4=GOOD'), qr/$index_txt/, 'location context, phase 4, index page');
134140

135141
my $auditlog_location = do {
136142
local $/ = undef;
@@ -139,25 +145,40 @@ my $auditlog_location = do {
139145
<$fh>;
140146
};
141147

148+
like($auditlog_location, qr/phase1=BAD/, 'location context, phase 1, BAD in auditlog');
149+
unlike($auditlog_location, qr/phase1=GOOD/, 'location context, phase 1, GOOD not in auditlog');
150+
like($auditlog_location, qr/phase2=BAD/, 'location context, phase 2, BAD in auditlog');
151+
unlike($auditlog_location, qr/phase2=GOOD/, 'location context, phase 2, GOOD not in auditlog');
152+
like($auditlog_location, qr/phase3=BAD/, 'location context, phase 3, BAD in auditlog');
153+
unlike($auditlog_location, qr/phase3=GOOD/, 'location context, phase 3, GOOD not in auditlog');
154+
like($auditlog_location, qr/phase4=BAD/, 'location context, phase 4, BAD in auditlog');
155+
unlike($auditlog_location, qr/phase4=GOOD/, 'location context, phase 4, GOOD not in auditlog');
156+
157+
# Performing requests to a server with ModSecurity enabled at server context
158+
like(http_get_host('s2', '/?phase1=BAD'), qr/$error_txt/, 'server context, phase 1, error page');
159+
like(http_get_host('s2', '/?phase1=GOOD'), qr/$index_txt/, 'server context, phase 1, index page');
160+
like(http_get_host('s2', '/?phase2=BAD'), qr/$error_txt/, 'server context, phase 2, error page');
161+
like(http_get_host('s2', '/?phase2=GOOD'), qr/$index_txt/, 'server context, phase 2, index page');
162+
like(http_get_host('s2', '/?phase3=BAD'), qr/$error_txt/, 'server context, phase 3, error page');
163+
like(http_get_host('s2', '/?phase3=GOOD'), qr/$index_txt/, 'server context, phase 3, index page');
164+
is(http_get_host('s2', '/?phase4=BAD'), '', 'server context, phase 4, drop');
165+
like(http_get_host('s2', '/?phase4=GOOD'), qr/$index_txt/, 'server context, phase 4, index page');
166+
142167
my $auditlog_server = do {
143168
local $/ = undef;
144169
open my $fh, "<", "$d/auditlog-server.txt"
145170
or die "could not open: $!";
146171
<$fh>;
147172
};
148173

149-
like($t1, qr/$error_txt/, 'ModSecurity at location / root');
150-
like($t2, qr/$index_txt/, 'ModSecurity at location / other');
151-
like($auditlog_location, qr/what=root/, 'ModSecurity at location / root present in auditlog');
152-
unlike($auditlog_location, qr/what=other/, 'ModSecurity at location / other not present in auditlog');
153-
154-
like($t3, qr/$error_txt/, 'ModSecurity at server / root');
155-
like($t4, qr/$index_txt/, 'ModSecurity at server / other');
156-
like($auditlog_server, qr/what=root/, 'ModSecurity at server / root present in auditlog');
157-
unlike($auditlog_server, qr/what=other/, 'ModSecurity at server / other not present in auditlog');
158-
159-
like($auditlog_location, qr/Access denied with code 403/, 'ModSecurity at location / 403 in auditlog');
160-
like($auditlog_server, qr/Access denied with code 403/, 'ModSecurity at server / 403 in auditlog');
174+
like($auditlog_server, qr/phase1=BAD/, 'server context, phase 1, BAD in auditlog');
175+
unlike($auditlog_server, qr/phase1=GOOD/, 'server context, phase 1, GOOD not in auditlog');
176+
like($auditlog_server, qr/phase2=BAD/, 'server context, phase 2, BAD in auditlog');
177+
unlike($auditlog_server, qr/phase2=GOOD/, 'server context, phase 2, GOOD not in auditlog');
178+
like($auditlog_server, qr/phase3=BAD/, 'server context, phase 3, BAD in auditlog');
179+
unlike($auditlog_server, qr/phase3=GOOD/, 'server context, phase 3, GOOD not in auditlog');
180+
like($auditlog_server, qr/phase4=BAD/, 'server context, phase 4, BAD in auditlog');
181+
unlike($auditlog_server, qr/phase4=GOOD/, 'server context, phase 4, GOOD not in auditlog');
161182

162183
###############################################################################
163184

0 commit comments

Comments
 (0)