diff --git a/pkg/services/alerting/service.go b/pkg/services/alerting/service.go index 34e683561fc..7c30b0a635f 100644 --- a/pkg/services/alerting/service.go +++ b/pkg/services/alerting/service.go @@ -9,6 +9,7 @@ import ( "github.com/grafana/grafana/pkg/services/notifications" "github.com/grafana/grafana/pkg/services/sqlstore" "github.com/grafana/grafana/pkg/setting" + "github.com/grafana/grafana/pkg/util" ) type AlertNotificationService struct { @@ -33,6 +34,10 @@ func (s *AlertNotificationService) GetAlertNotifications(ctx context.Context, qu } func (s *AlertNotificationService) CreateAlertNotificationCommand(ctx context.Context, cmd *models.CreateAlertNotificationCommand) error { + if util.IsShortUIDTooLong(cmd.Uid) { + return ValidationError{Reason: "Invalid UID: Must be 40 characters or less"} + } + var err error cmd.EncryptedSecureSettings, err = s.EncryptionService.EncryptJsonData(ctx, cmd.SecureSettings, setting.SecretKey) if err != nil { @@ -53,6 +58,10 @@ func (s *AlertNotificationService) CreateAlertNotificationCommand(ctx context.Co } func (s *AlertNotificationService) UpdateAlertNotification(ctx context.Context, cmd *models.UpdateAlertNotificationCommand) error { + if util.IsShortUIDTooLong(cmd.Uid) { + return ValidationError{Reason: "Invalid UID: Must be 40 characters or less"} + } + var err error cmd.EncryptedSecureSettings, err = s.EncryptionService.EncryptJsonData(ctx, cmd.SecureSettings, setting.SecretKey) if err != nil { @@ -99,6 +108,10 @@ func (s *AlertNotificationService) GetAlertNotificationsWithUid(ctx context.Cont } func (s *AlertNotificationService) UpdateAlertNotificationWithUid(ctx context.Context, cmd *models.UpdateAlertNotificationWithUidCommand) error { + if util.IsShortUIDTooLong(cmd.Uid) || util.IsShortUIDTooLong(cmd.NewUid) { + return ValidationError{Reason: "Invalid UID: Must be 40 characters or less"} + } + return s.SQLStore.UpdateAlertNotificationWithUid(ctx, cmd) } diff --git a/pkg/services/alerting/service_test.go b/pkg/services/alerting/service_test.go index edd58a5da30..0d8e19245a3 100644 --- a/pkg/services/alerting/service_test.go +++ b/pkg/services/alerting/service_test.go @@ -2,6 +2,7 @@ package alerting import ( "context" + "strings" "testing" "github.com/grafana/grafana/pkg/components/simplejson" @@ -112,6 +113,26 @@ func TestService(t *testing.T) { err = s.DeleteAlertNotification(context.Background(), &delCmd) require.NoError(t, err) }) + + t.Run("create alert notification should reject an invalid command", func(t *testing.T) { + uid := strings.Repeat("A", 41) + + err := s.CreateAlertNotificationCommand(context.Background(), &models.CreateAlertNotificationCommand{Uid: uid}) + require.ErrorIs(t, err, ValidationError{Reason: "Invalid UID: Must be 40 characters or less"}) + }) + + t.Run("update alert notification should reject an invalid command", func(t *testing.T) { + ctx := context.Background() + + uid := strings.Repeat("A", 41) + expectedErr := ValidationError{Reason: "Invalid UID: Must be 40 characters or less"} + + err := s.UpdateAlertNotification(ctx, &models.UpdateAlertNotificationCommand{Uid: uid}) + require.ErrorIs(t, err, expectedErr) + + err = s.UpdateAlertNotificationWithUid(ctx, &models.UpdateAlertNotificationWithUidCommand{NewUid: uid}) + require.ErrorIs(t, err, expectedErr) + }) } func registerTestNotifier(notifierType string) {