diff --git a/gunicorn/glogging.py b/gunicorn/glogging.py index 88f0c133..e876aac8 100644 --- a/gunicorn/glogging.py +++ b/gunicorn/glogging.py @@ -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: diff --git a/tests/test_logger.py b/tests/test_logger.py index e719ce02..ed23e98d 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -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'] == '-'