summary refs log tree commit diff
path: root/synapse/http/site.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2020-10-23 17:12:59 +0100
committerGitHub <noreply@github.com>2020-10-23 17:12:59 +0100
commitc850dd9a8e4e4f78fbe0b44686f3824b901236f6 (patch)
tree6ba78d1e55160a9f0cd70a40c88f6100abc00b5a /synapse/http/site.py
parentFix email notifications for invites without local state. (#8627) (diff)
downloadsynapse-c850dd9a8e4e4f78fbe0b44686f3824b901236f6.tar.xz
Fix handling of User-Agent headers with bad utf-8. (#8632)
Diffstat (limited to '')
-rw-r--r--synapse/http/site.py16
1 files changed, 9 insertions, 7 deletions
diff --git a/synapse/http/site.py b/synapse/http/site.py
index 6e79b47828..ca673028e4 100644
--- a/synapse/http/site.py
+++ b/synapse/http/site.py
@@ -109,8 +109,14 @@ class SynapseRequest(Request):
             method = self.method.decode("ascii")
         return method
 
-    def get_user_agent(self):
-        return self.requestHeaders.getRawHeaders(b"User-Agent", [None])[-1]
+    def get_user_agent(self, default: str) -> str:
+        """Return the last User-Agent header, or the given default.
+        """
+        user_agent = self.requestHeaders.getRawHeaders(b"User-Agent", [None])[-1]
+        if user_agent is None:
+            return default
+
+        return user_agent.decode("ascii", "replace")
 
     def render(self, resrc):
         # this is called once a Resource has been found to serve the request; in our
@@ -274,11 +280,7 @@ class SynapseRequest(Request):
         # 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", "replace")
-        else:
-            user_agent = "-"
+        user_agent = self.get_user_agent("-")
 
         code = str(self.code)
         if not self.finished: