Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix handling of User-Agent headers with bad utf-8. #8632

Merged
merged 5 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/8632.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix handling of User-Agent headers that are invalid UTF-8, which caused user agents of users to not get correctly recorded.
4 changes: 1 addition & 3 deletions synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@ async def get_user_by_req(
"""
try:
ip_addr = self.hs.get_ip_from_request(request)
user_agent = request.requestHeaders.getRawHeaders(
b"User-Agent", default=[b""]
)[0].decode("ascii", "surrogateescape")
user_agent = request.get_user_agent("")

access_token = self.get_access_token_from_request(request)

Expand Down
4 changes: 1 addition & 3 deletions synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,7 @@ async def check_ui_auth(
# authentication flow.
await self.store.set_ui_auth_clientdict(sid, clientdict)

user_agent = request.requestHeaders.getRawHeaders(b"User-Agent", default=[b""])[
0
].decode("ascii", "surrogateescape")
user_agent = request.get_user_agent(" ")
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved

await self.store.add_user_agent_ip_to_ui_auth_session(
session.session_id, user_agent, clientip
Expand Down
4 changes: 1 addition & 3 deletions synapse/handlers/cas_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ async def handle_ticket(
else:
if not registered_user_id:
# Pull out the user-agent and IP from the request.
user_agent = request.requestHeaders.getRawHeaders(
b"User-Agent", default=[b""]
)[0].decode("ascii", "surrogateescape")
user_agent = request.get_user_agent("")
ip_address = self.hs.get_ip_from_request(request)

registered_user_id = await self._registration_handler.register_user(
Expand Down
4 changes: 1 addition & 3 deletions synapse/handlers/oidc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,7 @@ async def handle_oidc_callback(self, request: SynapseRequest) -> None:
return

# Pull out the user-agent and IP from the request.
user_agent = request.requestHeaders.getRawHeaders(b"User-Agent", default=[b""])[
0
].decode("ascii", "surrogateescape")
user_agent = request.get_user_agent("")
ip_address = self.hs.get_ip_from_request(request)

# Call the mapper to register/login the user
Expand Down
4 changes: 1 addition & 3 deletions synapse/handlers/saml_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,7 @@ async def handle_saml_response(self, request: SynapseRequest) -> None:
return

# Pull out the user-agent and IP from the request.
user_agent = request.requestHeaders.getRawHeaders(b"User-Agent", default=[b""])[
0
].decode("ascii", "surrogateescape")
user_agent = request.get_user_agent("")
ip_address = self.hs.get_ip_from_request(request)

# Call the mapper to register/login the user
Expand Down
16 changes: 9 additions & 7 deletions synapse/http/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ def get_method(self):
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
Expand Down Expand Up @@ -274,11 +280,7 @@ def _finished_processing(self):
# 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:
Expand Down