Skip to content

Commit 29f644a

Browse files
committed
fix: validate uuid
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 89a1187 commit 29f644a

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
@@ -12,6 +12,7 @@ import (
1212

1313
"cdr.dev/slog/v3"
1414
"github.com/coder/aibridge/circuitbreaker"
15+
"github.com/google/uuid"
1516
aibcontext "github.com/coder/aibridge/context"
1617
"github.com/coder/aibridge/mcp"
1718
"github.com/coder/aibridge/metrics"
@@ -177,19 +178,26 @@ func newInterceptionProcessor(p provider.Provider, cbs *circuitbreaker.ProviderC
177178
// to the correct user rather than the service-level identity.
178179
if client == ClientCoderAgents {
179180
if ownerID := r.Header.Get("X-Coder-Owner-Id"); ownerID != "" {
180-
existingActor := aibcontext.ActorFromContext(ctx)
181-
var md recorder.Metadata
182-
var previousActorID string
183-
if existingActor != nil {
184-
md = existingActor.Metadata
185-
previousActorID = existingActor.ID
181+
if _, err := uuid.Parse(ownerID); err != nil {
182+
logger.Warn(ctx, "ignoring invalid X-Coder-Owner-Id, expected UUID",
183+
slog.F("value", ownerID),
184+
slog.Error(err),
185+
)
186+
} else {
187+
existingActor := aibcontext.ActorFromContext(ctx)
188+
var md recorder.Metadata
189+
var previousActorID string
190+
if existingActor != nil {
191+
md = existingActor.Metadata
192+
previousActorID = existingActor.ID
193+
}
194+
logger.Debug(ctx, "overriding initiator with X-Coder-Owner-Id",
195+
slog.F("previous_actor_id", previousActorID),
196+
slog.F("new_actor_id", ownerID),
197+
)
198+
ctx = aibcontext.AsActor(ctx, ownerID, md)
199+
r = r.WithContext(ctx)
186200
}
187-
logger.Debug(ctx, "overriding initiator with X-Coder-Owner-Id",
188-
slog.F("previous_actor_id", previousActorID),
189-
slog.F("new_actor_id", ownerID),
190-
)
191-
ctx = aibcontext.AsActor(ctx, ownerID, md)
192-
r = r.WithContext(ctx)
193201
}
194202
}
195203

internal/integrationtest/bridge_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,13 +2088,13 @@ func TestActorHeaders(t *testing.T) {
20882088
func TestCoderAgentsInitiatorOverride(t *testing.T) {
20892089
t.Parallel()
20902090

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

20932093
cases := []struct {
2094-
name string
2095-
userAgent string
2096-
ownerIDHeader string
2097-
expectInitiator string
2094+
name string
2095+
userAgent string
2096+
ownerIDHeader string
2097+
expectInitiator string
20982098
expectLogOverride bool
20992099
}{
21002100
{
@@ -2110,6 +2110,12 @@ func TestCoderAgentsInitiatorOverride(t *testing.T) {
21102110
ownerIDHeader: "",
21112111
expectInitiator: defaultActorID,
21122112
},
2113+
{
2114+
name: "coder_agents_with_invalid_owner_id",
2115+
userAgent: "coder-agents/v2.24.0 (linux/amd64)",
2116+
ownerIDHeader: "not-a-uuid",
2117+
expectInitiator: defaultActorID,
2118+
},
21132119
{
21142120
name: "non_coder_agents_with_owner_id_header",
21152121
userAgent: "claude-code/1.0.0",

0 commit comments

Comments
 (0)