validate sns message body

Signed-off-by: Yijie Qin <qinyijie@amazon.com>
This commit is contained in:
Yijie Qin 2022-02-14 01:46:40 -08:00
parent 4030e3670b
commit cda63f9f08
2 changed files with 14 additions and 1 deletions

View File

@ -36,6 +36,12 @@ import (
"github.com/prometheus/alertmanager/types"
)
const (
// The errors
MessageNotValidUtf8 = "Error - not a valid UTF-8 encoded string"
MessageIsEmpty = "Error - the message should not be empty"
)
// Notifier implements a Notifier for SNS notifications.
type Notifier struct {
conf *config.SNSConfig
@ -189,7 +195,10 @@ func (n *Notifier) createPublishInput(ctx context.Context, tmpl func(string) str
func validateAndTruncateMessage(message string, maxMessageSizeInBytes int) (string, bool, error) {
if !utf8.ValidString(message) {
return "", false, fmt.Errorf("non utf8 encoded message string")
return MessageNotValidUtf8, false, fmt.Errorf("non utf8 encoded message string")
}
if len(message) == 0 {
return MessageIsEmpty, false, fmt.Errorf("message is empty")
}
if len(message) <= maxMessageSizeInBytes {
return message, false, nil

View File

@ -42,4 +42,8 @@ func TestValidateAndTruncateMessage(t *testing.T) {
invalidUtf8String := "\xc3\x28"
_, _, err = validateAndTruncateMessage(invalidUtf8String, 100)
require.Error(t, err)
emptyString := ""
_, _, err = validateAndTruncateMessage(emptyString, 100)
require.Error(t, err)
}