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(calendar): enforce label creation permission and reuse date formatter

Tinbc449d4b89a1
diff --git a/apps/api/src/calendar-feed/ical.ts b/apps/api/src/calendar-feed/ical.ts
index 5eecfe317..0d5b89b7b 100644
--- a/apps/api/src/calendar-feed/ical.ts
+++ b/apps/api/src/calendar-feed/ical.ts
@@ -42,13 +42,8 @@ function timestamp(date: Date) {
.replace(/\.\d{3}Z$/, "Z");
}
-function calendarDate(date: Date, timeZone: string) {
- const parts = new Intl.DateTimeFormat("en-US", {
- timeZone,
- year: "numeric",
- month: "2-digit",
- day: "2-digit",
- }).formatToParts(date);
+function calendarDate(date: Date, formatter: Intl.DateTimeFormat) {
+ const parts = formatter.formatToParts(date);
return ["year", "month", "day"]
.map((type) => parts.find((part) => part.type === type)?.value)
.join("");
@@ -71,6 +66,12 @@ export function buildCalendar({
timeZone: string;
tasks: CalendarTask[];
}) {
+ const formatter = new Intl.DateTimeFormat("en-US", {
+ timeZone,
+ year: "numeric",
+ month: "2-digit",
+ day: "2-digit",
+ });
const lines = [
"BEGIN:VCALENDAR",
"VERSION:2.0",
@@ -81,8 +82,8 @@ export function buildCalendar({
for (const task of tasks) {
const scheduledDate = task.startDate ?? task.dueDate;
if (!scheduledDate) continue;
- const start = calendarDate(scheduledDate, timeZone);
- const due = calendarDate(task.dueDate ?? scheduledDate, timeZone);
+ const start = calendarDate(scheduledDate, formatter);
+ const due = calendarDate(task.dueDate ?? scheduledDate, formatter);
lines.push(
"BEGIN:VEVENT",
`UID:${task.id}@kaneo`,
diff --git a/apps/api/src/calendar-feed/index.ts b/apps/api/src/calendar-feed/index.ts
index 57926d410..5753e491e 100644
--- a/apps/api/src/calendar-feed/index.ts
+++ b/apps/api/src/calendar-feed/index.ts
@@ -6,7 +6,10 @@ import {
jsonResponse,
z,
} from "../openapi";
-import { requireWorkspacePermission } from "../utils/require-workspace-permission";
+import {
+ hasWorkspacePermission,
+ requireWorkspacePermission,
+} from "../utils/require-workspace-permission";
import { workspaceAccess } from "../utils/workspace-access-middleware";
import { calendarFeedSchema } from "./response";
import {
@@ -96,7 +99,7 @@ const calendarFeed = apiRouter<BaseVariables & { workspaceId: string }>()
tags: ["Calendar feeds"],
summary: "Create a calendar feed",
description:
- "Create a calendar subscription matching any selected label. Tasks need a start or due date. All-day dates use the supplied time zone.",
+ "Create a calendar subscription matching any selected label. Tasks need a start or due date. All-day dates use the supplied time zone. Creating a missing workspace label definition also requires label:create permission.",
middleware: sharingMiddleware,
request: {
params: calendarFeedProjectParam,
@@ -108,6 +111,9 @@ const calendarFeed = apiRouter<BaseVariables & { workspaceId: string }>()
responses: {
201: jsonResponse("Calendar feed created", calendarFeedSchema),
...managementErrors,
+ 403: errorResponse(
+ "No workspace access, missing project:share permission, or missing label:create permission for a new workspace label definition",
+ ),
},
}),
async (c) => {
@@ -119,6 +125,7 @@ const calendarFeed = apiRouter<BaseVariables & { workspaceId: string }>()
c.get("workspaceId"),
labelIds,
timeZone,
+ await hasWorkspacePermission(c, { label: ["create"] }),
),
201,
);
diff --git a/apps/api/src/calendar-feed/service.ts b/apps/api/src/calendar-feed/service.ts
index db837fbe3..f72fc8178 100644
--- a/apps/api/src/calendar-feed/service.ts
+++ b/apps/api/src/calendar-feed/service.ts
@@ -15,6 +15,7 @@ export async function createCalendarFeed(
workspaceId: string,
labelIds: string[],
timeZone: string,
+ canCreateLabels: boolean,
) {
return db.transaction(async (tx) => {
const ids = [...new Set(labelIds)];
@@ -42,20 +43,22 @@ export async function createCalendarFeed(
const definitions = [
...new Map(labels.map((label) => [label.name, label])).values(),
].sort((a, b) => a.name.localeCompare(b.name));
- await tx
- .insert(labelTable)
- .values(
- definitions.map(({ name, color }) => ({
- name,
- color,
- workspaceId,
- taskId: null,
- })),
- )
- .onConflictDoNothing({
- target: [labelTable.workspaceId, labelTable.name],
- where: isNull(labelTable.taskId),
- });
+ if (canCreateLabels) {
+ await tx
+ .insert(labelTable)
+ .values(
+ definitions.map(({ name, color }) => ({
+ name,
+ color,
+ workspaceId,
+ taskId: null,
+ })),
+ )
+ .onConflictDoNothing({
+ target: [labelTable.workspaceId, labelTable.name],
+ where: isNull(labelTable.taskId),
+ });
+ }
const roots = await tx
.select({ id: labelTable.id })
.from(labelTable)
@@ -72,6 +75,12 @@ export async function createCalendarFeed(
)
.orderBy(asc(labelTable.name));
if (roots.length !== definitions.length) {
+ if (!canCreateLabels) {
+ throw new HTTPException(403, {
+ message:
+ "Creating a workspace label definition requires label:create permission",
+ });
+ }
throw new HTTPException(400, {
message: "Select labels that are not being deleted",
});
diff --git a/apps/docs/openapi.json b/apps/docs/openapi.json
index 4f468e75c..d570d4a49 100644
--- a/apps/docs/openapi.json
+++ b/apps/docs/openapi.json
@@ -6959,7 +6959,7 @@
"Calendar feeds"
],
"summary": "Create a calendar feed",
- "description": "Create a calendar subscription matching any selected label. Tasks need a start or due date. All-day dates use the supplied time zone.",
+ "description": "Create a calendar subscription matching any selected label. Tasks need a start or due date. All-day dates use the supplied time zone. Creating a missing workspace label definition also requires label:create permission.",
"parameters": [
{
"schema": {
@@ -7030,7 +7030,7 @@
}
},
"403": {
- "description": "No workspace access or missing project sharing permission",
+ "description": "No workspace access, missing project:share permission, or missing label:create permission for a new workspace label definition",
"content": {
"text/plain": {
"schema": {
diff --git a/tests/api-integration/calendar-feed.test.ts b/tests/api-integration/calendar-feed.test.ts
index 695d9d0bc..5e48a446e 100644
--- a/tests/api-integration/calendar-feed.test.ts
+++ b/tests/api-integration/calendar-feed.test.ts
@@ -244,6 +244,47 @@ describe("API integration: calendar feeds", () => {
},
);
+ it("requires label creation permission only when a workspace definition is missing", async () => {
+ const { member, project, labels, create } =
+ await setup("calendar-publisher");
+ const [role] = await db
+ .insert(schema.workspaceRoleTable)
+ .values({
+ workspaceId: member.workspace.id,
+ role: "calendar-publisher",
+ permission: JSON.stringify({ project: ["share"] }),
+ })
+ .returning();
+ expect((await create({ labelIds: [labels[0].id] })).status).toBe(201);
+ const [task] = await db
+ .insert(schema.taskTable)
+ .values({ title: "Legacy assignment", projectId: project.id })
+ .returning();
+ const [copy] = await db
+ .insert(schema.labelTable)
+ .values({
+ name: "Legacy",
+ color: "gray",
+ workspaceId: member.workspace.id,
+ taskId: task.id,
+ })
+ .returning();
+ expect((await create({ labelIds: [copy.id] })).status).toBe(403);
+ expect(
+ await db
+ .select()
+ .from(schema.labelTable)
+ .where(eq(schema.labelTable.name, "Legacy")),
+ ).toHaveLength(1);
+ await db
+ .update(schema.workspaceRoleTable)
+ .set({
+ permission: JSON.stringify({ project: ["share"], label: ["create"] }),
+ })
+ .where(eq(schema.workspaceRoleTable.id, role.id));
+ expect((await create({ labelIds: [copy.id] })).status).toBe(201);
+ });
+
it("revokes one link without affecting others and cascades project deletion", async () => {
const { app, project, endpoint, create } = await setup();
const first = (await (await create()).json()) as Feed;