Skip to content

Apply the strict local part length limit to the quoted form - #198

Closed
hemtilva wants to merge 1 commit into
JoshData:mainfrom
hemtilva:strict-quoted-local-length
Closed

hemtilva wants to merge 1 commit into
JoshData:mainfrom
hemtilva:strict-quoted-local-length

Conversation

@hemtilva

Copy link
Copy Markdown

Fixes #197.

Problem

strict=True checks the local part length (RFC 5321 4.5.3.1.1, 64 octets) on the unquoted, unescaped local part. When the local part is a quoted string, the local_part and normalized values that come back (which are what would be transmitted) also contain the two quotes and any backslash escapes that are still needed. As a result, strict=True, allow_quoted_local=True accepted local parts of up to about twice the limit, for example '"' + '\\"' * 32 + '"@example.com' (a 66-character local part).

Change

In validate_email_local_part, after a quoted-string local part is re-quoted and re-escaped, check the length again under strict. The error message is the same as the existing one ("The email address is too long before the @-sign (N character(s) too many).").

  • The existing early check is still there, so unquoted local parts behave exactly as before, with the same errors.
  • Because the check runs on the normalized (minimally quoted) form, unnecessary escapes in the input are not counted. For example, "\a\a...\a" with 64 as normalizes to an unquoted 64-character local part and is still accepted. This matches RFC 5321 4.1.2, which says all quoted forms are equivalent.
  • The NFC re-validation in validate_email calls the same function, so the check also applies to the normalized form.

Tests

  • test_email_invalid_syntax_strict_quoted: three addresses that pass without strict and are rejected with strict=True, each with an exact error message. They cover the quotes added because of a space, escaped quotes, and an escaped backslash.
  • test_email_valid_strict_quoted: a quoted local part that is exactly 64 characters including the quotes, and an over-escaped input that normalizes to 64 characters. Both are still accepted.

The three new invalid cases fail on main and pass with this change.

pytest -k "not network": 348 passed. flake8 --ignore=E501,E126,W503 email_validator tests: clean.

With strict=True, the 64-octet local part limit (RFC 5321 4.5.3.1.1)
was checked against the unquoted, unescaped local part. For a
quoted-string local part, the local part that is returned (and that
would be transmitted) also includes the surrounding quotes and any
backslash escapes that are still needed, so addresses whose local part
was up to twice the limit were accepted, e.g. 32 escaped quotes give a
66-character local part.

Check the length again after the local part is re-quoted.
Copilot AI balanced review requested due to automatic review settings November 15, 2026 22:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hemtilva

Copy link
Copy Markdown
Author

Fair enough, closing this. Sorry for the noise.

@hemtilva hemtilva closed this Nov 16, 2026
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

Successfully merging this pull request may close these issues.

strict=True accepts quoted local parts longer than 64 characters

2 participants