Skip to content

email() counts Unicode characters instead of UTF-8 octets for the local-part limit #573

Open

Description

@prathamchoksi

email() checks len(username_part) > 64, which measures Unicode characters rather than UTF-8 octets. The validator accepts non-ASCII local parts, so a value can pass this check while exceeding SMTP's 64-octet limit.

Reproduced on master at 22786cc (validators 0.36.0, Python 3.14):

from validators import email

local = "\u00fc" * 33
print(len(local))                 # 33 characters
print(len(local.encode("utf-8"))) # 66 octets
print(email(local + "@example.com")) # True

Expected: ValidationError, just as for a 65-character ASCII local part. The 32-character variant occupies exactly 64 UTF-8 octets and should continue to pass.

RFC 5321 section 4.5.3.1.1 specifies the local-part limit in octets, explicitly distinguishing it from character counts. This matters for callers validating internationalized addresses before storing or using them: values over that limit currently pass the input check.

The fix can be limited to measuring the UTF-8-encoded local part. Domain handling and the existing accepted character set need no change. I have verified regression cases for 64-byte ASCII, 64-byte Unicode, and mixed ASCII/Unicode local parts at and above the boundary.

Codex assisted with reproducing this behavior and preparing the fix and tests. The example above was executed locally.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions