Skip to content

Commit 4997691

Browse files
authored
fix: increase group name limit to 36 from 32 (#14443)
1 parent 88d7181 commit 4997691

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

coderd/httpapi/httpapi.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func init() {
4646
valid := NameValid(str)
4747
return valid == nil
4848
}
49-
for _, tag := range []string{"username", "organization_name", "template_name", "group_name", "workspace_name", "oauth2_app_name"} {
49+
for _, tag := range []string{"username", "organization_name", "template_name", "workspace_name", "oauth2_app_name"} {
5050
err := Validate.RegisterValidation(tag, nameValidator)
5151
if err != nil {
5252
panic(err)
@@ -96,6 +96,20 @@ func init() {
9696
if err != nil {
9797
panic(err)
9898
}
99+
100+
groupNameValidator := func(fl validator.FieldLevel) bool {
101+
f := fl.Field().Interface()
102+
str, ok := f.(string)
103+
if !ok {
104+
return false
105+
}
106+
valid := GroupNameValid(str)
107+
return valid == nil
108+
}
109+
err = Validate.RegisterValidation("group_name", groupNameValidator)
110+
if err != nil {
111+
panic(err)
112+
}
99113
}
100114

101115
// Is404Error returns true if the given error should return a 404 status code.

coderd/httpapi/name.go

+17
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ func UserRealNameValid(str string) error {
9696
return nil
9797
}
9898

99+
// GroupNameValid returns whether the input string is a valid group name.
100+
func GroupNameValid(str string) error {
101+
// 36 is to support using UUIDs as the group name.
102+
if len(str) > 36 {
103+
return xerrors.New("must be <= 36 characters")
104+
}
105+
// Avoid conflicts with routes like /groups/new and /groups/create.
106+
if str == "new" || str == "create" {
107+
return xerrors.Errorf("cannot use %q as a name", str)
108+
}
109+
matched := UsernameValidRegex.MatchString(str)
110+
if !matched {
111+
return xerrors.New("must be alphanumeric with hyphens")
112+
}
113+
return nil
114+
}
115+
99116
// NormalizeUserRealName normalizes a user name such that it will pass
100117
// validation by UserRealNameValid. This is done to avoid blocking
101118
// little Bobby Whitespace from using Coder.

enterprise/coderd/groups_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func TestPatchGroup(t *testing.T) {
159159
const displayName = "foobar"
160160
ctx := testutil.Context(t, testutil.WaitLong)
161161
group, err := userAdminClient.CreateGroup(ctx, user.OrganizationID, codersdk.CreateGroupRequest{
162-
Name: "hi",
162+
Name: "ff7dcee2-e7c4-4bc4-a9e4-84870770e4c5", // GUID should fit.
163163
AvatarURL: "https://example.com",
164164
QuotaAllowance: 10,
165165
DisplayName: "",
@@ -168,14 +168,14 @@ func TestPatchGroup(t *testing.T) {
168168
require.Equal(t, 10, group.QuotaAllowance)
169169

170170
group, err = userAdminClient.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{
171-
Name: "bye",
171+
Name: "ddd502d2-2984-4724-b5bf-1109a4d7462d", // GUID should fit.
172172
AvatarURL: ptr.Ref("https://google.com"),
173173
QuotaAllowance: ptr.Ref(20),
174174
DisplayName: ptr.Ref(displayName),
175175
})
176176
require.NoError(t, err)
177177
require.Equal(t, displayName, group.DisplayName)
178-
require.Equal(t, "bye", group.Name)
178+
require.Equal(t, "ddd502d2-2984-4724-b5bf-1109a4d7462d", group.Name)
179179
require.Equal(t, "https://google.com", group.AvatarURL)
180180
require.Equal(t, 20, group.QuotaAllowance)
181181
})

0 commit comments

Comments
 (0)