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 issue column and type to the issue's project (#39)

Croff84b82e52041f
diff --git a/apps/api/src/issues/__tests__/integration/issues.test.ts b/apps/api/src/issues/__tests__/integration/issues.test.ts
index 6a661aee..5dc184f4 100644
--- a/apps/api/src/issues/__tests__/integration/issues.test.ts
+++ b/apps/api/src/issues/__tests__/integration/issues.test.ts
@@ -38,6 +38,17 @@ async function foreignLabel(client: Api) {
return res.data!;
}
+// A column and an issue type of a second project, which no issue of the first one
+// may point at.
+async function foreignProject(client: Api) {
+ await client.projects.post({ key: 'OPS', name: 'Operations' });
+ const view = await client.projects({ projectKey: 'OPS' }).get();
+ const type = await client
+ .projects({ projectKey: 'OPS' })
+ ['issue-types'].post({ name: 'Incident' });
+ return { columnId: view.data!.columns[0].id, typeId: type.data!.id };
+}
+
// The board's issue list (carries labelIds and set custom field values).
async function issuesOf(client: Api, projectKey = 'MKT') {
const board = await client.projects({ projectKey }).issues.board.get();
@@ -139,6 +150,24 @@ describe('issues', () => {
expect(await issuesOf(asOwner)).toHaveLength(0);
});
+ it('rejects a column of another project and creates no issue', async () => {
+ const { asOwner } = await setupProject();
+ const foreign = await foreignProject(asOwner);
+
+ const created = await createIssue(asOwner, foreign.columnId);
+ expect(created.status).toBe(400);
+ expect(await issuesOf(asOwner)).toHaveLength(0);
+ });
+
+ it('rejects an issue type of another project and creates no issue', async () => {
+ const { asOwner, columnId } = await setupProject();
+ const foreign = await foreignProject(asOwner);
+
+ const created = await createIssue(asOwner, columnId, { typeId: foreign.typeId });
+ expect(created.status).toBe(400);
+ expect(await issuesOf(asOwner)).toHaveLength(0);
+ });
+
it('rejects an empty title', async () => {
const { asOwner, columnId } = await setupProject();
const res = await createIssue(asOwner, columnId, { title: '' });
@@ -288,6 +317,35 @@ describe('issues', () => {
expect(list.find((i) => i.id === issue.id)?.labelIds).toEqual([own.id]);
});
+ it('rejects a column of another project and keeps the current one', async () => {
+ const { asOwner, columnId } = await setupProject();
+ const foreign = await foreignProject(asOwner);
+ const issue = (await createIssue(asOwner, columnId)).data!;
+
+ const patched = await asOwner.issues({ issueId: issue.id }).patch({
+ columnId: foreign.columnId,
+ });
+ expect(patched.status).toBe(400);
+
+ const read = await asOwner.issues({ issueId: issue.id }).get();
+ expect(read.data?.columnId).toBe(columnId);
+ });
+
+ it('rejects an issue type of another project and keeps the current one', async () => {
+ const { asOwner, columnId } = await setupProject();
+ const own = (
+ await asOwner.projects({ projectKey: 'MKT' })['issue-types'].post({ name: 'Bug' })
+ ).data!;
+ const foreign = await foreignProject(asOwner);
+ const issue = (await createIssue(asOwner, columnId, { typeId: own.id })).data!;
+
+ const patched = await asOwner.issues({ issueId: issue.id }).patch({ typeId: foreign.typeId });
+ expect(patched.status).toBe(400);
+
+ const read = await asOwner.issues({ issueId: issue.id }).get();
+ expect(read.data?.typeId).toBe(own.id);
+ });
+
it('changes only the given fields', async () => {
const { asOwner, columnId } = await setupProject();
const issue = (await createIssue(asOwner, columnId, { title: 'Keep', priority: 'low' }))
@@ -954,6 +1012,20 @@ describe('issues', () => {
expect(list.find((i) => i.id === two.id)?.labelIds).toEqual([b.id]);
});
+ it("rejects a bulk move to another project's column", async () => {
+ const { asOwner, columnId } = await setupProject();
+ const foreign = await foreignProject(asOwner);
+ const issue = (await createIssue(asOwner, columnId)).data!;
+
+ const res = await asOwner
+ .projects({ projectKey: 'MKT' })
+ .issues.bulk.patch({ ids: [issue.id], patch: { columnId: foreign.columnId } });
+ expect(res.status).toBe(400);
+
+ const list = await issuesOf(asOwner);
+ expect(list.find((i) => i.id === issue.id)?.columnId).toBe(columnId);
+ });
+
it("rejects a bulk add of another project's label", async () => {
const { asOwner, columnId } = await setupProject();
const foreign = await foreignLabel(asOwner);
diff --git a/apps/api/src/issues/store.ts b/apps/api/src/issues/store.ts
index 3627944b..46927280 100644
--- a/apps/api/src/issues/store.ts
+++ b/apps/api/src/issues/store.ts
@@ -5,6 +5,8 @@ import {
issueActivity,
issueLabel,
label,
+ projectColumn,
+ issueType,
issueFieldValue,
issueFieldOption,
issueAttachment,
@@ -503,11 +505,13 @@ async function attachFieldValues(issues: IssueRow[]): Promise<void> {
// The snapshot columns of an issue, without the label and project-key joins a
// full load does. Used as the "before" state for the change-log diff, which
-// needs only these columns.
-async function loadSnapshot(id: number): Promise<IssueSnapshot | null> {
+// needs only these columns, plus the project id the update's scope checks run
+// against.
+async function loadSnapshot(id: number): Promise<(IssueSnapshot & { projectId: number }) | null> {
const rows = await db
.select({
id: issue.id,
+ projectId: issue.projectId,
title: issue.title,
description: issue.description,
columnId: issue.columnId,
@@ -608,6 +612,32 @@ async function assertInitiative(
throw new HttpError(400, 'Initiative must belong to this project');
}
+// Enforces that a column belongs to the issue's project — issue.column_id only
+// requires the column to exist somewhere, so an issue could otherwise sit on
+// another project's board. Throws 400 otherwise.
+async function assertColumn(projectId: number, columnId: number | undefined): Promise<void> {
+ if (columnId === undefined) return;
+ const rows = await db
+ .select({ id: projectColumn.id })
+ .from(projectColumn)
+ .where(and(eq(projectColumn.id, columnId), eq(projectColumn.projectId, projectId)));
+ if (rows.length === 0) throw new HttpError(400, 'Column must belong to this project');
+}
+
+// Enforces that an issue type belongs to the issue's project. Only checks when set
+// to a non-null value (clearing the type is always allowed). Throws 400 otherwise.
+async function assertIssueType(
+ projectId: number,
+ typeId: number | null | undefined,
+): Promise<void> {
+ if (typeId == null) return;
+ const rows = await db
+ .select({ id: issueType.id })
+ .from(issueType)
+ .where(and(eq(issueType.id, typeId), eq(issueType.projectId, projectId)));
+ if (rows.length === 0) throw new HttpError(400, 'Issue type must belong to this project');
+}
+
// Enforces that every label belongs to the issue's project — the issue_label
// foreign key only requires the label to exist somewhere. Throws 400 otherwise.
async function assertIssueLabels(projectId: number, labelIds?: number[]): Promise<void> {
@@ -630,6 +660,8 @@ export async function createIssue(
): Promise<IssueRow> {
await assertAssignments(project.id, input);
await assertInitiative(project.id, input.initiativeId);
+ await assertColumn(project.id, input.columnId);
+ await assertIssueType(project.id, input.typeId);
// Also checked by setIssueLabels below, but here it fails before the issue exists.
await assertIssueLabels(project.id, input.labelIds);
const issueId = await db.transaction(async (tx) => {
@@ -709,13 +741,10 @@ export async function updateIssue(
const before = await loadSnapshot(id);
if (!before) return null;
- if (patch.assigneeUserId || patch.delegateUserId || patch.initiativeId) {
- const projectId = await getIssueProjectId(id);
- if (projectId) {
- await assertAssignments(projectId, patch);
- await assertInitiative(projectId, patch.initiativeId);
- }
- }
+ await assertAssignments(before.projectId, patch);
+ await assertInitiative(before.projectId, patch.initiativeId);
+ await assertColumn(before.projectId, patch.columnId);
+ await assertIssueType(before.projectId, patch.typeId);
const set: Partial<typeof issue.$inferInsert> = {};
if (patch.columnId !== undefined) set.columnId = patch.columnId;