Skip to content

Commit 4153b5c

Browse files
mateuszmanderatimabbott
authored andcommitted
remote_server: Improve uuid validation at the server/register endpoint.
As explained in the comments in the code, just doing UUID(string) and catching ValueError is not enough, because the uuid library sometimes tries to modify the string to convert it into a valid UUID: >>> a = '18cedb98-5222-5f34-50a9-fc418e1ba972' >>> uuid.UUID(a, version=4) UUID('18cedb98-5222-4f34-90a9-fc418e1ba972')
1 parent 9d85f64 commit 4153b5c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

zerver/tests/test_push_notifications.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,6 +2470,14 @@ def test_push_signup_invalid_zulip_org_id(self) -> None:
24702470
result = self.client_post("/api/v1/remotes/server/register", request)
24712471
self.assert_json_error(result, "Invalid UUID")
24722472

2473+
# This looks mostly like a proper UUID, but isn't actually a valid UUIDv4,
2474+
# which makes it slip past a basic validation via initializing uuid.UUID with it.
2475+
# Thus we should test this scenario separately.
2476+
zulip_org_id = "18cedb98-5222-5f34-50a9-fc418e1ba972"
2477+
request["zulip_org_id"] = zulip_org_id
2478+
result = self.client_post("/api/v1/remotes/server/register", request)
2479+
self.assert_json_error(result, "Invalid UUID")
2480+
24732481
def test_push_signup_success(self) -> None:
24742482
zulip_org_id = str(uuid.uuid4())
24752483
zulip_org_key = get_random_string(64)

zilencer/views.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ def validate_entity(entity: Union[UserProfile, RemoteZulipServer]) -> RemoteZuli
5353

5454
def validate_uuid(uuid: str) -> None:
5555
try:
56-
UUID(uuid, version=4)
56+
uuid_object = UUID(uuid, version=4)
57+
# The UUID initialization under some circumstances will modify the uuid
58+
# string to create a valid UUIDv4, instead of raising a ValueError.
59+
# The submitted uuid needing to be modified means it's invalid, so
60+
# we need to check for that condition.
61+
if str(uuid_object) != uuid:
62+
raise ValidationError(err_("Invalid UUID"))
5763
except ValueError:
5864
raise ValidationError(err_("Invalid UUID"))
5965

0 commit comments

Comments
 (0)