Skip to content

Commit f874855

Browse files
committed
Add bad format tests
1 parent 278d6c0 commit f874855

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed

tests/regression/rule/10-xml.t

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,142 @@
428428
),
429429
),
430430
},
431+
{
432+
type => "rule",
433+
comment => "xml ProcessPartial, bad format and whole body before limit",
434+
conf => qq(
435+
SecRuleEngine On
436+
SecRequestBodyAccess On
437+
SecRequestBodyLimitAction ProcessPartial
438+
SecRequestBodyLimit 57
439+
SecRequestBodyNoFilesLimit 57
440+
SecXmlExternalEntity Off
441+
SecDebugLog $ENV{DEBUG_LOG}
442+
SecDebugLogLevel 9
443+
SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" "id:500005, \\
444+
phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
445+
SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345,id:500006
446+
SecRule REQBODY_ERROR "!\@eq 0" \\
447+
"id:'500007', phase:2,t:none,log,deny,status:400,msg:'Failed to parse request body.',logdata:'%{reqbody_error_msg}',severity:2"
448+
SecRule XML:/* "bad_value" "id:'500008',phase:2,t:none,deny"
449+
),
450+
match_log => {
451+
error => [ qr/Access denied with code 400 \(phase 2\). Match of "eq 0" against "REQBODY_ERROR" required\./, 1 ],
452+
},
453+
match_response => {
454+
status => qr/^400$/,
455+
},
456+
request => new HTTP::Request(
457+
POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
458+
[
459+
"Content-Type" => "text/xml",
460+
"Content-Length" => "57",
461+
],
462+
'<?xml version="1.0" encoding="utf-8"?><a><b>value</b></a ',
463+
),
464+
},
465+
{
466+
type => "rule",
467+
comment => "xml ProcessPartial, bad format and length exceeds limit",
468+
conf => qq(
469+
SecRuleEngine On
470+
SecRequestBodyAccess On
471+
SecRequestBodyLimitAction ProcessPartial
472+
SecRequestBodyLimit 57
473+
SecRequestBodyNoFilesLimit 57
474+
SecXmlExternalEntity Off
475+
SecDebugLog $ENV{DEBUG_LOG}
476+
SecDebugLogLevel 9
477+
SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" "id:500005, \\
478+
phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
479+
SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345,id:500006
480+
SecRule REQBODY_ERROR "!\@eq 0" \\
481+
"id:'500007', phase:2,t:none,log,deny,status:400,msg:'Failed to parse request body.',logdata:'%{reqbody_error_msg}',severity:2"
482+
SecRule XML:/* "bad_value" "id:'500008',phase:2,t:none,deny"
483+
),
484+
match_log => {
485+
-error => [ qr/Access denied with code 400 \(phase 2\). Match of "eq 0" against "REQBODY_ERROR" required\./, 1 ],
486+
},
487+
match_response => {
488+
status => qr/^200$/,
489+
},
490+
request => new HTTP::Request(
491+
POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
492+
[
493+
"Content-Type" => "text/xml",
494+
"Content-Length" => "58",
495+
],
496+
'<?xml version="1.0" encoding="utf-8"?><a><b>value</b></a ',
497+
),
498+
},
499+
{
500+
type => "rule",
501+
comment => "xml ProcessPartial, bad format and whole body before limit, no declaration",
502+
conf => qq(
503+
SecRuleEngine On
504+
SecRequestBodyAccess On
505+
SecRequestBodyLimitAction ProcessPartial
506+
SecRequestBodyLimit 19
507+
SecRequestBodyNoFilesLimit 19
508+
SecXmlExternalEntity Off
509+
SecDebugLog $ENV{DEBUG_LOG}
510+
SecDebugLogLevel 9
511+
SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" "id:500005, \\
512+
phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
513+
SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345,id:500006
514+
SecRule REQBODY_ERROR "!\@eq 0" \\
515+
"id:'500007', phase:2,t:none,log,deny,status:400,msg:'Failed to parse request body.',logdata:'%{reqbody_error_msg}',severity:2"
516+
SecRule XML:/* "bad_value" "id:'500008',phase:2,t:none,deny"
517+
),
518+
match_log => {
519+
error => [ qr/Access denied with code 400 \(phase 2\). Match of "eq 0" against "REQBODY_ERROR" required\./, 1 ],
520+
},
521+
match_response => {
522+
status => qr/^400$/,
523+
},
524+
request => new HTTP::Request(
525+
POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
526+
[
527+
"Content-Type" => "text/xml",
528+
"Content-Length" => "19",
529+
],
530+
'<a><b>value</b></a ',
531+
),
532+
},
533+
{
534+
type => "rule",
535+
comment => "xml ProcessPartial, bad format and length exceeds limit, no declaration",
536+
conf => qq(
537+
SecRuleEngine On
538+
SecRequestBodyAccess On
539+
SecRequestBodyLimitAction ProcessPartial
540+
SecRequestBodyLimit 19
541+
SecRequestBodyNoFilesLimit 19
542+
SecXmlExternalEntity Off
543+
SecDebugLog $ENV{DEBUG_LOG}
544+
SecDebugLogLevel 9
545+
SecRule REQUEST_HEADERS:Content-Type "^text/xml\$" "id:500005, \\
546+
phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML"
547+
SecRule REQBODY_PROCESSOR "!^XML\$" nolog,pass,skipAfter:12345,id:500006
548+
SecRule REQBODY_ERROR "!\@eq 0" \\
549+
"id:'500007', phase:2,t:none,log,deny,status:400,msg:'Failed to parse request body.',logdata:'%{reqbody_error_msg}',severity:2"
550+
SecRule XML:/* "bad_value" "id:'500008',phase:2,t:none,deny"
551+
),
552+
match_log => {
553+
-error => [ qr/Access denied with code 400 \(phase 2\). Match of "eq 0" against "REQBODY_ERROR" required\./, 1 ],
554+
},
555+
match_response => {
556+
status => qr/^200$/,
557+
},
558+
request => new HTTP::Request(
559+
POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
560+
[
561+
"Content-Type" => "text/xml",
562+
"Content-Length" => "20",
563+
],
564+
'<a><b>value</b></a ',
565+
),
566+
},
431567
{
432568
type => "rule",
433569
comment => "xml ProcessPartial, bad value and whole body before limit",

tests/regression/rule/15-json.t

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,73 @@
259259
),
260260
),
261261
},
262+
{
263+
type => "rule",
264+
comment => "LimitAction ProcessPartial, bad format and whole body before limit",
265+
conf => qq(
266+
SecRuleEngine On
267+
SecRequestBodyAccess On
268+
SecRequestBodyLimitAction ProcessPartial
269+
SecRequestBodyNoFilesLimit 21
270+
SecRequestBodyLimit 21
271+
SecDebugLog $ENV{DEBUG_LOG}
272+
SecDebugLogLevel 9
273+
SecRule REQUEST_HEADERS:Content-Type "application/json" \\
274+
"id:'200001',phase:1,t:none,t:lowercase,pass,nolog,ctl:requestBodyProcessor=JSON"
275+
SecRule REQBODY_ERROR "!\@eq 0" \\
276+
"id:'200002', phase:2,t:none,log,deny,status:400,msg:'Failed to parse request body.',logdata:'%{reqbody_error_msg}',severity:2"
277+
SecRule ARGS "bad_value" "id:'200003',phase:2,t:none,deny"
278+
),
279+
match_log => {
280+
error => [ qr/Access denied with code 400 \(phase 2\)\. Match of "eq 0" against "REQBODY_ERROR" required\./, 1 ],
281+
debug => [ qr/Adding JSON argument 'b' with value 'value'|JSON support was not enabled/, 1 ],
282+
-debug => [ qr/JSON: Allow partial processing of request body|JSON support was not enabled/, 1 ],
283+
},
284+
match_response => {
285+
status => qr/^400$/,
286+
},
287+
request => new HTTP::Request(
288+
POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
289+
[
290+
"Content-Type" => "application/json",
291+
"Content-Length" => "21",
292+
],
293+
q({"a":1234,"b":"value"),
294+
),
295+
},
296+
{
297+
type => "rule",
298+
comment => "LimitAction ProcessPartial, bad format and length exceeds limit",
299+
conf => qq(
300+
SecRuleEngine On
301+
SecRequestBodyAccess On
302+
SecRequestBodyLimitAction ProcessPartial
303+
SecRequestBodyNoFilesLimit 21
304+
SecRequestBodyLimit 21
305+
SecDebugLog $ENV{DEBUG_LOG}
306+
SecDebugLogLevel 9
307+
SecRule REQUEST_HEADERS:Content-Type "application/json" \\
308+
"id:'200001',phase:1,t:none,t:lowercase,pass,nolog,ctl:requestBodyProcessor=JSON"
309+
SecRule REQBODY_ERROR "!\@eq 0" \\
310+
"id:'200002', phase:2,t:none,log,deny,status:400,msg:'Failed to parse request body.',logdata:'%{reqbody_error_msg}',severity:2"
311+
SecRule ARGS "bad_value" "id:'200003',phase:2,t:none,deny"
312+
),
313+
match_log => {
314+
-error => [ qr/Access denied with code 400 \(phase 2\)\. Match of "eq 0" against "REQBODY_ERROR" required\./, 1 ],
315+
debug => [ qr/Adding JSON argument 'b' with value 'value'|JSON support was not enabled/, 1 ],
316+
},
317+
match_response => {
318+
status => qr/^200$/,
319+
},
320+
request => new HTTP::Request(
321+
POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
322+
[
323+
"Content-Type" => "application/json",
324+
"Content-Length" => "22",
325+
],
326+
q({"a":1234,"b":"value" ),
327+
),
328+
},
262329
{
263330
type => "rule",
264331
comment => "LimitAction ProcessPartial, bad value and whole body before limit",

0 commit comments

Comments
 (0)