Skip to content

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

fix(pki-sync): scope ownership to org family and reclaim renamed identifiers

bernie-g5b0bcca30b11
diff --git a/backend/src/services/certificate-sync/certificate-sync-dal.ts b/backend/src/services/certificate-sync/certificate-sync-dal.ts
index 97148208c3..fd404474a3 100644
--- a/backend/src/services/certificate-sync/certificate-sync-dal.ts
+++ b/backend/src/services/certificate-sync/certificate-sync-dal.ts
@@ -77,11 +77,24 @@ export const certificateSyncDALFactory = (db: TDbClient) => {
): Promise<Set<string>> => {
try {
if (externalIdentifiers.length === 0) return new Set();
- const docs = (await (tx || db)(TableName.CertificateSync)
+ const knex = tx || db;
+ const excludedSyncRootOrgId = knex(TableName.PkiSync)
+ .join(TableName.Project, `${TableName.Project}.id`, `${TableName.PkiSync}.projectId`)
+ .join(TableName.Organization, `${TableName.Organization}.id`, `${TableName.Project}.orgId`)
+ .where(`${TableName.PkiSync}.id`, excludePkiSyncId)
+ .select(knex.raw("COALESCE(??, ??)", [`${TableName.Organization}.rootOrgId`, `${TableName.Organization}.id`]));
+ const docs = (await knex(TableName.CertificateSync)
.join(TableName.PkiSync, `${TableName.PkiSync}.id`, `${TableName.CertificateSync}.pkiSyncId`)
+ .join(TableName.Project, `${TableName.Project}.id`, `${TableName.PkiSync}.projectId`)
+ .join(TableName.Organization, `${TableName.Organization}.id`, `${TableName.Project}.orgId`)
.whereIn(`${TableName.CertificateSync}.externalIdentifier`, externalIdentifiers)
.where(`${TableName.PkiSync}.destination`, destination)
.whereNot(`${TableName.CertificateSync}.pkiSyncId`, excludePkiSyncId)
+ .whereRaw("COALESCE(??, ??) = (?)", [
+ `${TableName.Organization}.rootOrgId`,
+ `${TableName.Organization}.id`,
+ excludedSyncRootOrgId
+ ])
.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) {
@@ -102,7 +115,9 @@ export const certificateSyncDALFactory = (db: TDbClient) => {
.ignore();
await (tx || db)(TableName.CertificateSync)
.where({ pkiSyncId, certificateId })
- .whereNull("externalIdentifier")
+ .where((qb) => {
+ void qb.whereNull("externalIdentifier").orWhereNot("externalIdentifier", externalIdentifier);
+ })
.update({ externalIdentifier });
} catch (error) {
throw new DatabaseError({ error, name: "ClaimExternalIdentifier" });