Skip to content

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

test: add unit tests for handling out-of-scope instance admin identity in delete and update operations

Victor Hugo dos Santos9a221d010087
diff --git a/backend/src/services/identity-v2/identity-service.test.ts b/backend/src/services/identity-v2/identity-service.test.ts
index 4fcba977ee..f3d4e0f4a7 100644
--- a/backend/src/services/identity-v2/identity-service.test.ts
+++ b/backend/src/services/identity-v2/identity-service.test.ts
@@ -186,4 +186,23 @@ describe("instance admin identities are protected on the v2 surface", () => {
expect(identityDAL.deleteById).toHaveBeenCalledWith(IDENTITY_ID, TX);
});
+
+ test("an out-of-scope instance admin identity is not found, not refused as privileged", async () => {
+ const { service, identityDAL, deleteAlertsForDeletedResource } = createService({ existingIdentity: null });
+
+ await expect(service.deleteIdentity(buildDto())).rejects.toThrow(`Identity with id ${IDENTITY_ID} not found`);
+
+ expect(identityDAL.deleteById).not.toHaveBeenCalled();
+ expect(deleteAlertsForDeletedResource).not.toHaveBeenCalled();
+ });
+
+ test("an out-of-scope instance admin identity is not found on update either", async () => {
+ const { service, identityDAL } = createService({ existingIdentity: null });
+
+ await expect(service.updateIdentity({ ...buildDto(), data: { name: "renamed" } } as never)).rejects.toThrow(
+ `Identity with id ${IDENTITY_ID} not found`
+ );
+
+ expect(identityDAL.updateById).not.toHaveBeenCalled();
+ });
});
diff --git a/backend/src/services/identity-v2/identity-service.ts b/backend/src/services/identity-v2/identity-service.ts
index 3a8186f2ec..7340ae6360 100644
--- a/backend/src/services/identity-v2/identity-service.ts
+++ b/backend/src/services/identity-v2/identity-service.ts
@@ -323,7 +323,6 @@ export const identityV2ServiceFactory = ({
const factory = scopeFactory[scopeData.scope];
await factory.onUpdateIdentityGuard(dto);
- await validateIdentityUpdateForSuperAdminPrivileges(dto.selector.identityId, dto.isActorSuperAdmin);
const existingIdentity = await identityDAL.findOne({
id: dto.selector.identityId,
@@ -333,6 +332,8 @@ export const identityV2ServiceFactory = ({
if (!existingIdentity)
throw new NotFoundError({ message: `Identity with id ${dto.selector.identityId} not found` });
+ await validateIdentityUpdateForSuperAdminPrivileges(dto.selector.identityId, dto.isActorSuperAdmin);
+
const identity = await identityDAL.transaction(async (tx) => {
const updatedIdentity =
data?.name || data?.hasDeleteProtection
@@ -378,7 +379,6 @@ export const identityV2ServiceFactory = ({
const factory = scopeFactory[scopeData.scope];
await factory.onDeleteIdentityGuard(dto);
- await validateIdentityUpdateForSuperAdminPrivileges(dto.selector.identityId, dto.isActorSuperAdmin);
const existingIdentity = await identityDAL.findOne({
id: dto.selector.identityId,
@@ -387,6 +387,8 @@ export const identityV2ServiceFactory = ({
});
if (!existingIdentity)
throw new NotFoundError({ message: `Identity with id ${dto.selector.identityId} not found` });
+
+ await validateIdentityUpdateForSuperAdminPrivileges(dto.selector.identityId, dto.isActorSuperAdmin);
if (existingIdentity.hasDeleteProtection) {
throw new BadRequestError({ message: "Cannot delete identity while delete protection is enabled" });
}