Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(api): tighten member project access changes

Andrej447b07df97cc
diff --git a/apps/api/src/project-access/list-workspace-project-access.ts b/apps/api/src/project-access/list-workspace-project-access.ts
index 4ac9ccb7e..c1bfb72e4 100644
--- a/apps/api/src/project-access/list-workspace-project-access.ts
+++ b/apps/api/src/project-access/list-workspace-project-access.ts
@@ -1,9 +1,11 @@
-import { eq } from "drizzle-orm";
+import { and, eq } from "drizzle-orm";
import db, { schema } from "../database";
import type { MemberProjectAccess } from "./member-project-access-type";
+import { projectAccessCondition } from "./project-access-condition";
export async function listWorkspaceProjectAccess(
workspaceId: string,
+ viewerId: string,
): Promise<MemberProjectAccess[]> {
const [rules, grants] = await Promise.all([
db
@@ -19,7 +21,15 @@ export async function listWorkspaceProjectAccess(
projectId: schema.workspaceMemberProjectTable.projectId,
})
.from(schema.workspaceMemberProjectTable)
- .where(eq(schema.workspaceMemberProjectTable.workspaceId, workspaceId)),
+ .where(
+ and(
+ eq(schema.workspaceMemberProjectTable.workspaceId, workspaceId),
+ projectAccessCondition(
+ viewerId,
+ schema.workspaceMemberProjectTable.projectId,
+ ),
+ ),
+ ),
]);
return rules
diff --git a/apps/api/src/project-access/lock-actor-membership.ts b/apps/api/src/project-access/lock-actor-membership.ts
new file mode 100644
index 000000000..35763622e
--- /dev/null
+++ b/apps/api/src/project-access/lock-actor-membership.ts
@@ -0,0 +1,31 @@
+import { and, eq } from "drizzle-orm";
+import { schema } from "../database";
+import { hasInstanceAdminRole } from "../utils/instance-admin-role";
+import type { DbOrTx } from "./db-or-tx";
+
+export async function lockActorMembership(
+ database: DbOrTx,
+ workspaceId: string,
+ userId: string,
+): Promise<boolean> {
+ const [user] = await database
+ .select({ role: schema.userTable.role })
+ .from(schema.userTable)
+ .where(eq(schema.userTable.id, userId))
+ .limit(1);
+ if (hasInstanceAdminRole(user?.role)) return true;
+
+ const [member] = await database
+ .select({ id: schema.workspaceUserTable.id })
+ .from(schema.workspaceUserTable)
+ .where(
+ and(
+ eq(schema.workspaceUserTable.workspaceId, workspaceId),
+ eq(schema.workspaceUserTable.userId, userId),
+ ),
+ )
+ .limit(1)
+ .for("share");
+
+ return Boolean(member);
+}
diff --git a/apps/api/src/project-access/unassign-inaccessible-tasks.ts b/apps/api/src/project-access/unassign-inaccessible-tasks.ts
new file mode 100644
index 000000000..4a7b43616
--- /dev/null
+++ b/apps/api/src/project-access/unassign-inaccessible-tasks.ts
@@ -0,0 +1,50 @@
+import { and, eq, inArray, not } from "drizzle-orm";
+import createActivities from "../activity/controllers/create-activities";
+import { schema } from "../database";
+import type { DbOrTx } from "./db-or-tx";
+import { projectAccessCondition } from "./project-access-condition";
+
+export async function unassignInaccessibleTasks(
+ database: DbOrTx,
+ change: { workspaceId: string; userId: string; actorId: string },
+): Promise<{ id: string; projectId: string }[]> {
+ const unassigned = await database
+ .update(schema.taskTable)
+ .set({ userId: null })
+ .where(
+ and(
+ eq(schema.taskTable.userId, change.userId),
+ inArray(
+ schema.taskTable.projectId,
+ database
+ .select({ id: schema.projectTable.id })
+ .from(schema.projectTable)
+ .where(
+ and(
+ eq(schema.projectTable.workspaceId, change.workspaceId),
+ not(
+ projectAccessCondition(change.userId, schema.projectTable.id),
+ ),
+ ),
+ ),
+ ),
+ ),
+ )
+ .returning({
+ id: schema.taskTable.id,
+ projectId: schema.taskTable.projectId,
+ });
+
+ await createActivities(
+ unassigned.map((task) => ({
+ taskId: task.id,
+ type: "unassigned",
+ userId: change.actorId,
+ content: null,
+ eventData: {},
+ })),
+ database,
+ );
+
+ return unassigned;
+}
diff --git a/apps/api/src/workspace/controllers/update-member-project-access.ts b/apps/api/src/workspace/controllers/update-member-project-access.ts
index 5c0083330..7b3bf0c48 100644
--- a/apps/api/src/workspace/controllers/update-member-project-access.ts
+++ b/apps/api/src/workspace/controllers/update-member-project-access.ts
@@ -3,9 +3,11 @@ import { HTTPException } from "hono/http-exception";
import db, { schema } from "../../database";
import { publishEvent } from "../../events";
import { keepHiddenGrants } from "../../project-access/keep-hidden-grants";
+import { lockActorMembership } from "../../project-access/lock-actor-membership";
import { replaceMemberProjectAccess } from "../../project-access/replace-member-project-access";
import type { ProjectAccessMode } from "../../project-access/project-access-mode";
import { resolveProjectAccessRequest } from "../../project-access/resolve-project-access-request";
+import { unassignInaccessibleTasks } from "../../project-access/unassign-inaccessible-tasks";
async function updateMemberProjectAccess(request: {
workspaceId: string;
@@ -62,17 +64,26 @@ async function updateMemberProjectAccess(request: {
throw new HTTPException(403, { message: outcome.message });
}
- await db.transaction((tx) =>
- replaceMemberProjectAccess(tx, {
+ const unassigned = await db.transaction(async (tx) => {
+ if (!(await lockActorMembership(tx, workspaceId, actorId))) {
+ throw new HTTPException(403, {
+ message: "You don't have access to this workspace",
+ });
+ }
+ await replaceMemberProjectAccess(tx, {
workspaceId,
userId,
...outcome.access,
- }),
- );
+ });
+ return unassignInaccessibleTasks(tx, { workspaceId, userId, actorId });
+ });
await publishEvent("project_access.updated", { workspaceId, userId });
+ for (const projectId of new Set(unassigned.map((task) => task.projectId))) {
+ await publishEvent("task.bulk_unassigned", { projectId, userId: actorId });
+ }
- return { userId, ...outcome.access };
+ return { userId, ...resolution.access };
}
export default updateMemberProjectAccess;
diff --git a/apps/api/src/workspace/index.ts b/apps/api/src/workspace/index.ts
index b85757fba..96e3a309f 100644
--- a/apps/api/src/workspace/index.ts
+++ b/apps/api/src/workspace/index.ts
@@ -47,7 +47,7 @@ const getWorkspaceProjectAccessRoute = createRoute({
tags: ["Workspaces"],
summary: "Get member project access",
description:
- "List the members who are limited to selected projects, with the projects each one can access. Everyone else in the workspace can access every project.",
+ "List the members who are limited to selected projects, with the projects each one can access. Everyone else in the workspace can access every project. A caller whose own access is limited only sees the projects they can access.",
middleware: [
workspaceAccess.fromParam("workspaceId"),
requireWorkspacePermission({ member: ["update"] }),
@@ -110,7 +110,10 @@ const workspace = apiRouter<BaseVariables & { workspaceId: string }>()
),
)
.openapi(getWorkspaceProjectAccessRoute, async (c) =>
- c.json(await listWorkspaceProjectAccess(c.get("workspaceId")), 200),
+ c.json(
+ await listWorkspaceProjectAccess(c.get("workspaceId"), c.get("userId")),
+ 200,
+ ),
)
.openapi(updateMemberProjectAccessRoute, async (c) => {
const { userId } = c.req.valid("param");
diff --git a/apps/api/src/workspace/response.ts b/apps/api/src/workspace/response.ts
index 7410a4e97..561d1675c 100644
--- a/apps/api/src/workspace/response.ts
+++ b/apps/api/src/workspace/response.ts
@@ -28,5 +28,5 @@ export const memberProjectAccessListSchema = z
.array(memberProjectAccessSchema)
.openapi({
description:
- "Members limited to selected projects. Members not listed can access every project.",
+ "Members limited to selected projects. Members not listed can access every project. Project IDs only include projects the caller can access.",
});
diff --git a/apps/docs/core/functional/collaborate-with-team.mdx b/apps/docs/core/functional/collaborate-with-team.mdx
index e18bc44b8..4d6fe4dd7 100644
--- a/apps/docs/core/functional/collaborate-with-team.mdx
+++ b/apps/docs/core/functional/collaborate-with-team.mdx
@@ -62,7 +62,7 @@ Some rules keep this predictable:
- Owners always have access to every project.
- You need permission to change roles, and you can't change your own project access.
- If your own access is limited, you can only give others access to projects you can open, and you can't give anyone **All projects** or limit someone who has it. Projects you can't open stay as they are on the member you edit.
-- Tasks can only be assigned to people who can see the project.
+- Tasks can only be assigned to people who can see the project. Limiting someone unassigns them from tasks in the projects they lose, and moving a task into a project its assignee can't see unassigns it.
Project access set on an invitation applies when the person accepts it. To change it before then, cancel the invitation and invite them again.
diff --git a/apps/docs/openapi.json b/apps/docs/openapi.json
index 5c451309d..27351f7cc 100644
--- a/apps/docs/openapi.json
+++ b/apps/docs/openapi.json
@@ -18838,7 +18838,7 @@
"Workspaces"
],
"summary": "Get member project access",
- "description": "List the members who are limited to selected projects, with the projects each one can access. Everyone else in the workspace can access every project.",
+ "description": "List the members who are limited to selected projects, with the projects each one can access. Everyone else in the workspace can access every project. A caller whose own access is limited only sees the projects they can access.",
"parameters": [
{
"schema": {
@@ -18859,7 +18859,7 @@
"items": {
"$ref": "#/components/schemas/MemberProjectAccess"
},
- "description": "Members limited to selected projects. Members not listed can access every project."
+ "description": "Members limited to selected projects. Members not listed can access every project. Project IDs only include projects the caller can access."
}
}
}
diff --git a/tests/api-integration/project-access-management.test.ts b/tests/api-integration/project-access-management.test.ts
index 40ca77f93..807a046a0 100644
--- a/tests/api-integration/project-access-management.test.ts
+++ b/tests/api-integration/project-access-management.test.ts
@@ -187,8 +187,54 @@ describe("managing member project access", () => {
);
expect(visible.status).toBe(200);
expect(
- ((await visible.json()) as { projectIds: string[] }).projectIds.sort(),
- ).toEqual([ctx.alpha.id, ctx.beta.id].sort());
+ ((await visible.json()) as { projectIds: string[] }).projectIds,
+ ).toEqual([ctx.alpha.id]);
+ const stored = await readMemberAccessRows(ctx.workspace.id, member.id);
+ expect(stored.grants.map((grant) => grant.projectId).sort()).toEqual(
+ [ctx.alpha.id, ctx.beta.id].sort(),
+ );
+
+ const listed = await request(
+ `/workspace/${ctx.workspace.id}/project-access`,
+ );
+ expect(listed.status).toBe(200);
+ const entries = (await listed.json()) as {
+ userId: string;
+ projectIds: string[];
+ }[];
+ expect(JSON.stringify(entries)).not.toContain(ctx.beta.id);
+ expect(entries.find((entry) => entry.userId === member.id)).toMatchObject({
+ projectIds: [ctx.alpha.id],
+ });
+ });
+
+ it("unassigns a member from tasks in projects they lose", async () => {
+ const ctx = await createRestrictedWorkspace();
+ const member = await addWorkspaceMember(ctx.workspace.id);
+ await db
+ .update(schema.taskTable)
+ .set({ userId: member.id })
+ .where(eq(schema.taskTable.id, ctx.betaTask.id));
+ mockAuthenticatedSession(ctx.owner);
+
+ const restricted = await putMemberProjectAccess(
+ projectAccessApi(),
+ ctx.workspace.id,
+ member.id,
+ { projectAccess: "selected", projectIds: [ctx.alpha.id] },
+ );
+
+ expect(restricted.status).toBe(200);
+ const [task] = await db
+ .select({ userId: schema.taskTable.userId })
+ .from(schema.taskTable)
+ .where(eq(schema.taskTable.id, ctx.betaTask.id));
+ expect(task?.userId).toBeNull();
+ const activities = await db
+ .select({ type: schema.activityTable.type })
+ .from(schema.activityTable)
+ .where(eq(schema.activityTable.taskId, ctx.betaTask.id));
+ expect(activities.map((activity) => activity.type)).toContain("unassigned");
});
it("rejects projects from another workspace", async () => {