validate sns message body
Signed-off-by: Yijie Qin <qinyijie@amazon.com>
This commit is contained in:
parent
4030e3670b
commit
cda63f9f08
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue