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(integrations): bind rule saves to authorized workspace

Tin4770fe487d14
diff --git a/apps/api/src/integration-sync/controllers/save-rules.ts b/apps/api/src/integration-sync/controllers/save-rules.ts
index a40de8a0c..154144535 100644
--- a/apps/api/src/integration-sync/controllers/save-rules.ts
+++ b/apps/api/src/integration-sync/controllers/save-rules.ts
@@ -1,7 +1,7 @@
import { and, eq, not, or } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
-import { integrationTable } from "../../database/schema";
+import { integrationTable, projectTable } from "../../database/schema";
import { publishEvent } from "../../events";
import { pauseIssueLinks } from "../../plugins/sync/pause-issue-links";
import { outgoingPredicate } from "../../plugins/sync/task-predicate";
@@ -14,9 +14,25 @@ export async function saveSyncRules(
provider: string,
rules: SyncRules,
previewToken: string,
+ authorizedWorkspaceId: string,
) {
const integration = await getSyncIntegration(projectId, provider);
- await db.transaction(async (tx) => {
+ const savedPreview = await db.transaction(async (tx) => {
+ // Keep the workspace authorized by middleware stable through the save.
+ const [project] = await tx
+ .select()
+ .from(projectTable)
+ .where(
+ and(
+ eq(projectTable.id, projectId),
+ eq(projectTable.workspaceId, authorizedWorkspaceId),
+ ),
+ )
+ .for("share");
+ if (!project)
+ throw new HTTPException(403, {
+ message: "Project no longer belongs to the authorized workspace",
+ });
const [current] = await tx
.select()
.from(integrationTable)
@@ -31,11 +47,8 @@ export async function saveSyncRules(
throw new HTTPException(409, {
message: "Integration changed; preview again before saving",
});
- const preview = await previewSyncRules(
- { ...integration, ...current },
- rules,
- tx,
- );
+ const currentIntegration = { ...integration, ...current, project };
+ const preview = await previewSyncRules(currentIntegration, rules, tx);
if (preview.previewToken !== previewToken)
throw new HTTPException(409, {
message: "Sync impact changed; preview again before saving",
@@ -45,12 +58,12 @@ export async function saveSyncRules(
message: "Select existing labels from this workspace",
});
const oldScope = await outgoingPredicate(
- integration.project.workspaceId,
+ project.workspaceId,
readSyncRules(current.config)!.outgoing,
tx,
);
const nextScope = await outgoingPredicate(
- integration.project.workspaceId,
+ project.workspaceId,
rules.outgoing,
tx,
);
@@ -61,18 +74,25 @@ export async function saveSyncRules(
tx,
);
const config = JSON.parse(current.config) as Record<string, unknown>;
+ const nextConfig = JSON.stringify({ ...config, syncRules: rules });
await tx
.update(integrationTable)
.set({
- config: JSON.stringify({ ...config, syncRules: rules }),
+ config: nextConfig,
updatedAt: new Date(),
})
.where(eq(integrationTable.id, integration.id));
+ // A post-commit project move must not expose the destination's labels.
+ return previewSyncRules(
+ { ...currentIntegration, config: nextConfig },
+ rules,
+ tx,
+ );
});
await publishEvent("integration.sync_rules_changed", {
projectId,
integrationId: integration.id,
});
await publishEvent("project.updated", { projectId, linksChanged: true });
- return previewSyncRules(await getSyncIntegration(projectId, provider), rules);
+ return savedPreview;
}
diff --git a/apps/api/src/integration-sync/index.ts b/apps/api/src/integration-sync/index.ts
index 24d06aaf4..3fd50e979 100644
--- a/apps/api/src/integration-sync/index.ts
+++ b/apps/api/src/integration-sync/index.ts
@@ -1,5 +1,6 @@
import {
apiRouter,
+ type BaseVariables,
createRoute,
errorResponse,
jsonResponse,
@@ -130,7 +131,7 @@ const resumeRoute = createRoute({
},
});
-export default apiRouter()
+export default apiRouter<BaseVariables & { workspaceId: string }>()
.openapi(getRoute, async (c) => {
const { projectId, provider } = c.req.valid("param");
const integration = await getSyncIntegration(projectId, provider);
@@ -158,7 +159,13 @@ export default apiRouter()
const { projectId, provider } = c.req.valid("param");
const { rules, previewToken } = c.req.valid("json");
return c.json(
- await saveSyncRules(projectId, provider, rules, previewToken),
+ await saveSyncRules(
+ projectId,
+ provider,
+ rules,
+ previewToken,
+ c.get("workspaceId"),
+ ),
200,
);
})
diff --git a/tests/api-integration/integration-sync-reconnect.test.ts b/tests/api-integration/integration-sync-reconnect.test.ts
index f0e63b48a..3e1150bc4 100644
--- a/tests/api-integration/integration-sync-reconnect.test.ts
+++ b/tests/api-integration/integration-sync-reconnect.test.ts
@@ -95,7 +95,13 @@ it.each(["gitea", "gitlab"] as const)(
await getSyncIntegration(project.id, type),
rules,
);
- await saveSyncRules(project.id, type, rules, preview.previewToken);
+ await saveSyncRules(
+ project.id,
+ type,
+ rules,
+ preview.previewToken,
+ workspace.id,
+ );
} finally {
release();
await conflict;
diff --git a/tests/api-integration/integration-sync-rules.test.ts b/tests/api-integration/integration-sync-rules.test.ts
index 8020d3dfd..49c89f88a 100644
--- a/tests/api-integration/integration-sync-rules.test.ts
+++ b/tests/api-integration/integration-sync-rules.test.ts
@@ -12,6 +12,7 @@ import { resumeSync } from "../../apps/api/src/integration-sync/controllers/resu
import { reviewSyncResume } from "../../apps/api/src/integration-sync/controllers/review-resume";
import { lockResumeScope } from "../../apps/api/src/integration-sync/controllers/lock-resume-scope";
import { saveSyncRules } from "../../apps/api/src/integration-sync/controllers/save-rules";
+import moveProject from "../../apps/api/src/project/controllers/move-project";
import * as linkManager from "../../apps/api/src/plugins/github/services/link-manager";
import { createExternalLink } from "../../apps/api/src/plugins/github/services/link-manager";
import { withIntegrationLink } from "../../apps/api/src/plugins/github/services/with-integration-link";
@@ -406,12 +407,91 @@ describe("integration label policies", () => {
expect((await f.request("", "GET")).status).toBe(403);
});
+ it("rejects a rule save if the project moves after workspace authorization", async () => {
+ const f = await setup();
+ const target = await createWorkspaceMember({ role: "owner" });
+ const rules = { ...defaultSyncRules, incoming: f.rules.incoming };
+ const preview = await f.preview(rules);
+ const transaction = getDatabase().transaction.bind(getDatabase());
+ vi.spyOn(getDatabase(), "transaction").mockImplementationOnce(
+ async (apply, config) => {
+ await moveProject(
+ f.project.id,
+ f.workspace.id,
+ target.workspace.id,
+ target.user.id,
+ );
+ return transaction(apply, config);
+ },
+ );
+ const publish = vi
+ .spyOn(events, "publishEvent")
+ .mockResolvedValue(undefined);
+ const response = await f.request("", "PATCH", {
+ rules,
+ previewToken: preview.previewToken,
+ });
+ expect(response.status).toBe(403);
+ expect(
+ await db.query.integrationTable.findFirst({
+ where: eq(schema.integrationTable.id, f.integration.id),
+ }),
+ ).toMatchObject({ config: f.integration.config });
+ expect(publish).not.toHaveBeenCalledWith(
+ "integration.sync_rules_changed",
+ expect.anything(),
+ );
+ });
+
+ it("returns the authorized workspace's saved preview if the project moves after commit", async () => {
+ const f = await setup();
+ const target = await createWorkspaceMember({ role: "owner" });
+ const [privateLabel] = await db
+ .insert(schema.labelTable)
+ .values({
+ workspaceId: target.workspace.id,
+ name: "Private destination label",
+ color: "#abcdef",
+ })
+ .returning();
+ const preview = await f.preview(defaultSyncRules);
+ vi.spyOn(events, "publishEvent").mockImplementation(async (name) => {
+ if (name === "integration.sync_rules_changed")
+ await moveProject(
+ f.project.id,
+ f.workspace.id,
+ target.workspace.id,
+ target.user.id,
+ );
+ });
+ const response = await f.request("", "PATCH", {
+ rules: defaultSyncRules,
+ previewToken: preview.previewToken,
+ });
+ expect(response.status).toBe(200);
+ const saved = await response.json();
+ expect(saved.labels).toEqual([expect.objectContaining({ id: f.label.id })]);
+ expect(JSON.stringify(saved)).not.toContain(privateLabel!.id);
+ expect(JSON.stringify(saved)).not.toContain(privateLabel!.name);
+ expect(
+ await db.query.projectTable.findFirst({
+ where: eq(schema.projectTable.id, f.project.id),
+ }),
+ ).toMatchObject({ workspaceId: target.workspace.id });
+ });
+
it("pauses excluded links, preserves metadata, and never silently resumes", async () => {
const f = await setup();
const link = await f.link();
const preview = await f.preview();
expect(preview).toMatchObject({ willPause: 1, matching: 0 });
- await saveSyncRules(f.project.id, "gitea", f.rules, preview.previewToken);
+ await saveSyncRules(
+ f.project.id,
+ "gitea",
+ f.rules,
+ preview.previewToken,
+ f.workspace.id,
+ );
expect(await canSyncTask(f.task.id, f.integration.id)).toBe(false);
let stored = await db.query.externalLinkTable.findFirst({
where: eq(schema.externalLinkTable.id, link.id),
@@ -547,7 +627,13 @@ it("pages a large paused scope and preserves metadata across batched rule saves"
expect(preview).toMatchObject({ total: 206, willPause: 206, paused: 206 });
expect(preview.pausedTasks).toHaveLength(25);
expect(preview.pausedNextCursor).toBeTruthy();
- await saveSyncRules(f.project.id, "gitea", f.rules, preview.previewToken);
+ await saveSyncRules(
+ f.project.id,
+ "gitea",
+ f.rules,
+ preview.previewToken,
+ f.workspace.id,
+ );
const links = await db.query.externalLinkTable.findMany();
expect(links.every((link) => isSyncPaused(link.metadata))).toBe(true);
expect(
diff --git a/tests/api-integration/integration-sync-settings.test.ts b/tests/api-integration/integration-sync-settings.test.ts
index 3bc954765..34b61083e 100644
--- a/tests/api-integration/integration-sync-settings.test.ts
+++ b/tests/api-integration/integration-sync-settings.test.ts
@@ -82,7 +82,13 @@ it.each(["gitea", "gitlab"] as const)(
await getSyncIntegration(project.id, type),
rules,
);
- await saveSyncRules(project.id, type, rules, preview.previewToken);
+ await saveSyncRules(
+ project.id,
+ type,
+ rules,
+ preview.previewToken,
+ member.workspace.id,
+ );
} finally {
release();
}