diff options
author | Richard van der Hoff <richard@matrix.org> | 2018-01-16 18:25:28 +0000 |
---|---|---|
committer | Richard van der Hoff <richard@matrix.org> | 2018-01-17 14:28:54 +0000 |
commit | a5213df1f7448c1810e6ae842448c81abe57ba69 (patch) | |
tree | 0526323ebbb920ed95b9e6a93b1cca8a4005deba /synapse/handlers/devicemessage.py | |
parent | Merge pull request #2792 from matrix-org/rav/optimise_logging_context (diff) | |
download | synapse-a5213df1f7448c1810e6ae842448c81abe57ba69.tar.xz |
Sanity checking for user ids
Check the user_id passed to a couple of APIs for validity, to avoid "IndexError: list index out of range" exception which looks scary and results in a 500 rather than a more useful error. Fixes #1432, among other things
Diffstat (limited to 'synapse/handlers/devicemessage.py')
-rw-r--r-- | synapse/handlers/devicemessage.py | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/synapse/handlers/devicemessage.py b/synapse/handlers/devicemessage.py index f7fad15c62..d996aa90bb 100644 --- a/synapse/handlers/devicemessage.py +++ b/synapse/handlers/devicemessage.py @@ -17,7 +17,8 @@ import logging from twisted.internet import defer -from synapse.types import get_domain_from_id +from synapse.api.errors import SynapseError +from synapse.types import get_domain_from_id, UserID from synapse.util.stringutils import random_string @@ -33,7 +34,7 @@ class DeviceMessageHandler(object): """ self.store = hs.get_datastore() self.notifier = hs.get_notifier() - self.is_mine_id = hs.is_mine_id + self.is_mine = hs.is_mine self.federation = hs.get_federation_sender() hs.get_replication_layer().register_edu_handler( @@ -52,6 +53,12 @@ class DeviceMessageHandler(object): message_type = content["type"] message_id = content["message_id"] for user_id, by_device in content["messages"].items(): + # we use UserID.from_string to catch invalid user ids + if not self.is_mine(UserID.from_string(user_id)): + logger.warning("Request for keys for non-local user %s", + user_id) + raise SynapseError(400, "Not a user here") + messages_by_device = { device_id: { "content": message_content, @@ -77,7 +84,8 @@ class DeviceMessageHandler(object): local_messages = {} remote_messages = {} for user_id, by_device in messages.items(): - if self.is_mine_id(user_id): + # we use UserID.from_string to catch invalid user ids + if self.is_mine(UserID.from_string(user_id)): messages_by_device = { device_id: { "content": message_content, |