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

Commit d2f0ec1

Browse files
authored
Add type hints to groups code. (#9393)
1 parent e1071fd commit d2f0ec1

File tree

9 files changed

+341
-124
lines changed

9 files changed

+341
-124
lines changed

changelog.d/9321.bugfix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Assert a maximum length for the `client_secret` parameter for spec compliance.
1+
Assert a maximum length for some parameters for spec compliance.

changelog.d/9393.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Assert a maximum length for some parameters for spec compliance.

mypy.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ files =
2323
synapse/events/validator.py,
2424
synapse/events/spamcheck.py,
2525
synapse/federation,
26+
synapse/groups,
2627
synapse/handlers,
2728
synapse/http/client.py,
2829
synapse/http/federation/matrix_federation_agent.py,

synapse/api/constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
# the maximum length for a user id is 255 characters
2828
MAX_USERID_LENGTH = 255
2929

30+
# The maximum length for a group id is 255 characters
31+
MAX_GROUPID_LENGTH = 255
32+
MAX_GROUP_CATEGORYID_LENGTH = 255
33+
MAX_GROUP_ROLEID_LENGTH = 255
34+
3035

3136
class Membership:
3237

synapse/federation/transport/server.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from typing import Optional, Tuple, Type
2222

2323
import synapse
24+
from synapse.api.constants import MAX_GROUP_CATEGORYID_LENGTH, MAX_GROUP_ROLEID_LENGTH
2425
from synapse.api.errors import Codes, FederationDeniedError, SynapseError
2526
from synapse.api.room_versions import RoomVersions
2627
from synapse.api.urls import (
@@ -1118,7 +1119,17 @@ async def on_POST(self, origin, content, query, group_id, category_id, room_id):
11181119
raise SynapseError(403, "requester_user_id doesn't match origin")
11191120

11201121
if category_id == "":
1121-
raise SynapseError(400, "category_id cannot be empty string")
1122+
raise SynapseError(
1123+
400, "category_id cannot be empty string", Codes.INVALID_PARAM
1124+
)
1125+
1126+
if len(category_id) > MAX_GROUP_CATEGORYID_LENGTH:
1127+
raise SynapseError(
1128+
400,
1129+
"category_id may not be longer than %s characters"
1130+
% (MAX_GROUP_CATEGORYID_LENGTH,),
1131+
Codes.INVALID_PARAM,
1132+
)
11221133

11231134
resp = await self.handler.update_group_summary_room(
11241135
group_id,
@@ -1184,6 +1195,14 @@ async def on_POST(self, origin, content, query, group_id, category_id):
11841195
if category_id == "":
11851196
raise SynapseError(400, "category_id cannot be empty string")
11861197

1198+
if len(category_id) > MAX_GROUP_CATEGORYID_LENGTH:
1199+
raise SynapseError(
1200+
400,
1201+
"category_id may not be longer than %s characters"
1202+
% (MAX_GROUP_CATEGORYID_LENGTH,),
1203+
Codes.INVALID_PARAM,
1204+
)
1205+
11871206
resp = await self.handler.upsert_group_category(
11881207
group_id, requester_user_id, category_id, content
11891208
)
@@ -1240,7 +1259,17 @@ async def on_POST(self, origin, content, query, group_id, role_id):
12401259
raise SynapseError(403, "requester_user_id doesn't match origin")
12411260

12421261
if role_id == "":
1243-
raise SynapseError(400, "role_id cannot be empty string")
1262+
raise SynapseError(
1263+
400, "role_id cannot be empty string", Codes.INVALID_PARAM
1264+
)
1265+
1266+
if len(role_id) > MAX_GROUP_ROLEID_LENGTH:
1267+
raise SynapseError(
1268+
400,
1269+
"role_id may not be longer than %s characters"
1270+
% (MAX_GROUP_ROLEID_LENGTH,),
1271+
Codes.INVALID_PARAM,
1272+
)
12441273

12451274
resp = await self.handler.update_group_role(
12461275
group_id, requester_user_id, role_id, content
@@ -1285,6 +1314,14 @@ async def on_POST(self, origin, content, query, group_id, role_id, user_id):
12851314
if role_id == "":
12861315
raise SynapseError(400, "role_id cannot be empty string")
12871316

1317+
if len(role_id) > MAX_GROUP_ROLEID_LENGTH:
1318+
raise SynapseError(
1319+
400,
1320+
"role_id may not be longer than %s characters"
1321+
% (MAX_GROUP_ROLEID_LENGTH,),
1322+
Codes.INVALID_PARAM,
1323+
)
1324+
12881325
resp = await self.handler.update_group_summary_user(
12891326
group_id,
12901327
requester_user_id,

synapse/groups/attestations.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@
3737

3838
import logging
3939
import random
40-
from typing import Tuple
40+
from typing import TYPE_CHECKING, Optional, Tuple
4141

4242
from signedjson.sign import sign_json
4343

4444
from synapse.api.errors import HttpResponseException, RequestSendFailed, SynapseError
4545
from synapse.metrics.background_process_metrics import run_as_background_process
46-
from synapse.types import get_domain_from_id
46+
from synapse.types import JsonDict, get_domain_from_id
47+
48+
if TYPE_CHECKING:
49+
from synapse.app.homeserver import HomeServer
4750

4851
logger = logging.getLogger(__name__)
4952

@@ -63,15 +66,19 @@
6366
class GroupAttestationSigning:
6467
"""Creates and verifies group attestations."""
6568

66-
def __init__(self, hs):
69+
def __init__(self, hs: "HomeServer"):
6770
self.keyring = hs.get_keyring()
6871
self.clock = hs.get_clock()
6972
self.server_name = hs.hostname
7073
self.signing_key = hs.signing_key
7174

7275
async def verify_attestation(
73-
self, attestation, group_id, user_id, server_name=None
74-
):
76+
self,
77+
attestation: JsonDict,
78+
group_id: str,
79+
user_id: str,
80+
server_name: Optional[str] = None,
81+
) -> None:
7582
"""Verifies that the given attestation matches the given parameters.
7683
7784
An optional server_name can be supplied to explicitly set which server's
@@ -100,16 +107,18 @@ async def verify_attestation(
100107
if valid_until_ms < now:
101108
raise SynapseError(400, "Attestation expired")
102109

110+
assert server_name is not None
103111
await self.keyring.verify_json_for_server(
104112
server_name, attestation, now, "Group attestation"
105113
)
106114

107-
def create_attestation(self, group_id, user_id):
115+
def create_attestation(self, group_id: str, user_id: str) -> JsonDict:
108116
"""Create an attestation for the group_id and user_id with default
109117
validity length.
110118
"""
111-
validity_period = DEFAULT_ATTESTATION_LENGTH_MS
112-
validity_period *= random.uniform(*DEFAULT_ATTESTATION_JITTER)
119+
validity_period = DEFAULT_ATTESTATION_LENGTH_MS * random.uniform(
120+
*DEFAULT_ATTESTATION_JITTER
121+
)
113122
valid_until_ms = int(self.clock.time_msec() + validity_period)
114123

115124
return sign_json(
@@ -126,7 +135,7 @@ def create_attestation(self, group_id, user_id):
126135
class GroupAttestionRenewer:
127136
"""Responsible for sending and receiving attestation updates."""
128137

129-
def __init__(self, hs):
138+
def __init__(self, hs: "HomeServer"):
130139
self.clock = hs.get_clock()
131140
self.store = hs.get_datastore()
132141
self.assestations = hs.get_groups_attestation_signing()
@@ -139,7 +148,9 @@ def __init__(self, hs):
139148
self._start_renew_attestations, 30 * 60 * 1000
140149
)
141150

142-
async def on_renew_attestation(self, group_id, user_id, content):
151+
async def on_renew_attestation(
152+
self, group_id: str, user_id: str, content: JsonDict
153+
) -> JsonDict:
143154
"""When a remote updates an attestation"""
144155
attestation = content["attestation"]
145156

@@ -154,10 +165,10 @@ async def on_renew_attestation(self, group_id, user_id, content):
154165

155166
return {}
156167

157-
def _start_renew_attestations(self):
168+
def _start_renew_attestations(self) -> None:
158169
return run_as_background_process("renew_attestations", self._renew_attestations)
159170

160-
async def _renew_attestations(self):
171+
async def _renew_attestations(self) -> None:
161172
"""Called periodically to check if we need to update any of our attestations"""
162173

163174
now = self.clock.time_msec()
@@ -166,7 +177,7 @@ async def _renew_attestations(self):
166177
now + UPDATE_ATTESTATION_TIME_MS
167178
)
168179

169-
async def _renew_attestation(group_user: Tuple[str, str]):
180+
async def _renew_attestation(group_user: Tuple[str, str]) -> None:
170181
group_id, user_id = group_user
171182
try:
172183
if not self.is_mine_id(group_id):

0 commit comments

Comments
 (0)