urls: improve removal of credentials in repository header.

- fixes #5623
This commit is contained in:
Marcin Lulek 2020-06-22 21:15:44 +02:00
parent 95e41e0e69
commit ca3d6a3816
2 changed files with 23 additions and 64 deletions

View file

@ -587,51 +587,17 @@ def cleaned_uri(uri):
return urllib.quote(uri, safe='@$:/')
def uri_filter(uri):
"""
Removes user:password from given url string
:param uri:
:rtype: unicode
:returns: filtered list of strings
"""
if not uri:
return ''
proto = ''
for pat in ('https://', 'http://'):
if uri.startswith(pat):
uri = uri[len(pat):]
proto = pat
break
# remove passwords and username
uri = uri[uri.find('@') + 1:]
# get the port
cred_pos = uri.find(':')
if cred_pos == -1:
host, port = uri, None
else:
host, port = uri[:cred_pos], uri[cred_pos + 1:]
return filter(None, [proto, host, port])
def credentials_filter(uri):
"""
Returns a url with removed credentials
:param uri:
"""
import urlobject
url_obj = urlobject.URLObject(cleaned_uri(uri))
url_obj = url_obj.without_password().without_username()
uri = uri_filter(uri)
# check if we have port
if len(uri) > 2 and uri[2]:
uri[2] = ':' + uri[2]
return ''.join(uri)
return url_obj
def get_host_info(request):

View file

@ -38,36 +38,29 @@ from rhodecode.lib.utils2 import AttributeDict
from rhodecode.model.db import Repository, CacheKey
def _urls_for_proto(proto):
return [
('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
'%s://127.0.0.1' % proto),
('%s://marcink@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
'%s://127.0.0.1' % proto),
('%s://marcink:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
'%s://127.0.0.1' % proto),
('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'],
'%s://127.0.0.1:8080' % proto),
('%s://domain.org' % proto, ['%s://' % proto, 'domain.org'],
'%s://domain.org' % proto),
('%s://user:pass@domain.org:8080' % proto,
['%s://' % proto, 'domain.org', '8080'],
'%s://domain.org:8080' % proto),
TEST_URLS = [
('127.0.0.1', '127.0.0.1'),
('marcink@127.0.0.1', '127.0.0.1'),
('marcink:pass@127.0.0.1', '127.0.0.1'),
('marcink@domain.name:pass@127.0.0.1', '127.0.0.1'),
('127.0.0.1:8080', '127.0.0.1:8080'),
('marcink@127.0.0.1:8080', '127.0.0.1:8080'),
('marcink:pass@127.0.0.1:8080', '127.0.0.1:8080'),
('marcink@domain.name:pass@127.0.0.1:8080', '127.0.0.1:8080'),
('domain.org', 'domain.org'),
('user:pass@domain.org:8080', 'domain.org:8080'),
('user@domain.org:pass@domain.org:8080', 'domain.org:8080'),
]
TEST_URLS = _urls_for_proto('http') + _urls_for_proto('https')
@pytest.mark.parametrize("test_url, expected, expected_creds", TEST_URLS)
def test_uri_filter(test_url, expected, expected_creds):
from rhodecode.lib.utils2 import uri_filter
assert uri_filter(test_url) == expected
@pytest.mark.parametrize("test_url, expected, expected_creds", TEST_URLS)
def test_credentials_filter(test_url, expected, expected_creds):
@pytest.mark.parametrize("protocol", ['http://', 'https://'])
@pytest.mark.parametrize("test_url, expected", TEST_URLS)
def test_credentials_filter(protocol, test_url, expected):
from rhodecode.lib.utils2 import credentials_filter
assert credentials_filter(test_url) == expected_creds
test_url = protocol + test_url
assert credentials_filter(test_url) == protocol + expected
@pytest.mark.parametrize("str_bool, expected", [