Skip to content

Commit d094205

Browse files
chore: ignore invalid names
1 parent db59693 commit d094205

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

agent/agentcontainers/api.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/coder/coder/v2/coderd/httpapi"
2929
"github.com/coder/coder/v2/codersdk"
3030
"github.com/coder/coder/v2/codersdk/agentsdk"
31+
"github.com/coder/coder/v2/provisioner"
3132
"github.com/coder/quartz"
3233
)
3334

@@ -1180,7 +1181,14 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
11801181
// This restricts the ability for a feature to specify the agent name. We may revisit
11811182
// this in the future, but for now we want to restrict this behavior.
11821183
if len(coderCustomization) > 0 {
1183-
possibleAgentName = coderCustomization[len(coderCustomization)-1].Name
1184+
name := coderCustomization[len(coderCustomization)-1].Name
1185+
1186+
// We only want to pick this name if it is a valid name.
1187+
if provisioner.AgentNameRegex.Match([]byte(name)) {
1188+
possibleAgentName = name
1189+
} else {
1190+
logger.Warn(ctx, "invalid agent name in devcontainer customization, ignoring", slog.F("name", name))
1191+
}
11841192
}
11851193
}
11861194

agent/agentcontainers/api_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,17 @@ func TestAPI(t *testing.T) {
17761776
require.NotEmpty(t, subAgent.Name)
17771777
},
17781778
},
1779+
{
1780+
name: "InvalidNameIsIgnored",
1781+
customization: []agentcontainers.CoderCustomization{
1782+
{
1783+
Name: "This--Is_An_Invalid--Name",
1784+
},
1785+
},
1786+
afterCreate: func(t *testing.T, subAgent agentcontainers.SubAgent) {
1787+
require.NotEqual(t, "This--Is_An_Invalid--Name", subAgent.Name)
1788+
},
1789+
},
17791790
}
17801791

17811792
for _, tt := range tests {

0 commit comments

Comments
 (0)