forms: Set EmailField max_length to match Django Model.EmailField.

Django's Model.EmailField's default max_length is 254 characters,
while the Form.EmailField's default max length is 320 characters.
The longer valid length for form email fields raises an error
when an email with over 254 characters is validated and the server
attempts to create a preregistration user or realm.

Sets the max length on current form EmailFields to match the max
length on corresponding email fields in the database.

For the form MultiEmailField used on the find account/team page,
we don't need to set the max length to 254, but we don't expect
any emails longer than that to match any existing user accounts.
Adds tests in `zerver/tests/test_signup.py` for form submissions
with long email addresses.
This commit is contained in:
Lauryn Menard
2025-10-09 17:03:25 +02:00
committed by Alex Vandiver
parent fdcfafd13d
commit b42d3e77e7
2 changed files with 42 additions and 3 deletions

View File

@@ -68,6 +68,11 @@ DEACTIVATED_ACCOUNT_ERROR = gettext_lazy(
)
PASSWORD_TOO_WEAK_ERROR = gettext_lazy("The password is too weak.")
# Set Form.EmailField to match the default max_length on Model.EmailField,
# can be removed when https://code.djangoproject.com/ticket/35119 is
# completed.
EMAIL_MAX_LENGTH = 254
class OverridableValidationError(ValidationError):
pass
@@ -242,7 +247,7 @@ class ToSForm(forms.Form):
class HomepageForm(forms.Form):
email = forms.EmailField()
email = forms.EmailField(max_length=EMAIL_MAX_LENGTH)
def __init__(self, *args: Any, **kwargs: Any) -> None:
self.realm = kwargs.pop("realm", None)
@@ -321,7 +326,9 @@ class ImportRealmOwnerSelectionForm(forms.Form):
class RealmCreationForm(RealmDetailsForm):
# This form determines whether users can create a new realm.
email = forms.EmailField(validators=[email_not_system_bot, email_is_not_disposable])
email = forms.EmailField(
validators=[email_not_system_bot, email_is_not_disposable], max_length=EMAIL_MAX_LENGTH
)
import_from = forms.ChoiceField(
choices=PreregistrationRealm.IMPORT_FROM_CHOICES,
required=False,
@@ -539,7 +546,7 @@ def rate_limit_password_reset_form_by_email(email: str) -> None:
class CreateUserForm(forms.Form):
full_name = forms.CharField(max_length=100)
email = forms.EmailField()
email = forms.EmailField(max_length=EMAIL_MAX_LENGTH)
class OurAuthenticationForm(AuthenticationForm):