From 12c1ef4ff2781482e996d88521090db3e00c5af7 Mon Sep 17 00:00:00 2001 From: samay43 Date: Wed, 5 Aug 2026 14:03:42 +0530 Subject: [PATCH] validate backup name format before contacting the API server Signed-off-by: samay43 --- pkg/cmd/cli/backup/create.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/cli/backup/create.go b/pkg/cmd/cli/backup/create.go index 5e18f468f..f42a4dc70 100644 --- a/pkg/cmd/cli/backup/create.go +++ b/pkg/cmd/cli/backup/create.go @@ -26,6 +26,7 @@ import ( "github.com/spf13/pflag" kubeerrs "k8s.io/apimachinery/pkg/util/errors" "k8s.io/client-go/tools/cache" + "k8s.io/apimachinery/pkg/util/validation" kbclient "sigs.k8s.io/controller-runtime/pkg/client" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" @@ -186,10 +187,12 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto return err } - // Ensure that unless FromSchedule is set, args contains a backup name - if o.FromSchedule == "" && len(args) != 1 { - return fmt.Errorf("a backup name is required, unless you are creating based on a schedule") - } + // Ensure the backup name is a valid Kubernetes resource name + if o.FromSchedule == "" { + if errs := validation.IsDNS1123Subdomain(o.Name); len(errs) > 0 { + return fmt.Errorf("invalid backup name %q: %s", o.Name, strings.Join(errs, "; ")) + } +} errs := collections.ValidateNamespaceIncludesExcludes(o.IncludeNamespaces, o.ExcludeNamespaces) if len(errs) > 0 {