Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Add support for special intl chars for email formatting (#64060)

Denis Berezinf43c6462d8d4
diff --git a/frontend/src/metabase/lib/email.ts b/frontend/src/metabase/lib/email.ts
index 3e4a74aa58f..5a6992016d1 100644
--- a/frontend/src/metabase/lib/email.ts
+++ b/frontend/src/metabase/lib/email.ts
@@ -1,7 +1,10 @@
+// Enhanced email regex with Unicode support for international characters
+// Uses Unicode character classes to support international domains and names
+// Based on RFC 5322 with practical constraints for security and usability
const EMAIL_REGEX =
- /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
+ /^(?=.{1,254}$)(?=.{1,64}@)[\p{L}\p{N}!#$%&'*+\/=?\^`{|}~_-]+(?:\.[\p{L}\p{N}!#$%&'*+\/=?\^`{|}~_-]+)*@([\p{L}\p{N}](?:[\p{L}\p{N}-]{0,61}[\p{L}\p{N}])?(?:\.[\p{L}\p{N}](?:[\p{L}\p{N}-]{0,61}[\p{L}\p{N}])?)*)$/u;
-export function isEmail(email: string | undefined | null) {
+export function isEmail(email: string | undefined | null): boolean {
if (email === null || email === undefined) {
return false;
}
@@ -10,5 +13,5 @@ export function isEmail(email: string | undefined | null) {
export function getEmailDomain(email: string) {
const match = EMAIL_REGEX.exec(email);
- return match && match[5];
+ return match && match[1];
}
diff --git a/frontend/src/metabase/lib/email.unit.spec.ts b/frontend/src/metabase/lib/email.unit.spec.ts
new file mode 100644
index 00000000000..aaf647061d9
--- /dev/null
+++ b/frontend/src/metabase/lib/email.unit.spec.ts
@@ -0,0 +1,43 @@
+import { isEmail } from "metabase/lib/email";
+
+describe("isEmail", () => {
+ it("should work with email addresses containing ASCII special characters", () => {
+ const specialEmails = [
+ ];
+
+ specialEmails.forEach((email) => {
+ expect(isEmail(email)).toBe(true);
+ });
+ });
+
+ it("should handle emails with Unicode/international characters", () => {
+ const unicodeEmails = [
+ "hafthór_júlíus_bjö[email protected]", // Original example with accented characters
+ "test.用户@example.com", // Chinese characters
+ "josé.garcía@café.org", // Spanish accented characters
+ "müller@münchen.de", // German umlauts
+ "andré@québec.ca", // French accented characters
+ ];
+
+ unicodeEmails.forEach((email) => {
+ expect(isEmail(email)).toBe(true);
+ });
+ });
+
+ it("should handle emails with unusual but valid characters", () => {
+ const unusualEmails = [
+ "user!#$%&'*+-/=?^_`{|}[email protected]", // All allowed special chars in local part
+ ];
+
+ unusualEmails.forEach((email) => {
+ expect(isEmail(email)).toBe(true);
+ });
+ });
+});
diff --git a/frontend/src/metabase/lib/formatting/email.tsx b/frontend/src/metabase/lib/formatting/email.tsx
index 2e444b8cdf0..fc0545ad8ed 100644
--- a/frontend/src/metabase/lib/formatting/email.tsx
+++ b/frontend/src/metabase/lib/formatting/email.tsx
@@ -1,14 +1,11 @@
import ExternalLink from "metabase/common/components/ExternalLink";
+import { isEmail } from "metabase/lib/email";
import { getDataFromClicked } from "metabase-lib/v1/parameters/utils/click-behavior";
import { renderLinkTextForClick } from "./link";
import { removeNewLines } from "./strings";
import type { OptionsType } from "./types";
-// https://github.com/angular/angular.js/blob/v1.6.3/src/ng/directive/input.js#L27
-const EMAIL_ALLOW_LIST_REGEX =
- /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/;
-
export function formatEmail(
value: string,
{
@@ -30,7 +27,7 @@ export function formatEmail(
jsx &&
rich &&
(view_as === "email_link" || view_as === "auto") &&
- EMAIL_ALLOW_LIST_REGEX.test(email)
+ isEmail(email)
) {
let displayText = label || email;
if (collapseNewlines) {
diff --git a/frontend/src/metabase/lib/formatting/email.unit.spec.tsx b/frontend/src/metabase/lib/formatting/email.unit.spec.tsx
new file mode 100644
index 00000000000..112d526a793
--- /dev/null
+++ b/frontend/src/metabase/lib/formatting/email.unit.spec.tsx
@@ -0,0 +1,167 @@
+import { cleanup, render, screen } from "@testing-library/react";
+import { isElementOfType } from "react-dom/test-utils";
+
+import ExternalLink from "metabase/common/components/ExternalLink";
+import { createMockColumn } from "metabase-types/api/mocks";
+
+import { formatEmail } from "./email";
+
+describe("formatEmail", () => {
+ describe("email link generation", () => {
+ it("should return the email as a string when jsx is false", () => {
+ const result = formatEmail("[email protected]");
+ expect(result).toBe("[email protected]");
+ });
+
+ it("should create an ExternalLink for valid emails in jsx + rich mode", () => {
+ const result = formatEmail("[email protected]", {
+ jsx: true,
+ rich: true,
+ });
+ expect(isElementOfType(result as JSX.Element, ExternalLink)).toBe(true);
+
+ render(result);
+
+ expect(screen.getByRole("link")).toHaveAttribute(
+ "href",
+ "mailto:[email protected]",
+ );
+ expect(screen.getByRole("link")).toHaveTextContent("[email protected]");
+ });
+
+ it("should handle complex valid email addresses", () => {
+ const complexEmails = [
+ ];
+
+ complexEmails.forEach((email) => {
+ const result = formatEmail(email, { jsx: true, rich: true });
+ expect(isElementOfType(result as JSX.Element, ExternalLink)).toBe(true);
+
+ render(result);
+
+ expect(screen.getByRole("link")).toHaveAttribute(
+ "href",
+ `mailto:${email}`,
+ );
+ expect(screen.getByRole("link")).toHaveTextContent(email);
+
+ cleanup();
+ });
+ });
+ });
+
+ describe("invalid email handling", () => {
+ it("should return string for invalid email addresses", () => {
+ const invalidEmails = [
+ "not-an-email",
+ "@example.com",
+ "user@",
+ "",
+ ];
+
+ invalidEmails.forEach((email) => {
+ const result = formatEmail(email, { jsx: true, rich: true });
+ expect(result).toBe(email);
+ });
+ });
+
+ it("should handle very long email addresses that exceed regex limits", () => {
+ // Create an email that's too long (over 254 characters)
+ const longEmail = `${"a".repeat(250)}@example.com`;
+ const result = formatEmail(longEmail, { jsx: true, rich: true });
+ expect(result).toBe(longEmail);
+ });
+ });
+
+ describe("link text handling", () => {
+ it("should use custom link_text when provided with clicked data", () => {
+ const column = createMockColumn({ name: "email" });
+ const clicked = {
+ value: "[email protected]",
+ column,
+ data: [{ value: "[email protected]", col: column }],
+ };
+
+ const result = formatEmail("[email protected]", {
+ jsx: true,
+ rich: true,
+ link_text: "Custom Label",
+ clicked,
+ });
+
+ expect(isElementOfType(result as JSX.Element, ExternalLink)).toBe(true);
+
+ render(result);
+
+ expect(screen.getByRole("link")).toHaveAttribute(
+ "href",
+ "mailto:[email protected]",
+ );
+ expect(screen.getByRole("link")).toHaveTextContent("Custom Label");
+ });
+ });
+
+ describe("newline collapsing", () => {
+ it("should collapse newlines in email text when collapseNewlines is true", () => {
+ const emailWithNewlines = "[email protected]\n\rextra text";
+ const result = formatEmail(emailWithNewlines, {
+ jsx: true,
+ rich: true,
+ collapseNewlines: true,
+ });
+
+ expect(isElementOfType(result as JSX.Element, ExternalLink)).toBe(false);
+ expect(result).toBe("[email protected] extra text");
+ });
+
+ it("should collapse newlines in custom link text when collapseNewlines is true", () => {
+ const column = createMockColumn({ name: "email" });
+ const clicked = {
+ value: "[email protected]",
+ column,
+ data: [{ value: "[email protected]", col: column }],
+ };
+
+ const result = formatEmail("[email protected]", {
+ jsx: true,
+ rich: true,
+ link_text: "Custom\nLabel",
+ clicked,
+ collapseNewlines: true,
+ });
+
+ expect(isElementOfType(result as JSX.Element, ExternalLink)).toBe(true);
+
+ render(result);
+
+ expect(screen.getByRole("link")).toHaveTextContent("Custom Label");
+ });
+
+ it("should collapse newlines in string output when not in jsx mode", () => {
+ const emailWithNewlines = "[email protected]\nwith newlines";
+ const result = formatEmail(emailWithNewlines, {
+ collapseNewlines: true,
+ });
+
+ expect(result).toBe("[email protected] with newlines");
+ });
+ });
+
+ describe("edge cases", () => {
+ it("should handle empty string", () => {
+ const result = formatEmail("", { jsx: true, rich: true });
+ expect(result).toBe("");
+ });
+
+ it("should handle null and undefined by converting to string", () => {
+ expect(formatEmail(null as any)).toBe("null");
+ expect(formatEmail(undefined as any)).toBe("undefined");
+ });
+ });
+});