Skip to content

Commit 18a95de

Browse files
committed
fix(plugin-auth): refuse a mapping re-save whose same-key row lives in another organization
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5mEnsnzoPLq43wQyYy
1 parent 82e0cee commit 18a95de

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

‎packages/plugins/plugin-auth/src/idp-group-mapping-admin.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ interface FieldRefusal {
6868
}
6969

7070
export interface IdpGroupMappingAdminResult {
71-
status: 200 | 400 | 404 | 503;
71+
status: 200 | 400 | 404 | 409 | 503;
7272
body:
7373
| { success: true; data: Record<string, unknown> }
7474
| { success: false; error: { code: string; message: string; fields?: FieldRefusal[] } };
@@ -278,9 +278,21 @@ export async function runSetIdpGroupMapping(
278278
{ where: { source_kind: SCIM_MAPPING_SOURCE_KIND, source_id: connectionId, permission_set: permissionSet } },
279279
{ context: SYSTEM_CTX } as never,
280280
);
281-
const existing = (Array.isArray(sameRows) ? (sameRows as Record<string, unknown>[]) : []).find(
281+
const sameKey = (Array.isArray(sameRows) ? (sameRows as Record<string, unknown>[]) : []).filter(
282282
(r) => text(r.group_external_id) === (groupExternalId ?? '') && text(r.group_display_name) === (groupExternalId ? '' : groupDisplayName ?? ''),
283283
);
284+
// A same-key row saved while another organization owned the connection is
285+
// not this mapping: its row and its grants stay in that organization. Never
286+
// answer an organization the row does not carry, and never write a twin.
287+
const elsewhere = sameKey.find((r) => text(r.organization_id) !== organizationId);
288+
if (elsewhere) {
289+
return fail(
290+
409,
291+
'RESOURCE_CONFLICT',
292+
`IdP group mapping '${text(elsewhere.id)}' already maps this group to '${permissionSet}' for SCIM connection '${connectionId}' in organization '${text(elsewhere.organization_id)}', but the connection now belongs to organization '${organizationId}'. Delete mapping '${text(elsewhere.id)}' first (POST /admin/scim/delete-group-mapping), then save again.`,
293+
);
294+
}
295+
const existing = sameKey.find((r) => text(r.organization_id) === organizationId);
284296
let mappingId: string;
285297
if (existing) {
286298
mappingId = text(existing.id);

0 commit comments

Comments
 (0)