From 8677e7d52b374b025c4820d09049dc6b008beee8 Mon Sep 17 00:00:00 2001 From: DevCats Date: Fri, 22 Aug 2025 08:45:15 -0500 Subject: [PATCH] feat: add validation for module and namespace names (#359) Closes # ## Description add validation for module and namespace names to ensure they contain only alphanumeric characters and hyphens ## Type of Change - [ ] New module - [ ] Bug fix - [X] Feature/enhancement - [ ] Documentation - [ ] Other ## Testing & Validation - [X] Tests pass (`bun test`) - [X] Code formatted (`bun run fmt`) - [X] Changes tested locally --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Michael Smith --- cmd/readmevalidation/repostructure.go | 17 +++++++++++++++++ .../README.md | 0 .../main.test.ts | 0 .../main.tf | 0 .../scripts/install.sh | 0 .../scripts/start.sh | 0 .../testdata/amp-mock.sh | 0 7 files changed, 17 insertions(+) rename registry/coder-labs/modules/{sourcegraph_amp => sourcegraph-amp}/README.md (100%) rename registry/coder-labs/modules/{sourcegraph_amp => sourcegraph-amp}/main.test.ts (100%) rename registry/coder-labs/modules/{sourcegraph_amp => sourcegraph-amp}/main.tf (100%) rename registry/coder-labs/modules/{sourcegraph_amp => sourcegraph-amp}/scripts/install.sh (100%) rename registry/coder-labs/modules/{sourcegraph_amp => sourcegraph-amp}/scripts/start.sh (100%) rename registry/coder-labs/modules/{sourcegraph_amp => sourcegraph-amp}/testdata/amp-mock.sh (100%) diff --git a/cmd/readmevalidation/repostructure.go b/cmd/readmevalidation/repostructure.go index 7a529c01..c77c723b 100644 --- a/cmd/readmevalidation/repostructure.go +++ b/cmd/readmevalidation/repostructure.go @@ -4,6 +4,7 @@ import ( "errors" "os" "path" + "regexp" "slices" "strings" @@ -12,6 +13,10 @@ import ( var supportedUserNameSpaceDirectories = append(supportedResourceTypes, ".images") +// validNameRe validates that names contain only alphanumeric characters and hyphens +var validNameRe = regexp.MustCompile(`^[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`) + + // validateCoderResourceSubdirectory validates that the structure of a module or template within a namespace follows all // expected file conventions func validateCoderResourceSubdirectory(dirPath string) []error { @@ -42,6 +47,12 @@ func validateCoderResourceSubdirectory(dirPath string) []error { continue } + // Validate module/template name + if !validNameRe.MatchString(f.Name()) { + errs = append(errs, xerrors.Errorf("%q: name contains invalid characters (only alphanumeric characters and hyphens are allowed)", path.Join(dirPath, f.Name()))) + continue + } + resourceReadmePath := path.Join(dirPath, f.Name(), "README.md") if _, err := os.Stat(resourceReadmePath); err != nil { if errors.Is(err, os.ErrNotExist) { @@ -79,6 +90,12 @@ func validateRegistryDirectory() []error { continue } + // Validate namespace name + if !validNameRe.MatchString(nDir.Name()) { + allErrs = append(allErrs, xerrors.Errorf("%q: namespace name contains invalid characters (only alphanumeric characters and hyphens are allowed)", namespacePath)) + continue + } + contributorReadmePath := path.Join(namespacePath, "README.md") if _, err := os.Stat(contributorReadmePath); err != nil { allErrs = append(allErrs, err) diff --git a/registry/coder-labs/modules/sourcegraph_amp/README.md b/registry/coder-labs/modules/sourcegraph-amp/README.md similarity index 100% rename from registry/coder-labs/modules/sourcegraph_amp/README.md rename to registry/coder-labs/modules/sourcegraph-amp/README.md diff --git a/registry/coder-labs/modules/sourcegraph_amp/main.test.ts b/registry/coder-labs/modules/sourcegraph-amp/main.test.ts similarity index 100% rename from registry/coder-labs/modules/sourcegraph_amp/main.test.ts rename to registry/coder-labs/modules/sourcegraph-amp/main.test.ts diff --git a/registry/coder-labs/modules/sourcegraph_amp/main.tf b/registry/coder-labs/modules/sourcegraph-amp/main.tf similarity index 100% rename from registry/coder-labs/modules/sourcegraph_amp/main.tf rename to registry/coder-labs/modules/sourcegraph-amp/main.tf diff --git a/registry/coder-labs/modules/sourcegraph_amp/scripts/install.sh b/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh similarity index 100% rename from registry/coder-labs/modules/sourcegraph_amp/scripts/install.sh rename to registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh diff --git a/registry/coder-labs/modules/sourcegraph_amp/scripts/start.sh b/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh similarity index 100% rename from registry/coder-labs/modules/sourcegraph_amp/scripts/start.sh rename to registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh diff --git a/registry/coder-labs/modules/sourcegraph_amp/testdata/amp-mock.sh b/registry/coder-labs/modules/sourcegraph-amp/testdata/amp-mock.sh similarity index 100% rename from registry/coder-labs/modules/sourcegraph_amp/testdata/amp-mock.sh rename to registry/coder-labs/modules/sourcegraph-amp/testdata/amp-mock.sh