Skip to content

Commit

Permalink
Fixed #12989 - Fixed verification of IDN URLs. Thanks to Fraser Nevet…
Browse files Browse the repository at this point in the history
…t for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12620 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jezdez committed Feb 28, 2010
1 parent d7abb33 commit 68f216a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
19 changes: 7 additions & 12 deletions django/core/validators.py
Expand Up @@ -58,23 +58,18 @@ def __call__(self, value):
except ValidationError, e: except ValidationError, e:
# Trivial case failed. Try for possible IDN domain # Trivial case failed. Try for possible IDN domain
if value: if value:
original = value
value = smart_unicode(value) value = smart_unicode(value)
splitted = urlparse.urlsplit(value) scheme, netloc, path, query, fragment = urlparse.urlsplit(value)
try: try:
netloc_ace = splitted[1].encode('idna') # IDN -> ACE netloc = netloc.encode('idna') # IDN -> ACE
except UnicodeError: # invalid domain part except UnicodeError: # invalid domain part
raise e raise e
value = value.replace(splitted[1], netloc_ace) url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
# If no URL path given, assume / super(URLValidator, self).__call__(url)
if not splitted[2]:
value += u'/'
super(URLValidator, self).__call__(value)
# After validation revert ACE encoded domain-part to
# original (IDN) value as suggested by RFC 3490
value = original
else: else:
raise raise
else:
url = value


if self.verify_exists: if self.verify_exists:
import urllib2 import urllib2
Expand All @@ -86,7 +81,7 @@ def __call__(self, value):
"User-Agent": self.user_agent, "User-Agent": self.user_agent,
} }
try: try:
req = urllib2.Request(value, None, headers) req = urllib2.Request(url, None, headers)
u = urllib2.urlopen(req) u = urllib2.urlopen(req)
except ValueError: except ValueError:
raise ValidationError(_(u'Enter a valid URL.'), code='invalid') raise ValidationError(_(u'Enter a valid URL.'), code='invalid')
Expand Down
7 changes: 7 additions & 0 deletions tests/regressiontests/forms/fields.py
Expand Up @@ -531,6 +531,13 @@ def test_urlfield_39(self):
f.clean('http://google.com/we-love-microsoft.html') # good domain, bad page f.clean('http://google.com/we-love-microsoft.html') # good domain, bad page
except ValidationError, e: except ValidationError, e:
self.assertEqual("[u'This URL appears to be a broken link.']", str(e)) self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
# Valid and existent IDN
self.assertEqual(u'http://\u05e2\u05d1\u05e8\u05d9\u05ea.idn.icann.org/', f.clean(u'http://עברית.idn.icann.org/'))
# Valid but non-existent IDN
try:
f.clean(u'http://broken.עברית.idn.icann.org/')
except ValidationError, e:
self.assertEqual("[u'This URL appears to be a broken link.']", str(e))


def test_urlfield_40(self): def test_urlfield_40(self):
f = URLField(verify_exists=True, required=False) f = URLField(verify_exists=True, required=False)
Expand Down

0 comments on commit 68f216a

Please sign in to comment.