Skip to content

Commit

Permalink
chore: change default value and name to AutoCompleteHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
theseion committed Jun 24, 2023
1 parent 2b2e288 commit 8a78f33
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 98 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions runner/run_input_override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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",
},
}

Expand Down Expand Up @@ -301,18 +301,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)
s.NoError(err, "Failed to apply input overrides")
// 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() {
Expand Down
12 changes: 6 additions & 6 deletions test/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.NoError(err)
s.True(*input.NoAutocompleteHeaders)
s.True(*input.AutocompleteHeaders)
}

func (s *dataTestSuite) TestGetPartialDataFromYAML() {
Expand All @@ -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.NoError(err)
s.Empty(*input.Version)
s.False(*input.NoAutocompleteHeaders)
s.False(*input.AutocompleteHeaders)
}

func (s *dataTestSuite) TestDataTemplateFromYAML() {
Expand All @@ -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{}
Expand All @@ -83,5 +83,5 @@ uri: "/"
data = input.ParseData()
s.Equal([]byte(repeatTestSprig), data)

s.True(*input.NoAutocompleteHeaders)
s.True(*input.AutocompleteHeaders)
}
36 changes: 18 additions & 18 deletions test/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")))
Expand Down
2 changes: 1 addition & 1 deletion test/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var yamlTest = `
-
stage:
input:
no_autocomplete_headers: false
autocomplete_headers: false
dest_addr: "127.0.0.1"
port: 80
headers:
Expand Down
41 changes: 22 additions & 19 deletions test/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 }()
}
Loading

0 comments on commit 8a78f33

Please sign in to comment.