From 7305713b9742cf7fc3a97023eb8cb6eaa7c9f936 Mon Sep 17 00:00:00 2001 From: Mitsuki Ogasahara Date: Mon, 17 Jun 2024 22:32:59 +0900 Subject: [PATCH] fix: allow special characters in email (#5327) Co-authored-by: crazywoola <427733928@qq.com> --- api/libs/helper.py | 2 +- api/tests/unit_tests/libs/test_email.py | 25 +++++++++++++++++++++++++ web/app/signin/normalForm.tsx | 5 ++--- web/config/index.ts | 2 +- 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 api/tests/unit_tests/libs/test_email.py diff --git a/api/libs/helper.py b/api/libs/helper.py index f4be9c5531..fa326c5a53 100644 --- a/api/libs/helper.py +++ b/api/libs/helper.py @@ -25,7 +25,7 @@ class TimestampField(fields.Raw): def email(email): # Define a regex pattern for email addresses - pattern = r"^[\w\.-]+@([\w-]+\.)+[\w-]{2,}$" + pattern = r"^[\w\.!#$%&'*+\-/=?^_`{|}~]+@([\w-]+\.)+[\w-]{2,}$" # Check if the email matches the pattern if re.match(pattern, email) is not None: return email diff --git a/api/tests/unit_tests/libs/test_email.py b/api/tests/unit_tests/libs/test_email.py new file mode 100644 index 0000000000..f8234f3f3b --- /dev/null +++ b/api/tests/unit_tests/libs/test_email.py @@ -0,0 +1,25 @@ +from libs.helper import email + + +def test_email_with_valid_email(): + assert email("test@example.com") == "test@example.com" + assert email("TEST12345@example.com") == "TEST12345@example.com" + assert email("test+test@example.com") == "test+test@example.com" + assert email("!#$%&'*+-/=?^_{|}~`@example.com") == "!#$%&'*+-/=?^_{|}~`@example.com" + + +def test_email_with_invalid_email(): + try: + email("invalid_email") + except ValueError as e: + assert str(e) == "invalid_email is not a valid email." + + try: + email("@example.com") + except ValueError as e: + assert str(e) == "@example.com is not a valid email." + + try: + email("()@example.com") + except ValueError as e: + assert str(e) == "()@example.com is not a valid email." diff --git a/web/app/signin/normalForm.tsx b/web/app/signin/normalForm.tsx index f6abf0d6c0..60f21c0341 100644 --- a/web/app/signin/normalForm.tsx +++ b/web/app/signin/normalForm.tsx @@ -7,11 +7,10 @@ import useSWR from 'swr' import Link from 'next/link' import Toast from '../components/base/toast' import style from './page.module.css' -import { IS_CE_EDITION, SUPPORT_MAIL_LOGIN, apiPrefix } from '@/config' +import { IS_CE_EDITION, SUPPORT_MAIL_LOGIN, apiPrefix, emailRegex } from '@/config' import Button from '@/app/components/base/button' import { login, oauth } from '@/service/common' import { getPurifyHref } from '@/utils' -const validEmailReg = /^[\w\.-]+@([\w-]+\.)+[\w-]{2,}$/ type IState = { formValid: boolean @@ -78,7 +77,7 @@ const NormalForm = () => { const [isLoading, setIsLoading] = useState(false) const handleEmailPasswordLogin = async () => { - if (!validEmailReg.test(email)) { + if (!emailRegex.test(email)) { Toast.notify({ type: 'error', message: t('login.error.emailInValid'), diff --git a/web/config/index.ts b/web/config/index.ts index 16f458d580..600becf68c 100644 --- a/web/config/index.ts +++ b/web/config/index.ts @@ -102,7 +102,7 @@ export const DEFAULT_PARAGRAPH_VALUE_MAX_LEN = 1000 export const zhRegex = /^[\u4E00-\u9FA5]$/m export const emojiRegex = /^[\uD800-\uDBFF][\uDC00-\uDFFF]$/m -export const emailRegex = /^[\w\.-]+@([\w-]+\.)+[\w-]{2,}$/m +export const emailRegex = /^[\w.!#$%&'*+\-/=?^{|}~]+@([\w-]+\.)+[\w-]{2,}$/m const MAX_ZN_VAR_NAME_LENGHT = 8 const MAX_EN_VAR_VALUE_LENGHT = 30 export const getMaxVarNameLength = (value: string) => {