Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: ensure GenerateFallback cannot be an invalid workspace name
  • Loading branch information
DanielleMaywood committed Aug 21, 2025
commit a9ccd7241b9fdcdf6fb8d1e11990c85bee42a119
9 changes: 9 additions & 0 deletions coderd/taskname/taskname.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@ func generateSuffix() string {
}

func GenerateFallback() string {
// We have a 32 character limit for the name.
// We have a 5 character prefix `task-`.
// We have a 4 character suffix `-fff`.
// This leaves us with 23 characters for the middle.
//
// Unfortunately, `namesgenerator.GetRandomName(0)` will
// generate names that are longer than 23 characters, so
// we just trim these down to length.
name := strings.ReplaceAll(namesgenerator.GetRandomName(0), "_", "-")
name = name[:min(len(name), 23)]

return fmt.Sprintf("task-%s-%s", name, generateSuffix())
}
Expand Down
8 changes: 8 additions & 0 deletions coderd/taskname/taskname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ const (
anthropicEnvVar = "ANTHROPIC_API_KEY"
)

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

name := taskname.GenerateFallback()
err := codersdk.NameValid(name)
require.NoErrorf(t, err, "expected fallback to be valid workspace name, instead found %s", name)
}

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

Expand Down
Loading