Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(server): stop dropping member_ids on PUT /api/projects for private projects (MUL-9325) (#10662)
Jiayuan Zhang547e5212f172
diff --git a/server/cmd/multica/cmd_project.go b/server/cmd/multica/cmd_project.goindex 01cbc3bb44..0e8d0506e8 100644--- a/server/cmd/multica/cmd_project.go+++ b/server/cmd/multica/cmd_project.go@@ -638,8 +638,9 @@ func runProjectUpdate(cmd *cobra.Command, args []string) error {return fmt.Errorf("no fields to update; use flags like --title, --status, --description, --icon, --lead, --start-date, --due-date, --visibility")}- // The server reads member_ids only when the visibility changes, so members- // named for a project that is already private go through the member API.+ // Servers before MUL-9325 read member_ids only when the visibility+ // changes, so members named for a project that is already private go+ // through the member API.if ids, ok := body["member_ids"].([]string); ok {var project map[string]anyif err := client.GetJSON(ctx, "/api/projects/"+projectRef.ID, &project); err != nil {diff --git a/server/internal/handler/private_project_members_only_test.go b/server/internal/handler/private_project_members_only_test.goindex 363c6d6cbf..f4a0edc5c7 100644--- a/server/internal/handler/private_project_members_only_test.go+++ b/server/internal/handler/private_project_members_only_test.go@@ -5,6 +5,7 @@ import ("encoding/base64""encoding/json""net/http"+ "strings""testing""github.com/multica-ai/multica/server/internal/projectaccess"@@ -498,3 +499,102 @@ func TestSearchIndexCopyFromRoleBasedAccessIsRebuilt(t *testing.T) {t.Errorf("returned cursor access rules = %d, want %d", next.AccessRules, searchIndexAccessRules)}}++// MUL-9325: PUT /api/projects/{id} used to read member_ids only when the+// visibility changed, so naming members for a project that was already+// private succeeded and added nobody. Older CLIs still send exactly that.+func TestProjectUpdateMemberIDsAreNeverDropped(t *testing.T) {+ f := newPrivateProjectFixture(t)+ dbfx.Exec(t, `UPDATE project SET lead_type = 'member', lead_id = $1 WHERE id = $2`, f.insider, f.projectID)+ plain := createPlainMember(t, f.marker+"[email protected]")+ addProjectMemberRow(t, f.projectID, plain)+ newcomer := createPlainMember(t, f.marker+"[email protected]")+ title := func() string {+ var s string+ if err := testPool.QueryRow(context.Background(), `SELECT title FROM project WHERE id = $1`, f.projectID).Scan(&s); err != nil {+ t.Fatal(err)+ }+ return s+ }+ visibility := func() string {+ var s string+ if err := testPool.QueryRow(context.Background(), `SELECT visibility FROM project WHERE id = $1`, f.projectID).Scan(&s); err != nil {+ t.Fatal(err)+ }+ return s+ }+ refused := map[string]any{"visibility": "private", "member_ids": []string{newcomer}, "title": "must not change"}+ assertUnchanged := func(t *testing.T) {+ t.Helper()+ if projectMemberIDs(t, f.projectID)[newcomer] || title() == "must not change" {+ t.Fatal("a refused update still changed the project")+ }+ }++ t.Run("callers who could not add members directly are refused alike", func(t *testing.T) {+ const denied = "only workspace admins and the member who manages this project's access can add members"+ addProjectMembersAs(t, plain, f.projectID, newcomer).Want(http.StatusForbidden)+ if body := updateProjectAs(t, plain, f.projectID, refused).Want(http.StatusForbidden).Text(); !strings.Contains(body, denied) {+ t.Fatalf("non-manager: body = %s, want the member endpoint's message", body)+ }+ assertUnchanged(t)++ agent := dbfx.Agent(t, f.marker+" insider agent", testRuntimeID, testutil.Cols{"owner_id": f.insider})+ task := dbfx.Task(t, agent, testutil.Cols{"runtime_id": testRuntimeID, "issue_id": f.secretIssue, "status": "running",+ "originator_user_id": f.insider, "accountable_user_id": f.insider})+ req := asUser(newRequest(http.MethodPut, "/api/projects/"+f.projectID, refused), f.insider)+ req.Header.Set("X-Actor-Source", "task_token")+ req.Header.Set("X-Agent-ID", agent)+ req.Header.Set("X-Task-ID", task)+ testutil.Call(t, testHandler.UpdateProject, withURLParam(req, "id", f.projectID)).Want(http.StatusForbidden)+ assertUnchanged(t)++ updateProjectAs(t, f.insider, f.projectID, map[string]any{"member_ids": []string{"00000000-0000-0000-0000-00000000beef"}, "title": "must not change"}).+ Want(http.StatusBadRequest)+ assertUnchanged(t)+ })++ t.Run("the access manager adds them, with or without restating the visibility", func(t *testing.T) {+ t.Cleanup(func() { removeProjectMemberRow(t, f.projectID, newcomer) })+ updateProjectAs(t, f.insider, f.projectID, map[string]any{"visibility": "private", "member_ids": []string{newcomer}}).Want(http.StatusOK)+ if !projectMemberIDs(t, f.projectID)[newcomer] {+ t.Fatal("member_ids with an unchanged visibility added nobody")+ }+ removeProjectMemberRow(t, f.projectID, newcomer)+ updateProjectAs(t, testUserID, f.projectID, map[string]any{"member_ids": []string{newcomer}}).Want(http.StatusOK)+ if !projectMemberIDs(t, f.projectID)[newcomer] {+ t.Fatal("member_ids without visibility added nobody")+ }+ })++ t.Run("together with a new lead who is not yet a member", func(t *testing.T) {+ t.Cleanup(func() {+ dbfx.Exec(t, `UPDATE project SET lead_type = 'member', lead_id = $1 WHERE id = $2`, f.insider, f.projectID)+ removeProjectMemberRow(t, f.projectID, newcomer)+ removeProjectMemberRow(t, f.projectID, f.outsider)+ })+ updateProjectAs(t, f.insider, f.projectID, map[string]any{"lead_type": "member", "lead_id": f.outsider, "member_ids": []string{newcomer}}).+ Want(http.StatusOK)+ if members := projectMemberIDs(t, f.projectID); !members[newcomer] || !members[f.outsider] {+ t.Fatalf("members = %v, want the named member and the new lead", members)+ }+ })++ t.Run("a project made workspace-visible takes no new members", func(t *testing.T) {+ updateProjectAs(t, testUserID, f.projectID, map[string]any{"visibility": "workspace", "member_ids": []string{newcomer}}).+ Want(http.StatusBadRequest)+ if visibility() != projectaccess.VisibilityPrivate || projectMemberIDs(t, f.projectID)[newcomer] {+ t.Fatal("a refused switch still changed the project")+ }+ // The web app's access dialog restates the current list.+ updateProjectAs(t, testUserID, f.projectID, map[string]any{"visibility": "workspace", "member_ids": []string{testUserID, f.insider, plain}}).+ Want(http.StatusOK)+ if visibility() != projectaccess.VisibilityWorkspace {+ t.Fatal("project did not turn workspace-visible")+ }+ updateProjectAs(t, testUserID, f.projectID, map[string]any{"member_ids": []string{newcomer}}).Want(http.StatusBadRequest)+ if projectMemberIDs(t, f.projectID)[newcomer] {+ t.Fatal("a workspace-visible project gained a member")+ }+ })+}diff --git a/server/internal/handler/project.go b/server/internal/handler/project.goindex c713b7f1ed..6fcbe9163d 100644--- a/server/internal/handler/project.go+++ b/server/internal/handler/project.go@@ -188,7 +188,8 @@ type UpdateProjectRequest struct {StartDate *string `json:"start_date"`DueDate *string `json:"due_date"`// Visibility switches between "workspace" and "private" (MUL-8472).- // MemberIDs is only read together with a switch to "private".+ // MemberIDs adds project members to a project that is or becomes+ // "private"; see prepareProjectAccessUpdate.Visibility *string `json:"visibility"`MemberIDs []string `json:"member_ids,omitempty"`}diff --git a/server/internal/handler/project_member.go b/server/internal/handler/project_member.goindex 05336a174b..c09b71bbab 100644--- a/server/internal/handler/project_member.go+++ b/server/internal/handler/project_member.go@@ -6,6 +6,7 @@ import ("errors""log/slog""net/http"+ "slices""github.com/go-chi/chi/v5""github.com/jackc/pgx/v5"@@ -136,15 +137,20 @@ func parseProjectMemberIDs(w http.ResponseWriter, raw []string, field string) ([}// prepareProjectAccessUpdate validates the access side of a project update-// before anything is written. Two things change who can see a project:+// before anything is written. Three things change who can see a project://// - a visibility switch (requested = nil leaves it alone), which takes a// human who manages the project's access;+// - member_ids on a project that is or stays private, which adds them as+// POST /members would and takes the same human;// - on a private project, a new member lead who is not yet a project member.// A member lead must be a project member, so the update adds them, which// is granting access: it takes a human who could add members directly.// Agents never grant access, here or anywhere else.//+// member_ids is never dropped silently: on a project that is or becomes+// workspace-visible, naming anyone not already a project member is a 400.+//// newLeadType / newLeadID are the lead the project will have after the update.func (h *Handler) prepareProjectAccessUpdate(w http.ResponseWriter, r *http.Request, project db.Project, requested *string, rawMemberIDs []string, newLeadType pgtype.Text, newLeadID pgtype.UUID, leadChanged bool) (projectAccessUpdate, bool) {workspaceID := uuidToString(project.WorkspaceID)@@ -210,22 +216,78 @@ func (h *Handler) prepareProjectAccessUpdate(w http.ResponseWriter, r *http.Requ}}update.addIDs = ids+ } else if !h.requireExistingProjectMembers(w, r, project, rawMemberIDs) {+ return projectAccessUpdate{}, false}return update, true}- if current != projectaccess.VisibilityPrivate || !leadChanged || !memberLead {+ if current != projectaccess.VisibilityPrivate {+ if !h.requireExistingProjectMembers(w, r, project, rawMemberIDs) {+ return projectAccessUpdate{}, false+ }return projectAccessUpdate{}, true}++ var update projectAccessUpdate+ if len(rawMemberIDs) > 0 {+ if !h.requireHumanProjectAccessActor(w, r, workspaceID) {+ return projectAccessUpdate{}, false+ }+ ids, ok := parseProjectMemberIDs(w, rawMemberIDs, "member_ids")+ if !ok {+ return projectAccessUpdate{}, false+ }+ member, ok := h.workspaceMember(w, r, workspaceID)+ if !ok {+ return projectAccessUpdate{}, false+ }+ if !h.requireProjectAccessManager(w, r, member, project, "only workspace admins and the member who manages this project's access can add members") {+ return projectAccessUpdate{}, false+ }+ update.addIDs = ids+ }+ if !leadChanged || !memberLead {+ return update, true+ }isMember, err := h.isProjectMember(r, project, newLeadID)if err != nil {writeError(w, http.StatusInternalServerError, "failed to check project membership")return projectAccessUpdate{}, false}- if isMember {- return projectAccessUpdate{}, true+ if !isMember && !slices.Contains(update.addIDs, newLeadID) {+ update.addIDs = append(update.addIDs, newLeadID)+ }+ return update, true+}++// requireExistingProjectMembers answers 400 unless every id in member_ids is+// already a project member. A workspace-visible project gains no members from+// an update; the web app's access dialog sends the current list when it makes+// a project public, which stays accepted.+func (h *Handler) requireExistingProjectMembers(w http.ResponseWriter, r *http.Request, project db.Project, rawMemberIDs []string) bool {+ if len(rawMemberIDs) == 0 {+ return true+ }+ ids, ok := parseProjectMemberIDs(w, rawMemberIDs, "member_ids")+ if !ok {+ return false+ }+ existing, err := h.Queries.ListProjectMemberUserIDs(r.Context(), db.ListProjectMemberUserIDsParams{+ ProjectID: project.ID,+ WorkspaceID: project.WorkspaceID,+ })+ if err != nil {+ writeError(w, http.StatusInternalServerError, "failed to list project members")+ return false}- return projectAccessUpdate{addIDs: []pgtype.UUID{newLeadID}}, true+ for _, id := range ids {+ if !slices.Contains(existing, id) {+ writeError(w, http.StatusBadRequest, "member_ids only adds members to a private project")+ return false+ }+ }+ return true}// writeProjectUpdate stores params and, in the same transaction, a prepared