Allow hostnames with underscores when parsing URLs (#324)

This commit is contained in:
Sebastian Thomschke 2020-11-24 01:36:08 +01:00 committed by GitHub
parent 2c2722f61f
commit fa05cfa81e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 5 deletions

5
.gitignore vendored
View File

@ -67,3 +67,8 @@ target/
#PyCharm #PyCharm
.idea .idea
#PyDev (Eclipse)
.project
.pydevproject
.settings

View File

@ -120,9 +120,9 @@ GET_EMAIL_RE = re.compile(
r'(?P<email>(?P<userid>[a-z0-9$%=_~-]+' r'(?P<email>(?P<userid>[a-z0-9$%=_~-]+'
r'(?:\.[a-z0-9$%+=_~-]+)' r'(?:\.[a-z0-9$%+=_~-]+)'
r'*)@(?P<domain>(' r'*)@(?P<domain>('
r'(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+' r'(?:[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?\.)+'
r'[a-z0-9](?:[a-z0-9-]*[a-z0-9]))|' r'[a-z0-9](?:[a-z0-9_-]*[a-z0-9]))|'
r'[a-z0-9][a-z0-9-]{5,})))' r'[a-z0-9][a-z0-9_-]{5,})))'
r'\s*>?', re.IGNORECASE) r'\s*>?', re.IGNORECASE)
# Regular expression used to extract a phone number # Regular expression used to extract a phone number
@ -232,9 +232,12 @@ def is_hostname(hostname, ipv4=True, ipv6=True):
# - Hostnames can ony be comprised of alpha-numeric characters and the # - Hostnames can ony be comprised of alpha-numeric characters and the
# hyphen (-) character. # hyphen (-) character.
# - Hostnames can not start with the hyphen (-) character. # - Hostnames can not start with the hyphen (-) character.
# - as a workaround for https://github.com/docker/compose/issues/229 to
# being able to address services in other stacks, we also allow
# underscores in hostnames
# - labels can not exceed 63 characters # - labels can not exceed 63 characters
allowed = re.compile( allowed = re.compile(
r'(?!-)[a-z0-9][a-z0-9-]{1,62}(?<!-)$', r'^[a-z0-9][a-z0-9_-]{1,62}(?<!-)$',
re.IGNORECASE, re.IGNORECASE,
) )

View File

@ -532,6 +532,8 @@ def test_is_hostname():
assert utils.is_hostname('yahoo.ca.') == 'yahoo.ca' assert utils.is_hostname('yahoo.ca.') == 'yahoo.ca'
assert utils.is_hostname('valid-dashes-in-host.ca') == \ assert utils.is_hostname('valid-dashes-in-host.ca') == \
'valid-dashes-in-host.ca' 'valid-dashes-in-host.ca'
assert utils.is_hostname('valid-underscores_in_host.ca') == \
'valid-underscores_in_host.ca'
# Invalid Hostnames # Invalid Hostnames
assert utils.is_hostname('-hostname.that.starts.with.a.dash') is False assert utils.is_hostname('-hostname.that.starts.with.a.dash') is False
@ -539,7 +541,6 @@ def test_is_hostname():
assert utils.is_hostname(' spaces ') is False assert utils.is_hostname(' spaces ') is False
assert utils.is_hostname(' ') is False assert utils.is_hostname(' ') is False
assert utils.is_hostname('') is False assert utils.is_hostname('') is False
assert utils.is_hostname('valid-underscores_in_host.ca') is False
# Valid IPv4 Addresses # Valid IPv4 Addresses
assert utils.is_hostname('127.0.0.1') == '127.0.0.1' assert utils.is_hostname('127.0.0.1') == '127.0.0.1'
@ -625,6 +626,14 @@ def test_is_email():
assert 'test' == results['user'] assert 'test' == results['user']
assert '' == results['label'] assert '' == results['label']
results = utils.is_email('test@my-valid_host.com')
assert '' == results['name']
assert 'test@my-valid_host.com' == results['email']
assert 'test@my-valid_host.com' == results['full_email']
assert 'my-valid_host.com' == results['domain']
assert 'test' == results['user']
assert '' == results['label']
results = utils.is_email('tag+test@gmail.com') results = utils.is_email('tag+test@gmail.com')
assert '' == results['name'] assert '' == results['name']
assert 'test@gmail.com' == results['email'] assert 'test@gmail.com' == results['email']