Skip to content

fix: don't allow "new" or "create" as url-friendly names #13596

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 5 commits into from
Jun 18, 2024
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.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions coderd/httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
valid := NameValid(str)
return valid == nil
}
for _, tag := range []string{"username", "organization_name", "template_name", "workspace_name", "oauth2_app_name"} {
for _, tag := range []string{"username", "organization_name", "template_name", "group_name", "workspace_name", "oauth2_app_name"} {
err := Validate.RegisterValidation(tag, nameValidator)
if err != nil {
panic(err)
Expand All @@ -62,7 +62,7 @@ func init() {
valid := DisplayNameValid(str)
return valid == nil
}
for _, displayNameTag := range []string{"organization_display_name", "template_display_name"} {
for _, displayNameTag := range []string{"organization_display_name", "template_display_name", "group_display_name"} {
err := Validate.RegisterValidation(displayNameTag, displayNameValidator)
if err != nil {
panic(err)
Expand Down
4 changes: 4 additions & 0 deletions coderd/httpapi/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func NameValid(str string) error {
if len(str) < 1 {
return xerrors.New("must be >= 1 character")
}
// Avoid conflicts with routes like /templates/new and /groups/create.
if str == "new" || str == "create" {
return xerrors.Errorf("cannot use %q as a name", str)
}
matched := UsernameValidRegex.MatchString(str)
if !matched {
return xerrors.New("must be alphanumeric with hyphens")
Expand Down
54 changes: 27 additions & 27 deletions coderd/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ func TestPostOrganizationsByUser(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitLong)

o, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "new",
DisplayName: "New",
Name: "new-org",
DisplayName: "New organization",
Description: "A new organization to love and cherish forever.",
Icon: "/emojis/1f48f-1f3ff.png",
})
require.NoError(t, err)
require.Equal(t, "new", o.Name)
require.Equal(t, "New", o.DisplayName)
require.Equal(t, "new-org", o.Name)
require.Equal(t, "New organization", o.DisplayName)
require.Equal(t, "A new organization to love and cherish forever.", o.Description)
require.Equal(t, "/emojis/1f48f-1f3ff.png", o.Icon)
})
Expand All @@ -159,11 +159,11 @@ func TestPostOrganizationsByUser(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitLong)

o, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "new",
Name: "new-org",
})
require.NoError(t, err)
require.Equal(t, "new", o.Name)
require.Equal(t, "new", o.DisplayName) // should match the given `Name`
require.Equal(t, "new-org", o.Name)
require.Equal(t, "new-org", o.DisplayName) // should match the given `Name`
})
}

Expand Down Expand Up @@ -238,16 +238,16 @@ func TestPatchOrganizationsByUser(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitMedium)

o, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "new",
DisplayName: "New",
Name: "new-org",
DisplayName: "New organization",
})
require.NoError(t, err)

o, err = client.UpdateOrganization(ctx, o.ID.String(), codersdk.UpdateOrganizationRequest{
Name: "new-new",
Name: "new-new-org",
})
require.NoError(t, err)
require.Equal(t, "new-new", o.Name)
require.Equal(t, "new-new-org", o.Name)
})

t.Run("UpdateByName", func(t *testing.T) {
Expand All @@ -257,17 +257,17 @@ func TestPatchOrganizationsByUser(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitMedium)

o, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "new",
DisplayName: "New",
Name: "new-org",
DisplayName: "New organization",
})
require.NoError(t, err)

o, err = client.UpdateOrganization(ctx, o.Name, codersdk.UpdateOrganizationRequest{
Name: "new-new",
Name: "new-new-org",
})
require.NoError(t, err)
require.Equal(t, "new-new", o.Name)
require.Equal(t, "New", o.DisplayName) // didn't change
require.Equal(t, "new-new-org", o.Name)
require.Equal(t, "New organization", o.DisplayName) // didn't change
})

t.Run("UpdateDisplayName", func(t *testing.T) {
Expand All @@ -277,16 +277,16 @@ func TestPatchOrganizationsByUser(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitMedium)

o, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "new",
DisplayName: "New",
Name: "new-org",
DisplayName: "New organization",
})
require.NoError(t, err)

o, err = client.UpdateOrganization(ctx, o.Name, codersdk.UpdateOrganizationRequest{
DisplayName: "The Newest One",
})
require.NoError(t, err)
require.Equal(t, "new", o.Name) // didn't change
require.Equal(t, "new-org", o.Name) // didn't change
require.Equal(t, "The Newest One", o.DisplayName)
})

Expand All @@ -297,8 +297,8 @@ func TestPatchOrganizationsByUser(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitMedium)

o, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "new",
DisplayName: "New",
Name: "new-org",
DisplayName: "New organization",
})
require.NoError(t, err)

Expand All @@ -307,8 +307,8 @@ func TestPatchOrganizationsByUser(t *testing.T) {
})

require.NoError(t, err)
require.Equal(t, "new", o.Name) // didn't change
require.Equal(t, "New", o.DisplayName) // didn't change
require.Equal(t, "new-org", o.Name) // didn't change
require.Equal(t, "New organization", o.DisplayName) // didn't change
require.Equal(t, "wow, this organization description is so updated!", o.Description)
})

Expand All @@ -319,8 +319,8 @@ func TestPatchOrganizationsByUser(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitMedium)

o, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "new",
DisplayName: "New",
Name: "new-org",
DisplayName: "New organization",
})
require.NoError(t, err)

Expand All @@ -329,8 +329,8 @@ func TestPatchOrganizationsByUser(t *testing.T) {
})

require.NoError(t, err)
require.Equal(t, "new", o.Name) // didn't change
require.Equal(t, "New", o.DisplayName) // didn't change
require.Equal(t, "new-org", o.Name) // didn't change
require.Equal(t, "New organization", o.DisplayName) // didn't change
require.Equal(t, "/emojis/1f48f-1f3ff.png", o.Icon)
})
}
Expand Down
Loading
Loading