Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Match ALLOWED_EMAILS case-insensitively and trim pattern whitespace (#2566)
Luke Vella53784dfd1611
diff --git a/apps/web/src/features/auth/utils.test.ts b/apps/web/src/features/auth/utils.test.tsnew file mode 100644index 000000000..30b4a8ae7--- /dev/null+++ b/apps/web/src/features/auth/utils.test.ts@@ -0,0 +1,48 @@+import { afterEach, describe, expect, it, vi } from "vitest";+import { isEmailBlocked } from "./utils";++describe("isEmailBlocked", () => {+ afterEach(() => {+ vi.unstubAllEnvs();+ });++ it("allows all emails when ALLOWED_EMAILS is not set", () => {+ vi.stubEnv("ALLOWED_EMAILS", "");+ expect(isEmailBlocked("[email protected]")).toBe(false);+ });++ it("allows an email matching a wildcard pattern", () => {+ vi.stubEnv("ALLOWED_EMAILS", "*@company.com");+ expect(isEmailBlocked("[email protected]")).toBe(false);+ });++ it("blocks an email that matches no pattern", () => {+ vi.stubEnv("ALLOWED_EMAILS", "*@company.com");+ expect(isEmailBlocked("[email protected]")).toBe(true);+ });++ it("matches case-insensitively", () => {+ vi.stubEnv("ALLOWED_EMAILS", "*@company.com");+ expect(isEmailBlocked("[email protected]")).toBe(false);+ });++ it("ignores whitespace around comma-separated patterns", () => {+ vi.stubEnv("ALLOWED_EMAILS", "*@company.com, *@example.com");+ expect(isEmailBlocked("[email protected]")).toBe(false);+ });++ it("ignores empty entries from trailing commas", () => {+ vi.stubEnv("ALLOWED_EMAILS", "*@company.com,");+ expect(isEmailBlocked("[email protected]")).toBe(true);+ });++ it("allows an exact email entry regardless of case", () => {+ vi.stubEnv("ALLOWED_EMAILS", "[email protected]");+ expect(isEmailBlocked("[email protected]")).toBe(false);+ });++ it("does not treat pattern dots as regex wildcards", () => {+ vi.stubEnv("ALLOWED_EMAILS", "*@company.com");+ expect(isEmailBlocked("user@companyxcom")).toBe(true);+ });+});diff --git a/apps/web/src/features/auth/utils.ts b/apps/web/src/features/auth/utils.tsindex 71e330e9d..5fbd414eb 100644--- a/apps/web/src/features/auth/utils.ts+++ b/apps/web/src/features/auth/utils.ts@@ -45,13 +45,16 @@ export const passwordQualityThresholds: Record<PasswordQuality, number> = {export function isEmailBlocked(email: string) {if (process.env.ALLOWED_EMAILS) {- const allowedEmails = process.env.ALLOWED_EMAILS.split(",");+ const allowedEmails = process.env.ALLOWED_EMAILS.split(",")+ .map((allowedEmail) => allowedEmail.trim())+ .filter(Boolean);// Check whether the email matches enough of the patterns specified in ALLOWED_EMAILSconst isAllowed = allowedEmails.some((allowedEmail) => {const regex = new RegExp(`^${allowedEmail.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replaceAll(/[*]/g, ".*")}$`,+ "i",);return regex.test(email);});