8000 fix: extend regex for template version name by mtojek · Pull Request #6876 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix: extend regex for template version name #6876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
8000 Loading
Diff view
Diff view
fix: extend regex for template version name
  • Loading branch information
mtojek committed Mar 30, 2023
commit b7750f707c877769d5e4180de822e4f8b60f330f
16 changes: 15 additions & 1 deletion coderd/httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func init() {
valid := NameValid(str)
return valid == nil
}
for _, tag := range []string{"username", "template_name", "workspace_name", "template_version_name"} {
for _, tag := range []string{"username", "template_name", "workspace_name"} {
err := Validate.RegisterValidation(tag, nameValidator)
if err != nil {
panic(err)
Expand All @@ -64,6 +64,20 @@ func init() {
if err != nil {
panic(err)
}

templateVersionNameValidator := func(fl validator.FieldLevel) bool {
f := fl.Field().Interface()
str, ok := f.(string)
if !ok {
return false
}
valid := TemplateVersionNameValid(str)
return valid == nil
}
err = Validate.RegisterValidation("template_version_name", templateVersionNameValidator)
if err != nil {
panic(err)
}
}

// Convenience error functions don't take contexts since their responses are
Expand Down
13 changes: 13 additions & 0 deletions coderd/httpapi/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
UsernameValidRegex = regexp.MustCompile("^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$")
usernameReplace = regexp.MustCompile("[^a-zA-Z0-9-]*")

templateVersionName = regexp.MustCompile(`^[a-zA-Z0-9]+(?:[_.-]{1}[a-zA-Z0-9]+)*$`)
templateDisplayName = regexp.MustCompile(`^[^\s](.*[^\s])?$`)
)

Expand Down Expand Up @@ -52,6 +53,18 @@ func NameValid(str string) error {
return nil
}

// TemplateVersionNameValid returns whether the input string is a valid template version name.
func TemplateVersionNameValid(str string) error {
if len(str) > 64 {
return xerrors.New("must be <= 64 characters")
}
matched := templateVersionName.MatchString(str)
if !matched {
return xerrors.New("must be alphanumeric with underscores and dots")
}
return nil
}

// TemplateDisplayNameValid returns whether the input string is a valid template display name.
func TemplateDisplayNameValid(str string) error {
if len(str) == 0 {
Expand Down
52 changes: 52 additions & 0 deletions coderd/httpapi/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpapi_test
import (
"testing"

"github.com/moby/moby/pkg/namesgenerator"
"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd/httpapi"
Expand Down Expand Up @@ -120,6 +121,57 @@ func TestTemplateDisplayNameValid(t *testing.T) {
}
}

func TestTemplateVersionNameValid(t *testing.T) {
t.Parallel()

testCases := []struct {
Name string
Valid bool
}{
{"1", true},
{"12", true},
{"1_2", true},
{"1-2", true},
{"cray", true},
{"123_456", true},
{"123-456", true},
{"1234_678901234567890", true},
{"1234-678901234567890", true},
{"S", true},
{"a1", true},
{"a1K2", true},
{"fuzzy_bear3", true},
{"fuzzy-bear3", true},
{"v1.0.0", true},
{"heuristic_cray2", true},

{"", false},
{".v1", false},
{"v1..0", false},
{"4--4", false},
{"<b> </b>", false},
{"!!!!1 ?????", false},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Name, func(t *testing.T) {
t.Parallel()
valid := httpapi.TemplateVersionNameValid(testCase.Name)
require.Equal(t, testCase.Valid, valid == nil)
})
}
}

func TestGeneratedTemplateVersionNameValid(t *testing.T) {
t.Parallel()

for i := 0; i < 1000; i++ {
name := namesgenerator.GetRandomName(1)
err := httpapi.TemplateVersionNameValid(name)
require.NoError(t, err, "invalid template version name: %s", name)
}
}

func TestFrom(t *testing.T) {
t.Parallel()
testCases := []struct {
Expand Down
0