From ef7c49100ccd5e59437fc4d60ac1b1934dc29465 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Mon, 2 Jul 2018 13:31:22 +0100 Subject: [PATCH 1/2] Fix bug caused by logging raw bytes and unicode --- synapse/http/site.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/synapse/http/site.py b/synapse/http/site.py index 74a752d6cf1d..14ea9c21c817 100644 --- a/synapse/http/site.py +++ b/synapse/http/site.py @@ -107,13 +107,28 @@ def _finished_processing(self): end_time = time.time() + # need to decode as it could be raw utf-8 bytes + # from a IDN servname in an auth header + authenticated_entity = self.authenticated_entity + if authenticated_entity is not None: + authenticated_entity = authenticated_entity.decode("utf-8") + + # ...or could be raw utf-8 bytes in the User-Agent header. + # N.B. if you don't do this, the logger explodes cryptically + # with maximum recursion trying to log errors about + # the charset problem. + # c.f. https://github.com/matrix-org/synapse/issues/3471 + user_agent = self.get_user_agent() + if user_agent is not None: + user_agent = user_agent.decode("utf-8") + self.site.access_logger.info( "%s - %s - {%s}" " Processed request: %.3fsec (%.3fsec, %.3fsec) (%.3fsec/%.3fsec/%d)" " %sB %s \"%s %s %s\" \"%s\" [%d dbevts]", self.getClientIP(), self.site.site_tag, - self.authenticated_entity, + authenticated_entity, end_time - self.start_time, ru_utime, ru_stime, @@ -125,7 +140,7 @@ def _finished_processing(self): self.method, self.get_redacted_uri(), self.clientproto, - self.get_user_agent(), + user_agent, evt_db_fetch_count, ) From 36bbe3a5fc466ab2664756482b3deecedbaccb84 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Mon, 2 Jul 2018 13:35:27 +0100 Subject: [PATCH 2/2] Newsfile --- changelog.d/3475.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/3475.bugfix diff --git a/changelog.d/3475.bugfix b/changelog.d/3475.bugfix new file mode 100644 index 000000000000..1308931191fc --- /dev/null +++ b/changelog.d/3475.bugfix @@ -0,0 +1 @@ +Fix bug where synapse would explode when receiving unicode in HTTP User-Agent header