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

Commit 0f20d37

Browse files
author
Sean Quah
committed
Fix error in is_mine_id when encountering a malformed ID
Fixes #13040. Signed-off-by: Sean Quah <seanq@matrix.org>
1 parent b7e4bfd commit 0f20d37

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

changelog.d/13746.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long standing bug where Synapse would fail to handle malformed user IDs or room aliases gracefully in certain cases.

synapse/server.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,17 @@ def is_mine(self, domain_specific_string: DomainSpecificString) -> bool:
341341
return domain_specific_string.domain == self.hostname
342342

343343
def is_mine_id(self, string: str) -> bool:
344-
return string.split(":", 1)[1] == self.hostname
344+
"""Determines whether a user ID or room alias originates from this homeserver.
345+
346+
Returns:
347+
`True` if the hostname part of the user ID or room alias matches this
348+
homeserver.
349+
`False` otherwise, or if the user ID or room alias is malformed.
350+
"""
351+
localpart_hostname = string.split(":", 1)
352+
if len(localpart_hostname) < 2:
353+
return False
354+
return localpart_hostname[1] == self.hostname
345355

346356
@cache_in_self
347357
def get_clock(self) -> Clock:

tests/test_types.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,29 @@
1313
# limitations under the License.
1414

1515
from synapse.api.errors import SynapseError
16-
from synapse.types import RoomAlias, UserID, map_username_to_mxid_localpart
16+
from synapse.types import RoomAlias, UserID, get_domain_from_id, get_localpart_from_id, map_username_to_mxid_localpart
1717

1818
from tests import unittest
1919

2020

21+
class IsMineIDTests(unittest.HomeserverTestCase):
22+
def test_is_mine_id(self) -> None:
23+
self.assertTrue(self.hs.is_mine_id("@user:test"))
24+
self.assertTrue(self.hs.is_mine_id("#room:test"))
25+
self.assertTrue(self.hs.is_mine_id("invalid:test"))
26+
27+
self.assertFalse(self.hs.is_mine_id("@user:test\0"))
28+
self.assertFalse(self.hs.is_mine_id("@user"))
29+
30+
def test_two_colons(self) -> None:
31+
"""Test handling of IDs containing more than one colon."""
32+
# The domain starts after the first colon.
33+
# These functions must interpret things consistently.
34+
self.assertFalse(self.hs.is_mine_id("@user:test:test"))
35+
self.assertEqual("user", get_localpart_from_id("@user:test:test"))
36+
self.assertEqual("test:test", get_domain_from_id("@user:test:test"))
37+
38+
2139
class UserIDTestCase(unittest.HomeserverTestCase):
2240
def test_parse(self):
2341
user = UserID.from_string("@1234abcd:test")

0 commit comments

Comments
 (0)