Skip to content

Commit

Permalink
Check ValueError in url_validator
Browse files Browse the repository at this point in the history
If an invalid URL is provided to urlparse (e.g. `https://example.com]`)
it might raise a ValueError. This breaks the url_validator.
This PR aims to fix that and simply add the error msg if a ValueError
occurs.
  • Loading branch information
Stefan Oderbolz authored and smotornyuk committed Jun 16, 2019
1 parent cf50b15 commit 0bc91d8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
14 changes: 9 additions & 5 deletions ckan/logic/validators.py
Expand Up @@ -697,11 +697,15 @@ def url_validator(key, data, errors, context):
if not url:
return

pieces = urlparse.urlparse(url)
if all([pieces.scheme, pieces.netloc]) and \
set(pieces.netloc) <= set(string.letters + string.digits + '-.') and \
pieces.scheme in ['http', 'https']:
return
try:
pieces = urlparse.urlparse(url)
if all([pieces.scheme, pieces.netloc]) and \
set(pieces.netloc) <= set(string.letters + string.digits + '-.') and \
pieces.scheme in ['http', 'https']:
return
except ValueError:
# url is invalid
pass

errors[key].append(_('Please provide a valid URL'))

Expand Down
27 changes: 27 additions & 0 deletions ckan/tests/logic/test_validators.py
Expand Up @@ -611,4 +611,31 @@ def call_validator(*args, **kwargs):
errors[key] = []
call_validator(key, {key: password}, errors, None)


class TestUrlValidator(object):

def test_ok(self):
urls = ['http://example.com', 'https://example.com', 'https://example.com/path?test=1&key=2']
key = ('url',)

@t.does_not_modify_errors_dict
def call_validator(*args, **kwargs):
return validators.url_validator(*args, **kwargs)
for url in urls:
errors = factories.validator_errors_dict()
errors[key] = []
call_validator(key, {key: url}, errors, None)

def test_invalid(self):
urls = ['ftp://example.com', 'test123', 'https://example.com]']
key = ('url',)

@adds_message_to_errors_dict('Please provide a valid URL')
def call_validator(*args, **kwargs):
return validators.url_validator(*args, **kwargs)
for url in urls:
errors = factories.validator_errors_dict()
errors[key] = []
call_validator(key, {key: url}, errors, None)

# TODO: Need to test when you are not providing owner_org and the validator queries for the dataset with package_show

0 comments on commit 0bc91d8

Please sign in to comment.