Handle UnicodeDecodeError in _get_user() (#1684)

Fixes #1683
This commit is contained in:
Jacob Eiting 2018-01-17 17:29:25 -08:00 committed by Berker Peksag
parent 76eaa0805b
commit f12ef22281
2 changed files with 20 additions and 4 deletions

View File

@ -471,10 +471,7 @@ class Logger(object):
if PY3: # b64decode returns a byte string in Python 3
auth = auth.decode('utf-8')
auth = auth.split(":", 1)
except TypeError as exc:
self.debug("Couldn't get username: %s", exc)
return user
except binascii.Error as exc:
except (TypeError, binascii.Error, UnicodeDecodeError) as exc:
self.debug("Couldn't get username: %s", exc)
return user
if len(auth) == 2:

View File

@ -46,3 +46,22 @@ def test_get_username_from_basic_auth_header():
logger = Logger(Config())
atoms = logger.atoms(response, request, environ, datetime.timedelta(seconds=1))
assert atoms['u'] == 'brk0v'
def test_get_username_handles_malformed_basic_auth_header():
"""Should catch a malformed auth header"""
request = SimpleNamespace(headers=())
response = SimpleNamespace(
status='200', response_length=1024, sent=1024,
headers=(('Content-Type', 'text/plain'),),
)
environ = {
'REQUEST_METHOD': 'GET', 'RAW_URI': '/my/path?foo=bar',
'PATH_INFO': '/my/path', 'QUERY_STRING': 'foo=bar',
'SERVER_PROTOCOL': 'HTTP/1.1',
'HTTP_AUTHORIZATION': 'Basic ixsTtkKzIpVTncfQjbBcnoRNoDfbnaXG',
}
logger = Logger(Config())
atoms = logger.atoms(response, request, environ, datetime.timedelta(seconds=1))
assert atoms['u'] == '-'