Skip to content

Commit

Permalink
Fix: missing http check regex validations (#612)
Browse files Browse the repository at this point in the history
* fix missing http check validations

* nolint regex field validations
  • Loading branch information
The-9880 committed Feb 13, 2024
1 parent 892cdd5 commit c1890e1
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 10 deletions.
54 changes: 44 additions & 10 deletions pkg/pb/synthetic_monitoring/checks_extra.go
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 {
_, 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
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

0 comments on commit c1890e1

Please sign in to comment.