Skip to content
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

Fix: missing http check regex validations #612

Merged
merged 2 commits into from
Feb 13, 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
54 changes: 44 additions & 10 deletions pkg/pb/synthetic_monitoring/checks_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"mime"
"net"
"net/url"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -67,16 +68,20 @@ var (
ErrInvalidDnsRecordTypeString = errors.New("invalid DNS record type string")
ErrInvalidDnsRecordTypeValue = errors.New("invalid DNS record type value")

ErrInvalidHttpUrl = errors.New("invalid HTTP URL")
ErrInvalidHttpMethodString = errors.New("invalid HTTP method string")
ErrInvalidHttpMethodValue = errors.New("invalid HTTP method value")
ErrInvalidHttpUrlHost = errors.New("invalid HTTP URL host")
ErrInvalidHttpHeaders = errors.New("invalid HTTP headers")
ErrHttpUrlContainsPassword = errors.New("HTTP URL contains username and password")
ErrHttpUrlContainsUsername = errors.New("HTTP URL contains username")
ErrInvalidProxyConnectHeaders = errors.New("invalid HTTP proxy connect headers")
ErrInvalidProxyUrl = errors.New("invalid proxy URL")
ErrInvalidProxySettings = errors.New("invalid proxy settings")
ErrInvalidHttpUrl = errors.New("invalid HTTP URL")
ErrInvalidHttpMethodString = errors.New("invalid HTTP method string")
ErrInvalidHttpMethodValue = errors.New("invalid HTTP method value")
ErrInvalidHttpUrlHost = errors.New("invalid HTTP URL host")
ErrInvalidHttpHeaders = errors.New("invalid HTTP headers")
ErrInvalidHttpFailIfBodyMatchesRegexp = errors.New("invalid HTTP fail if body matches regexp")
ErrInvalidHttpFailIfBodyNotMatchesRegexp = errors.New("invalid HTTP fail if body not matches regexp")
ErrInvalidHttpFailIfHeaderMatchesRegexp = errors.New("invalid HTTP fail if header matches regexp")
ErrInvalidHttpFailIfHeaderNotMatchesRegexp = errors.New("invalid HTTP fail if header not matches regexp")
ErrHttpUrlContainsPassword = errors.New("HTTP URL contains username and password")
ErrHttpUrlContainsUsername = errors.New("HTTP URL contains username")
ErrInvalidProxyConnectHeaders = errors.New("invalid HTTP proxy connect headers")
ErrInvalidProxyUrl = errors.New("invalid proxy URL")
ErrInvalidProxySettings = errors.New("invalid proxy settings")

ErrInvalidTracerouteHostname = errors.New("invalid traceroute hostname")

Expand Down Expand Up @@ -581,6 +586,7 @@ func (s *PingSettings) Validate() error {
return nil
}

//nolint:gocyclo
func (s *HttpSettings) Validate() error {
for _, h := range s.Headers {
fields := strings.SplitN(h, ":", 2)
Expand Down Expand Up @@ -633,6 +639,34 @@ func (s *HttpSettings) Validate() error {
}
}

for _, reg := range s.FailIfBodyMatchesRegexp {
The-9880 marked this conversation as resolved.
Show resolved Hide resolved
_, err := regexp.Compile(reg)
if err != nil {
return ErrInvalidHttpFailIfBodyMatchesRegexp
}
}

for _, reg := range s.FailIfBodyNotMatchesRegexp {
_, err := regexp.Compile(reg)
if err != nil {
return ErrInvalidHttpFailIfBodyNotMatchesRegexp
}
}

for _, match := range s.FailIfHeaderMatchesRegexp {
_, err := regexp.Compile(match.Regexp)
if err != nil {
return ErrInvalidHttpFailIfHeaderMatchesRegexp
}
}

for _, match := range s.FailIfHeaderNotMatchesRegexp {
_, err := regexp.Compile(match.Regexp)
if err != nil {
return ErrInvalidHttpFailIfHeaderNotMatchesRegexp
}
}

return nil
}

Expand Down
53 changes: 53 additions & 0 deletions pkg/pb/synthetic_monitoring/checks_extra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,59 @@ func TestMultiHttpEntryAssertionValidate(t *testing.T) {
})
}

func TestHttpRegexFields(t *testing.T) {
testValidate(t, TestCases[*HttpSettings]{
"body matches regexp parses": {
input: &HttpSettings{
FailIfBodyMatchesRegexp: []string{".*good stuff.*"},
},
expectError: false,
},
"body matches invalid regexp errors": {
input: &HttpSettings{
FailIfBodyMatchesRegexp: []string{"*good stuff*"},
},
expectError: true,
},
"body not matches regexp parses": {
input: &HttpSettings{
FailIfBodyNotMatchesRegexp: []string{".*good stuff.*"},
},
expectError: false,
},
"body not matches invalid regexp errors": {
input: &HttpSettings{
FailIfBodyNotMatchesRegexp: []string{"*good stuff*"},
},
expectError: true,
},
"header matches regexp parses": {
input: &HttpSettings{
FailIfHeaderMatchesRegexp: []HeaderMatch{{Regexp: ".*good stuff.*"}},
},
expectError: false,
},
"header matches invalid regexp errors": {
input: &HttpSettings{
FailIfHeaderMatchesRegexp: []HeaderMatch{{Regexp: "*good stuff*"}},
},
expectError: true,
},
"header not matches regexp parses": {
input: &HttpSettings{
FailIfHeaderNotMatchesRegexp: []HeaderMatch{{Regexp: ".*good stuff.*"}},
},
expectError: false,
},
"header not matches invalid regexp errors": {
input: &HttpSettings{
FailIfHeaderNotMatchesRegexp: []HeaderMatch{{Regexp: "*good stuff*"}},
},
expectError: true,
},
})
}

func TestMultiHttpEntryVariableValidate(t *testing.T) {
testValidate(t, TestCases[*MultiHttpEntryVariable]{
"zero value": {
Expand Down
Loading