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: enforce bulk task permissions

ShiroKSH79872a312822
diff --git a/apps/api/src/task/controllers/bulk-update-tasks.ts b/apps/api/src/task/controllers/bulk-update-tasks.ts
index cabfbbc69..309f39495 100644
--- a/apps/api/src/task/controllers/bulk-update-tasks.ts
+++ b/apps/api/src/task/controllers/bulk-update-tasks.ts
@@ -226,6 +226,12 @@ async function bulkUpdateTasks({
throw new HTTPException(404, { message: "Label not found" });
}
+ if (label.workspaceId && label.workspaceId !== workspaceId) {
+ throw new HTTPException(400, {
+ message: "Label and tasks must belong to the same workspace",
+ });
+ }
+
for (const task of tasks) {
const existingAssignment = await db.query.labelTable.findFirst({
where: and(
diff --git a/apps/api/src/task/index.ts b/apps/api/src/task/index.ts
index 228ca6cb4..0ef694688 100644
--- a/apps/api/src/task/index.ts
+++ b/apps/api/src/task/index.ts
@@ -1,5 +1,5 @@
import { eq } from "drizzle-orm";
-import { Hono } from "hono";
+import { type Context, Hono, type Next } from "hono";
import { HTTPException } from "hono/http-exception";
import { describeRoute, resolver, validator } from "hono-openapi";
import * as v from "valibot";
@@ -38,6 +38,54 @@ import updateTaskStatus from "./controllers/update-task-status";
import updateTaskTitle from "./controllers/update-task-title";
import { VALID_PRIORITIES } from "./validate-task-fields";
+async function requireBulkTaskPermission(c: Context, next: Next) {
+ const { operation } = c.req.valid("json");
+
+ if (operation === "delete") {
+ return requireWorkspacePermission({ task: ["delete"] })(c, next);
+ }
+
+ if (operation === "updateAssignee") {
+ return requireWorkspacePermission({ task: ["assign"] })(c, next);
+ }
+
+ if (operation === "addLabel" || operation === "removeLabel") {
+ return requireWorkspacePermission({ label: ["update"] })(c, next);
+ }
+
+ return requireWorkspacePermission({ task: ["update"] })(c, next);
+}
+
+async function requireBulkTaskEntitlement(c: Context, next: Next) {
+ const { operation } = c.req.valid("json");
+
+ if (
+ operation === "delete" ||
+ operation === "addLabel" ||
+ operation === "removeLabel"
+ ) {
+ return next();
+ }
+
+ return requireEntitlement(c, next);
+}
+
+async function requireTaskAssigneePermission(c: Context, next: Next) {
+ const { id } = c.req.valid("param");
+ const { userId } = c.req.valid("json");
+ const [existingTask] = await db
+ .select({ userId: taskTable.userId })
+ .from(taskTable)
+ .where(eq(taskTable.id, id))
+ .limit(1);
+
+ if (existingTask && existingTask.userId !== (userId || null)) {
+ return requireWorkspacePermission({ task: ["assign"] })(c, next);
+ }
+
+ return next();
+}
+
const task = new Hono<{
Variables: {
userId: string;
@@ -132,6 +180,9 @@ const task = new Hono<{
value: v.optional(v.nullable(v.string())),
}),
),
+ workspaceAccess.fromTasks(),
+ requireBulkTaskPermission,
+ requireBulkTaskEntitlement,
async (c) => {
const { taskIds, operation, value } = c.req.valid("json");
const userId = c.get("userId");
@@ -323,6 +374,7 @@ const task = new Hono<{
),
workspaceAccess.fromTask(),
requireWorkspacePermission({ task: ["update"] }),
+ requireTaskAssigneePermission,
requireEntitlement,
async (c) => {
const { id } = c.req.valid("param");
diff --git a/apps/api/src/utils/workspace-access-middleware.ts b/apps/api/src/utils/workspace-access-middleware.ts
index 2c51bc879..dd1959e48 100644
--- a/apps/api/src/utils/workspace-access-middleware.ts
+++ b/apps/api/src/utils/workspace-access-middleware.ts
@@ -1,4 +1,4 @@
-import { and, eq } from "drizzle-orm";
+import { and, eq, inArray } from "drizzle-orm";
import type { Context, Next } from "hono";
import { HTTPException } from "hono/http-exception";
import db, { schema } from "../database";
@@ -20,6 +20,11 @@ type WorkspaceIdSource =
| "column"
| "workflowRule";
idKey: string;
+ }
+ | {
+ type: "lookupMany";
+ resource: "task";
+ idKey: string;
};
type WorkspaceAccessMiddlewareConfig = {
@@ -66,6 +71,26 @@ export function workspaceAccessMiddleware(
if (id) {
workspaceId = await lookupWorkspaceId(source.resource, id);
}
+ } else if (source.type === "lookupMany") {
+ const body = await readJsonObjectBody(c);
+ const ids = body[source.idKey];
+ if (Array.isArray(ids)) {
+ const taskIds = ids.filter(
+ (id): id is string => typeof id === "string",
+ );
+ if (taskIds.length > 0) {
+ const [task] = await db
+ .select({ workspaceId: schema.projectTable.workspaceId })
+ .from(schema.taskTable)
+ .innerJoin(
+ schema.projectTable,
+ eq(schema.taskTable.projectId, schema.projectTable.id),
+ )
+ .where(inArray(schema.taskTable.id, taskIds))
+ .limit(1);
+ workspaceId = task?.workspaceId || null;
+ }
+ }
}
if (workspaceId) {
@@ -269,6 +294,11 @@ export const workspaceAccess = {
],
}),
+ fromTasks: (idKey = "taskIds") =>
+ workspaceAccessMiddleware({
+ sources: [{ type: "lookupMany", resource: "task", idKey }],
+ }),
+
fromLabel: (idKey = "id") =>
workspaceAccessMiddleware({
sources: [
diff --git a/tests/api-integration/workspace-rbac.test.ts b/tests/api-integration/workspace-rbac.test.ts
index 898110ed8..513d94ca3 100644
--- a/tests/api-integration/workspace-rbac.test.ts
+++ b/tests/api-integration/workspace-rbac.test.ts
@@ -17,7 +17,11 @@ type CreateTaskBody = {
status: string;
};
-async function seedTask(projectId: string, columnId: string | null) {
+async function seedTask(
+ projectId: string,
+ columnId: string | null,
+ userId?: string,
+) {
const [task] = await db
.insert(schema.taskTable)
.values({
@@ -29,6 +33,7 @@ async function seedTask(projectId: string, columnId: string | null) {
columnId,
number: 1,
position: 1,
+ ...(userId ? { userId } : {}),
})
.returning();
return task;
@@ -217,6 +222,178 @@ describe("API integration: workspace RBAC enforcement", () => {
});
});
+ describe("bulk task mutations", () => {
+ it("blocks a viewer from changing task priority in bulk", async () => {
+ const viewer = await createWorkspaceMember({ role: "viewer" });
+ const { project, columns } = await createProjectFixture({
+ workspaceId: viewer.workspace.id,
+ });
+ const task = await seedTask(project.id, columns.todo.id);
+
+ mockAuthenticatedSession(viewer.user);
+ const { app } = createApp();
+
+ const response = await app.request("/api/task/bulk", {
+ method: "PATCH",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ taskIds: [task.id],
+ operation: "updatePriority",
+ value: "high",
+ }),
+ });
+ expect(response.status).toBe(403);
+
+ const persisted = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ expect(persisted?.priority).toBe("medium");
+ });
+
+ it("allows a member to change task priority in bulk", async () => {
+ const member = await createWorkspaceMember({ role: "member" });
+ const { project, columns } = await createProjectFixture({
+ workspaceId: member.workspace.id,
+ });
+ const task = await seedTask(project.id, columns.todo.id);
+
+ mockAuthenticatedSession(member.user);
+ const { app } = createApp();
+
+ const response = await app.request("/api/task/bulk", {
+ method: "PATCH",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ taskIds: [task.id],
+ operation: "updatePriority",
+ value: "high",
+ }),
+ });
+ expect(response.status).toBe(200);
+
+ const persisted = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ expect(persisted?.priority).toBe("high");
+ });
+
+ it("blocks a member from deleting a task in bulk", async () => {
+ const member = await createWorkspaceMember({ role: "member" });
+ const { project, columns } = await createProjectFixture({
+ workspaceId: member.workspace.id,
+ });
+ const task = await seedTask(project.id, columns.todo.id);
+
+ mockAuthenticatedSession(member.user);
+ const { app } = createApp();
+
+ const response = await app.request("/api/task/bulk", {
+ method: "PATCH",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({ taskIds: [task.id], operation: "delete" }),
+ });
+ expect(response.status).toBe(403);
+
+ const persisted = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ expect(persisted).toBeDefined();
+ });
+
+ it("blocks a member from assigning a task in bulk", async () => {
+ const member = await createWorkspaceMember({ role: "member" });
+ const { project, columns } = await createProjectFixture({
+ workspaceId: member.workspace.id,
+ });
+ const task = await seedTask(project.id, columns.todo.id);
+
+ mockAuthenticatedSession(member.user);
+ const { app } = createApp();
+
+ const response = await app.request("/api/task/bulk", {
+ method: "PATCH",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ taskIds: [task.id],
+ operation: "updateAssignee",
+ value: member.user.id,
+ }),
+ });
+ expect(response.status).toBe(403);
+
+ const persisted = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ expect(persisted?.userId).toBeNull();
+ });
+
+ it("allows an admin to assign a task in bulk", async () => {
+ const admin = await createWorkspaceMember({ role: "admin" });
+ const { project, columns } = await createProjectFixture({
+ workspaceId: admin.workspace.id,
+ });
+ const task = await seedTask(project.id, columns.todo.id);
+
+ mockAuthenticatedSession(admin.user);
+ const { app } = createApp();
+
+ const response = await app.request("/api/task/bulk", {
+ method: "PATCH",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ taskIds: [task.id],
+ operation: "updateAssignee",
+ value: admin.user.id,
+ }),
+ });
+ expect(response.status).toBe(200);
+
+ const persisted = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ expect(persisted?.userId).toBe(admin.user.id);
+ });
+
+ it("does not copy a label from another workspace in bulk", async () => {
+ const member = await createWorkspaceMember({ role: "member" });
+ const foreign = await createWorkspaceMember({ role: "admin" });
+ const { project, columns } = await createProjectFixture({
+ workspaceId: member.workspace.id,
+ });
+ const task = await seedTask(project.id, columns.todo.id);
+ const [foreignLabel] = await db
+ .insert(schema.labelTable)
+ .values({
+ name: "private",
+ color: "#000000",
+ workspaceId: foreign.workspace.id,
+ })
+ .returning();
+
+ mockAuthenticatedSession(member.user);
+ const { app } = createApp();
+
+ const response = await app.request("/api/task/bulk", {
+ method: "PATCH",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ taskIds: [task.id],
+ operation: "addLabel",
+ value: foreignLabel.id,
+ }),
+ });
+ expect(response.status).toBe(400);
+
+ const copiedLabel = await db.query.labelTable.findFirst({
+ where: and(
+ eq(schema.labelTable.taskId, task.id),
+ eq(schema.labelTable.name, foreignLabel.name),
+ ),
+ });
+ expect(copiedLabel).toBeUndefined();
+ });
+ });
+
describe("custom workspace roles", () => {
it("blocks a custom role that only grants task:read from creating a task", async () => {
const member = await createWorkspaceMember({ role: "readonly" });
@@ -353,6 +530,131 @@ describe("API integration: workspace RBAC enforcement", () => {
expect(response.status).toBe(200);
});
+ it("allows a member to update an assigned task without changing its assignee", async () => {
+ const member = await createWorkspaceMember({ role: "member" });
+ const { project, columns } = await createProjectFixture({
+ workspaceId: member.workspace.id,
+ });
+ const task = await seedTask(project.id, columns.todo.id, member.user.id);
+
+ mockAuthenticatedSession(member.user);
+ const { app } = createApp();
+
+ const response = await app.request(`/api/task/${task.id}`, {
+ method: "PUT",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ title: "Updated by member",
+ description: "edit",
+ priority: "high",
+ status: "to-do",
+ projectId: project.id,
+ position: 1,
+ userId: member.user.id,
+ }),
+ });
+ expect(response.status).toBe(200);
+
+ const persisted = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ expect(persisted?.title).toBe("Updated by member");
+ expect(persisted?.userId).toBe(member.user.id);
+ });
+
+ it("blocks a member from assigning an unassigned task through full update", async () => {
+ const member = await createWorkspaceMember({ role: "member" });
+ const { project, columns } = await createProjectFixture({
+ workspaceId: member.workspace.id,
+ });
+ const task = await seedTask(project.id, columns.todo.id);
+
+ mockAuthenticatedSession(member.user);
+ const { app } = createApp();
+
+ const response = await app.request(`/api/task/${task.id}`, {
+ method: "PUT",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ title: "Member attempt",
+ description: "nope",
+ priority: "low",
+ status: "to-do",
+ projectId: project.id,
+ position: 1,
+ userId: member.user.id,
+ }),
+ });
+ expect(response.status).toBe(403);
+
+ const persisted = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ expect(persisted?.userId).toBeNull();
+ });
+
+ it("blocks a member from unassigning a task through full update", async () => {
+ const member = await createWorkspaceMember({ role: "member" });
+ const { project, columns } = await createProjectFixture({
+ workspaceId: member.workspace.id,
+ });
+ const task = await seedTask(project.id, columns.todo.id, member.user.id);
+
+ mockAuthenticatedSession(member.user);
+ const { app } = createApp();
+
+ const response = await app.request(`/api/task/${task.id}`, {
+ method: "PUT",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ title: "Member attempt",
+ description: "nope",
+ priority: "low",
+ status: "to-do",
+ projectId: project.id,
+ position: 1,
+ userId: "",
+ }),
+ });
+ expect(response.status).toBe(403);
+
+ const persisted = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ expect(persisted?.userId).toBe(member.user.id);
+ });
+
+ it("allows an admin to assign a task through full update", async () => {
+ const admin = await createWorkspaceMember({ role: "admin" });
+ const { project, columns } = await createProjectFixture({
+ workspaceId: admin.workspace.id,
+ });
+ const task = await seedTask(project.id, columns.todo.id);
+
+ mockAuthenticatedSession(admin.user);
+ const { app } = createApp();
+
+ const response = await app.request(`/api/task/${task.id}`, {
+ method: "PUT",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ title: "Assigned by admin",
+ description: "",
+ priority: "medium",
+ status: "to-do",
+ projectId: project.id,
+ position: 1,
+ userId: admin.user.id,
+ }),
+ });
+ expect(response.status).toBe(200);
+
+ const persisted = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ expect(persisted?.userId).toBe(admin.user.id);
+ });
+
it("blocks a viewer from updating a task", async () => {
const member = await createWorkspaceMember({ role: "viewer" });
const { project, columns } = await createProjectFixture({