fix(coderd): restrict chat workspace rebinding to the chat owner (#30185)

`PATCH /api/v2/chats/{chat}` let any caller with update permission on a
chat set or clear its `workspace_id`, and it checked the new workspace
against the caller's SSH access instead of the owner's. Site owners and
organization admins can update any chat in scope, so they could bind
another user's chat to a workspace they control, and the owner's later
prompts and agent tool calls would then run there. Checking the owner's
SSH access instead would not help, because the caller can share their
own workspace with the owner first.

`updateChat`, which backs the PATCH route and the in-process
`UpdateChatAsUser`, now returns 403 ("Only the chat owner can change its
workspace.") when anyone other than the owner sends `workspace_id`,
including the nil UUID that clears the binding. The check sits next to
the existing owner-only read-state check, before any field is written,
so a rejected multi-field request changes nothing. The UI already offers
workspace changes only to the chat owner.

Other paths checked, no change needed: the PATCH route is the only HTTP
route that writes the binding, chatd binds workspaces directly on the
owner's behalf, chat sharing grants read only, and creating a chat for
another user (`owner_id`) already requires the authority to mint that
user's API key and validates the workspace as that user.

Security review follow-up (ISSUE-14).

## Testing
- `TestPatchChat/WorkspaceBinding/NonOwnerForbidden`: the deployment
owner renames a member's chat while rebinding it to the owner's own
workspace, then while clearing it. Both requests get 403 and the chat is
unchanged. The test fails without the check.
- Remote dogfood
[UAT](https://dogfood.cdr.dev/agents/0f64f91d-a03e-4067-8038-a6d846511d8d)
passed on `08059d73a82`. Workspace changes from the site owner and an
org admin got 403 with the chat unchanged (rebind, rebind plus title,
clear, current workspace, unknown ID, after sharing the target
workspace), a title-only PATCH still returns 204, and the owner
detached, attached, and switched workspaces in the UI.

## Review record
- Test audit (Xum agent) on `08059d73a82`: passed, no changes.
- Remote UAT round 1 on `08059d73a82`: passed.
- Codex r1 on `08059d73a82`: no major issues, no threads.
- Rebased onto `main` at `34ef30ee902` as `21102abadb5`. `main` had
moved the PATCH body into `updateChat`, so the check now returns an
`httperror` with the caller ID passed in. The test merged unchanged,
passes, and fails again with the check disabled. The UAT and test audit
above ran on the pre-rebase head.

> 🤖 This PR was opened by [Xum](https://mux.coder.com) on behalf of
@ibetitsmike.
This commit is contained in:
Michael Suchacz authored and GitHub committed 2026-10-27 02:14:33 +01:00
1 parent 34ef30ee90
commit 27db7ace68
2 files changed
+51

No files matched your search

+9
View File
@@ -2684,6 +2684,15 @@ func (api *API) updateChat(
})
}
// The binding decides where the owner's later prompts and tool calls
// run. Checking the owner's SSH access to the target is not enough,
// because a non-owner can first share their own workspace with them.
if req.WorkspaceID != nil && chat.OwnerID != callerID {
return database.Chat{}, httperror.NewResponseError(http.StatusForbidden, codersdk.Response{
Message: "Only the chat owner can change its workspace.",
})
}
var title string
if req.Title != nil {
normalized, resp := normalizeChatTitle(*req.Title)
+42
View File
@@ -6731,6 +6731,48 @@ func TestPatchChat(t *testing.T) {
UserID: firstUser.UserID,
}))
})
t.Run("NonOwnerForbidden", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
client, db := newChatClientWithDatabase(t)
firstUser := coderdtest.CreateFirstUser(t, client.Client)
modelConfig := createChatModel(t, client)
_, member := coderdtest.CreateAnotherUser(t, client.Client, firstUser.OrganizationID)
memberWorkspace := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
OrganizationID: firstUser.OrganizationID,
OwnerID: member.ID,
}).WithAgent().Do()
// The deployment owner can SSH into its own workspace, so only
// the ownership check can stop this rebind.
ownerWorkspace := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
OrganizationID: firstUser.OrganizationID,
OwnerID: firstUser.UserID,
}).WithAgent().Do()
chat := dbgen.Chat(t, db, database.Chat{
OrganizationID: firstUser.OrganizationID,
OwnerID: member.ID,
LastModelConfigID: modelConfig.ID,
Title: "member chat",
WorkspaceID: uuid.NullUUID{UUID: memberWorkspace.Workspace.ID, Valid: true},
})
for _, workspaceID := range []uuid.UUID{ownerWorkspace.Workspace.ID, uuid.Nil} {
err := client.UpdateChat(ctx, chat.ID, codersdk.UpdateChatRequest{
Title: ptr.Ref("renamed by admin"),
WorkspaceID: &workspaceID,
})
sdkErr := requireSDKError(t, err, http.StatusForbidden)
require.Equal(t, "Only the chat owner can change its workspace.", sdkErr.Message)
persisted := getChat(ctx, t, client, chat.ID)
require.Equal(t, "member chat", persisted.Title)
require.NotNil(t, persisted.WorkspaceID)
require.Equal(t, memberWorkspace.Workspace.ID, *persisted.WorkspaceID)
}
})
})
t.Run("Title", func(t *testing.T) {