From fbe06d062448a4ebb496925b0363aff93ae7cc1e Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 26 Nov 2026 19:03:43 -0500 Subject: [PATCH] fix: Match existing users by case-insensitive email on invite (#14762) * fix: Match existing users by case-insensitive email on invite * test: Cover case-insensitive User.findByEmail lookup --- server/commands/userInviter.test.ts | 18 ++++++++++++++++++ server/commands/userInviter.ts | 11 ++++++++--- server/models/User.test.ts | 17 +++++++++++++++++ server/models/User.ts | 9 +++++++-- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/server/commands/userInviter.test.ts b/server/commands/userInviter.test.ts index bf43e049fe..6ae4e8644c 100644 --- a/server/commands/userInviter.test.ts +++ b/server/commands/userInviter.test.ts @@ -231,4 +231,22 @@ describe("userInviter", () => { ); expect(response.sent.length).toEqual(0); }); + + it("should not send invites to existing team members with different email case", async () => { + const email = faker.internet.email().toLowerCase(); + const user = await buildUser({ email: email.toUpperCase() }); + const response = await withContext(user, (ctx) => + userInviter(ctx, { + invites: [ + { + role: UserRole.Member, + email, + name: user.name, + }, + ], + }) + ); + expect(response.sent.length).toEqual(0); + expect(response.unsent.length).toEqual(1); + }); }); diff --git a/server/commands/userInviter.ts b/server/commands/userInviter.ts index 702fbc97a7..f54b723b8a 100644 --- a/server/commands/userInviter.ts +++ b/server/commands/userInviter.ts @@ -1,4 +1,5 @@ import { partition, uniqBy } from "es-toolkit/compat"; +import { Op, Sequelize } from "sequelize"; import { UserRole } from "@shared/types"; import InviteEmail from "@server/emails/templates/InviteEmail"; import env from "@server/env"; @@ -61,11 +62,15 @@ export default async function userInviter( const existingUsers = await User.findAll({ where: { teamId: user.teamId, - email: emails, + [Op.and]: [ + Sequelize.where(Sequelize.fn("lower", Sequelize.col("email")), { + [Op.in]: emails, + }), + ], }, }); - const existingEmails = existingUsers.map( - (existingUser) => existingUser.email + const existingEmails = existingUsers.map((existingUser) => + existingUser.email?.toLowerCase() ); const [existingInvites, filteredInvites] = partition( normalizedInvites, diff --git a/server/models/User.test.ts b/server/models/User.test.ts index 7d2286e4f4..51d3c1ce29 100644 --- a/server/models/User.test.ts +++ b/server/models/User.test.ts @@ -55,6 +55,23 @@ describe("user model", () => { }); }); + describe("findByEmail", () => { + it("should match email case-insensitively within the workspace only", async () => { + const email = faker.internet.email().toLowerCase(); + const user = await buildUser({ email: email.toUpperCase() }); + const otherTeamUser = await buildUser(); + + const found = await User.findByEmail(createContext({ user }), email); + expect(found?.id).toEqual(user.id); + + const notFound = await User.findByEmail( + createContext({ user: otherTeamUser }), + email + ); + expect(notFound).toBeNull(); + }); + }); + describe("create", () => { it("should not allow URLs in name", async () => { await expect( diff --git a/server/models/User.ts b/server/models/User.ts index 6a98df6511..1a1c2a2675 100644 --- a/server/models/User.ts +++ b/server/models/User.ts @@ -8,7 +8,7 @@ import type { InferAttributes, InferCreationAttributes, } from "sequelize"; -import { QueryTypes, Op } from "sequelize"; +import { QueryTypes, Op, Sequelize } from "sequelize"; import { type InstanceUpdateOptions } from "sequelize"; import { Table, @@ -996,7 +996,12 @@ class User extends ParanoidModel< return this.findOne({ where: { teamId: ctx.auth.user.teamId, - email: email.trim().toLowerCase(), + [Op.and]: [ + Sequelize.where( + Sequelize.fn("lower", Sequelize.col("email")), + email.trim().toLowerCase() + ), + ], }, ...toHookContext(ctx), });