Skip to content

Commit 347ab5b

Browse files
fix(coderd/taskname): ensure generated name is within 32 byte limit (coder#19612)
The previous logic verified a generated name was valid, _and then appended a suffix to it_. This was flawed as it would allow a 32 character name, and then append an extra 5 characters to it. Instead we now append the suffix _and then_ verify it is valid.
1 parent 8c731a0 commit 347ab5b

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

coderd/taskname/taskname.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
Requirements:
2525
- Only lowercase letters, numbers, and hyphens
2626
- Start with "task-"
27-
- Maximum 28 characters total
27+
- Maximum 27 characters total
2828
- Descriptive of the main task
2929
3030
Examples:
@@ -145,17 +145,23 @@ func Generate(ctx context.Context, prompt string, opts ...Option) (string, error
145145
return "", ErrNoNameGenerated
146146
}
147147

148-
generatedName := acc.Messages()[0].Content
149-
150-
if err := codersdk.NameValid(generatedName); err != nil {
151-
return "", xerrors.Errorf("generated name %v not valid: %w", generatedName, err)
148+
taskName := acc.Messages()[0].Content
149+
if taskName == "task-unnamed" {
150+
return "", ErrNoNameGenerated
152151
}
153152

154-
if generatedName == "task-unnamed" {
155-
return "", ErrNoNameGenerated
153+
// We append a suffix to the end of the task name to reduce
154+
// the chance of collisions. We truncate the task name to
155+
// to a maximum of 27 bytes, so that when we append the
156+
// 5 byte suffix (`-` and 4 byte hex slug), it should
157+
// remain within the 32 byte workspace name limit.
158+
taskName = taskName[:min(len(taskName), 27)]
159+
taskName = fmt.Sprintf("%s-%s", taskName, generateSuffix())
160+
if err := codersdk.NameValid(taskName); err != nil {
161+
return "", xerrors.Errorf("generated name %v not valid: %w", taskName, err)
156162
}
157163

158-
return fmt.Sprintf("%s-%s", generatedName, generateSuffix()), nil
164+
return taskName, nil
159165
}
160166

161167
func anthropicDataStream(ctx context.Context, client anthropic.Client, model anthropic.Model, input []aisdk.Message) (aisdk.DataStream, error) {

0 commit comments

Comments
 (0)