Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error messages in uris.py #2768

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions udata/translations/fr/LC_MESSAGES/udata.po
Original file line number Diff line number Diff line change
Expand Up @@ -1555,3 +1555,34 @@ msgstr "Assez réutilisé"
msgid "Heavily reused"
msgstr "Souvent réutilisé"

#: udata/uris.py:53
msgid "URL invalide : %(reason)s"
msgstr ""

#: udata/uris.py:103
msgid "Schéma invalide {0}, schémas autorisés : {1}"
msgstr ""

#: udata/uris.py:106
msgid "Les informations d'identification dans l'URL ne sont pas autorisées"
msgstr ""

#: udata/uris.py:110
msgid "TLD invalide {0}"
msgstr ""

#: udata/uris.py:119
msgid "{0} est une IP multicast"
msgstr ""

#: udata/uris.py:121
msgid "{0} est un masque IP"
msgstr ""

#: udata/uris.py:125
msgid "est une URL locale"
msgstr ""

#: udata/uris.py:128
msgid "est une URL privée"
msgstr ""
31 changes: 31 additions & 0 deletions udata/translations/udata.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1575,3 +1575,34 @@ msgstr ""
msgid "Heavily reused"
msgstr ""

#: udata/uris.py:53
msgid "Invalid URL: %(reason)s"
msgstr ""

#: udata/uris.py:103
msgid "Invalid scheme {0}, allowed schemes: {1}"
msgstr ""

#: udata/uris.py:106
msgid "Credentials in URL are not allowed"
msgstr ""

#: udata/uris.py:110
msgid "Invalid TLD {0}"
msgstr ""

#: udata/uris.py:119
msgid "{0} is a multicast IP"
msgstr ""

#: udata/uris.py:121
msgid "{0} is a mask IP"
msgstr ""

#: udata/uris.py:125
msgid "is a local URL"
msgstr ""

#: udata/uris.py:128
msgid "is a private URL"
msgstr ""
19 changes: 9 additions & 10 deletions udata/uris.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from werkzeug.routing import BuildError
from flask import current_app, url_for
from netaddr import IPAddress, AddrFormatError
from udata.i18n import _

from udata.settings import Defaults

Expand Down Expand Up @@ -49,9 +50,7 @@ def error(url, reason=None):
__tracebackhide__ = True
if not isinstance(url, str):
url = url.decode('utf8')
msg = 'Invalid URL "{0}"'.format(url)
if reason:
msg = ': '.join((msg, reason))
msg = _('Invalid URL: %(reason)s', reason = reason)
raise ValidationError(msg.encode('utf8'))


Expand Down Expand Up @@ -101,14 +100,14 @@ def validate(url, schemes=None, tlds=None, private=None, local=None,

scheme = (match.group('scheme') or '').lower()
if scheme and scheme not in schemes:
error(url, 'Invalid scheme {0}'.format(scheme))
error(url, _('Invalid scheme {0}, allowed schemes: {1}').format(scheme, ", ".join(schemes)))

if not credentials and match.group('credentials'):
error(url, 'Credentials in URL are not allowed')
error(url, _('Credentials in URL are not allowed'))

tld = match.group('tld')
if tld and tld not in tlds and idna(tld) not in tlds:
error(url, 'Invalid TLD {0}'.format(tld))
error(url, _('Invalid TLD {0}').format(tld))

ip = match.group('ipv6') or match.group('ipv4')
if ip:
Expand All @@ -117,15 +116,15 @@ def validate(url, schemes=None, tlds=None, private=None, local=None,
except AddrFormatError:
error(url)
if ip.is_multicast():
error(url, '{0} is a multicast IP'.format(ip))
error(url, _('{0} is a multicast IP').format(ip))
elif not ip.is_loopback() and ip.is_hostmask() or ip.is_netmask():
error(url, '{0} is a mask IP'.format(ip))
error(url, _('{0} is a mask IP').format(ip))

if not local:
if ip and ip.is_loopback() or match.group('localhost'):
error(url, 'is a local URL')
error(url, _('is a local URL'))

if not private and ip and ip.is_private():
error(url, 'is a private URL')
error(url, _('is a private URL'))

return url