Skip to content

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

Go

fix(chat): keep owners' chats from runs other members started (#11370)

pseudoyudefd7100491a
diff --git a/server/internal/handler/agent_builder.go b/server/internal/handler/agent_builder.go
index 3b0ed868a8..6b38e5b1e2 100644
--- a/server/internal/handler/agent_builder.go
+++ b/server/internal/handler/agent_builder.go
@@ -64,6 +64,9 @@ func (h *Handler) CreateAgentBuilderSession(w http.ResponseWriter, r *http.Reque
if !ok {
return
}
+ if !h.requireChatOwnerAccess(w, r, userID, workspaceID) {
+ return
+ }
var req CreateAgentBuilderSessionRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
@@ -210,6 +213,9 @@ func (h *Handler) ListAgentBuilderSessions(w http.ResponseWriter, r *http.Reques
if !ok {
return
}
+ if !h.requireChatOwnerAccess(w, r, userID, workspaceID) {
+ return
+ }
workspaceUUID, ok := parseUUIDOrBadRequest(w, workspaceID, "workspace id")
if !ok {
return
diff --git a/server/internal/handler/chat.go b/server/internal/handler/chat.go
index 1a585eac34..60ebfedca6 100644
--- a/server/internal/handler/chat.go
+++ b/server/internal/handler/chat.go
@@ -43,6 +43,9 @@ func (h *Handler) CreateChatSession(w http.ResponseWriter, r *http.Request) {
return
}
workspaceID := ctxWorkspaceID(r.Context())
+ if !h.requireChatOwnerAccess(w, r, userID, workspaceID) {
+ return
+ }
var req CreateChatSessionRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
@@ -161,6 +164,9 @@ func (h *Handler) ListChatSessions(w http.ResponseWriter, r *http.Request) {
return
}
workspaceID := ctxWorkspaceID(r.Context())
+ if !h.requireChatOwnerAccess(w, r, userID, workspaceID) {
+ return
+ }
// Compute the accessible-agents set once and use it to drop sessions
// whose target agent the caller no longer has access to — without this,
@@ -252,6 +258,29 @@ func (h *Handler) ListChatSessions(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, resp)
}
+// requireChatOwnerAccess gates every Chat route that acts as the caller's own
+// conversations. A task token's user id is its runtime owner whoever started
+// the run, so only a live task the owner started may list, read or write the
+// owner's chats; agents read their own session through /api/chat/history.
+// Returns false having already written the error response.
+func (h *Handler) requireChatOwnerAccess(w http.ResponseWriter, r *http.Request, userID, workspaceID string) bool {
+ workspaceUUID, ok := parseUUIDOrBadRequest(w, workspaceID, "workspace id")
+ if !ok {
+ return false
+ }
+ ok, err := h.requestRepresentsUser(r, userID, workspaceUUID)
+ if err != nil {
+ slog.Warn("resolve chat access failed", append(logger.RequestAttrs(r), "error", err)...)
+ writeError(w, http.StatusInternalServerError, "failed to resolve chat access")
+ return false
+ }
+ if !ok {
+ writeError(w, http.StatusForbidden, "chat access requires the session owner's verified human trigger")
+ return false
+ }
+ return true
+}
+
func (h *Handler) loadChatSessionForUser(w http.ResponseWriter, r *http.Request, userID, workspaceID, sessionID string) (db.ChatSession, bool) {
sessionUUID, ok := parseUUIDOrBadRequest(w, sessionID, "chat session id")
if !ok {
@@ -261,6 +290,9 @@ func (h *Handler) loadChatSessionForUser(w http.ResponseWriter, r *http.Request,
if !ok {
return db.ChatSession{}, false
}
+ if !h.requireChatOwnerAccess(w, r, userID, workspaceID) {
+ return db.ChatSession{}, false
+ }
session, err := h.Queries.GetChatSessionInWorkspace(r.Context(), db.GetChatSessionInWorkspaceParams{
ID: sessionUUID,
WorkspaceID: workspaceUUID,
@@ -1572,6 +1604,9 @@ func (h *Handler) ListPendingChatTasks(w http.ResponseWriter, r *http.Request) {
return
}
workspaceID := ctxWorkspaceID(r.Context())
+ if !h.requireChatOwnerAccess(w, r, userID, workspaceID) {
+ return
+ }
member, ok := h.workspaceMember(w, r, workspaceID)
if !ok {
@@ -1642,6 +1677,9 @@ func (h *Handler) HasPendingChatTasks(w http.ResponseWriter, r *http.Request) {
return
}
workspaceID := ctxWorkspaceID(r.Context())
+ if !h.requireChatOwnerAccess(w, r, userID, workspaceID) {
+ return
+ }
member, ok := h.workspaceMember(w, r, workspaceID)
if !ok {
@@ -1932,6 +1970,9 @@ func (h *Handler) CancelTaskByUser(w http.ResponseWriter, r *http.Request) {
if task.ChatSessionID.Valid {
// Chat privacy: only the member who opened the conversation may
// cancel its task, even though the workspace is shared.
+ if !h.requireChatOwnerAccess(w, r, userID, workspaceID) {
+ return
+ }
cs, err := h.Queries.GetChatSessionInWorkspace(r.Context(), db.GetChatSessionInWorkspaceParams{
ID: task.ChatSessionID,
WorkspaceID: wsUUID,
diff --git a/server/internal/handler/chat_task_access_test.go b/server/internal/handler/chat_task_access_test.go
new file mode 100644
index 0000000000..c8246febab
--- /dev/null
+++ b/server/internal/handler/chat_task_access_test.go
@@ -0,0 +1,129 @@
+package handler
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+
+ "github.com/google/uuid"
+ "github.com/multica-ai/multica/server/internal/middleware"
+ "github.com/multica-ai/multica/server/internal/testutil"
+)
+
+// A task token's user id is its runtime owner, whoever started the run. A chat
+// session belongs to the member who opened it, so a run another member started
+// on the owner's runtime must not list, read, write or open the owner's chats.
+func TestChatSessionTaskTokenOwnerAccess(t *testing.T) {
+ other := createPlainMember(t, "chat-originator-"+uuid.NewString()+"@example.test")
+ const secret = "runtime owner's private chat"
+ chatRuntimeID := dbfx.Runtime(t, "Chat access chat runtime")
+ chatAgentID := dbfx.Agent(t, "Chat access agent", chatRuntimeID)
+ sessionID := dbfx.Insert(t, "chat_session", testutil.Cols{
+ "workspace_id": testWorkspaceID, "agent_id": chatAgentID, "creator_id": testUserID,
+ "title": "Owner private chat", "status": "active",
+ })
+ dbfx.Insert(t, "chat_message", testutil.Cols{
+ "chat_session_id": sessionID, "role": "user", "content": secret,
+ })
+ dbfx.Cleanup(t, `DELETE FROM chat_message WHERE chat_session_id = $1`, sessionID)
+ dbfx.Cleanup(t, `DELETE FROM agent_task_queue WHERE chat_session_id = $1`, sessionID)
+ dbfx.Cleanup(t, `DELETE FROM chat_session WHERE workspace_id = $1 AND agent_id = $2`, testWorkspaceID, chatAgentID)
+ chatTaskID := dbfx.Task(t, chatAgentID, testutil.Cols{
+ "runtime_id": chatRuntimeID, "chat_session_id": sessionID, "status": "queued",
+ "originator_user_id": testUserID, "accountable_user_id": testUserID,
+ })
+ issueID := dbfx.Issue(t, "Chat task access", nil)
+
+ serve := func(handler func(*Handler, http.ResponseWriter, *http.Request)) http.HandlerFunc {
+ return middleware.Auth(testHandler.Queries, nil, nil, nil)(
+ middleware.RequireWorkspaceMember(testHandler.Queries)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ handler(testHandler, w, r)
+ })),
+ ).ServeHTTP
+ }
+ request := func(token, method, path string, body any) *http.Request {
+ req := testutil.WithHeaders(testutil.JSONRequest(method, path, body),
+ "Authorization", "Bearer "+token, "X-Workspace-ID", testWorkspaceID)
+ return testutil.WithURLParams(req, "sessionId", sessionID)
+ }
+ sessionPath := "/api/chat/sessions/" + sessionID
+
+ for _, tc := range []struct {
+ name string
+ originator any
+ allowed bool
+ }{
+ {"runtime owner started the run", testUserID, true},
+ {"another member started the run", other, false},
+ {"no human started the run", nil, false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ runtimeID := dbfx.Runtime(t, "Chat access runtime")
+ agentID := dbfx.Agent(t, "Chat access task agent", runtimeID)
+ accountable := tc.originator
+ if accountable == nil {
+ accountable = testUserID
+ }
+ taskID := dbfx.Task(t, agentID, testutil.Cols{
+ "runtime_id": runtimeID, "issue_id": issueID, "status": "running",
+ "originator_user_id": tc.originator, "accountable_user_id": accountable,
+ })
+ token := inboxTaskCredential(t, taskID, agentID, testWorkspaceID, testUserID)
+
+ list := testutil.Call(t, serve((*Handler).ListChatSessions),
+ request(token, http.MethodGet, "/api/chat/sessions?status=all", nil))
+ messages := testutil.Call(t, serve((*Handler).ListChatMessages),
+ request(token, http.MethodGet, sessionPath+"/messages", nil))
+ wakeups := testutil.Call(t, serve((*Handler).ListMyChatWakeups),
+ request(token, http.MethodGet, "/api/chat/my-wakeups", nil))
+ if !tc.allowed {
+ // An empty list is also a refusal; what matters is that
+ // neither the session nor its transcript comes back.
+ if list.Code == http.StatusOK && strings.Contains(list.Body.String(), sessionID) {
+ t.Fatalf("list returned the owner's chat session: %s", list.Body.String())
+ }
+ if strings.Contains(messages.Body.String(), secret) {
+ t.Fatalf("messages returned the owner's transcript: %d %s", messages.Code, messages.Body.String())
+ }
+ wakeups.Want(http.StatusForbidden)
+ testutil.Call(t, serve((*Handler).GetChatSession),
+ request(token, http.MethodGet, sessionPath, nil)).Want(http.StatusForbidden)
+ testutil.Call(t, serve((*Handler).SendChatMessage),
+ request(token, http.MethodPost, sessionPath+"/messages", map[string]any{"content": "impersonated"})).
+ Want(http.StatusForbidden)
+ testutil.Call(t, serve((*Handler).CreateChatSession),
+ request(token, http.MethodPost, "/api/chat/sessions", map[string]any{"agent_id": chatAgentID, "title": "impersonated"})).
+ Want(http.StatusForbidden)
+ testutil.Call(t, serve((*Handler).ListAgentBuilderSessions),
+ request(token, http.MethodGet, "/api/agent-builder/sessions", nil)).Want(http.StatusForbidden)
+ cancel := testutil.WithURLParams(request(token, http.MethodPost, "/api/tasks/"+chatTaskID+"/cancel", nil), "taskId", chatTaskID)
+ testutil.Call(t, serve((*Handler).CancelTaskByUser), cancel).Want(http.StatusForbidden)
+ var status string
+ dbfx.QueryRow(t, `SELECT status FROM agent_task_queue WHERE id = $1`, chatTaskID).Scan(&status)
+ if status != "queued" {
+ t.Fatalf("refused cancel still moved the owner's chat task to %q", status)
+ }
+ return
+ }
+ list.Want(http.StatusOK)
+ if !strings.Contains(list.Body.String(), sessionID) {
+ t.Fatalf("owner-started run lost the owner's chat session: %s", list.Body.String())
+ }
+ messages.Want(http.StatusOK)
+ wakeups.Want(http.StatusOK)
+ if !strings.Contains(messages.Body.String(), secret) {
+ t.Fatalf("owner-started run lost the owner's transcript: %s", messages.Body.String())
+ }
+ testutil.Call(t, serve((*Handler).ListAgentBuilderSessions),
+ request(token, http.MethodGet, "/api/agent-builder/sessions", nil)).Want(http.StatusOK)
+ })
+ }
+
+ t.Run("human session", func(t *testing.T) {
+ req := testutil.WithURLParams(newRequest(http.MethodGet, sessionPath+"/messages", nil), "sessionId", sessionID)
+ resp := testutil.Call(t, testHandler.ListChatMessages, chatPendingCtxAs(t, req, testUserID)).Want(http.StatusOK)
+ if !strings.Contains(resp.Body.String(), secret) {
+ t.Fatal("creator lost access to their own chat transcript")
+ }
+ })
+}
diff --git a/server/internal/handler/chat_wakeup.go b/server/internal/handler/chat_wakeup.go
index eb43f6ad9c..4adde89895 100644
--- a/server/internal/handler/chat_wakeup.go
+++ b/server/internal/handler/chat_wakeup.go
@@ -334,7 +334,11 @@ func (h *Handler) ListMyChatWakeups(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
- workspaceID, ok := parseUUIDOrBadRequest(w, ctxWorkspaceID(r.Context()), "workspace id")
+ workspaceID := ctxWorkspaceID(r.Context())
+ if !h.requireChatOwnerAccess(w, r, userID, workspaceID) {
+ return
+ }
+ workspaceUUID, ok := parseUUIDOrBadRequest(w, workspaceID, "workspace id")
if !ok {
return
}
@@ -342,7 +346,7 @@ func (h *Handler) ListMyChatWakeups(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
- rows, err := h.Queries.ListChatWakeupsForSessionOwner(r.Context(), db.ListChatWakeupsForSessionOwnerParams{WorkspaceID: workspaceID, OwnerID: owner})
+ rows, err := h.Queries.ListChatWakeupsForSessionOwner(r.Context(), db.ListChatWakeupsForSessionOwnerParams{WorkspaceID: workspaceUUID, OwnerID: owner})
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to list chat wakeups")
return