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 {