Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(scim): normalize userName casing in user POST flow (#13528)

Steffen Schmitz0bd8f740fed8
diff --git a/web/src/__tests__/server/scim-api.servertest.ts b/web/src/__tests__/server/scim-api.servertest.ts
index 3895138c94..d485f478fe 100644
--- a/web/src/__tests__/server/scim-api.servertest.ts
+++ b/web/src/__tests__/server/scim-api.servertest.ts
@@ -644,6 +644,54 @@ describe("SCIM API", () => {
expect(duplicateResult.status).toBe(409);
expect(duplicateResult.body.detail).toContain("already exists");
});
+
+ it("should return 409 when userName differs only by email case", async () => {
+ const localPart = `Mixed.Case.${randomUUID().substring(0, 8)}`;
+ const mixedCaseEmail = `${localPart}@Example.com`;
+ const lowerCaseEmail = `${localPart.toLowerCase()}@example.com`;
+
+ // Create with mixed-case userName
+ const createResponse = await makeAPICall(
+ "POST",
+ "/api/public/scim/Users",
+ {
+ userName: mixedCaseEmail,
+ name: {
+ formatted: "Test User",
+ },
+ },
+ createBasicAuthHeader(orgApiKey, orgSecretKey),
+ );
+ expect(createResponse.status).toBe(201);
+ testUserId = createResponse.body.id;
+
+ // The stored user email should be lowercased
+ const storedUser = await prisma.user.findUnique({
+ where: { id: testUserId },
+ });
+ expect(storedUser?.email).toBe(lowerCaseEmail);
+
+ // Re-POST with a case-variant userName: must be detected as duplicate
+ const duplicateResult = await makeAPICall(
+ "POST",
+ "/api/public/scim/Users",
+ {
+ userName: lowerCaseEmail,
+ name: {
+ formatted: "Another User",
+ },
+ },
+ createBasicAuthHeader(orgApiKey, orgSecretKey),
+ );
+ expect(duplicateResult.status).toBe(409);
+ expect(duplicateResult.body.detail).toContain("already exists");
+
+ // No duplicate org membership should have been created
+ const orgMemberships = await prisma.organizationMembership.findMany({
+ where: { userId: testUserId, orgId },
+ });
+ expect(orgMemberships.length).toBe(1);
+ });
});
describe("GET /api/public/scim/Users/{id}", () => {
diff --git a/web/src/pages/api/public/scim/Users/index.ts b/web/src/pages/api/public/scim/Users/index.ts
index 23631026ef..b9a5c53aa4 100644
--- a/web/src/pages/api/public/scim/Users/index.ts
+++ b/web/src/pages/api/public/scim/Users/index.ts
@@ -183,11 +183,15 @@ export default async function handler(
role = parsedRoles.data[0];
}
- // Check if user already exists
+ // Check if user already exists. Normalize the email to lowercase to
+ // stay consistent with the upsert below; otherwise a case-variant
+ // userName slips past the duplicate check and the upsert can collide
+ // with an existing user row.
+ const normalizedEmail = userName.toLowerCase();
const existingUser = await prisma.organizationMembership.findMany({
where: {
user: {
- email: userName,
+ email: normalizedEmail,
},
orgId: authCheck.scope.orgId,
},
@@ -207,10 +211,10 @@ export default async function handler(
// Create the user
const user = await prisma.user.upsert({
where: {
- email: userName.toLowerCase(),
+ email: normalizedEmail,
},
create: {
- email: userName.toLowerCase(),
+ email: normalizedEmail,
name: name?.formatted || displayName,
password: password ? await hashPassword(password) : undefined,
},