Email field zero width space #9862
Replies: 1 comment
|
Worth knowing before you patch anything in DRF: this isn't DRF-specific. Django's own validator accepts it too, so >>> from django.core.validators import validate_email
>>> validate_email("john@acme.com\u200b") # no exception
>>> validate_email("john@\u200bacme.com") # no exception>>> serializers.EmailField().run_validation("john@\u200bacme.com")
'john@\u200bacme.com'Both are accepted on DRF 3.18.0 / Django 5.2.17, matching what you saw on 3.16.1. So a fix in On the two halves of your suggestion, they're worth separating: Stripping in >>> "john@acme.com\u200b".strip() == "john@acme.com\u200b"
TrueSo this wouldn't be a tightening of existing behaviour — it'd be a new rule about which invisible characters get silently removed from every Rejecting in If you need this enforced now, a validator on the field does it without touching either project: import re
from rest_framework import serializers
ZERO_WIDTH = re.compile(r"[\u200b\u200c\u200d\ufeff]")
def no_zero_width(value):
if ZERO_WIDTH.search(value):
raise serializers.ValidationError("Email contains zero-width characters.")
email = serializers.EmailField(validators=[no_zero_width]) |
Uh oh!
There was an error while loading. Please reload this page.
EmailFieldallows a zero width space character to be included which is invalid.john@acme.com\u200band evenjohn@\u200bacme.comare allowed.I think that first of all
CharFieldshould be enhanced to strip the character from both ends of the string, and email field should be enhanced to prohibit it anywhere in the string.Tested on drf 3.16.1.
All reactions