@@ -352,7 +352,7 @@ func TestLifecycle(t *testing.T) {
352
352
{
353
353
name : "response body accepted" ,
354
354
inlineRules : `
355
- SecRuleEngine On\nSecResponseBodyAccess On\nSecRule RESPONSE_BODY \"@contains pooh\" \"id:101,phase:4,t:lowercase,deny\"
355
+ SecRuleEngine On\nSecResponseBodyAccess On\nSecResponseBodyMimeType text/plain\ nSecRule RESPONSE_BODY \"@contains pooh\" \"id:101,phase:4,t:lowercase,deny\"
356
356
` ,
357
357
requestHdrsAction : types .ActionContinue ,
358
358
requestBodyAction : types .ActionContinue ,
@@ -385,7 +385,7 @@ func TestLifecycle(t *testing.T) {
385
385
{
386
386
name : "response body accepted, no response body access" ,
387
387
inlineRules : `
388
- SecRuleEngine On\nSecResponseBodyAccess Off\nSecRule RESPONSE_BODY \"@contains hello\" \"id:101,phase:4,t:lowercase,deny\"
388
+ SecRuleEngine On\nSecResponseBodyAccess Off\nSecResponseBodyMimeType text/plain\ nSecRule RESPONSE_BODY \"@contains hello\" \"id:101,phase:4,t:lowercase,deny\"
389
389
` ,
390
390
requestHdrsAction : types .ActionContinue ,
391
391
requestBodyAction : types .ActionContinue ,
@@ -396,7 +396,7 @@ func TestLifecycle(t *testing.T) {
396
396
{
397
397
name : "response body accepted, payload above process partial" ,
398
398
inlineRules : `
399
- SecRuleEngine On\nSecResponseBodyAccess On\nSecResponseBodyLimit 2\nSecResponseBodyLimitAction ProcessPartial\nSecRule RESPONSE_BODY \"@contains hello\" \"id:101,phase:4,t:lowercase,deny\"
399
+ SecRuleEngine On\nSecResponseBodyAccess On\nSecResponseBodyLimit 2\nSecResponseBodyLimitAction ProcessPartial\nSecResponseBodyMimeType text/plain\ nSecRule RESPONSE_BODY \"@contains hello\" \"id:101,phase:4,t:lowercase,deny\"
400
400
` ,
401
401
requestHdrsAction : types .ActionContinue ,
402
402
requestBodyAction : types .ActionContinue ,
@@ -407,7 +407,7 @@ func TestLifecycle(t *testing.T) {
407
407
{
408
408
name : "response body denied, above limits" ,
409
409
inlineRules : `
410
- SecRuleEngine On\nSecResponseBodyAccess On\nSecResponseBodyLimit 2\nSecResponseBodyLimitAction Reject\nSecRule RESPONSE_BODY \"@contains hello\" \"id:101,phase:4,t:lowercase,deny\"
410
+ SecRuleEngine On\nSecResponseBodyAccess On\nSecResponseBodyLimit 2\nSecResponseBodyLimitAction Reject\nSecResponseBodyMimeType text/plain\ nSecRule RESPONSE_BODY \"@contains hello\" \"id:101,phase:4,t:lowercase,deny\"
411
411
` ,
412
412
requestHdrsAction : types .ActionContinue ,
413
413
requestBodyAction : types .ActionContinue ,
@@ -796,38 +796,66 @@ func TestPerAuthorityDirectives(t *testing.T) {
796
796
}
797
797
798
798
func TestEmptyBody (t * testing.T ) {
799
- vmTest (t , func (t * testing.T , vm types.VMContext ) {
800
- opt := proxytest .
801
- NewEmulatorOption ().
802
- WithVMContext (vm ).
803
- WithPluginConfiguration ([]byte (`{"directives_map": {"default": [ "SecRequestBodyAccess On", "SecResponseBodyAccess On" ]}, "default_directives": "default"}` ))
804
-
805
- host , reset := proxytest .NewHostEmulator (opt )
806
- defer reset ()
799
+ testCases := []struct {
800
+ title string
801
+ isRespBodyProcessable bool
802
+ }{
803
+ {
804
+ title : "Response body processable" ,
805
+ isRespBodyProcessable : true ,
806
+ },
807
+ {
808
+ title : "Response body NOT processable" ,
809
+ isRespBodyProcessable : false ,
810
+ },
811
+ }
807
812
808
- require .Equal (t , types .OnPluginStartStatusOK , host .StartPlugin ())
813
+ for _ , tc := range testCases {
814
+ t .Run (tc .title , func (t * testing.T ) {
815
+ vmTest (t , func (t * testing.T , vm types.VMContext ) {
816
+ opt := proxytest .
817
+ NewEmulatorOption ().
818
+ WithVMContext (vm ).
819
+ WithPluginConfiguration ([]byte (`{"directives_map": {"default": [ "SecRequestBodyAccess On", "SecResponseBodyAccess On", "SecResponseBodyMimeType text/plain"]}, "default_directives": "default"}` ))
809
820
810
- id := host .InitializeHttpContext ()
821
+ host , reset := proxytest .NewHostEmulator (opt )
822
+ defer reset ()
811
823
812
- host .CallOnRequestHeaders (id , [][2 ]string {
813
- {":path" , "/hello" },
814
- {":method" , "GET" },
815
- {":authority" , "localhost" },
816
- }, false )
824
+ require .Equal (t , types .OnPluginStartStatusOK , host .StartPlugin ())
817
825
818
- action := host .CallOnRequestBody (id , []byte {}, false )
819
- require .Equal (t , types .ActionPause , action )
820
- action = host .CallOnRequestBody (id , []byte {}, true )
821
- require .Equal (t , types .ActionContinue , action )
826
+ id := host .InitializeHttpContext ()
827
+ host .CallOnRequestHeaders (id , [][2 ]string {
828
+ {":path" , "/hello" },
829
+ {":method" , "GET" },
830
+ {":authority" , "localhost" },
831
+ }, false )
832
+ action := host .CallOnRequestBody (id , []byte {}, false )
833
+ require .Equal (t , types .ActionPause , action )
834
+ action = host .CallOnRequestBody (id , []byte {}, true )
835
+ require .Equal (t , types .ActionContinue , action )
822
836
823
- action = host .CallOnResponseBody (id , []byte {}, false )
824
- require .Equal (t , types .ActionPause , action )
825
- action = host .CallOnResponseBody (id , []byte {}, true )
826
- require .Equal (t , types .ActionContinue , action )
837
+ if tc .isRespBodyProcessable {
838
+ host .CallOnResponseHeaders (id , [][2 ]string {
839
+ {":status" , "200" },
840
+ {"content-length" , "0" },
841
+ {"content-type" , "text/plain" }}, false )
827
842
828
- logs := strings .Join (host .GetCriticalLogs (), "\n " )
829
- require .Empty (t , logs )
830
- })
843
+ action = host .CallOnResponseBody (id , []byte {}, false )
844
+ require .Equal (t , types .ActionPause , action )
845
+ action = host .CallOnResponseBody (id , []byte {}, true )
846
+ require .Equal (t , types .ActionContinue , action )
847
+ } else {
848
+ // If the ResponseBodyMimeType is not matched, we should just continue and not store the body
849
+ action = host .CallOnResponseBody (id , []byte {}, false )
850
+ require .Equal (t , types .ActionContinue , action )
851
+ action = host .CallOnResponseBody (id , []byte {}, true )
852
+ require .Equal (t , types .ActionContinue , action )
853
+ }
854
+ logs := strings .Join (host .GetCriticalLogs (), "\n " )
855
+ require .Empty (t , logs )
856
+ })
857
+ })
858
+ }
831
859
}
832
860
833
861
func TestLogError (t * testing.T ) {
0 commit comments