From a02485f1054df991c0e8289c6893650f6190ce77 Mon Sep 17 00:00:00 2001 From: Anant Sharma Date: Mon, 12 Feb 2024 01:25:45 -0500 Subject: [PATCH 1/2] fix missing http check validations --- pkg/pb/synthetic_monitoring/checks_extra.go | 53 +++++++++++++++---- .../synthetic_monitoring/checks_extra_test.go | 53 +++++++++++++++++++ 2 files changed, 96 insertions(+), 10 deletions(-) diff --git a/pkg/pb/synthetic_monitoring/checks_extra.go b/pkg/pb/synthetic_monitoring/checks_extra.go index 34e9c0a52..275d7d293 100644 --- a/pkg/pb/synthetic_monitoring/checks_extra.go +++ b/pkg/pb/synthetic_monitoring/checks_extra.go @@ -25,6 +25,7 @@ import ( "mime" "net" "net/url" + "regexp" "strconv" "strings" "time" @@ -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") @@ -633,6 +638,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 } diff --git a/pkg/pb/synthetic_monitoring/checks_extra_test.go b/pkg/pb/synthetic_monitoring/checks_extra_test.go index 82b27c2f3..ceb557454 100644 --- a/pkg/pb/synthetic_monitoring/checks_extra_test.go +++ b/pkg/pb/synthetic_monitoring/checks_extra_test.go @@ -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": { From c159dd61a4e6304ee600ea7f051e17319a53c3a4 Mon Sep 17 00:00:00 2001 From: Anant Sharma Date: Mon, 12 Feb 2024 14:31:11 -0500 Subject: [PATCH 2/2] nolint regex field validations --- pkg/pb/synthetic_monitoring/checks_extra.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/pb/synthetic_monitoring/checks_extra.go b/pkg/pb/synthetic_monitoring/checks_extra.go index 275d7d293..6b68825f9 100644 --- a/pkg/pb/synthetic_monitoring/checks_extra.go +++ b/pkg/pb/synthetic_monitoring/checks_extra.go @@ -586,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)