Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(pki): scope sync slot ownership to the project

bernie-g058afa37cef7
diff --git a/backend/src/services/certificate-sync/certificate-sync-dal.ts b/backend/src/services/certificate-sync/certificate-sync-dal.ts
index 1ebdf26b5a..c4b273057d 100644
--- a/backend/src/services/certificate-sync/certificate-sync-dal.ts
+++ b/backend/src/services/certificate-sync/certificate-sync-dal.ts
@@ -72,18 +72,29 @@ export const certificateSyncDALFactory = (db: TDbClient) => {
const findExternalIdentifiersInUse = async (
externalIdentifiers: string[],
- { excludePkiSyncId, destination }: { excludePkiSyncId: string; destination: string | string[] },
+ {
+ excludePkiSyncId,
+ destination,
+ projectId
+ }: { excludePkiSyncId: string; destination: string | string[]; projectId?: string },
tx?: Knex
): Promise<Set<string>> => {
try {
if (externalIdentifiers.length === 0) return new Set();
const destinations = Array.isArray(destination) ? destination : [destination];
- const docs = (await (tx || db)(TableName.CertificateSync)
+ const query = (tx || db)(TableName.CertificateSync)
.join(TableName.PkiSync, `${TableName.PkiSync}.id`, `${TableName.CertificateSync}.pkiSyncId`)
.whereIn(`${TableName.CertificateSync}.externalIdentifier`, externalIdentifiers)
.whereIn(`${TableName.PkiSync}.destination`, destinations)
- .whereNot(`${TableName.CertificateSync}.pkiSyncId`, excludePkiSyncId)
- .select(`${TableName.CertificateSync}.externalIdentifier`)) as Array<{ externalIdentifier: string | null }>;
+ .whereNot(`${TableName.CertificateSync}.pkiSyncId`, excludePkiSyncId);
+
+ // An identifier is only another sync's to hold inside the same project. Without this, anyone
+ // able to create a sync could claim a name belonging to another tenant and stop its cleanup.
+ if (projectId) void query.where(`${TableName.PkiSync}.projectId`, projectId);
+
+ const docs = (await query.select(`${TableName.CertificateSync}.externalIdentifier`)) as Array<{
+ externalIdentifier: string | null;
+ }>;
return new Set(docs.map((doc) => doc.externalIdentifier).filter((v): v is string => Boolean(v)));
} catch (error) {
throw new DatabaseError({ error, name: "FindExternalIdentifiersInUse" });
diff --git a/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.test.ts b/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.test.ts
index 2c841cecf4..7515c3fb1e 100644
--- a/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.test.ts
+++ b/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.test.ts
@@ -311,3 +311,45 @@ describe("Imperva slot ownership", () => {
expect(removeCertificatesFromDal).toHaveBeenCalledWith("sync-1", ["cert-old"]);
});
});
+
+describe("Imperva ownership scoping", () => {
+ test("asks only about slots held inside the same project", async () => {
+ const { impervaPkiSyncFactory } = await import("./imperva-pki-sync-fns");
+ const findExternalIdentifiersInUse = vi.fn().mockResolvedValue(new Set<string>());
+
+ impervaRequest.mockReset();
+ impervaRequest.mockResolvedValue({});
+
+ const factory = impervaPkiSyncFactory({
+ certificateSyncDAL: {
+ findByPkiSyncId: vi.fn().mockResolvedValue([{ certificateId: "cert-old", externalIdentifier: "111:RSA" }]),
+ removeCertificates: vi.fn(),
+ addCertificates: vi.fn(),
+ findByPkiSyncAndCertificate: vi.fn().mockResolvedValue(undefined),
+ updateById: vi.fn(),
+ updateSyncStatus: vi.fn(),
+ findExternalIdentifiersInUse,
+ claimExternalIdentifier: vi.fn()
+ } as never,
+ certificateDAL: { findById: vi.fn().mockResolvedValue(undefined) } as never
+ });
+
+ await factory.syncCertificates(
+ {
+ id: "sync-1",
+ destination: "imperva",
+ projectId: "project-1",
+ destinationConfig: { siteId: "111", siteName: "current.example.com" },
+ syncOptions: { canRemoveCertificates: true },
+ connection: { credentials: { apiId: "id", apiKey: "key" }, gatewayId: null, gatewayPoolId: null }
+ } as never,
+ {}
+ );
+
+ expect(findExternalIdentifiersInUse).toHaveBeenCalledWith(["111:RSA"], {
+ excludePkiSyncId: "sync-1",
+ destination: "imperva",
+ projectId: "project-1"
+ });
+ });
+});
diff --git a/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.ts b/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.ts
index c8ef7d44cb..145643a6ba 100644
--- a/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.ts
+++ b/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.ts
@@ -406,7 +406,7 @@ export const impervaPkiSyncFactory = ({
const ownedByOtherSync = await certificateSyncDAL.findExternalIdentifiersInUse(
orphanedRecords.map((record: TCertificateSyncs) => record.externalIdentifier as string),
- { excludePkiSyncId: pkiSync.id, destination: pkiSync.destination }
+ { excludePkiSyncId: pkiSync.id, destination: pkiSync.destination, projectId: pkiSync.projectId }
);
for (const record of orphanedRecords) {
@@ -534,7 +534,8 @@ export const impervaPkiSyncFactory = ({
const ownedByOtherSync = await certificateSyncDAL.findExternalIdentifiersInUse([...slotsToRemove.keys()], {
excludePkiSyncId: pkiSync.id,
- destination: pkiSync.destination
+ destination: pkiSync.destination,
+ projectId: pkiSync.projectId
});
for (const [externalIdentifier, certificateIds] of slotsToRemove) {