Skip to content

Commit

Permalink
feat!: replace StopMagic with NoAutocompleteHeaders
Browse files Browse the repository at this point in the history
BREAKING CHANGE: clients will need to replace `stop_magic` in their test
files with `no_autocomplete_headers`.
  • Loading branch information
theseion committed Jun 10, 2023
1 parent 0bf56f9 commit 02f1a51
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 17 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
- `stopmagic`: prevent header autocompletion (currently sets `Connection: close` and `Content-Length` for requests with body data)
- `no_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
2 changes: 1 addition & 1 deletion ftwhttp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type RequestLine struct {
}

// Request represents a request
// No Defaults represents the previous "autocomplete headers" behavior
// This struct without defaults represents the previous "autocomplete headers" behavior
type Request struct {
requestLine *RequestLine
headers Header
Expand Down
74 changes: 74 additions & 0 deletions runner/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,56 @@ tests:
status: [413]
`

var yamlNoAutocompleteHeadersTest = `---
meta:
author: "tester"
enabled: true
name: "gotest-ftw.yaml"
description: "Example Test"
tests:
- test_title: "001"
description: "autocomplete headers by default"
stages:
- stage:
input:
dest_addr: "localhost"
headers:
User-Agent: "ModSecurity CRS 3 Tests"
Accept: "*/*"
Host: "localhost"
output:
expect_error: False
status: [200]
- test_title: "002"
description: "autocomplete headers explicitly"
stages:
- stage:
input:
no_autocomplete_headers: false
dest_addr: "localhost"
headers:
User-Agent: "ModSecurity CRS 3 Tests"
Accept: "*/*"
Host: "localhost"
output:
expect_error: False
status: [200]
- test_title: "003"
description: "do not autocomplete"
stages:
- stage:
input:
no_autocomplete_headers: true
dest_addr: "localhost"
headers:
User-Agent: "ModSecurity CRS 3 Tests"
Accept: "*/*"
Host: "localhost"
output:
expect_error: False
status: [200]
`

// Error checking omitted for brevity
func newTestServer(t *testing.T, cfg *config.FTWConfiguration, logLines string) (destination *ftwhttp.Destination, logFilePath string) {
logFilePath = setUpLogFileForTestServer(t, cfg)
Expand Down Expand Up @@ -1024,3 +1074,27 @@ func TestIgnoredTestsRun(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, res.Stats.TotalFailed(), 1, "Oops, test run failed!")
}

func TestNoAutocompleteHeadersDefault(t *testing.T) {
ftwTest, err := test.GetTestFromYaml([]byte(yamlNoAutocompleteHeadersTest))
assert.NoError(t, err)

request := getRequestFromTest(ftwTest.Tests[0].Stages[0].Stage.Input)
assert.True(t, request.WithAutoCompleteHeaders())
}

func TestNoAutocompleteHeadersFalse(t *testing.T) {
ftwTest, err := test.GetTestFromYaml([]byte(yamlNoAutocompleteHeadersTest))
assert.NoError(t, err)

request := getRequestFromTest(ftwTest.Tests[1].Stages[0].Stage.Input)
assert.True(t, request.WithAutoCompleteHeaders())
}

func TestNoAutocompleteHeadersTrue(t *testing.T) {
ftwTest, err := test.GetTestFromYaml([]byte(yamlNoAutocompleteHeadersTest))
assert.NoError(t, err)

request := getRequestFromTest(ftwTest.Tests[2].Stages[0].Stage.Input)
assert.False(t, request.WithAutoCompleteHeaders())
}
5 changes: 3 additions & 2 deletions test/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ Host: "localhost"
Content-Type: "application/x-www-form-urlencoded"
data: "hi=test"
protocol: "http"
stop_magic: true
no_autocomplete_headers: true
uri: "/"
`
input := Input{}
err := yaml.Unmarshal([]byte(yamlString), &input)
assert.NoError(t, err)
assert.True(t, input.StopMagic)
assert.True(t, input.NoAutocompleteHeaders)
}

Expand All @@ -51,6 +49,7 @@ uri: "/"
err := yaml.Unmarshal([]byte(yamlString), &input)
assert.NoError(t, err)
assert.Empty(t, *input.Version)
assert.False(t, input.NoAutocompleteHeaders)
}

func TestDataTemplateFromYAML(t *testing.T) {
Expand All @@ -75,4 +74,6 @@ uri: "/"
assert.NoError(t, err)
data = input.ParseData()
assert.Equal(t, []byte(repeatTestSprig), data)

assert.True(t, input.NoAutocompleteHeaders)
}
26 changes: 13 additions & 13 deletions test/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import "github.com/coreruleset/go-ftw/ftwhttp"
// The fields `Version`, `Method` and `URI` we want to explicitly know when they are set to ""

type Input struct {
DestAddr *string `yaml:"dest_addr,omitempty" koanf:"dest_addr,omitempty"`
Port *int `yaml:"port,omitempty" koanf:"port,omitempty"`
Protocol *string `yaml:"protocol,omitempty" koanf:"protocol,omitempty"`
URI *string `yaml:"uri,omitempty" koanf:"uri,omitempty"`
Version *string `yaml:"version,omitempty" koanf:"version,omitempty"`
Headers ftwhttp.Header `yaml:"headers,omitempty" koanf:"headers,omitempty"`
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"`
StopMagic bool `yaml:"stop_magic" koanf:"stop_magic,omitempty"`
EncodedRequest string `yaml:"encoded_request,omitempty" koanf:"encoded_request,omitempty"`
RAWRequest string `yaml:"raw_request,omitempty" koanf:"raw_request,omitempty"`
DestAddr *string `yaml:"dest_addr,omitempty" koanf:"dest_addr,omitempty"`
Port *int `yaml:"port,omitempty" koanf:"port,omitempty"`
Protocol *string `yaml:"protocol,omitempty" koanf:"protocol,omitempty"`
URI *string `yaml:"uri,omitempty" koanf:"uri,omitempty"`
Version *string `yaml:"version,omitempty" koanf:"version,omitempty"`
Headers ftwhttp.Header `yaml:"headers,omitempty" koanf:"headers,omitempty"`
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"`
NoAutocompleteHeaders bool `yaml:"no_autocomplete_headers" koanf:"no_autocomplete_headers,omitempty"`
EncodedRequest string `yaml:"encoded_request,omitempty" koanf:"encoded_request,omitempty"`
RAWRequest string `yaml:"raw_request,omitempty" koanf:"raw_request,omitempty"`
}

// Overrides represents the overridden inputs that have to be applied to tests
Expand All @@ -31,7 +31,7 @@ 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"`
StopMagic *bool `yaml:"stop_magic" koanf:"stop_magic,omitempty"`
// NoAutocompleteHeaders bool `yaml:"no_autocomplete_headers" koanf:"no_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

0 comments on commit 02f1a51

Please sign in to comment.