Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(api): scope a label's groupId to the project (#349)

Egor Vasilyevf7b09331bd63
diff --git a/apps/api/src/modules/labels/__tests__/integration/labels.test.ts b/apps/api/src/modules/labels/__tests__/integration/labels.test.ts
index fe851c31..5eb032d1 100644
--- a/apps/api/src/modules/labels/__tests__/integration/labels.test.ts
+++ b/apps/api/src/modules/labels/__tests__/integration/labels.test.ts
@@ -411,6 +411,45 @@ describe('labels', () => {
const ops = await groupsOf(api, 'OPS');
expect(ops.some((g) => g.id === group.id)).toBe(true);
});
+
+ // label.group_id only requires the group to exist somewhere, so the service
+ // has to check the group's project itself. A foreign group is rejected with
+ // 400, the same status as an unknown one, so the response does not reveal
+ // whether the id exists.
+ it('does not create a label in a group from another project', async () => {
+ const { api, group } = await twoProjects();
+ const res = await api
+ .projects({ projectKey: 'MKT' })
+ .labels.post({ name: 'urgent', groupId: group.id });
+ expect(res.status).toBe(400);
+ expect(res.error?.value).toEqual({ error: 'Label group must belong to this project' });
+
+ const mkt = await labelsOf(api, 'MKT');
+ expect(mkt).toEqual([]);
+ });
+
+ it('does not move a label into a group from another project', async () => {
+ const { api, group } = await twoProjects();
+ const label = (await api.projects({ projectKey: 'MKT' }).labels.post({ name: 'urgent' }))
+ .data!;
+
+ const res = await api
+ .projects({ projectKey: 'MKT' })
+ .labels({ labelId: label.id })
+ .patch({ groupId: group.id });
+ expect(res.status).toBe(400);
+
+ const mkt = await labelsOf(api, 'MKT');
+ expect(mkt.find((l) => l.id === label.id)?.groupId).toBeNull();
+ });
+
+ it('rejects an unknown group id with the same status', async () => {
+ const { api } = await twoProjects();
+ const res = await api
+ .projects({ projectKey: 'MKT' })
+ .labels.post({ name: 'urgent', groupId: 999999 });
+ expect(res.status).toBe(400);
+ });
});
describe('access', () => {
diff --git a/apps/api/src/modules/labels/service.ts b/apps/api/src/modules/labels/service.ts
index 0239c0c8..67453c09 100644
--- a/apps/api/src/modules/labels/service.ts
+++ b/apps/api/src/modules/labels/service.ts
@@ -1,5 +1,6 @@
import { db, label, labelGroup } from '@repo/db';
import { and, eq } from 'drizzle-orm';
+import { HttpError } from '#shared/lib';
// Data access for labels and label groups. Both belong to one project. A label
// has at most one group; deleting a group ungroups its labels (group_id → SET
@@ -35,12 +36,24 @@ export async function listLabels(projectId: number): Promise<LabelRow[]> {
return rows.map(mapLabel);
}
+// Enforces that the group belongs to the label's project — the label.group_id
+// foreign key only requires the group to exist somewhere. Throws 400 otherwise.
+async function assertLabelGroup(projectId: number, groupId?: number | null): Promise<void> {
+ if (groupId == null) return;
+ const rows = await db
+ .select({ id: labelGroup.id })
+ .from(labelGroup)
+ .where(and(eq(labelGroup.id, groupId), eq(labelGroup.projectId, projectId)));
+ if (rows.length === 0) throw new HttpError(400, 'Label group must belong to this project');
+}
+
export async function createLabel(input: {
projectId: number;
name: string;
color?: string;
groupId?: number | null;
}): Promise<LabelRow> {
+ await assertLabelGroup(input.projectId, input.groupId);
const [row] = await db
.insert(label)
.values({
@@ -60,6 +73,7 @@ export async function updateLabel(
projectId: number,
patch: { name?: string; color?: string; groupId?: number | null },
): Promise<LabelRow | null> {
+ await assertLabelGroup(projectId, patch.groupId);
const scope = and(eq(label.id, id), eq(label.projectId, projectId));
const set: Partial<typeof label.$inferInsert> = {};
if (patch.name !== undefined) set.name = patch.name;