Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(workspaces): refuse workspace deletion from machine credentials (#10994)

pseudoyud0d0b1f49eb0
diff --git a/server/internal/handler/workspace.go b/server/internal/handler/workspace.go
index e280bc2fa3..4d9cadb532 100644
--- a/server/internal/handler/workspace.go
+++ b/server/internal/handler/workspace.go
@@ -1244,7 +1244,14 @@ func lockWorkspaceTaskOwners(ctx context.Context, qtx *db.Queries, workspaceID p
return nil
}
+// DeleteWorkspace refuses machine credentials: a task token or cloud node PAT
+// authenticates as its owner, and deleting a workspace takes every issue, doc
+// and agent in it, so only a human may do it.
func (h *Handler) DeleteWorkspace(w http.ResponseWriter, r *http.Request) {
+ if isMachineCredentialActor(r) {
+ writeError(w, http.StatusForbidden, "this endpoint is only available to human actors")
+ return
+ }
workspaceID := workspaceIDFromURL(r, "id")
// Defense in depth: the route is already gated by the
diff --git a/server/internal/handler/workspace_delete_actor_test.go b/server/internal/handler/workspace_delete_actor_test.go
new file mode 100644
index 0000000000..ff06346c31
--- /dev/null
+++ b/server/internal/handler/workspace_delete_actor_test.go
@@ -0,0 +1,58 @@
+package handler
+
+import (
+ "context"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+)
+
+// An agent task token and a cloud node PAT both authenticate as their owner,
+// so the owner-role check alone would let a running agent delete its owner's
+// whole workspace. Deleting a workspace takes every issue, doc and agent with
+// it, and doc deletion is already human-only.
+func TestDeleteWorkspaceRefusesMachineCredentials(t *testing.T) {
+ if testPool == nil {
+ t.Skip("DATABASE_URL not set")
+ }
+ ctx := context.Background()
+
+ const slug = "handler-tests-delete-machine-actor"
+ _, _ = testPool.Exec(ctx, `DELETE FROM workspace WHERE slug = $1`, slug)
+ var wsID string
+ if err := testPool.QueryRow(ctx, `
+INSERT INTO workspace (name, slug, description)
+VALUES ($1, $2, $3)
+RETURNING id
+`, "Handler Test Delete Machine Actor", slug, "DeleteWorkspace machine actor test").Scan(&wsID); err != nil {
+ t.Fatalf("create workspace: %v", err)
+ }
+ t.Cleanup(func() {
+ _, _ = testPool.Exec(context.Background(), `DELETE FROM workspace WHERE id = $1`, wsID)
+ })
+ if _, err := testPool.Exec(ctx, `
+INSERT INTO member (workspace_id, user_id, role)
+VALUES ($1, $2, 'owner')
+`, wsID, testUserID); err != nil {
+ t.Fatalf("create owner member: %v", err)
+ }
+
+ for _, source := range []string{"task_token", "cloud_pat"} {
+ t.Run(source, func(t *testing.T) {
+ w := httptest.NewRecorder()
+ req := withURLParam(newRequest("DELETE", "/api/workspaces/"+wsID, nil), "id", wsID)
+ req.Header.Set("X-Actor-Source", source)
+ testHandler.DeleteWorkspace(w, req)
+ if w.Code != http.StatusForbidden {
+ t.Fatalf("status = %d, want 403: %s", w.Code, w.Body.String())
+ }
+ var exists bool
+ if err := testPool.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM workspace WHERE id = $1)`, wsID).Scan(&exists); err != nil {
+ t.Fatalf("verify workspace: %v", err)
+ }
+ if !exists {
+ t.Fatal("workspace was deleted by a machine credential")
+ }
+ })
+ }
+}