diff --git a/cli/azd/pkg/project/service_manager.go b/cli/azd/pkg/project/service_manager.go index 554d93b5a45..82be6a05aae 100644 --- a/cli/azd/pkg/project/service_manager.go +++ b/cli/azd/pkg/project/service_manager.go @@ -12,6 +12,7 @@ import ( "log" "os" "path/filepath" + "regexp" "strings" "github.com/azure/azure-dev/cli/azd/pkg/alpha" @@ -278,6 +279,11 @@ func (sm *serviceManager) Package( options = &PackageOptions{} } + // Check if the application name is valid. + if err := validateAppName(serviceConfig.Project.Name); err != nil { + return nil, err + } + cachedResult, ok := sm.getOperationResult(serviceConfig, string(ServiceEventPackage)) if ok && cachedResult != nil { return cachedResult.(*ServicePackageResult), nil @@ -685,3 +691,21 @@ func moveFile(sourcePath string, destinationPath string) error { return nil } + +func validateAppName(name string) error { + if len(name) < 2 { + return fmt.Errorf("invalid application name '%s': must be at least 2 characters", name) + } + matched, err := regexp.MatchString(`^[a-zA-Z0-9_-]+$`, name) + if err != nil { + return err + } + if !matched { + return fmt.Errorf( + "invalid application name '%s': only letters, numbers, underscores (_), and hyphens (-) are allowed", + name, + ) + + } + return nil +} diff --git a/schemas/alpha/azure.yaml.json b/schemas/alpha/azure.yaml.json index 1f64b3969e5..9af35489fd0 100644 --- a/schemas/alpha/azure.yaml.json +++ b/schemas/alpha/azure.yaml.json @@ -10,7 +10,9 @@ "name": { "type": "string", "minLength": 2, - "title": "Name of the application" + "title": "Name of the application", + "pattern": "^[a-zA-Z0-9_-]+$", + "description": "The application name. Only letters, numbers, underscores, and hyphens are allowed. Characters like /, \\, spaces, or other special symbols are not permitted." }, "resourceGroup": { "type": "string", diff --git a/schemas/v1.0/azure.yaml.json b/schemas/v1.0/azure.yaml.json index 351740f2ca5..114b6591575 100644 --- a/schemas/v1.0/azure.yaml.json +++ b/schemas/v1.0/azure.yaml.json @@ -10,7 +10,9 @@ "name": { "type": "string", "minLength": 2, - "title": "Name of the application" + "title": "Name of the application", + "pattern": "^[a-zA-Z0-9_-]+$", + "description": "The application name. Only letters, numbers, underscores, and hyphens are allowed. Characters like /, \\, spaces, or other special symbols are not permitted." }, "resourceGroup": { "type": "string",