Skip to content

Commit 3f328ea

Browse files
committed
fix: validate uuid
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent a8854ed commit 3f328ea

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

bridge.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"cdr.dev/slog/v3"
1515
"github.com/coder/aibridge/circuitbreaker"
16+
"github.com/google/uuid"
1617
aibcontext "github.com/coder/aibridge/context"
1718
"github.com/coder/aibridge/mcp"
1819
"github.com/coder/aibridge/metrics"
@@ -201,19 +202,26 @@ func newInterceptionProcessor(p provider.Provider, cbs *circuitbreaker.ProviderC
201202
// to the correct user rather than the service-level identity.
202203
if client == ClientCoderAgents {
203204
if ownerID := r.Header.Get("X-Coder-Owner-Id"); ownerID != "" {
204-
existingActor := aibcontext.ActorFromContext(ctx)
205-
var md recorder.Metadata
206-
var previousActorID string
207-
if existingActor != nil {
208-
md = existingActor.Metadata
209-
previousActorID = existingActor.ID
205+
if _, err := uuid.Parse(ownerID); err != nil {
206+
logger.Warn(ctx, "ignoring invalid X-Coder-Owner-Id, expected UUID",
207+
slog.F("value", ownerID),
208+
slog.Error(err),
209+
)
210+
} else {
211+
existingActor := aibcontext.ActorFromContext(ctx)
212+
var md recorder.Metadata
213+
var previousActorID string
214+
if existingActor != nil {
215+
md = existingActor.Metadata
216+
previousActorID = existingActor.ID
217+
}
218+
logger.Debug(ctx, "overriding initiator with X-Coder-Owner-Id",
219+
slog.F("previous_actor_id", previousActorID),
220+
slog.F("new_actor_id", ownerID),
221+
)
222+
ctx = aibcontext.AsActor(ctx, ownerID, md)
223+
r = r.WithContext(ctx)
210224
}
211-
logger.Debug(ctx, "overriding initiator with X-Coder-Owner-Id",
212-
slog.F("previous_actor_id", previousActorID),
213-
slog.F("new_actor_id", ownerID),
214-
)
215-
ctx = aibcontext.AsActor(ctx, ownerID, md)
216-
r = r.WithContext(ctx)
217225
}
218226
}
219227

internal/integrationtest/bridge_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,13 +2096,13 @@ func TestActorHeaders(t *testing.T) {
20962096
func TestCoderAgentsInitiatorOverride(t *testing.T) {
20972097
t.Parallel()
20982098

2099-
const overrideActorID = "owner-id-from-coder"
2099+
const overrideActorID = "b1c2d3e4-5678-4a9b-8c0d-1e2f3a4b5c6d"
21002100

21012101
cases := []struct {
2102-
name string
2103-
userAgent string
2104-
ownerIDHeader string
2105-
expectInitiator string
2102+
name string
2103+
userAgent string
2104+
ownerIDHeader string
2105+
expectInitiator string
21062106
expectLogOverride bool
21072107
}{
21082108
{
@@ -2118,6 +2118,12 @@ func TestCoderAgentsInitiatorOverride(t *testing.T) {
21182118
ownerIDHeader: "",
21192119
expectInitiator: defaultActorID,
21202120
},
2121+
{
2122+
name: "coder_agents_with_invalid_owner_id",
2123+
userAgent: "coder-agents/v2.24.0 (linux/amd64)",
2124+
ownerIDHeader: "not-a-uuid",
2125+
expectInitiator: defaultActorID,
2126+
},
21212127
{
21222128
name: "non_coder_agents_with_owner_id_header",
21232129
userAgent: "claude-code/1.0.0",

0 commit comments

Comments
 (0)