This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate device_keys for C-S /keys/query requests
Closes #10354 A small, not particularly critical fix. I'm interested in seeing if we can find a more systematic approach though. Wishlist: 1. Declaratively specify the data we expect 2. Automatic validation 3. Gradually roll this out across synapse 4. don't eat too many CPU cycles 5. type hints available to Python to mypy/PyCharm etc can make use of the data A quick search mentions jsonschema, fastjsonschema, pydantic. Attrs has this in but I think it's quite verbose to get validation?
- Loading branch information
David Robertson
committed
Aug 12, 2021
1 parent
4a76d01
commit 427c0de
Showing
3 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from http import HTTPStatus | ||
|
||
from synapse.api.errors import Codes | ||
from synapse.rest import admin | ||
from synapse.rest.client.v1 import login | ||
from synapse.rest.client.v2_alpha import keys | ||
from tests import unittest | ||
|
||
|
||
class KeyQueryTestCase(unittest.HomeserverTestCase): | ||
servlets = [ | ||
keys.register_servlets, | ||
admin.register_servlets_for_client_rest_resource, | ||
login.register_servlets, | ||
] | ||
|
||
def test_rejects_device_id_ice_key_outside_of_list(self): | ||
self.register_user("alice", "wonderland") | ||
alice_token = self.login("alice", "wonderland") | ||
bob = self.register_user("bob", "uncle") | ||
channel = self.make_request( | ||
"POST", | ||
"/_matrix/client/r0/keys/query", | ||
{ | ||
"device_keys": { | ||
bob: "device_id1", | ||
}, | ||
}, | ||
alice_token, | ||
) | ||
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result) | ||
self.assertEqual( | ||
channel.json_body["errcode"], | ||
Codes.BAD_JSON, | ||
channel.result, | ||
) | ||
|
||
def test_rejects_device_key_given_as_map_to_bool(self): | ||
self.register_user("alice", "wonderland") | ||
alice_token = self.login("alice", "wonderland") | ||
bob = self.register_user("bob", "uncle") | ||
channel = self.make_request( | ||
"POST", | ||
"/_matrix/client/r0/keys/query", | ||
{ | ||
"device_keys": { | ||
bob: { | ||
"device_id1": True, | ||
}, | ||
}, | ||
}, | ||
alice_token, | ||
) | ||
|
||
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result) | ||
self.assertEqual( | ||
channel.json_body["errcode"], | ||
Codes.BAD_JSON, | ||
channel.result, | ||
) | ||
|
||
def test_requires_device_key(self): | ||
"""`device_keys` is required. We should complain if it's missing.""" | ||
self.register_user("alice", "wonderland") | ||
alice_token = self.login("alice", "wonderland") | ||
channel = self.make_request( | ||
"POST", | ||
"/_matrix/client/r0/keys/query", | ||
{}, | ||
alice_token, | ||
) | ||
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result) | ||
self.assertEqual( | ||
channel.json_body["errcode"], | ||
Codes.BAD_JSON, | ||
channel.result, | ||
) |