Imported Upstream version 2.5.11
[libapache-mod-security.git] / apache2 / t / regression / rule / 10-xml.t
1 ### Test for XML operator rules
2
3 ### Validate Scheme
4 # OK
5 {
6         type => "rule",
7         comment => "validateSchema (validate ok)",
8         conf => qq(
9                 SecRuleEngine On
10                 SecRequestBodyAccess On
11                 SecDebugLog $ENV{DEBUG_LOG}
12                 SecDebugLogLevel 9
13                 SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" \\
14                         "phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
15                 SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345
16                 SecRule XML "\@validateSchema $ENV{CONF_DIR}/SoapEnvelope.xsd" \\
17                         "phase:2,deny,id:12345"
18         ),
19         match_log => {
20                 debug => [ qr/XML: Initialising parser.*XML: Parsing complete \(well_formed 1\).*Target value: "\[XML document tree\]".*Successfully validated payload against Schema/s, 1 ],
21                 -debug => [ qr/XML parser error|validation failed|Failed to load/, 1 ],
22                 -error => [ qr/XML parser error|validation failed|Failed to load/, 1 ],
23         },
24         match_response => {
25                 status => qr/^200$/,
26         },
27         request => new HTTP::Request(
28                 POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
29                 [
30                         "Content-Type" => "text/xml",
31                 ],
32                 normalize_raw_request_data(
33                         q(
34                                 <?xml version="1.0" encoding="utf-8"?>
35                                 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
36                                 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
37                                 xmlns:tns="http://www.bluebank.example.com/axis/getBalance.jws"
38                                 xmlns:types="http://www.bluebank.example.com/axis/getBalance.jws/encodedTypes"
39                                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
40                                 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
41                                                 <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
42                                                                 <q1:getInput xmlns:q1="http://DefaultNamespace">
43                                                                                 <id xsi:type="xsd:string">12123</id>
44                                                                 </q1:getInput>
45                                                 </soap:Body>
46                                 </soap:Envelope>
47                         ),
48                 ),
49         ),
50 },
51 # Failed attribute value
52 {
53         type => "rule",
54         comment => "validateSchema (validate attribute value failed)",
55         conf => qq(
56                 SecRuleEngine On
57                 SecRequestBodyAccess On
58                 SecDebugLog $ENV{DEBUG_LOG}
59                 SecDebugLogLevel 9
60                 SecAuditEngine RelevantOnly
61                 SecAuditLog "$ENV{AUDIT_LOG}"
62                 SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" \\
63                         "phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
64                 SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345
65                 SecRule XML "\@validateSchema $ENV{CONF_DIR}/SoapEnvelope.xsd" \\
66                         "phase:2,deny,log,auditlog,id:12345"
67         ),
68         match_log => {
69                 debug => [ qr/XML: Initialising parser.*XML: Parsing complete \(well_formed 1\).*Target value: "\[XML document tree\]".*'badval' is not a valid value of the local atomic type.*Schema validation failed/s, 1 ],
70                 -debug => [ qr/Successfully validated payload against Schema|\n\r?\n/, 1 ],
71                 audit => [ qr/^Message: Element.*'badval' is not a valid value of the local atomic type\.\nMessage:/m, 1 ],
72         },
73         match_response => {
74                 status => qr/^403$/,
75         },
76         request => new HTTP::Request(
77                 POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
78                 [
79                         "Content-Type" => "text/xml",
80                 ],
81                 normalize_raw_request_data(
82                         q(
83                                 <?xml version="1.0" encoding="utf-8"?>
84                                 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
85                                 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
86                                 xmlns:tns="http://www.bluebank.example.com/axis/getBalance.jws"
87                                 xmlns:types="http://www.bluebank.example.com/axis/getBalance.jws/encodedTypes"
88                                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
89                                 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
90                                                 <soap:Body soap:mustUnderstand="badval" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
91                                                                 <q1:getInput xmlns:q1="http://DefaultNamespace">
92                                                                                 <id xsi:type="xsd:string">12123</id>
93                                                                 </q1:getInput>
94                                                 </soap:Body>
95                                 </soap:Envelope>
96                         ),
97                 ),
98         ),
99 },
100 # Failed validation
101 {
102         type => "rule",
103         comment => "validateSchema (validate failed)",
104         conf => qq(
105                 SecRuleEngine On
106                 SecRequestBodyAccess On
107                 SecDebugLog $ENV{DEBUG_LOG}
108                 SecDebugLogLevel 9
109                 SecAuditEngine RelevantOnly
110                 SecAuditLog "$ENV{AUDIT_LOG}"
111                 SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" \\
112                         "phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
113                 SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345
114                 SecRule XML "\@validateSchema $ENV{CONF_DIR}/SoapEnvelope.xsd" \\
115                         "phase:2,deny,id:12345"
116         ),
117         match_log => {
118                 debug => [ qr/XML: Initialising parser.*XML: Parsing complete \(well_formed 1\).*Target value: "\[XML document tree\]".*element is not expected/s, 1 ],
119                 -debug => [ qr/XML parser error|Failed to load/, 1 ],
120                 -error => [ qr/XML parser error|Failed to load/, 1 ],
121                 audit => [ qr/^Message: Element.*This element is not expected.*\nMessage:/m, 1 ],
122         },
123         match_response => {
124                 status => qr/^403$/,
125         },
126         request => new HTTP::Request(
127                 POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
128                 [
129                         "Content-Type" => "text/xml",
130                 ],
131                 normalize_raw_request_data(
132                         q(
133                                 <?xml version="1.0" encoding="utf-8"?>
134                                 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
135                                 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
136                                 xmlns:tns="http://www.bluebank.example.com/axis/getBalance.jws"
137                                 xmlns:types="http://www.bluebank.example.com/axis/getBalance.jws/encodedTypes"
138                                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
139                                 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
140                                                 <soap:xBody soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
141                                                                 <q1:getInput xmlns:q1="http://DefaultNamespace">
142                                                                                 <id xsi:type="xsd:string">12123</id>
143                                                                 </q1:getInput>
144                                                 </soap:xBody>
145                                 </soap:Envelope>
146                         ),
147                 ),
148         ),
149 },
150 # Bad XML
151 {
152         type => "rule",
153         comment => "validateSchema (bad XML)",
154         conf => qq(
155                 SecRuleEngine On
156                 SecRequestBodyAccess On
157                 SecDebugLog $ENV{DEBUG_LOG}
158                 SecDebugLogLevel 9
159                 SecAuditEngine RelevantOnly
160                 SecAuditLog "$ENV{AUDIT_LOG}"
161                 SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" \\
162                         "phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
163                 SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345
164                 SecRule XML "\@validateSchema $ENV{CONF_DIR}/SoapEnvelope.xsd" \\
165                         "phase:2,deny,id:12345"
166         ),
167         match_log => {
168                 debug => [ qr/XML: Initialising parser.*XML: Parsing complete \(well_formed 0\).*XML parser error.*validation failed because content is not well formed/s, 1 ],
169                 -debug => [ qr/Failed to load|Successfully validated/, 1 ],
170                 -error => [ qr/Failed to load|Successfully validated/, 1 ],
171                 audit => [ qr/^Message: .*Failed parsing document.*\nMessage:/m, 1 ],
172         },
173         match_response => {
174                 status => qr/^403$/,
175         },
176         request => new HTTP::Request(
177                 POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
178                 [
179                         "Content-Type" => "text/xml",
180                 ],
181                 normalize_raw_request_data(
182                         q(
183                                 <?xml version="1.0" encoding="utf-8"?>
184                                 <soap:Envelop xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
185                                 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
186                                 xmlns:tns="http://www.bluebank.example.com/axis/getBalance.jws"
187                                 xmlns:types="http://www.bluebank.example.com/axis/getBalance.jws/encodedTypes"
188                                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
189                                 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
190                                                 <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
191                                                                 <q1:getInput xmlns:q1="http://DefaultNamespace">
192                                                                                 <id xsi:type="xsd:string">12123</id>
193                                                                 </q1:getInput>
194                                                 </soap:Body>
195                                 </soap:Envelope>
196                         ),
197                 ),
198         ),
199 },
200 # Bad schema
201 {
202         type => "rule",
203         comment => "validateSchema (bad schema)",
204         conf => qq(
205                 SecRuleEngine On
206                 SecRequestBodyAccess On
207                 SecDebugLog $ENV{DEBUG_LOG}
208                 SecDebugLogLevel 9
209                 SecAuditEngine RelevantOnly
210                 SecAuditLog "$ENV{AUDIT_LOG}"
211                 SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" \\
212                         "phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
213                 SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345
214                 SecRule XML "\@validateSchema $ENV{CONF_DIR}/SoapEnvelope-bad.xsd" \\
215                         "phase:2,deny,id:12345"
216         ),
217         match_log => {
218                 debug => [ qr/XML: Initialising parser.*XML: Parsing complete \(well_formed 1\).*Target value: "\[XML document tree\]".*Failed to parse the XML resource.*Failed to load Schema/s, 1 ],
219                 audit => [ qr/^Message: .*Failed to parse the XML resource.*\nMessage: Rule processing failed/m, 1 ],
220         },
221         match_response => {
222                 status => qr/^200$/,
223         },
224         request => new HTTP::Request(
225                 POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
226                 [
227                         "Content-Type" => "text/xml",
228                 ],
229                 normalize_raw_request_data(
230                         q(
231                                 <?xml version="1.0" encoding="utf-8"?>
232                                 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
233                                 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
234                                 xmlns:tns="http://www.bluebank.example.com/axis/getBalance.jws"
235                                 xmlns:types="http://www.bluebank.example.com/axis/getBalance.jws/encodedTypes"
236                                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
237                                 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
238                                                 <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
239                                                                 <q1:getInput xmlns:q1="http://DefaultNamespace">
240                                                                                 <id xsi:type="xsd:string">12123</id>
241                                                                 </q1:getInput>
242                                                 </soap:Body>
243                                 </soap:Envelope>
244                         ),
245                 ),
246         ),
247 },
248
249 # Validate DTD
250 # OK
251 {
252         type => "rule",
253         comment => "validateDTD (validate ok)",
254         conf => qq(
255                 SecRuleEngine On
256                 SecRequestBodyAccess On
257                 SecDebugLog $ENV{DEBUG_LOG}
258                 SecDebugLogLevel 9
259                 SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" \\
260                         "phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
261                 SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345
262                 SecRule XML "\@validateDTD $ENV{CONF_DIR}/SoapEnvelope.dtd" \\
263                         "phase:2,deny,id:12345"
264         ),
265         match_log => {
266                 debug => [ qr/XML: Initialising parser.*XML: Parsing complete \(well_formed 1\).*Target value: "\[XML document tree\]".*Successfully validated payload against DTD/s, 1 ],
267                 -debug => [ qr/XML parser error|validation failed|Failed to load/, 1 ],
268                 -error => [ qr/XML parser error|validation failed|Failed to load/, 1 ],
269         },
270         match_response => {
271                 status => qr/^200$/,
272         },
273         request => new HTTP::Request(
274                 POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
275                 [
276                         "Content-Type" => "text/xml",
277                 ],
278                 normalize_raw_request_data(
279                         q(
280                                 <?xml version="1.0" encoding="utf-8"?>
281                                 <!DOCTYPE Envelope SYSTEM "SoapEnvelope.dtd">
282                                 <Envelope>
283                                                 <Body>
284                                                                 <getInput>
285                                                                                 <id type="string">12123</id>
286                                                                 </getInput>
287                                                 </Body>
288                                 </Envelope>
289                         ),
290                 ),
291         ),
292 },
293 # Failed validation
294 {
295         type => "rule",
296         comment => "validateDTD (validate failed)",
297         conf => qq(
298                 SecRuleEngine On
299                 SecRequestBodyAccess On
300                 SecDebugLog $ENV{DEBUG_LOG}
301                 SecDebugLogLevel 9
302                 SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" \\
303                         "phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
304                 SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345
305                 SecRule XML "\@validateDTD $ENV{CONF_DIR}/SoapEnvelope.dtd" \\
306                         "phase:2,deny,id:12345"
307         ),
308         match_log => {
309                 debug => [ qr/XML: Initialising parser.*XML: Parsing complete \(well_formed 1\).*Target value: "\[XML document tree\]".*content does not follow the DTD/s, 1 ],
310                 -debug => [ qr/XML parser error|Failed to load/, 1 ],
311                 -error => [ qr/XML parser error|Failed to load/, 1 ],
312         },
313         match_response => {
314                 status => qr/^403$/,
315         },
316         request => new HTTP::Request(
317                 POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
318                 [
319                         "Content-Type" => "text/xml",
320                 ],
321                 normalize_raw_request_data(
322                         q(
323                                 <?xml version="1.0" encoding="utf-8"?>
324                                 <!DOCTYPE Envelope SYSTEM "SoapEnvelope.dtd">
325                                 <Envelope>
326                                                 <xBody>
327                                                                 <getInput>
328                                                                                 <id type="string">12123</id>
329                                                                 </getInput>
330                                                 </xBody>
331                                 </Envelope>
332                         ),
333                 ),
334         ),
335 },
336 # Bad XML
337 {
338         type => "rule",
339         comment => "validateDTD (bad XML)",
340         conf => qq(
341                 SecRuleEngine On
342                 SecRequestBodyAccess On
343                 SecDebugLog $ENV{DEBUG_LOG}
344                 SecDebugLogLevel 9
345                 SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" \\
346                         "phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
347                 SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345
348                 SecRule XML "\@validateDTD $ENV{CONF_DIR}/SoapEnvelope.dtd" \\
349                         "phase:2,deny,id:12345"
350         ),
351         match_log => {
352                 debug => [ qr/XML: Initialising parser.*XML: Parsing complete \(well_formed 0\).*XML parser error.*validation failed because content is not well formed/s, 1 ],
353                 -debug => [ qr/Failed to load|Successfully validated/, 1 ],
354                 -error => [ qr/Failed to load|Successfully validated/, 1 ],
355         },
356         match_response => {
357                 status => qr/^403$/,
358         },
359         request => new HTTP::Request(
360                 POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
361                 [
362                         "Content-Type" => "text/xml",
363                 ],
364                 normalize_raw_request_data(
365                         q(
366                                 <?xml version="1.0" encoding="utf-8"?>
367                                 <!DOCTYPE Envelope SYSTEM "SoapEnvelope.dtd">
368                                 <Envelop>
369                                                 <Body>
370                                                                 <getInput>
371                                                                                 <id type="string">12123</id>
372                                                                 </getInput>
373                                                 </Body>
374                                 </Envelope>
375                         ),
376                 ),
377         ),
378 },
379 # Bad DTD
380 {
381         type => "rule",
382         comment => "validateDTD (bad DTD)",
383         conf => qq(
384                 SecRuleEngine On
385                 SecRequestBodyAccess On
386                 SecDebugLog $ENV{DEBUG_LOG}
387                 SecDebugLogLevel 9
388                 SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" \\
389                         "phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
390                 SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345
391                 SecRule XML "\@validateDTD $ENV{CONF_DIR}/SoapEnvelope-bad.dtd" \\
392                         "phase:2,deny,id:12345"
393         ),
394         match_log => {
395                 debug => [ qr/XML: Initialising parser.*XML: Parsing complete \(well_formed 1\).*Target value: "\[XML document tree\]".*Failed to load DTD/s, 1 ],
396         },
397         match_response => {
398                 status => qr/^200$/,
399         },
400         request => new HTTP::Request(
401                 POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
402                 [
403                         "Content-Type" => "text/xml",
404                 ],
405                 normalize_raw_request_data(
406                         q(
407                                 <?xml version="1.0" encoding="utf-8"?>
408                                 <!DOCTYPE Envelope SYSTEM "SoapEnvelope.dtd">
409                                 <Envelope>
410                                                 <Body>
411                                                                 <getInput>
412                                                                                 <id type="string">12123</id>
413                                                                 </getInput>
414                                                 </Body>
415                                 </Envelope>
416                         ),
417                 ),
418         ),
419 },