Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: preserve integration tenant ownership

Richie McIlroy544df79fa0c3
diff --git a/apps/web/__tests__/unit/integration-installations-repository.test.ts b/apps/web/__tests__/unit/integration-installations-repository.test.ts
index 5d3799912..73a5d9b06 100644
--- a/apps/web/__tests__/unit/integration-installations-repository.test.ts
+++ b/apps/web/__tests__/unit/integration-installations-repository.test.ts
@@ -5,14 +5,29 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
const databaseMocks = vi.hoisted(() => {
const state = { rows: [] as unknown[] };
- const onDuplicateKeyUpdate = vi.fn(async () => undefined);
+ const onDuplicateKeyUpdate = vi.fn(
+ async (_input: { set: Record<string, unknown> }) => undefined,
+ );
const values = vi.fn(() => ({ onDuplicateKeyUpdate }));
const insert = vi.fn(() => ({ values }));
const orderBy = vi.fn(async () => state.rows);
const limit = vi.fn(async () => state.rows);
- const where = vi.fn(() => ({ orderBy, limit }));
+ const forUpdate = vi.fn(async () => state.rows);
+ const where = vi.fn(() => ({ orderBy, limit, for: forUpdate }));
const from = vi.fn(() => ({ where }));
const select = vi.fn(() => ({ from }));
+ const updateWhere = vi.fn(async () => undefined);
+ const set = vi.fn(() => ({ where: updateWhere }));
+ const update = vi.fn(() => ({ set }));
+ const transaction = vi.fn(
+ async (
+ callback: (tx: {
+ insert: typeof insert;
+ select: typeof select;
+ update: typeof update;
+ }) => Promise<unknown>,
+ ) => callback({ insert, select, update }),
+ );
const deleteWhere = vi.fn(async () => undefined);
const deleteRows = vi.fn(() => ({ where: deleteWhere }));
@@ -23,9 +38,14 @@ const databaseMocks = vi.hoisted(() => {
insert,
orderBy,
limit,
+ forUpdate,
where,
from,
select,
+ updateWhere,
+ set,
+ update,
+ transaction,
deleteWhere,
deleteRows,
};
@@ -40,7 +60,9 @@ vi.mock("@cap/database", () => ({
db: () => ({
insert: databaseMocks.insert,
select: databaseMocks.select,
+ update: databaseMocks.update,
delete: databaseMocks.deleteRows,
+ transaction: databaseMocks.transaction,
}),
}));
@@ -72,7 +94,14 @@ describe("integration installation repository", () => {
cryptoMocks.decrypt.mockResolvedValue("{}");
});
- it("encrypts credentials and atomically upserts provider installations", async () => {
+ it("updates credentials only after locking the owning tenant installation", async () => {
+ databaseMocks.state.rows = [
+ {
+ id: "existing-installation",
+ organizationId,
+ },
+ ];
+
await saveIntegrationInstallation({
provider: "slack",
externalId: "T123",
@@ -96,16 +125,46 @@ describe("integration installation repository", () => {
encryptedCredentials: "encrypted-credentials",
metadata: { scopes: ["links:read"] },
});
- expect(databaseMocks.onDuplicateKeyUpdate).toHaveBeenCalledWith({
- set: {
+
+ expect(databaseMocks.onDuplicateKeyUpdate).toHaveBeenCalledOnce();
+ const [upsert] = databaseMocks.onDuplicateKeyUpdate.mock.calls[0] ?? [];
+ expect(upsert?.set).toEqual({
+ externalId: expect.anything(),
+ });
+ expect(upsert?.set).not.toHaveProperty("organizationId");
+ expect(databaseMocks.forUpdate).toHaveBeenCalledWith("update");
+ expect(databaseMocks.set).toHaveBeenCalledWith({
+ displayName: "Cap",
+ installedByUserId,
+ encryptedCredentials: "encrypted-credentials",
+ metadata: { scopes: ["links:read"] },
+ updatedAt: expect.any(Date),
+ });
+ expect(databaseMocks.updateWhere).toHaveBeenCalledOnce();
+ });
+
+ it("rejects an installation already owned by another organization", async () => {
+ databaseMocks.state.rows = [
+ {
+ id: "existing-installation",
+ organizationId: Organisation.OrganisationId.make("other-org"),
+ },
+ ];
+
+ await expect(
+ saveIntegrationInstallation({
+ provider: "slack",
+ externalId: "T123",
displayName: "Cap",
organizationId,
installedByUserId,
- encryptedCredentials: "encrypted-credentials",
+ credentials: { accessToken: "xoxb-token" },
metadata: { scopes: ["links:read"] },
- updatedAt: expect.any(Date),
- },
- });
+ }),
+ ).rejects.toThrow(
+ "Integration is already connected to another organization",
+ );
+ expect(databaseMocks.set).not.toHaveBeenCalled();
});
it("ships a provider-agnostic schema with workload-matched indexes", () => {
diff --git a/apps/web/lib/integrations/installations.ts b/apps/web/lib/integrations/installations.ts
index 7fc1b77ff..26c0b8f4b 100644
--- a/apps/web/lib/integrations/installations.ts
+++ b/apps/web/lib/integrations/installations.ts
@@ -3,7 +3,7 @@ import { decrypt, encrypt } from "@cap/database/crypto";
import { nanoId } from "@cap/database/helpers";
import { integrationInstallations } from "@cap/database/schema";
import type { Organisation, User } from "@cap/web-domain";
-import { and, asc, eq } from "drizzle-orm";
+import { and, asc, eq, sql } from "drizzle-orm";
export type IntegrationInstallationSummary = {
id: string;
@@ -31,28 +31,60 @@ export const saveIntegrationInstallation = async ({
metadata: Record<string, unknown>;
}) => {
const encryptedCredentials = await encrypt(JSON.stringify(credentials));
- await db()
- .insert(integrationInstallations)
- .values({
- id: nanoId(),
- provider,
- externalId,
- displayName,
- organizationId,
- installedByUserId,
- encryptedCredentials,
- metadata,
- })
- .onDuplicateKeyUpdate({
- set: {
+ const candidateId = nanoId();
+ await db().transaction(async (tx) => {
+ await tx
+ .insert(integrationInstallations)
+ .values({
+ id: candidateId,
+ provider,
+ externalId,
displayName,
organizationId,
installedByUserId,
encryptedCredentials,
metadata,
+ })
+ .onDuplicateKeyUpdate({
+ set: {
+ externalId: sql`${integrationInstallations.externalId}`,
+ },
+ });
+
+ const [installation] = await tx
+ .select({
+ id: integrationInstallations.id,
+ organizationId: integrationInstallations.organizationId,
+ })
+ .from(integrationInstallations)
+ .where(
+ and(
+ eq(integrationInstallations.provider, provider),
+ eq(integrationInstallations.externalId, externalId),
+ ),
+ )
+ .for("update");
+ if (!installation) {
+ throw new Error("Integration installation could not be saved");
+ }
+ if (installation.organizationId !== organizationId) {
+ throw new Error(
+ "Integration is already connected to another organization",
+ );
+ }
+ if (installation.id === candidateId) return;
+
+ await tx
+ .update(integrationInstallations)
+ .set({
+ displayName,
+ installedByUserId,
+ encryptedCredentials,
+ metadata,
updatedAt: new Date(),
- },
- });
+ })
+ .where(eq(integrationInstallations.id, installation.id));
+ });
};
export const listIntegrationInstallations = async ({