diff --git a/alerting/alert/alert.go b/alerting/alert/alert.go index bb247729..e1a220e4 100644 --- a/alerting/alert/alert.go +++ b/alerting/alert/alert.go @@ -1,5 +1,15 @@ package alert +import ( + "errors" + "strings" +) + +var ( + // ErrAlertWithInvalidDescription is the error with which Gatus will panic if an alert has an invalid character + ErrAlertWithInvalidDescription = errors.New("alert description must not have \" or \\") +) + // Alert is a core.Endpoint's alert configuration type Alert struct { // Type of alert (required) @@ -44,6 +54,20 @@ type Alert struct { Triggered bool `yaml:"-"` } +// ValidateAndSetDefaults validates the alert's configuration and sets the default value of fields that have one +func (alert *Alert) ValidateAndSetDefaults() error { + if alert.FailureThreshold <= 0 { + alert.FailureThreshold = 3 + } + if alert.SuccessThreshold <= 0 { + alert.SuccessThreshold = 2 + } + if strings.ContainsAny(alert.GetDescription(), "\"\\") { + return ErrAlertWithInvalidDescription + } + return nil +} + // GetDescription retrieves the description of the alert func (alert Alert) GetDescription() string { if alert.Description == nil { diff --git a/core/endpoint.go b/core/endpoint.go index f46c15af..1b908ae2 100644 --- a/core/endpoint.go +++ b/core/endpoint.go @@ -41,6 +41,9 @@ var ( // ErrEndpointWithNoName is the error with which Gatus will panic if an endpoint is configured with no name ErrEndpointWithNoName = errors.New("you must specify a name for each endpoint") + + // ErrEndpointWithInvalidNameOrGroup is the error with which Gatus will panic if an endpoint has an invalid character where it shouldn't + ErrEndpointWithInvalidNameOrGroup = errors.New("endpoint name and group must not have \" or \\") ) // Endpoint is the configuration of a monitored @@ -132,16 +135,16 @@ func (endpoint *Endpoint) ValidateAndSetDefaults() error { endpoint.Headers[ContentTypeHeader] = "application/json" } for _, endpointAlert := range endpoint.Alerts { - if endpointAlert.FailureThreshold <= 0 { - endpointAlert.FailureThreshold = 3 - } - if endpointAlert.SuccessThreshold <= 0 { - endpointAlert.SuccessThreshold = 2 + if err := endpointAlert.ValidateAndSetDefaults(); err != nil { + return err } } if len(endpoint.Name) == 0 { return ErrEndpointWithNoName } + if strings.ContainsAny(endpoint.Name, "\"\\") || strings.ContainsAny(endpoint.Group, "\"\\") { + return ErrEndpointWithInvalidNameOrGroup + } if len(endpoint.URL) == 0 { return ErrEndpointWithNoURL } @@ -159,6 +162,14 @@ func (endpoint *Endpoint) ValidateAndSetDefaults() error { return nil } +// DisplayName returns an identifier made up of the Name and, if not empty, the Group. +func (endpoint Endpoint) DisplayName() string { + if len(endpoint.Group) > 0 { + return endpoint.Group + "/" + endpoint.Name + } + return endpoint.Name +} + // Key returns the unique key for the Endpoint func (endpoint Endpoint) Key() string { return util.ConvertGroupAndEndpointNameToKey(endpoint.Group, endpoint.Name)