Skip to content

Commit c404cdf

Browse files
committed
Add rate limit conf to user directory endpoint
1 parent 8b0083c commit c404cdf

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

changelog.d/19291.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a config to be able to rate limit search in the user directory.

docs/usage/configuration/config_documentation.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,25 @@ rc_room_creation:
20412041
burst_count: 5.0
20422042
```
20432043
---
2044+
### `rc_user_directory`
2045+
2046+
*(object)* This option allows admins to ratelimit searches in the user directory.
2047+
2048+
_Added in Synapse 1.145.0._
2049+
2050+
This setting has the following sub-options:
2051+
2052+
* `per_second` (number): Maximum number of requests a client can send per second.
2053+
2054+
* `burst_count` (number): Maximum number of requests a client can send before being throttled.
2055+
2056+
Default configuration:
2057+
```yaml
2058+
rc_user_directory:
2059+
per_second: 0.016
2060+
burst_count: 50.0
2061+
```
2062+
---
20442063
### `federation_rr_transactions_per_room_per_second`
20452064

20462065
*(integer)* Sets outgoing federation transaction frequency for sending read-receipts, per-room.

schema/synapse-config.schema.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,6 +2274,16 @@ properties:
22742274
examples:
22752275
- per_second: 1.0
22762276
burst_count: 5.0
2277+
rc_user_directory:
2278+
$ref: "#/$defs/rc"
2279+
description: >-
2280+
This option allows admins to ratelimit searches in the user directory.
2281+
2282+
2283+
_Added in Synapse 1.145.0._
2284+
default:
2285+
per_second: 0.016
2286+
burst_count: 50.0
22772287
federation_rr_transactions_per_room_per_second:
22782288
type: integer
22792289
description: >-

synapse/config/ratelimiting.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,9 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
252252
"rc_reports",
253253
defaults={"per_second": 1, "burst_count": 5},
254254
)
255+
256+
self.rc_user_directory = RatelimitSettings.parse(
257+
config,
258+
"rc_user_directory",
259+
defaults={"per_second": 0.016, "burst_count": 50},
260+
)

synapse/rest/client/user_directory.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from typing import TYPE_CHECKING
2424

2525
from synapse.api.errors import SynapseError
26+
from synapse.api.ratelimiting import Ratelimiter
2627
from synapse.http.server import HttpServer
2728
from synapse.http.servlet import RestServlet, parse_json_object_from_request
2829
from synapse.http.site import SynapseRequest
@@ -46,6 +47,12 @@ def __init__(self, hs: "HomeServer"):
4647
self.auth = hs.get_auth()
4748
self.user_directory_handler = hs.get_user_directory_handler()
4849

50+
self._per_user_limiter = Ratelimiter(
51+
store=hs.get_datastores().main,
52+
clock=hs.get_clock(),
53+
cfg=hs.config.ratelimiting.rc_user_directory,
54+
)
55+
4956
async def on_POST(self, request: SynapseRequest) -> tuple[int, JsonMapping]:
5057
"""Searches for users in directory
5158
@@ -69,6 +76,8 @@ async def on_POST(self, request: SynapseRequest) -> tuple[int, JsonMapping]:
6976
if not self.hs.config.userdirectory.user_directory_search_enabled:
7077
return 200, {"limited": False, "results": []}
7178

79+
await self._per_user_limiter.ratelimit(requester)
80+
7281
body = parse_json_object_from_request(request)
7382

7483
limit = int(body.get("limit", 10))

0 commit comments

Comments
 (0)