8000 feat: implement `encoded_data` by theseion · Pull Request #431 · coreruleset/go-ftw · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: implement encoded_data #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func getRequestFromTest(testInput test.Input) *ftwhttp.Request {
Version: testInput.GetVersion(),
}

data := testInput.ParseData()
data := testInput.GetData()
// create a new request
req = ftwhttp.NewRequest(rline, testInput.Headers,
data, *testInput.AutocompleteHeaders)
Expand Down
37 changes: 37 additions & 0 deletions runner/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package runner

import (
"bytes"
"encoding/base64"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -602,3 +603,39 @@ func (s *runTestSuite) TestVirtualHostMode_True() {

s.Equal("not-localhost_virtual-host", request.Headers().Get("Host"))
}

func (s *runTestSuite) TestGetRequestFromData() {
data := "This is Springfield"
boolean := true
method := "POST"
input := test.Input{
AutocompleteHeaders: &boolean,
Method: &method,
Headers: ftwhttp.Header{},
DestAddr: &s.dest.DestAddr,
Port: &s.dest.Port,
Protocol: &s.dest.Protocol,
Data: &data,
}
request := getRequestFromTest(input)

s.Equal(data, string(request.Data()))
}

func (s *runTestSuite) TestGetRequestFromEncodedData() {
data := base64.StdEncoding.EncodeToString([]byte("This is Springfield"))
boolean := true
method := "POST"
input := test.Input{
AutocompleteHeaders: &boolean,
Method: &method,
Headers: ftwhttp.Header{},
DestAddr: &s.dest.DestAddr,
Port: &s.dest.Port,
Protocol: &s.dest.Protocol,
Data: &data,
}
request := getRequestFromTest(input)

s.Equal(data, string(request.Data()))
}
44 changes: 33 additions & 11 deletions test/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,49 @@ package test

import (
"bytes"
"encoding/base64"
"text/template"

"github.com/Masterminds/sprig"
"github.com/rs/zerolog/log"
)

// ParseData returns the data from the test. Will parse and interpret Go text/template inside it.
func (i *Input) ParseData() []byte {
// GetData returns the body data for the request, whether specified via `data` or `encoded_data`.
// If `data` contains Go templates, these will be evaluated.
func (i *Input) GetData() []byte {
if i.Data != nil {
return i.parseData()
}
if i.EncodedData != nil {
decoded, err := base64.StdEncoding.DecodeString(*i.EncodedData)
if err != nil {
log.Debug().Msgf("test/data: error decoding data fro Base64: %s", err.Error())
return nil
}
return decoded
}

return nil
}

func (i *Input) parseData() []byte {
if i.Data == nil {
return nil
}

var err error
var tpl bytes.Buffer

// Parse data for Go template
if i.Data != nil {
t := template.New("ftw").Funcs(sprig.TxtFuncMap())
t, err = t.Parse(*i.Data)
if err != nil {
log.Debug().Msgf("test/data: error parsing template in data: %s", err.Error())
}
if err = t.Execute(&tpl, nil); err != nil {
log.Debug().Msgf("test/data: error executing template: %s", err.Error())
}
t := template.New("ftw").Funcs(sprig.TxtFuncMap())
t, err = t.Parse(*i.Data)
if err != nil {
log.Debug().Msgf("test/data: error parsing template in data: %s", err.Error())
return nil
}
if err = t.Execute(&tpl, nil); err != nil {
log.Debug().Msgf("test/data: error executing template: %s", err.Error())
return nil
}

return tpl.Bytes()
Expand Down
98 changes: 97 additions & 1 deletion test/data_test.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,104 @@ uri: "/"
err := yaml.Unmarshal([]byte(yamlString), &input)

s.Require().NoError(err)
data = input.ParseData()
data = input.parseData()
s.Equal([]byte(repeatTestSprig), data)

s.True(*input.AutocompleteHeaders)
}

func (s *dataTestSuite) TestGetData_FromDataWithTemplate() {
yamlString := `
dest_addr: "127.0.0.1"
method: ""
port: 80
headers:
User-Agent: "ModSecurity CRS 3 Tests"
Host: "localhost"
Content-Type: "application/x-www-form-urlencoded"
data: 'foo=%3d{{ "+" | repeat 34 }}'
version: ""
protocol: "http"
autocomplete_headers: true
uri: "/"
`
input := Input{}
var data []byte
err := yaml.Unmarshal([]byte(yamlString), &input)

s.Require().NoError(err)
data = input.GetData()
s.Equal([]byte(repeatTestSprig), data)

s.True(*input.AutocompleteHeaders)
}

func (s *dataTestSuite) TestGetData_FromData_InvalidTemplate() {
yamlString := `
dest_addr: "127.0.0.1"
method: ""
port: 80
headers:
User-Agent: "ModSecurity CRS 3 Tests"
Host: "localhost"
Content-Type: "application/x-www-form-urlencoded"
data: 'foo=%3d{{ "+" | repeat 34 }'
version: ""
protocol: "http"
autocomplete_headers: true
uri: "/"
`
input := Input{}
var data []byte
err := yaml.Unmarshal([]byte(yamlString), &input)

s.Require().NoError(err)
data = input.GetData()
s.Nil(data)
}

func (s *dataTestSuite) TestGetData_FromEncodedData() {
yamlString := `
dest_addr: "127.0.0.1"
method: ""
port: 80
headers:
User-Agent: "ModSecurity CRS 3 Tests"
Host: "localhost"
Content-Type: "application/x-www-form-urlencoded"
encoded_data: VGhpcyBpcyBTcHJpbmdmaWVsZA==
version: ""
protocol: "http"
uri: "/"
`
input := Input{}
var data []byte
err := yaml.Unmarshal([]byte(yamlString), &input)

s.Require().NoError(err)
data = input.GetData()
s.Equal("This is Springfield", string(data))
}

func (s *dataTestSuite) TestGetData_FromEncodedData_InvalidEncoding() {
yamlString := `
dest_addr: "127.0.0.1"
method: ""
port: 80
headers:
User-Agent: "ModSecurity CRS 3 Tests"
Host: "localhost"
Content-Type: "application/x-www-form-urlencoded"
encoded_data: VGhpcyBpcyBTcHJpbmdmaWVsZA===
version: ""
protocol: "http"
uri: "/"
`
input := Input{}
var data []byte
err := yaml.Unmarshal([]byte(yamlString), &input)

s.Require().NoError(err)
data = input.GetData()
s.Nil(data)
}
Loading
0