Skip to content

Commit adf66e4

Browse files
defanatorzimmerle
authored andcommitted
Add scoring test
1 parent bae1eff commit adf66e4

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

tests/modsecurity-scoring.t

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Andrei Belov
4+
5+
# Tests for ModSecurity-nginx connector (scoring).
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()->has(qw/http/);
25+
26+
$t->write_file_expand('nginx.conf', <<'EOF');
27+
28+
%%TEST_GLOBALS%%
29+
30+
daemon off;
31+
32+
events {
33+
}
34+
35+
http {
36+
%%TEST_GLOBALS_HTTP%%
37+
38+
server {
39+
listen 127.0.0.1:8080;
40+
server_name localhost;
41+
42+
modsecurity on;
43+
44+
location /absolute {
45+
modsecurity_rules '
46+
SecRuleEngine On
47+
SecRule ARGS "@streq badarg1" "id:11,phase:2,setvar:tx.score=1"
48+
SecRule ARGS "@streq badarg2" "id:12,phase:2,setvar:tx.score=2"
49+
SecRule TX:SCORE "@ge 2" "id:199,phase:request,deny,log,status:403"
50+
';
51+
}
52+
53+
location /iterative {
54+
modsecurity_rules '
55+
SecRuleEngine On
56+
SecRule ARGS "@streq badarg1" "id:21,phase:2,setvar:tx.score=+1"
57+
SecRule ARGS "@streq badarg2" "id:22,phase:2,setvar:tx.score=+1"
58+
SecRule ARGS "@streq badarg3" "id:23,phase:2,setvar:tx.score=+1"
59+
SecRule TX:SCORE "@ge 3" "id:299,phase:request,deny,log,status:403"
60+
';
61+
}
62+
}
63+
}
64+
EOF
65+
66+
$t->write_file("/absolute", "should be moved/blocked before this.");
67+
$t->write_file("/iterative", "should be moved/blocked before this.");
68+
$t->run();
69+
$t->plan(5);
70+
71+
###############################################################################
72+
73+
like(http_get('/absolute?what=badarg1'), qr/should be moved\/blocked before this./, 'absolute scoring 1 (pass)');
74+
like(http_get('/absolute?what=badarg2'), qr/403 Forbidden/, 'absolute scoring 2 (block)');
75+
76+
like(http_get('/iterative?arg1=badarg1'), qr/should be moved\/blocked before this./, 'iterative scoring 1 (pass)');
77+
like(http_get('/iterative?arg1=badarg1&arg2=badarg2'), qr/should be moved\/blocked before this./, 'iterative scoring 2 (pass)');
78+
like(http_get('/iterative?arg1=badarg1&arg2=badarg2&arg3=badarg3'), qr/403 Forbidden/, 'iterative scoring 3 (block)');
79+

0 commit comments

Comments
 (0)