register: Not all clients send a User-Agent header.

In 1fce1c3c73, we added logic to parse
the User-Agent in /register requests; this logic crashed if an HTTP request
was missing that header.

Includes a test for `/register` with no user agent passed; this should catch
similar regressions in the future.

Co-authored-by: Mateusz Mandera <mateusz.mandera@zulip.com>
This commit is contained in:
Alex Vandiver 2022-11-10 19:21:04 -05:00 committed by GitHub
parent 0e0a910304
commit 1f18fa6580
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 2 deletions

View File

@ -140,7 +140,7 @@ def is_unsupported_browser(user_agent: str) -> Tuple[bool, Optional[str]]:
return (False, browser_name) return (False, browser_name)
def is_pronouns_field_type_supported(user_agent_str: str) -> bool: def is_pronouns_field_type_supported(user_agent_str: Optional[str]) -> bool:
# In order to avoid users having a bad experience with these # In order to avoid users having a bad experience with these
# custom profile fields disappearing after applying migration # custom profile fields disappearing after applying migration
# 0421_migrate_pronouns_custom_profile_fields, we provide this # 0421_migrate_pronouns_custom_profile_fields, we provide this
@ -150,6 +150,9 @@ def is_pronouns_field_type_supported(user_agent_str: str) -> bool:
# TODO/compatibility(7.0): Because this is a relatively minor # TODO/compatibility(7.0): Because this is a relatively minor
# detail, we can remove this compatibility hack once most users # detail, we can remove this compatibility hack once most users
# have upgraded to a sufficiently new mobile client. # have upgraded to a sufficiently new mobile client.
if user_agent_str is None:
return True
user_agent = parse_user_agent(user_agent_str) user_agent = parse_user_agent(user_agent_str)
if user_agent["name"] != "ZulipMobile": if user_agent["name"] != "ZulipMobile":
return True return True

View File

@ -50,6 +50,10 @@ from zerver.views.events_register import (
class EventsEndpointTest(ZulipTestCase): class EventsEndpointTest(ZulipTestCase):
def test_events_register_without_user_agent(self) -> None:
result = self.client_post("/json/register", skip_user_agent=True)
self.assert_json_success(result)
def test_events_register_endpoint(self) -> None: def test_events_register_endpoint(self) -> None:
# This test is intended to get minimal coverage on the # This test is intended to get minimal coverage on the

View File

@ -124,7 +124,9 @@ def events_register_backend(
client = RequestNotes.get_notes(request).client client = RequestNotes.get_notes(request).client
assert client is not None assert client is not None
pronouns_field_type_supported = is_pronouns_field_type_supported(request.headers["User-Agent"]) pronouns_field_type_supported = is_pronouns_field_type_supported(
request.headers.get("User-Agent")
)
ret = do_events_register( ret = do_events_register(
user_profile, user_profile,
realm, realm,