4
4
package seclang
5
5
6
6
import (
7
+ "regexp"
7
8
"strings"
8
9
"testing"
9
10
@@ -34,7 +35,7 @@ func Test_NonImplementedDirective(t *testing.T) {
34
35
}
35
36
}
36
37
37
- func TestSecRuleUpdateTargetBy (t * testing.T ) {
38
+ func TestSecRuleUpdateTargetByID (t * testing.T ) {
38
39
waf := corazawaf .NewWAF ()
39
40
rule , err := ParseRule (RuleOptions {
40
41
Data : "REQUEST_URI \" ^/test\" \" id:181,tag:test\" " ,
@@ -158,6 +159,7 @@ func TestDirectives(t *testing.T) {
158
159
},
159
160
"SecRuleRemoveByTag" : {
160
161
{"" , expectErrorOnDirective },
162
+ {"attack-sqli" , expectNoErrorOnDirective },
161
163
},
162
164
"SecRuleRemoveByMsg" : {
163
165
{"" , expectErrorOnDirective },
@@ -168,10 +170,41 @@ func TestDirectives(t *testing.T) {
168
170
{"1-a" , expectErrorOnDirective },
169
171
{"a-2" , expectErrorOnDirective },
170
172
{"2-1" , expectErrorOnDirective },
173
+ {"-1" , expectErrorOnDirective },
174
+ {"-5--1" , expectErrorOnDirective },
175
+ {"5--1" , expectErrorOnDirective },
171
176
{"1" , expectNoErrorOnDirective },
172
177
{"1 2" , expectNoErrorOnDirective },
173
178
{"1 2 3-4" , expectNoErrorOnDirective },
174
179
},
180
+ "SecRuleUpdateTargetById" : {
181
+ {"" , expectErrorOnDirective },
182
+ {"a" , expectErrorOnDirective },
183
+ {"1-a" , expectErrorOnDirective },
184
+ {"a-2" , expectErrorOnDirective },
185
+ {"2-1" , expectErrorOnDirective },
186
+ {"1-a \" ARGS:wp_post\" " , expectErrorOnDirective },
187
+ {"a-2 \" ARGS:wp_post\" " , expectErrorOnDirective },
188
+ {"2-1 \" ARGS:wp_post\" " , expectErrorOnDirective },
189
+ {"-1 \" ARGS:wp_post\" " , expectErrorOnDirective },
190
+ {"-5--1 \" ARGS:wp_post\" " , expectErrorOnDirective },
191
+ {"5--1 \" ARGS:wp_post\" " , expectErrorOnDirective },
192
+ // Variables has also to be provided to the directive
193
+ {"1" , expectErrorOnDirective },
194
+ {"1 \" ARGS:wp_post\" " , expectNoErrorOnDirective },
195
+ {"7-7 \" ARGS:wp_post\" " , expectNoErrorOnDirective },
196
+ {"1 2 \" ARGS:wp_post\" " , expectNoErrorOnDirective },
197
+ {"1 2 3-4 \" ARGS:wp_post\" " , expectNoErrorOnDirective },
198
+ {"1 \" REQUEST_BODY|ARGS:wp_post\" " , expectNoErrorOnDirective },
199
+ {"1 2 3-4 \" ARGS:wp_post|RESPONSE_HEADERS\" " , expectNoErrorOnDirective },
200
+ },
201
+ "SecRuleUpdateTargetByTag" : {
202
+ {"" , expectErrorOnDirective },
203
+ {"a" , expectErrorOnDirective },
204
+ {"tag-1 \" ARGS:wp_post\" " , expectNoErrorOnDirective },
205
+ {"tag-1 tag-2 \" ARGS:wp_post\" " , expectErrorOnDirective }, // Multiple tags in line is not supported
206
+ {"tag-2 \" ARGS:wp_post|RESPONSE_HEADERS|!REQUEST_BODY\" " , expectNoErrorOnDirective },
207
+ },
175
208
"SecResponseBodyMimeTypesClear" : {
176
209
{"" , func (w * corazawaf.WAF ) bool { return len (w .ResponseBodyMimeTypes ) == 0 }},
177
210
{"x" , expectErrorOnDirective },
@@ -262,7 +295,12 @@ func TestDirectives(t *testing.T) {
262
295
}
263
296
} else {
264
297
if err != nil {
265
- t .Errorf ("unexpected error: %s" , err .Error ())
298
+ match , _ := regexp .MatchString (`rule "\d+" not found` , err .Error ())
299
+ // Logical errors are not checked by this test, therefore this specific pattern is allowed here
300
+ if ! match {
301
+ // Syntax errors are checked
302
+ t .Errorf ("unexpected error: %s" , err .Error ())
303
+ }
266
304
}
267
305
268
306
if ! tCase .check (waf ) {
0 commit comments