1
0
Fork 0
mirror of https://github.com/TwiN/gatus.git synced 2024-12-14 11:58:04 +00:00

Disallow certain characters in endpoint name, group and alert description

This commit is contained in:
TwiN 2021-12-12 16:28:24 -05:00
parent 3dd8ba1a99
commit 6da281bf4e
2 changed files with 40 additions and 5 deletions

View file

@ -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 {

View file

@ -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)