Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email validation does not validate emails according to RFC standards #3155

Closed
mpiltz opened this issue Jan 18, 2024 · 6 comments
Closed

Email validation does not validate emails according to RFC standards #3155

mpiltz opened this issue Jan 18, 2024 · 6 comments

Comments

@mpiltz
Copy link

mpiltz commented Jan 18, 2024

It seems that Zod email validation accepts emails that are not conforming to RFC specifications.

" In addition to restrictions on syntax, there is a length limit on email addresses. That limit is a maximum of 64 characters (octets) in the "local part" (before the "@") and a maximum of 255 characters (octets) in the domain part (after the "@") for a total length of 320 characters. Systems that handle email should be prepared to process addresses which are that long, even though they are rarely encountered."

So the issues is that Zod does not count total number of chars or separate "local part" and "domain part".

Should I open a PR or is this something that is already on someones table?

@camboui
Copy link

camboui commented Feb 8, 2024

Hello,

I'd like to complete this RFC issue :
Special characters are not (all) allowed when using z.email(), such as quote (" ' ") :

"[...]local-parts may consist of any combination of
alphabetic characters, digits, or any of the special characters
! # $ % & ' * + - / = ? ^ _ ` . { | } ~"

( see https://www.rfc-editor.org/rfc/rfc3696)

@kernwig
Copy link

kernwig commented Mar 1, 2024

Connected with this, I just got a Zod validation error on the email address containing an apostrophe, which @camboui just pointed out is permitted.


I see that's covered by #2888 , which has a PR.

@aidasbui
Copy link

aidasbui commented Mar 11, 2024

Hi there! It seems like this is related, so I'll share.

I just found out that Zod allows commas in the local part of the email. Example: john,doe@domain.com. A little research tells me commas are only allowed if they're wrapped in quotes.

EDIT: my bad, looks like this is also fixed in this PR #3286.

@cbeardsmore
Copy link

Ran into this today with this example failing the regex but allowed in the RFC: email11/29@gmail.com. Forward slash is valid but does not match via Zod.

@fernandollisboa
Copy link
Contributor

It's an old comment, but it may still be relevant: #3218 (comment)

@colinhacks
Copy link
Owner

This is intended, see #2157 for the justification.

You can use .superRefine() for custom behavior here.

const emailRegex =
  /^(?!\.)(?!.*\.\.)([A-Z0-9_'+-\.]*)[A-Z0-9_'+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;

const emailSchema = z.string().superRefine((data, ctx) => {
  if (!emailRegex.test(data)) {
    ctx.addIssue({
      code: z.ZodIssueCode.invalid_string,
      message: "Invalid email address",
      validation: "email",
    });
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants