diff --git a/coderd/chats.go b/coderd/chats.go index ce211c62cb..46ebc301aa 100644 --- a/coderd/chats.go +++ b/coderd/chats.go @@ -237,7 +237,7 @@ func (api *API) postChats(rw http.ResponseWriter, r *http.Request) { return } - workspaceSelection, validationStatus, validationError := api.validateCreateChatWorkspaceSelection(ctx, req) + workspaceSelection, validationStatus, validationError := api.validateCreateChatWorkspaceSelection(ctx, r, req) if validationError != nil { httpapi.Write(ctx, rw, validationStatus, *validationError) return @@ -1899,6 +1899,7 @@ type createChatWorkspaceSelection struct { func (api *API) validateCreateChatWorkspaceSelection( ctx context.Context, + r *http.Request, req codersdk.CreateChatRequest, ) ( createChatWorkspaceSelection, @@ -1927,6 +1928,12 @@ func (api *API) validateCreateChatWorkspaceSelection( Valid: true, } + if !api.Authorize(r, policy.ActionSSH, workspace) { + return selection, http.StatusBadRequest, &codersdk.Response{ + Message: "Workspace not found or you do not have access to this resource", + } + } + return selection, 0, nil } diff --git a/coderd/chats_test.go b/coderd/chats_test.go index 21d734d818..b1912a1bb5 100644 --- a/coderd/chats_test.go +++ b/coderd/chats_test.go @@ -23,6 +23,7 @@ import ( "github.com/coder/coder/v2/coderd/database/dbfake" "github.com/coder/coder/v2/coderd/externalauth" coderdpubsub "github.com/coder/coder/v2/coderd/pubsub" + "github.com/coder/coder/v2/coderd/rbac" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/testutil" "github.com/coder/websocket" @@ -162,6 +163,41 @@ func TestPostChats(t *testing.T) { ) }) + t.Run("WorkspaceAccessibleButNoSSH", func(t *testing.T) { + t.Parallel() + + ctx := testutil.Context(t, testutil.WaitLong) + adminClient, db := newChatClientWithDatabase(t) + firstUser := coderdtest.CreateFirstUser(t, adminClient) + orgAdminClient, _ := coderdtest.CreateAnotherUser( + t, + adminClient, + firstUser.OrganizationID, + rbac.ScopedRoleOrgAdmin(firstUser.OrganizationID), + ) + + workspaceBuild := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ + OrganizationID: firstUser.OrganizationID, + OwnerID: firstUser.UserID, + }).WithAgent().Do() + + _, err := orgAdminClient.CreateChat(ctx, codersdk.CreateChatRequest{ + Content: []codersdk.ChatInputPart{ + { + Type: codersdk.ChatInputPartTypeText, + Text: "hello", + }, + }, + WorkspaceID: &workspaceBuild.Workspace.ID, + }) + sdkErr := requireSDKError(t, err, http.StatusBadRequest) + require.Equal( + t, + "Workspace not found or you do not have access to this resource", + sdkErr.Message, + ) + }) + t.Run("WorkspaceNotFound", func(t *testing.T) { t.Parallel()