Skip to content

Commit 8677e7d

Browse files
DevelopmentCatsCopilotParkreiner
authored
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 <!-- Briefly describe what this PR does and why --> ## 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 <[email protected]> Co-authored-by: Michael Smith <[email protected]>
1 parent d6ae51f commit 8677e7d

File tree

7 files changed

+17
-0
lines changed

7 files changed

+17
-0
lines changed

cmd/readmevalidation/repostructure.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"os"
66
"path"
7+
"regexp"
78
"slices"
89
"strings"
910

@@ -12,6 +13,10 @@ import (
1213

1314
var supportedUserNameSpaceDirectories = append(supportedResourceTypes, ".images")
1415

16+
// validNameRe validates that names contain only alphanumeric characters and hyphens
17+
var validNameRe = regexp.MustCompile(`^[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`)
18+
19+
1520
// validateCoderResourceSubdirectory validates that the structure of a module or template within a namespace follows all
1621
// expected file conventions
1722
func validateCoderResourceSubdirectory(dirPath string) []error {
@@ -42,6 +47,12 @@ func validateCoderResourceSubdirectory(dirPath string) []error {
4247
continue
4348
}
4449

50+
// Validate module/template name
51+
if !validNameRe.MatchString(f.Name()) {
52+
errs = append(errs, xerrors.Errorf("%q: name contains invalid characters (only alphanumeric characters and hyphens are allowed)", path.Join(dirPath, f.Name())))
53+
continue
54+
}
55+
4556
resourceReadmePath := path.Join(dirPath, f.Name(), "README.md")
4657
if _, err := os.Stat(resourceReadmePath); err != nil {
4758
if errors.Is(err, os.ErrNotExist) {
@@ -79,6 +90,12 @@ func validateRegistryDirectory() []error {
7990
continue
8091
}
8192

93+
// Validate namespace name
94+
if !validNameRe.MatchString(nDir.Name()) {
95+
allErrs = append(allErrs, xerrors.Errorf("%q: namespace name contains invalid characters (only alphanumeric characters and hyphens are allowed)", namespacePath))
96+
continue
97+
}
98+
8299
contributorReadmePath := path.Join(namespacePath, "README.md")
83100
if _, err := os.Stat(contributorReadmePath); err != nil {
84101
allErrs = append(allErrs, err)
File renamed without changes.

0 commit comments

Comments
 (0)