Skip to content

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

fix edit and inactive boundary to use new grant role

Thiago Araujo da Silva342ac6d360e9
diff --git a/backend/src/services/membership-user/org/org-membership-user-boundary.test.ts b/backend/src/services/membership-user/org/org-membership-user-boundary.test.ts
index ae2f8fbcf5..00e25a94d8 100644
--- a/backend/src/services/membership-user/org/org-membership-user-boundary.test.ts
+++ b/backend/src/services/membership-user/org/org-membership-user-boundary.test.ts
@@ -44,16 +44,27 @@ const memberPrivilegeGranter = createMongoAbility<MongoAbility>([
}
]);
+// Holds the action that describes changing a member's activation, but not the one for assigning
+// roles. The pair with memberPrivilegeGranter is what pins each operation to its own action.
+const memberDeleter = createMongoAbility<MongoAbility>([
+ {
+ action: [OrgPermissionMemberActions.Read, OrgPermissionMemberActions.Edit, OrgPermissionMemberActions.Delete],
+ subject: OrgPermissionSubjects.Member
+ }
+]);
+
const createGuard = ({
actorPermission,
shouldUseNewPrivilegeSystem,
targetRole = "admin",
- incomingRole = "member"
+ incomingRole = "member",
+ data
}: {
actorPermission: MongoAbility;
shouldUseNewPrivilegeSystem: boolean;
targetRole?: string;
incomingRole?: string;
+ data?: Record<string, unknown>;
}) => {
const abilityByRole: Record<string, MongoAbility> = {
admin,
@@ -85,7 +96,7 @@ const createGuard = ({
permission: { type: "user", id: "actor-id", orgId: ORG_ID, authMethod: "email" },
scopeData: { scope: AccessScope.Organization, orgId: ORG_ID },
selector: { userId: TARGET_USER_ID },
- data: { roles: [{ role: incomingRole, isTemporary: false }] }
+ data: data ?? { roles: [{ role: incomingRole, isTemporary: false }] }
} as never);
};
@@ -158,4 +169,91 @@ describe("onUpdateMembershipUserGuard privilege boundary", () => {
await expect(guard()).resolves.toBeUndefined();
});
+
+ // The guard ran one boundary keyed on member:grant-privileges for every field on the route, so a
+ // deactivation was gated on a role-assignment permission. Each operation now keys on the action
+ // that describes it, which for metadata is still grant-privileges: it feeds ABAC.
+ describe("gating follows the operation being performed", () => {
+ test("a metadata-only edit is bounded, because metadata drives ABAC", async () => {
+ // Org member metadata is the {{identity.metadata.*}} attribute source that project policies
+ // interpolate, so rewriting an Admin's metadata rewrites what their roles grant. member:edit
+ // alone must not reach it.
+ const metadataOnly = { roles: [], metadata: [{ key: "team", value: "platform" }] };
+
+ await expect(
+ createGuard({ actorPermission: memberEditorOnly, shouldUseNewPrivilegeSystem: false, data: metadataOnly })()
+ ).rejects.toThrow(PermissionBoundaryError);
+
+ await expect(
+ createGuard({ actorPermission: memberEditorOnly, shouldUseNewPrivilegeSystem: true, data: metadataOnly })()
+ ).rejects.toThrow(PermissionBoundaryError);
+
+ // grant-privileges is the action that reaches it, and only on the new system.
+ await expect(
+ createGuard({
+ actorPermission: memberPrivilegeGranter,
+ shouldUseNewPrivilegeSystem: true,
+ data: metadataOnly
+ })()
+ ).resolves.toBeUndefined();
+
+ // member:delete does not: deactivating a member and rewriting their attributes are different
+ // operations gated on different actions.
+ await expect(
+ createGuard({ actorPermission: memberDeleter, shouldUseNewPrivilegeSystem: true, data: metadataOnly })()
+ ).rejects.toThrow(PermissionBoundaryError);
+ });
+
+ test("deactivation keys on member:delete, not member:grant-privileges", async () => {
+ const deactivate = { roles: [], isActive: false };
+
+ // Holds grant-privileges but not delete: it can assign roles, and that says nothing about
+ // being allowed to cut off an Admin's access.
+ await expect(
+ createGuard({ actorPermission: memberPrivilegeGranter, shouldUseNewPrivilegeSystem: true, data: deactivate })()
+ ).rejects.toThrow(PermissionBoundaryError);
+
+ await expect(
+ createGuard({ actorPermission: memberDeleter, shouldUseNewPrivilegeSystem: true, data: deactivate })()
+ ).resolves.toBeUndefined();
+ });
+
+ test("reactivating a privileged member is bounded the same way", async () => {
+ // Restoring an Admin's access is as much a privilege change as removing it, so it cannot be
+ // the ungated direction of the same field.
+ const reactivate = { roles: [], isActive: true };
+
+ await expect(
+ createGuard({ actorPermission: memberPrivilegeGranter, shouldUseNewPrivilegeSystem: true, data: reactivate })()
+ ).rejects.toThrow(PermissionBoundaryError);
+
+ await expect(
+ createGuard({ actorPermission: memberDeleter, shouldUseNewPrivilegeSystem: true, data: reactivate })()
+ ).resolves.toBeUndefined();
+ });
+
+ test("the legacy system still compares privilege levels, whatever the operation", async () => {
+ // memberDeleter holds the action, which buys it nothing here: it is still weaker than an Admin.
+ await expect(
+ createGuard({
+ actorPermission: memberDeleter,
+ shouldUseNewPrivilegeSystem: false,
+ data: { roles: [], isActive: false }
+ })()
+ ).rejects.toThrow(PermissionBoundaryError);
+ });
+
+ test("a role change and a deactivation in one request need both actions", async () => {
+ const both = { roles: [{ role: "member", isTemporary: false }], isActive: false };
+
+ // Each holds exactly one of the two actions, so neither can do both at once.
+ await expect(
+ createGuard({ actorPermission: memberPrivilegeGranter, shouldUseNewPrivilegeSystem: true, data: both })()
+ ).rejects.toThrow(PermissionBoundaryError);
+
+ await expect(
+ createGuard({ actorPermission: memberDeleter, shouldUseNewPrivilegeSystem: true, data: both })()
+ ).rejects.toThrow(PermissionBoundaryError);
+ });
+ });
});
diff --git a/backend/src/services/membership-user/org/org-membership-user-factory.ts b/backend/src/services/membership-user/org/org-membership-user-factory.ts
index d6a4019335..545f403fc5 100644
--- a/backend/src/services/membership-user/org/org-membership-user-factory.ts
+++ b/backend/src/services/membership-user/org/org-membership-user-factory.ts
@@ -330,29 +330,46 @@ export const newOrgMembershipUserFactory = ({
});
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionMemberActions.Edit, OrgPermissionSubjects.Member);
- const targetMembership = await membershipUserDAL.getUserById({
- scopeData: dto.scopeData,
- userId: dto.selector.userId
- });
- const targetRoles = targetMembership ? resolveMembershipRoleSlugs(targetMembership.roles) : [];
const { shouldUseNewPrivilegeSystem } = await requestMemoize(
requestMemoKeys.orgFindById(dto.permission.orgId),
() => orgDAL.findById(dto.permission.orgId)
);
- if (targetRoles.length) {
- const targetPermissions = await permissionService.getOrgPermissionByRoles(targetRoles, dto.permission.orgId, {
- ignoreUnresolvedRoles: true
+ const targetOps: { opAction: OrgPermissionMemberActions; baseMessage: string }[] = [];
+ if (dto.data.roles.length || dto.data.metadata !== undefined)
+ targetOps.push({
+ opAction: OrgPermissionMemberActions.GrantPrivileges,
+ baseMessage: "Failed to change the roles or attributes of a more privileged org member"
+ });
+ if (dto.data.isActive !== undefined)
+ targetOps.push({
+ opAction: OrgPermissionMemberActions.Delete,
+ baseMessage: "Failed to change the activation status of a more privileged org member"
});
- assertRoleSetBoundary({
- shouldUseNewPrivilegeSystem,
- opActions: OrgPermissionMemberActions.GrantPrivileges,
- opSubject: OrgPermissionSubjects.Member,
- actorPermission: permission,
- targetPermissions,
- baseMessage: "Failed to change the roles of a more privileged org member"
+ if (targetOps.length) {
+ const targetMembership = await membershipUserDAL.getUserById({
+ scopeData: dto.scopeData,
+ userId: dto.selector.userId
});
+ const targetRoles = targetMembership ? resolveMembershipRoleSlugs(targetMembership.roles) : [];
+
+ if (targetRoles.length) {
+ const targetPermissions = await permissionService.getOrgPermissionByRoles(targetRoles, dto.permission.orgId, {
+ ignoreUnresolvedRoles: true
+ });
+
+ for (const { opAction, baseMessage } of targetOps) {
+ assertRoleSetBoundary({
+ shouldUseNewPrivilegeSystem,
+ opActions: opAction,
+ opSubject: OrgPermissionSubjects.Member,
+ actorPermission: permission,
+ targetPermissions,
+ baseMessage
+ });
+ }
+ }
}
if (dto.data.roles.length) {
diff --git a/backend/src/services/org/org-service.ts b/backend/src/services/org/org-service.ts
index cfa970ebd2..00325a057b 100644
--- a/backend/src/services/org/org-service.ts
+++ b/backend/src/services/org/org-service.ts
@@ -871,20 +871,36 @@ export const orgServiceFactory = ({
orgDAL.findById(orgId)
);
- const targetRoles = resolveMembershipRoleSlugs(await membershipRoleDAL.findRolesByMembershipIds([membershipId]));
- if (targetRoles.length) {
- const targetPermissions = await permissionService.getOrgPermissionByRoles(targetRoles, orgId, {
- ignoreUnresolvedRoles: true
+ const targetOps: { opAction: OrgPermissionMemberActions; baseMessage: string }[] = [];
+ if (role !== undefined || metadata !== undefined)
+ targetOps.push({
+ opAction: OrgPermissionMemberActions.GrantPrivileges,
+ baseMessage: "Failed to change the roles or attributes of a more privileged org member"
});
-
- assertRoleSetBoundary({
- shouldUseNewPrivilegeSystem,
- opActions: OrgPermissionMemberActions.GrantPrivileges,
- opSubject: OrgPermissionSubjects.Member,
- actorPermission: permission,
- targetPermissions,
- baseMessage: "Failed to change the roles of a more privileged org member"
+ if (isActive !== undefined)
+ targetOps.push({
+ opAction: OrgPermissionMemberActions.Delete,
+ baseMessage: "Failed to change the activation status of a more privileged org member"
});
+
+ if (targetOps.length) {
+ const targetRoles = resolveMembershipRoleSlugs(await membershipRoleDAL.findRolesByMembershipIds([membershipId]));
+ if (targetRoles.length) {
+ const targetPermissions = await permissionService.getOrgPermissionByRoles(targetRoles, orgId, {
+ ignoreUnresolvedRoles: true
+ });
+
+ for (const { opAction, baseMessage } of targetOps) {
+ assertRoleSetBoundary({
+ shouldUseNewPrivilegeSystem,
+ opActions: opAction,
+ opSubject: OrgPermissionSubjects.Member,
+ actorPermission: permission,
+ targetPermissions,
+ baseMessage
+ });
+ }
+ }
}
const isCustomRole = !Object.values(OrgMembershipRole).includes(role as OrgMembershipRole);