diff --git a/README.md b/README.md index c9079e72..505ac244 100644 --- a/README.md +++ b/README.md @@ -321,7 +321,7 @@ Tests can be altered using four lists: - `headers`: overrides headers, the format is a map of strings - `method`: overrides the method used to perform the request - `data`: overrides data sent in the request - - `no_autocomplete_headers`: prevent header autocompletion (currently sets `Connection: close` and `Content-Length` for requests with body data) + - `autocomplete_headers`: prevent header autocompletion (currently sets `Connection: close` and `Content-Length` for requests with body data) - `encodedrequest`: overrides base64 encoded request - `rawrequest`: permits to provide a raw request. `method`, `uri` and `version` values will be ignored - `ignore` is for tests you want to ignore. You should add a comment on why you ignore the test diff --git a/runner/run.go b/runner/run.go index 1d266607..68914f0b 100644 --- a/runner/run.go +++ b/runner/run.go @@ -360,7 +360,7 @@ func getRequestFromTest(testInput test.Input) *ftwhttp.Request { // If we use raw or encoded request, then we don't use other fields if raw != nil { - req = ftwhttp.NewRawRequest(raw, !*testInput.NoAutocompleteHeaders) + req = ftwhttp.NewRawRequest(raw, !*testInput.AutocompleteHeaders) } else { rline := &ftwhttp.RequestLine{ Method: testInput.GetMethod(), @@ -371,7 +371,7 @@ func getRequestFromTest(testInput test.Input) *ftwhttp.Request { data := testInput.ParseData() // create a new request req = ftwhttp.NewRequest(rline, testInput.Headers, - data, !*testInput.NoAutocompleteHeaders) + data, !*testInput.AutocompleteHeaders) } return req diff --git a/runner/run_input_override_test.go b/runner/run_input_override_test.go index b342ae65..9a41dea3 100644 --- a/runner/run_input_override_test.go +++ b/runner/run_input_override_test.go @@ -28,7 +28,7 @@ var configTemplate = ` testoverride: input: {{ with .StopMagic }}stop_magic: {{ . }}{{ end }} - {{ with .NoAutocompleteHeaders }}no_autocomplete_headers: {{ . }}{{ end }} + {{ with .AutocompleteHeaders }}autocomplete_headers: {{ . }}{{ end }} {{ with .BrokenConfig }}this_does_not_exist: "test"{{ end }} {{ with .Port }}port: {{ . }}{{ end }} {{ with .DestAddr }}dest_addr: {{ . }}{{ end }} @@ -95,8 +95,8 @@ var overrideConfigMap = map[string]interface{}{ "TestApplyInputOverrideStopMagic": map[string]interface{}{ "StopMagic": "true", }, - "TestApplyInputOverrideNoAutocompleteHeaders": map[string]interface{}{ - "NoAutocompleteHeaders": "true", + "TestApplyInputOverrideAutocompleteHeaders": map[string]interface{}{ + "AutocompleteHeaders": "true", }, } @@ -299,18 +299,18 @@ func (s *inputOverrideTestSuite) TestApplyInputOverrideStopMagic() { s.Equal(overrideStopMagic, *testInput.StopMagic, "`StopMagic` should have been overridden") } -func (s *inputOverrideTestSuite) TestApplyInputOverrideNoAutocompleteHeaders() { - noAutocompleteHeadersBool, err := getOverrideConfigValue("NoAutocompleteHeaders") +func (s *inputOverrideTestSuite) TestApplyInputOverrideAutocompleteHeaders() { + autocompleteHeadersBool, err := getOverrideConfigValue("AutocompleteHeaders") s.NoError(err, "cannot get override value") - overrideNoAutocompleteHeaders, err := strconv.ParseBool(noAutocompleteHeadersBool) - s.NoError(err, "Failed to parse `NoAutocompleteHeaders` override value") + overrideAutocompleteHeaders, err := strconv.ParseBool(autocompleteHeadersBool) + s.NoError(err, "Failed to parse `AutocompleteHeaders` override value") testInput := test.Input{ - NoAutocompleteHeaders: func() *bool { b := false; return &b }(), + AutocompleteHeaders: func() *bool { b := false; return &b }(), } test.ApplyInputOverrides(&s.cfg.TestOverride.Overrides, &testInput) // nolint - s.Equal(overrideNoAutocompleteHeaders, *testInput.NoAutocompleteHeaders, "`NoAutocompleteHeaders` should have been overridden") + s.Equal(overrideAutocompleteHeaders, *testInput.AutocompleteHeaders, "`AutocompleteHeaders` should have been overridden") } func (s *inputOverrideTestSuite) TestApplyInputOverrideEncodedRequest() { diff --git a/test/data_test.go b/test/data_test.go index b140f3b0..50369c63 100644 --- a/test/data_test.go +++ b/test/data_test.go @@ -29,13 +29,13 @@ Host: "localhost" Content-Type: "application/x-www-form-urlencoded" data: "hi=test" protocol: "http" -no_autocomplete_headers: true +autocomplete_headers: true uri: "/" ` input := Input{} err := yaml.Unmarshal([]byte(yamlString), &input) s.Require().NoError(err) - s.True(*input.NoAutocompleteHeaders) + s.True(*input.AutocompleteHeaders) } func (s *dataTestSuite) TestGetPartialDataFromYAML() { @@ -50,14 +50,14 @@ Content-Type: "application/x-www-form-urlencoded" data: "hi=test" version: "" protocol: "http" -no_autocomplete_headers: false +autocomplete_headers: false uri: "/" ` input := Input{} err := yaml.Unmarshal([]byte(yamlString), &input) s.Require().NoError(err) s.Empty(*input.Version) - s.False(*input.NoAutocompleteHeaders) + s.False(*input.AutocompleteHeaders) } func (s *dataTestSuite) TestDataTemplateFromYAML() { @@ -72,7 +72,7 @@ Content-Type: "application/x-www-form-urlencoded" data: 'foo=%3d{{ "+" | repeat 34 }}' version: "" protocol: "http" -no_autocomplete_headers: true +autocomplete_headers: true uri: "/" ` input := Input{} @@ -83,5 +83,5 @@ uri: "/" data = input.ParseData() s.Equal([]byte(repeatTestSprig), data) - s.True(*input.NoAutocompleteHeaders) + s.True(*input.AutocompleteHeaders) } diff --git a/test/defaults_test.go b/test/defaults_test.go index 583cb135..71c3c401 100644 --- a/test/defaults_test.go +++ b/test/defaults_test.go @@ -21,10 +21,10 @@ func getTestInputDefaults() *Input { data := "My Data" inputDefaults := Input{ - Headers: make(ftwhttp.Header), - Data: &data, - SaveCookie: func() *bool { b := false; return &b }(), - NoAutocompleteHeaders: func() *bool { b := false; return &b }(), + Headers: make(ftwhttp.Header), + Data: &data, + SaveCookie: func() *bool { b := false; return &b }(), + AutocompleteHeaders: func() *bool { b := false; return &b }(), } return &inputDefaults } @@ -38,17 +38,17 @@ func getTestExampleInput() *Input { version := "HTTP/1.1" inputTest := Input{ - DestAddr: &destaddr, - Port: &port, - Protocol: &protocol, - URI: &uri, - Version: &version, - Headers: make(ftwhttp.Header), - Method: &method, - Data: nil, - EncodedRequest: "TXkgRGF0YQo=", - SaveCookie: func() *bool { b := false; return &b }(), - NoAutocompleteHeaders: func() *bool { b := false; return &b }(), + DestAddr: &destaddr, + Port: &port, + Protocol: &protocol, + URI: &uri, + Version: &version, + Headers: make(ftwhttp.Header), + Method: &method, + Data: nil, + EncodedRequest: "TXkgRGF0YQo=", + SaveCookie: func() *bool { b := false; return &b }(), + AutocompleteHeaders: func() *bool { b := false; return &b }(), } return &inputTest @@ -75,8 +75,8 @@ Keep-Alive: 300 Proxy-Connection: keep-alive User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) `, - SaveCookie: func() *bool { b := false; return &b }(), - NoAutocompleteHeaders: func() *bool { b := true; return &b }(), + SaveCookie: func() *bool { b := false; return &b }(), + AutocompleteHeaders: func() *bool { b := true; return &b }(), } return &inputTest @@ -128,7 +128,7 @@ func (s *defaultsTestSuite) TestDefaultGetters() { func (s *defaultsTestSuite) TestRaw() { raw := getRawInput() - s.True(*raw.NoAutocompleteHeaders) + s.True(*raw.AutocompleteHeaders) request, _ := raw.GetRawRequest() s.NotEqual(2, bytes.Index(request, []byte("Acunetix"))) diff --git a/test/files_test.go b/test/files_test.go index 52fc9a18..3219dd1c 100644 --- a/test/files_test.go +++ b/test/files_test.go @@ -23,7 +23,7 @@ var yamlTest = ` - stage: input: - no_autocomplete_headers: false + autocomplete_headers: false dest_addr: "127.0.0.1" port: 80 headers: diff --git a/test/types.go b/test/types.go index 051fe59b..e37036a6 100644 --- a/test/types.go +++ b/test/types.go @@ -17,11 +17,11 @@ type Input struct { Method *string `yaml:"method,omitempty"` Data *string `yaml:"data,omitempty"` SaveCookie *bool `yaml:"save_cookie,omitempty"` - // Deprecated: replaced with NoAutocompleteHeaders - StopMagic *bool `yaml:"stop_magic"` - NoAutocompleteHeaders *bool `yaml:"no_autocomplete_headers"` - EncodedRequest string `yaml:"encoded_request,omitempty"` - RAWRequest string `yaml:"raw_request,omitempty"` + // Deprecated: replaced with AutocompleteHeaders + StopMagic *bool `yaml:"stop_magic"` + AutocompleteHeaders *bool `yaml:"autocomplete_headers"` + EncodedRequest string `yaml:"encoded_request,omitempty"` + RAWRequest string `yaml:"raw_request,omitempty"` } // Overrides represents the overridden inputs that have to be applied to tests @@ -35,9 +35,9 @@ type Overrides struct { Method *string `yaml:"method,omitempty" koanf:"method,omitempty"` Data *string `yaml:"data,omitempty" koanf:"data,omitempty"` SaveCookie *bool `yaml:"save_cookie,omitempty" koanf:"save_cookie,omitempty"` - // Deprecated: replaced with NoAutocompleteHeaders + // Deprecated: replaced with AutocompleteHeaders StopMagic *bool `yaml:"stop_magic" koanf:"stop_magic,omitempty"` - NoAutocompleteHeaders *bool `yaml:"no_autocomplete_headers" koanf:"no_autocomplete_headers,omitempty"` + AutocompleteHeaders *bool `yaml:"autocomplete_headers" koanf:"autocomplete_headers,omitempty"` EncodedRequest *string `yaml:"encoded_request,omitempty" koanf:"encoded_request,omitempty"` RAWRequest *string `yaml:"raw_request,omitempty" koanf:"raw_request,omitempty"` OverrideEmptyHostHeader *bool `yaml:"override_empty_host_header,omitempty" koanf:"override_empty_host_header,omitempty"` @@ -84,7 +84,7 @@ func ApplyInputOverrides(overrides *Overrides, input *Input) { applySimpleOverrides(overrides, input) applyDestAddrOverride(overrides, input) applyHeadersOverride(overrides, input) - postProcessNoAutocompleteHeaders(overrides.NoAutocompleteHeaders, overrides.StopMagic, input) + postProcessAutocompleteHeaders(overrides.AutocompleteHeaders, overrides.StopMagic, input) } func applyDestAddrOverride(overrides *Overrides, input *Input) { @@ -165,19 +165,22 @@ func postLoadStage(stage *Stage) { } func postLoadInput(input *Input) { - postProcessNoAutocompleteHeaders(input.NoAutocompleteHeaders, input.StopMagic, input) + postProcessAutocompleteHeaders(input.AutocompleteHeaders, input.StopMagic, input) } -func postProcessNoAutocompleteHeaders(noAutocompleteHeaders *bool, stopMagic *bool, input *Input) { - noAutocompleteHeadersMissing := noAutocompleteHeaders == nil +func postProcessAutocompleteHeaders(autocompleteHeaders *bool, stopMagic *bool, input *Input) { + autocompleteHeadersMissing := autocompleteHeaders == nil stopMagicMissing := stopMagic == nil - finalValue := false - - if noAutocompleteHeadersMissing && !stopMagicMissing { - finalValue = *stopMagic - } else if !noAutocompleteHeadersMissing { - finalValue = *noAutocompleteHeaders + // default value + finalValue := true + + if autocompleteHeadersMissing && !stopMagicMissing { + // StopMagic has the inverse boolean logic + finalValue = !*stopMagic + } else if !autocompleteHeadersMissing { + finalValue = *autocompleteHeaders } - input.NoAutocompleteHeaders = &finalValue - input.StopMagic = &finalValue + input.AutocompleteHeaders = &finalValue + // StopMagic has the inverse boolean logic + input.StopMagic = func() *bool { b := !finalValue; return &b }() } diff --git a/test/types_test.go b/test/types_test.go index 0d4d614b..ef1d6ae1 100644 --- a/test/types_test.go +++ b/test/types_test.go @@ -14,7 +14,7 @@ func TestTypesTestSuite(t *testing.T) { suite.Run(t, new(typesTestSuite)) } -var noAutocompleteHeadersDefaultYaml = `--- +var autocompleteHeadersDefaultYaml = `--- meta: author: "tester" enabled: true @@ -64,7 +64,7 @@ tests: status: [200] ` -var noAutocompleteHeadersFalseYaml = `--- +var autocompleteHeadersFalseYaml = `--- meta: author: "tester" enabled: true @@ -76,7 +76,7 @@ tests: stages: - stage: input: - no_autocomplete_headers: false + autocomplete_headers: false dest_addr: "localhost" headers: User-Agent: "ModSecurity CRS 3 Tests" @@ -90,7 +90,7 @@ tests: stages: - stage: input: - no_autocomplete_headers: false + autocomplete_headers: false stop_magic: true dest_addr: "localhost" headers: @@ -105,7 +105,7 @@ tests: stages: - stage: input: - no_autocomplete_headers: false + autocomplete_headers: false stop_magic: false dest_addr: "localhost" headers: @@ -117,7 +117,7 @@ tests: status: [200] ` -var noAutocompleteHeadersTrueYaml = `--- +var autocompleteHeadersTrueYaml = `--- meta: author: "tester" enabled: true @@ -129,7 +129,7 @@ tests: stages: - stage: input: - no_autocomplete_headers: true + autocomplete_headers: true dest_addr: "localhost" headers: User-Agent: "ModSecurity CRS 3 Tests" @@ -143,7 +143,7 @@ tests: stages: - stage: input: - no_autocomplete_headers: true + autocomplete_headers: true stop_magic: true dest_addr: "localhost" headers: @@ -158,7 +158,7 @@ tests: stages: - stage: input: - no_autocomplete_headers: true + autocomplete_headers: true stop_magic: false dest_addr: "localhost" headers: @@ -170,82 +170,82 @@ tests: status: [200] ` -func (s *typesTestSuite) TestNoAutocompleteHeadersDefault_StopMagicDefault() { - test, err := GetTestFromYaml([]byte(noAutocompleteHeadersDefaultYaml)) +func (s *typesTestSuite) TestAutocompleteHeadersDefault_StopMagicDefault() { + test, err := GetTestFromYaml([]byte(autocompleteHeadersDefaultYaml)) s.NoError(err, "Parsing YAML shouldn't fail") input := test.Tests[0].Stages[0].Stage.Input - s.False(*input.NoAutocompleteHeaders) + s.True(*input.AutocompleteHeaders) s.False(*input.StopMagic) } -func (s *typesTestSuite) TestNoAutocompleteHeadersDefault_StopMagicTrue() { - test, err := GetTestFromYaml([]byte(noAutocompleteHeadersDefaultYaml)) +func (s *typesTestSuite) TestAutocompleteHeadersDefault_StopMagicTrue() { + test, err := GetTestFromYaml([]byte(autocompleteHeadersDefaultYaml)) s.NoError(err, "Parsing YAML shouldn't fail") input := test.Tests[1].Stages[0].Stage.Input - s.True(*input.NoAutocompleteHeaders) + s.False(*input.AutocompleteHeaders) s.True(*input.StopMagic) } -func (s *typesTestSuite) TestNoAutocompleteHeadersDefault_StopMagicFalse() { - test, err := GetTestFromYaml([]byte(noAutocompleteHeadersDefaultYaml)) +func (s *typesTestSuite) TestAutocompleteHeadersDefault_StopMagicFalse() { + test, err := GetTestFromYaml([]byte(autocompleteHeadersDefaultYaml)) s.NoError(err, "Parsing YAML shouldn't fail") input := test.Tests[2].Stages[0].Stage.Input - s.False(*input.NoAutocompleteHeaders) + s.True(*input.AutocompleteHeaders) s.False(*input.StopMagic) } -func (s *typesTestSuite) TestNoAutocompleteHeadersFalse_StopMagicDefault() { - test, err := GetTestFromYaml([]byte(noAutocompleteHeadersFalseYaml)) +func (s *typesTestSuite) TestAutocompleteHeadersFalse_StopMagicDefault() { + test, err := GetTestFromYaml([]byte(autocompleteHeadersFalseYaml)) s.NoError(err, "Parsing YAML shouldn't fail") input := test.Tests[0].Stages[0].Stage.Input - s.False(*input.NoAutocompleteHeaders) - s.False(*input.StopMagic) + s.False(*input.AutocompleteHeaders) + s.True(*input.StopMagic) } -func (s *typesTestSuite) TestNoAutocompleteHeadersFalse_StopMagicTrue() { - test, err := GetTestFromYaml([]byte(noAutocompleteHeadersFalseYaml)) +func (s *typesTestSuite) TestAutocompleteHeadersFalse_StopMagicTrue() { + test, err := GetTestFromYaml([]byte(autocompleteHeadersFalseYaml)) s.NoError(err, "Parsing YAML shouldn't fail") input := test.Tests[1].Stages[0].Stage.Input - s.False(*input.NoAutocompleteHeaders) - s.False(*input.StopMagic) + s.False(*input.AutocompleteHeaders) + s.True(*input.StopMagic) } -func (s *typesTestSuite) TestNoAutocompleteHeadersFalse_StopMagicFalse() { - test, err := GetTestFromYaml([]byte(noAutocompleteHeadersFalseYaml)) +func (s *typesTestSuite) TestAutocompleteHeadersFalse_StopMagicFalse() { + test, err := GetTestFromYaml([]byte(autocompleteHeadersFalseYaml)) s.NoError(err, "Parsing YAML shouldn't fail") input := test.Tests[2].Stages[0].Stage.Input - s.False(*input.NoAutocompleteHeaders) - s.False(*input.StopMagic) + s.False(*input.AutocompleteHeaders) + s.True(*input.StopMagic) } -func (s *typesTestSuite) TestNoAutocompleteHeadersTrue_StopMagicDefault() { - test, err := GetTestFromYaml([]byte(noAutocompleteHeadersTrueYaml)) +func (s *typesTestSuite) TestAutocompleteHeadersTrue_StopMagicDefault() { + test, err := GetTestFromYaml([]byte(autocompleteHeadersTrueYaml)) s.NoError(err, "Parsing YAML shouldn't fail") input := test.Tests[0].Stages[0].Stage.Input - s.True(*input.NoAutocompleteHeaders) - s.True(*input.StopMagic) + s.True(*input.AutocompleteHeaders) + s.False(*input.StopMagic) } -func (s *typesTestSuite) TestNoAutocompleteHeadersTrue_StopMagicTrue() { - test, err := GetTestFromYaml([]byte(noAutocompleteHeadersTrueYaml)) +func (s *typesTestSuite) TestAutocompleteHeadersTrue_StopMagicTrue() { + test, err := GetTestFromYaml([]byte(autocompleteHeadersTrueYaml)) s.NoError(err, "Parsing YAML shouldn't fail") input := test.Tests[1].Stages[0].Stage.Input - s.True(*input.NoAutocompleteHeaders) - s.True(*input.StopMagic) + s.True(*input.AutocompleteHeaders) + s.False(*input.StopMagic) } -func (s *typesTestSuite) TestNoAutocompleteHeadersTrue_StopMagicFalse() { - test, err := GetTestFromYaml([]byte(noAutocompleteHeadersTrueYaml)) +func (s *typesTestSuite) TestAutocompleteHeadersTrue_StopMagicFalse() { + test, err := GetTestFromYaml([]byte(autocompleteHeadersTrueYaml)) s.NoError(err, "Parsing YAML shouldn't fail") input := test.Tests[2].Stages[0].Stage.Input - s.True(*input.NoAutocompleteHeaders) - s.True(*input.StopMagic) + s.True(*input.AutocompleteHeaders) + s.False(*input.StopMagic) }