mirror of
https://github.com/element-hq/synapse.git
synced 2025-12-05 01:10:13 +00:00
Compare commits
22 Commits
anoa/allow
...
v1.132.0rc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5581fbb906 | ||
|
|
1ab35a0a78 | ||
|
|
341d956ee6 | ||
|
|
6521406a37 | ||
|
|
6e600c986e | ||
|
|
d285d76185 | ||
|
|
919c362466 | ||
|
|
82189cbde4 | ||
|
|
e80bc4b062 | ||
|
|
865d43b4b3 | ||
|
|
0b9f1757a7 | ||
|
|
8010377a88 | ||
|
|
586b82e580 | ||
|
|
9b2bc75ed4 | ||
|
|
28f21b4036 | ||
|
|
379356c0ea | ||
|
|
fbe7a898f0 | ||
|
|
08a0506f48 | ||
|
|
c47d8e0ee1 | ||
|
|
a4d8da7a1b | ||
|
|
461571fcf2 | ||
|
|
22db145da3 |
89
CHANGES.md
89
CHANGES.md
@@ -1,3 +1,92 @@
|
||||
# Synapse 1.132.0rc1 (2025-06-10)
|
||||
|
||||
### Features
|
||||
|
||||
- Add support for [MSC4155](https://github.com/matrix-org/matrix-spec-proposals/pull/4155) Invite Filtering. ([\#18288](https://github.com/element-hq/synapse/issues/18288))
|
||||
- Add experimental `user_may_send_state_event` module API callback. ([\#18455](https://github.com/element-hq/synapse/issues/18455))
|
||||
- Add experimental `get_media_config_for_user` and `is_user_allowed_to_upload_media_of_size` module API callbacks that allow overriding of media repository maximum upload size. ([\#18457](https://github.com/element-hq/synapse/issues/18457))
|
||||
- Add experimental `get_ratelimit_override_for_user` module API callback that allows overriding of per-user ratelimits. ([\#18458](https://github.com/element-hq/synapse/issues/18458))
|
||||
- Pass `room_config` argument to `user_may_create_room` spam checker module callback. ([\#18486](https://github.com/element-hq/synapse/issues/18486))
|
||||
- Support configuration of default and extra user types. ([\#18456](https://github.com/element-hq/synapse/issues/18456))
|
||||
- Successful requests to `/_matrix/app/v1/ping` will now force Synapse to reattempt delivering transactions to appservices. ([\#18521](https://github.com/element-hq/synapse/issues/18521))
|
||||
- Support the import of the `RatelimitOverride` type from `synapse.module_api` in modules and rename `messages_per_second` to `per_second`. ([\#18513](https://github.com/element-hq/synapse/issues/18513))
|
||||
|
||||
### Bugfixes
|
||||
|
||||
- Remove destinations from sending if not whitelisted. ([\#18484](https://github.com/element-hq/synapse/issues/18484))
|
||||
- Fixed room summary API incorrectly returning that a room is private in the room summary response when the join rule is omitted by the remote server. Contributed by @nexy7574. ([\#18493](https://github.com/element-hq/synapse/issues/18493))
|
||||
- Prevent users from adding themselves to their own user ignore list. ([\#18508](https://github.com/element-hq/synapse/issues/18508))
|
||||
|
||||
### Improved Documentation
|
||||
|
||||
- Generate config documentation from JSON Schema file. ([\#17892](https://github.com/element-hq/synapse/issues/17892))
|
||||
- Mention `CAP_NET_BIND_SERVICE` as an alternative to running Synapse as root in order to bind to a privileged port. ([\#18408](https://github.com/element-hq/synapse/issues/18408))
|
||||
- Surface hidden Admin API documentation regarding fetching of scheduled tasks. ([\#18516](https://github.com/element-hq/synapse/issues/18516))
|
||||
- Mark the new module APIs in this release as experimental. ([\#18536](https://github.com/element-hq/synapse/issues/18536))
|
||||
|
||||
### Internal Changes
|
||||
|
||||
- Mark dehydrated devices in the [List All User Devices Admin API](https://element-hq.github.io/synapse/latest/admin_api/user_admin_api.html#list-all-devices). ([\#18252](https://github.com/element-hq/synapse/issues/18252))
|
||||
- Reduce disk wastage by cleaning up `received_transactions` older than 1 day, rather than 30 days. ([\#18310](https://github.com/element-hq/synapse/issues/18310))
|
||||
- Distinguish all vs local events being persisted in the "Event Send Time Quantiles" graph (Grafana). ([\#18510](https://github.com/element-hq/synapse/issues/18510))
|
||||
|
||||
|
||||
|
||||
|
||||
# Synapse 1.131.0 (2025-06-03)
|
||||
|
||||
No significant changes since 1.131.0rc1.
|
||||
|
||||
# Synapse 1.131.0rc1 (2025-05-28)
|
||||
|
||||
### Features
|
||||
|
||||
- Add `msc4263_limit_key_queries_to_users_who_share_rooms` config option as per [MSC4263](https://github.com/matrix-org/matrix-spec-proposals/pull/4263). ([\#18180](https://github.com/element-hq/synapse/issues/18180))
|
||||
- Add option to allow registrations that begin with `_`. Contributed by `_` (@hex5f). ([\#18262](https://github.com/element-hq/synapse/issues/18262))
|
||||
- Include room ID in response to the [Room Deletion Status Admin API](https://element-hq.github.io/synapse/latest/admin_api/rooms.html#status-of-deleting-rooms). ([\#18318](https://github.com/element-hq/synapse/issues/18318))
|
||||
- Add support for calling Policy Servers ([MSC4284](https://github.com/matrix-org/matrix-spec-proposals/pull/4284)) to mark events as spam. ([\#18387](https://github.com/element-hq/synapse/issues/18387))
|
||||
|
||||
### Bugfixes
|
||||
|
||||
- Prevent race-condition in `_maybe_retry_device_resync` entrance. ([\#18391](https://github.com/element-hq/synapse/issues/18391))
|
||||
- Fix the `tests.handlers.test_worker_lock.WorkerLockTestCase.test_lock_contention` test which could spuriously time out on RISC-V architectures due to performance differences. ([\#18430](https://github.com/element-hq/synapse/issues/18430))
|
||||
- Fix admin redaction endpoint not redacting encrypted messages. ([\#18434](https://github.com/element-hq/synapse/issues/18434))
|
||||
|
||||
### Improved Documentation
|
||||
|
||||
- Update `room_list_publication_rules` docs to consider defaults that changed in v1.126.0. Contributed by @HarHarLinks. ([\#18286](https://github.com/element-hq/synapse/issues/18286))
|
||||
- Add advice for upgrading between major PostgreSQL versions to the database documentation. ([\#18445](https://github.com/element-hq/synapse/issues/18445))
|
||||
|
||||
### Internal Changes
|
||||
|
||||
- Fix a memory leak in `_NotifierUserStream`. ([\#18380](https://github.com/element-hq/synapse/issues/18380))
|
||||
- Fix a couple type annotations in the `RootConfig`/`Config`. ([\#18409](https://github.com/element-hq/synapse/issues/18409))
|
||||
- Explicitly enable PyPy builds in `cibuildwheel`s config to avoid it being disabled on a future upgrade to `cibuildwheel` v3. ([\#18417](https://github.com/element-hq/synapse/issues/18417))
|
||||
- Update the PR review template to remove an erroneous line break from the final bullet point. ([\#18419](https://github.com/element-hq/synapse/issues/18419))
|
||||
- Explain why we `flush_buffer()` for Python `print(...)` output. ([\#18420](https://github.com/element-hq/synapse/issues/18420))
|
||||
- Add lint to ensure we don't add a `CREATE/DROP INDEX` in a schema delta. ([\#18440](https://github.com/element-hq/synapse/issues/18440))
|
||||
- Allow checking only for the existence of a field in an SSO provider's response, rather than requiring the value(s) to check. ([\#18454](https://github.com/element-hq/synapse/issues/18454))
|
||||
- Add unit tests for homeserver usage statistics. ([\#18463](https://github.com/element-hq/synapse/issues/18463))
|
||||
- Don't move invited users to new room when shutting down room. ([\#18471](https://github.com/element-hq/synapse/issues/18471))
|
||||
|
||||
|
||||
|
||||
### Updates to locked dependencies
|
||||
|
||||
* Bump actions/setup-python from 5.5.0 to 5.6.0. ([\#18398](https://github.com/element-hq/synapse/issues/18398))
|
||||
* Bump authlib from 1.5.1 to 1.5.2. ([\#18452](https://github.com/element-hq/synapse/issues/18452))
|
||||
* Bump docker/build-push-action from 6.15.0 to 6.17.0. ([\#18397](https://github.com/element-hq/synapse/issues/18397), [\#18449](https://github.com/element-hq/synapse/issues/18449))
|
||||
* Bump lxml from 5.3.0 to 5.4.0. ([\#18480](https://github.com/element-hq/synapse/issues/18480))
|
||||
* Bump mypy-zope from 1.0.9 to 1.0.11. ([\#18428](https://github.com/element-hq/synapse/issues/18428))
|
||||
* Bump pyo3 from 0.23.5 to 0.24.2. ([\#18460](https://github.com/element-hq/synapse/issues/18460))
|
||||
* Bump pyo3-log from 0.12.3 to 0.12.4. ([\#18453](https://github.com/element-hq/synapse/issues/18453))
|
||||
* Bump pyopenssl from 25.0.0 to 25.1.0. ([\#18450](https://github.com/element-hq/synapse/issues/18450))
|
||||
* Bump ruff from 0.7.3 to 0.11.11. ([\#18451](https://github.com/element-hq/synapse/issues/18451), [\#18482](https://github.com/element-hq/synapse/issues/18482))
|
||||
* Bump tornado from 6.4.2 to 6.5.0. ([\#18459](https://github.com/element-hq/synapse/issues/18459))
|
||||
* Bump setuptools from 72.1.0 to 78.1.1. ([\#18461](https://github.com/element-hq/synapse/issues/18461))
|
||||
* Bump types-jsonschema from 4.23.0.20241208 to 4.23.0.20250516. ([\#18481](https://github.com/element-hq/synapse/issues/18481))
|
||||
* Bump types-requests from 2.32.0.20241016 to 2.32.0.20250328. ([\#18427](https://github.com/element-hq/synapse/issues/18427))
|
||||
|
||||
# Synapse 1.130.0 (2025-05-20)
|
||||
|
||||
### Bugfixes
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
Generate config documentation from JSON Schema file.
|
||||
@@ -1 +0,0 @@
|
||||
Add `msc4263_limit_key_queries_to_users_who_share_rooms` config option as per [MSC4263](https://github.com/matrix-org/matrix-spec-proposals/pull/4263).
|
||||
@@ -1 +0,0 @@
|
||||
Mark dehydrated devices in the [List All User Devices Admin API](https://element-hq.github.io/synapse/latest/admin_api/user_admin_api.html#list-all-devices).
|
||||
@@ -1 +0,0 @@
|
||||
Add option to allow registrations that begin with `_`. Contributed by `_` (@hex5f).
|
||||
@@ -1 +0,0 @@
|
||||
Update `room_list_publication_rules` docs to consider defaults that changed in v1.126.0. Contributed by @HarHarLinks.
|
||||
@@ -1 +0,0 @@
|
||||
Include room ID in room deletion status response.
|
||||
@@ -1 +0,0 @@
|
||||
Fix a memory leak in `_NotifierUserStream`.
|
||||
@@ -1 +0,0 @@
|
||||
Add support for calling Policy Servers ([MSC4284](https://github.com/matrix-org/matrix-spec-proposals/pull/4284)) to mark events as spam.
|
||||
@@ -1 +0,0 @@
|
||||
Prevent race-condition in `_maybe_retry_device_resync` entrance.
|
||||
@@ -1 +0,0 @@
|
||||
Fix a couple type annotations in the `RootConfig`/`Config`.
|
||||
@@ -1 +0,0 @@
|
||||
Explicitly enable PyPy builds in `cibuildwheel`s config to avoid it being disabled on a future upgrade to `cibuildwheel` v3.
|
||||
@@ -1 +0,0 @@
|
||||
Update the PR review template to remove an erroneous line break from the final bullet point.
|
||||
@@ -1 +0,0 @@
|
||||
Explain why we `flush_buffer()` for Python `print(...)` output.
|
||||
@@ -1 +0,0 @@
|
||||
Fix the `tests.handlers.test_worker_lock.WorkerLockTestCase.test_lock_contention` test which could spuriously time out on RISC-V architectures due to performance differences.
|
||||
@@ -1 +0,0 @@
|
||||
Fix admin redaction endpoint not redacting encrypted messages.
|
||||
@@ -1 +0,0 @@
|
||||
Add lint to ensure we don't add a `CREATE/DROP INDEX` in a schema delta.
|
||||
@@ -1 +0,0 @@
|
||||
Add advice for upgrading between major PostgreSQL versions to the database documentation.
|
||||
@@ -1 +0,0 @@
|
||||
Bump ruff from 0.7.3 to 0.11.10.
|
||||
@@ -1 +0,0 @@
|
||||
Allow checking only for the existence of a field in an SSO provider's response, rather than requiring the value(s) to check.
|
||||
@@ -1 +0,0 @@
|
||||
Bump tornado from 6.4.2 to 6.5.0.
|
||||
@@ -1 +0,0 @@
|
||||
Bump pyo3 from 0.23.5 to 0.24.2.
|
||||
@@ -1 +0,0 @@
|
||||
Add unit tests for homeserver usage statistics.
|
||||
@@ -1 +0,0 @@
|
||||
Don't move invited users to new room when shutting down room.
|
||||
@@ -220,29 +220,24 @@
|
||||
"yBucketBound": "auto"
|
||||
},
|
||||
{
|
||||
"aliasColors": {},
|
||||
"bars": false,
|
||||
"dashLength": 10,
|
||||
"dashes": false,
|
||||
"datasource": {
|
||||
"uid": "${DS_PROMETHEUS}"
|
||||
"uid": "${DS_PROMETHEUS}",
|
||||
"type": "prometheus"
|
||||
},
|
||||
"description": "",
|
||||
"aliasColors": {},
|
||||
"dashLength": 10,
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"links": []
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"fill": 0,
|
||||
"fillGradient": 0,
|
||||
"gridPos": {
|
||||
"h": 9,
|
||||
"w": 12,
|
||||
"x": 12,
|
||||
"y": 1
|
||||
},
|
||||
"hiddenSeries": false,
|
||||
"id": 152,
|
||||
"legend": {
|
||||
"avg": false,
|
||||
@@ -255,71 +250,81 @@
|
||||
"values": false
|
||||
},
|
||||
"lines": true,
|
||||
"linewidth": 0,
|
||||
"links": [],
|
||||
"nullPointMode": "connected",
|
||||
"options": {
|
||||
"alertThreshold": true
|
||||
},
|
||||
"paceLength": 10,
|
||||
"percentage": false,
|
||||
"pluginVersion": "9.2.2",
|
||||
"pluginVersion": "10.4.3",
|
||||
"pointradius": 5,
|
||||
"points": false,
|
||||
"renderer": "flot",
|
||||
"seriesOverrides": [
|
||||
{
|
||||
"alias": "Avg",
|
||||
"fill": 0,
|
||||
"linewidth": 3
|
||||
"linewidth": 3,
|
||||
"$$hashKey": "object:48"
|
||||
},
|
||||
{
|
||||
"alias": "99%",
|
||||
"color": "#C4162A",
|
||||
"fillBelowTo": "90%"
|
||||
"fillBelowTo": "90%",
|
||||
"$$hashKey": "object:49"
|
||||
},
|
||||
{
|
||||
"alias": "90%",
|
||||
"color": "#FF7383",
|
||||
"fillBelowTo": "75%"
|
||||
"fillBelowTo": "75%",
|
||||
"$$hashKey": "object:50"
|
||||
},
|
||||
{
|
||||
"alias": "75%",
|
||||
"color": "#FFEE52",
|
||||
"fillBelowTo": "50%"
|
||||
"fillBelowTo": "50%",
|
||||
"$$hashKey": "object:51"
|
||||
},
|
||||
{
|
||||
"alias": "50%",
|
||||
"color": "#73BF69",
|
||||
"fillBelowTo": "25%"
|
||||
"fillBelowTo": "25%",
|
||||
"$$hashKey": "object:52"
|
||||
},
|
||||
{
|
||||
"alias": "25%",
|
||||
"color": "#1F60C4",
|
||||
"fillBelowTo": "5%"
|
||||
"fillBelowTo": "5%",
|
||||
"$$hashKey": "object:53"
|
||||
},
|
||||
{
|
||||
"alias": "5%",
|
||||
"lines": false
|
||||
"lines": false,
|
||||
"$$hashKey": "object:54"
|
||||
},
|
||||
{
|
||||
"alias": "Average",
|
||||
"color": "rgb(255, 255, 255)",
|
||||
"lines": true,
|
||||
"linewidth": 3
|
||||
"linewidth": 3,
|
||||
"$$hashKey": "object:55"
|
||||
},
|
||||
{
|
||||
"alias": "Events",
|
||||
"alias": "Local events being persisted",
|
||||
"color": "#96d98D",
|
||||
"points": true,
|
||||
"yaxis": 2,
|
||||
"zindex": -3,
|
||||
"$$hashKey": "object:56"
|
||||
},
|
||||
{
|
||||
"$$hashKey": "object:329",
|
||||
"color": "#B877D9",
|
||||
"hideTooltip": true,
|
||||
"alias": "All events being persisted",
|
||||
"points": true,
|
||||
"yaxis": 2,
|
||||
"zindex": -3
|
||||
}
|
||||
],
|
||||
"spaceLength": 10,
|
||||
"stack": false,
|
||||
"steppedLine": false,
|
||||
"targets": [
|
||||
{
|
||||
"datasource": {
|
||||
@@ -384,7 +389,20 @@
|
||||
},
|
||||
"expr": "sum(rate(synapse_http_server_response_time_seconds_sum{servlet='RoomSendEventRestServlet',index=~\"$index\",instance=\"$instance\",code=~\"2..\"}[$bucket_size])) / sum(rate(synapse_http_server_response_time_seconds_count{servlet='RoomSendEventRestServlet',index=~\"$index\",instance=\"$instance\",code=~\"2..\"}[$bucket_size]))",
|
||||
"legendFormat": "Average",
|
||||
"refId": "H"
|
||||
"refId": "H",
|
||||
"editorMode": "code",
|
||||
"range": true
|
||||
},
|
||||
{
|
||||
"datasource": {
|
||||
"uid": "${DS_PROMETHEUS}"
|
||||
},
|
||||
"expr": "sum(rate(synapse_http_server_response_time_seconds_count{servlet='RoomSendEventRestServlet',index=~\"$index\",instance=\"$instance\",code=~\"2..\"}[$bucket_size]))",
|
||||
"hide": false,
|
||||
"instant": false,
|
||||
"legendFormat": "Local events being persisted",
|
||||
"refId": "E",
|
||||
"editorMode": "code"
|
||||
},
|
||||
{
|
||||
"datasource": {
|
||||
@@ -393,8 +411,9 @@
|
||||
"expr": "sum(rate(synapse_storage_events_persisted_events_total{instance=\"$instance\"}[$bucket_size]))",
|
||||
"hide": false,
|
||||
"instant": false,
|
||||
"legendFormat": "Events",
|
||||
"refId": "E"
|
||||
"legendFormat": "All events being persisted",
|
||||
"refId": "I",
|
||||
"editorMode": "code"
|
||||
}
|
||||
],
|
||||
"thresholds": [
|
||||
@@ -428,7 +447,9 @@
|
||||
"xaxis": {
|
||||
"mode": "time",
|
||||
"show": true,
|
||||
"values": []
|
||||
"values": [],
|
||||
"name": null,
|
||||
"buckets": null
|
||||
},
|
||||
"yaxes": [
|
||||
{
|
||||
@@ -450,7 +471,20 @@
|
||||
],
|
||||
"yaxis": {
|
||||
"align": false
|
||||
}
|
||||
},
|
||||
"bars": false,
|
||||
"dashes": false,
|
||||
"description": "",
|
||||
"fill": 0,
|
||||
"fillGradient": 0,
|
||||
"hiddenSeries": false,
|
||||
"linewidth": 0,
|
||||
"percentage": false,
|
||||
"points": false,
|
||||
"stack": false,
|
||||
"steppedLine": false,
|
||||
"timeFrom": null,
|
||||
"timeShift": null
|
||||
},
|
||||
{
|
||||
"aliasColors": {},
|
||||
|
||||
18
debian/changelog
vendored
18
debian/changelog
vendored
@@ -1,3 +1,21 @@
|
||||
matrix-synapse-py3 (1.132.0~rc1) stable; urgency=medium
|
||||
|
||||
* New Synapse release 1.132.0rc1.
|
||||
|
||||
-- Synapse Packaging team <packages@matrix.org> Tue, 10 Jun 2025 11:15:18 +0100
|
||||
|
||||
matrix-synapse-py3 (1.131.0) stable; urgency=medium
|
||||
|
||||
* New Synapse release 1.131.0.
|
||||
|
||||
-- Synapse Packaging team <packages@matrix.org> Tue, 03 Jun 2025 14:36:55 +0100
|
||||
|
||||
matrix-synapse-py3 (1.131.0~rc1) stable; urgency=medium
|
||||
|
||||
* New synapse release 1.131.0rc1.
|
||||
|
||||
-- Synapse Packaging team <packages@matrix.org> Wed, 28 May 2025 10:25:44 +0000
|
||||
|
||||
matrix-synapse-py3 (1.130.0) stable; urgency=medium
|
||||
|
||||
* New Synapse release 1.130.0.
|
||||
|
||||
@@ -127,6 +127,8 @@ experimental_features:
|
||||
msc3983_appservice_otk_claims: true
|
||||
# Proxy key queries to exclusive ASes
|
||||
msc3984_appservice_key_query: true
|
||||
# Invite filtering
|
||||
msc4155_enabled: true
|
||||
|
||||
server_notices:
|
||||
system_mxid_localpart: _server
|
||||
|
||||
@@ -49,6 +49,8 @@
|
||||
- [Background update controller callbacks](modules/background_update_controller_callbacks.md)
|
||||
- [Account data callbacks](modules/account_data_callbacks.md)
|
||||
- [Add extra fields to client events unsigned section callbacks](modules/add_extra_fields_to_client_events_unsigned.md)
|
||||
- [Media repository callbacks](modules/media_repository_callbacks.md)
|
||||
- [Ratelimit callbacks](modules/ratelimit_callbacks.md)
|
||||
- [Porting a legacy module to the new interface](modules/porting_legacy_module.md)
|
||||
- [Workers](workers.md)
|
||||
- [Using `synctl` with Workers](synctl_workers.md)
|
||||
@@ -66,6 +68,7 @@
|
||||
- [Registration Tokens](usage/administration/admin_api/registration_tokens.md)
|
||||
- [Manipulate Room Membership](admin_api/room_membership.md)
|
||||
- [Rooms](admin_api/rooms.md)
|
||||
- [Scheduled tasks](admin_api/scheduled_tasks.md)
|
||||
- [Server Notices](admin_api/server_notices.md)
|
||||
- [Statistics](admin_api/statistics.md)
|
||||
- [Users](admin_api/user_admin_api.md)
|
||||
|
||||
@@ -163,7 +163,8 @@ Body parameters:
|
||||
- `locked` - **bool**, optional. If unspecified, locked state will be left unchanged.
|
||||
- `user_type` - **string** or null, optional. If not provided, the user type will be
|
||||
not be changed. If `null` is given, the user type will be cleared.
|
||||
Other allowed options are: `bot` and `support`.
|
||||
Other allowed options are: `bot` and `support` and any extra values defined in the homserver
|
||||
[configuration](../usage/configuration/config_documentation.md#user_types).
|
||||
|
||||
## List Accounts
|
||||
### List Accounts (V2)
|
||||
|
||||
66
docs/modules/media_repository_callbacks.md
Normal file
66
docs/modules/media_repository_callbacks.md
Normal file
@@ -0,0 +1,66 @@
|
||||
# Media repository callbacks
|
||||
|
||||
Media repository callbacks allow module developers to customise the behaviour of the
|
||||
media repository on a per user basis. Media repository callbacks can be registered
|
||||
using the module API's `register_media_repository_callbacks` method.
|
||||
|
||||
The available media repository callbacks are:
|
||||
|
||||
### `get_media_config_for_user`
|
||||
|
||||
_First introduced in Synapse v1.132.0_
|
||||
|
||||
```python
|
||||
async def get_media_config_for_user(user_id: str) -> Optional[JsonDict]
|
||||
```
|
||||
|
||||
**<span style="color:red">
|
||||
Caution: This callback is currently experimental . The method signature or behaviour
|
||||
may change without notice.
|
||||
</span>**
|
||||
|
||||
Called when processing a request from a client for the
|
||||
[media config endpoint](https://spec.matrix.org/latest/client-server-api/#get_matrixclientv1mediaconfig).
|
||||
|
||||
The arguments passed to this callback are:
|
||||
|
||||
* `user_id`: The Matrix user ID of the user (e.g. `@alice:example.com`) making the request.
|
||||
|
||||
If the callback returns a dictionary then it will be used as the body of the response to the
|
||||
client.
|
||||
|
||||
If multiple modules implement this callback, they will be considered in order. If a
|
||||
callback returns `None`, Synapse falls through to the next one. The value of the first
|
||||
callback that does not return `None` will be used. If this happens, Synapse will not call
|
||||
any of the subsequent implementations of this callback.
|
||||
|
||||
If no module returns a non-`None` value then the default media config will be returned.
|
||||
|
||||
### `is_user_allowed_to_upload_media_of_size`
|
||||
|
||||
_First introduced in Synapse v1.132.0_
|
||||
|
||||
```python
|
||||
async def is_user_allowed_to_upload_media_of_size(user_id: str, size: int) -> bool
|
||||
```
|
||||
|
||||
**<span style="color:red">
|
||||
Caution: This callback is currently experimental . The method signature or behaviour
|
||||
may change without notice.
|
||||
</span>**
|
||||
|
||||
Called before media is accepted for upload from a user, in case the module needs to
|
||||
enforce a different limit for the particular user.
|
||||
|
||||
The arguments passed to this callback are:
|
||||
|
||||
* `user_id`: The Matrix user ID of the user (e.g. `@alice:example.com`) making the request.
|
||||
* `size`: The size in bytes of media that is being requested to upload.
|
||||
|
||||
If the module returns `False`, the current request will be denied with the error code
|
||||
`M_TOO_LARGE` and the HTTP status code 413.
|
||||
|
||||
If multiple modules implement this callback, they will be considered in order. If a callback
|
||||
returns `True`, Synapse falls through to the next one. The value of the first callback that
|
||||
returns `False` will be used. If this happens, Synapse will not call any of the subsequent
|
||||
implementations of this callback.
|
||||
43
docs/modules/ratelimit_callbacks.md
Normal file
43
docs/modules/ratelimit_callbacks.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# Ratelimit callbacks
|
||||
|
||||
Ratelimit callbacks allow module developers to override ratelimit settings dynamically whilst
|
||||
Synapse is running. Ratelimit callbacks can be registered using the module API's
|
||||
`register_ratelimit_callbacks` method.
|
||||
|
||||
The available ratelimit callbacks are:
|
||||
|
||||
### `get_ratelimit_override_for_user`
|
||||
|
||||
_First introduced in Synapse v1.132.0_
|
||||
|
||||
```python
|
||||
async def get_ratelimit_override_for_user(user: str, limiter_name: str) -> Optional[synapse.module_api.RatelimitOverride]
|
||||
```
|
||||
|
||||
**<span style="color:red">
|
||||
Caution: This callback is currently experimental . The method signature or behaviour
|
||||
may change without notice.
|
||||
</span>**
|
||||
|
||||
Called when constructing a ratelimiter of a particular type for a user. The module can
|
||||
return a `messages_per_second` and `burst_count` to be used, or `None` if
|
||||
the default settings are adequate. The user is represented by their Matrix user ID
|
||||
(e.g. `@alice:example.com`). The limiter name is usually taken from the `RatelimitSettings` key
|
||||
value.
|
||||
|
||||
The limiters that are currently supported are:
|
||||
|
||||
- `rc_invites.per_room`
|
||||
- `rc_invites.per_user`
|
||||
- `rc_invites.per_issuer`
|
||||
|
||||
The `RatelimitOverride` return type has the following fields:
|
||||
|
||||
- `per_second: float`. The number of actions that can be performed in a second. `0.0` means that ratelimiting is disabled.
|
||||
- `burst_count: int`. The number of actions that can be performed before being limited.
|
||||
|
||||
If multiple modules implement this callback, they will be considered in order. If a
|
||||
callback returns `None`, Synapse falls through to the next one. The value of the first
|
||||
callback that does not return `None` will be used. If this happens, Synapse will not call
|
||||
any of the subsequent implementations of this callback. If no module returns a non-`None` value
|
||||
then the default settings will be used.
|
||||
@@ -159,12 +159,19 @@ _First introduced in Synapse v1.37.0_
|
||||
|
||||
_Changed in Synapse v1.62.0: `synapse.module_api.NOT_SPAM` and `synapse.module_api.errors.Codes` can be returned by this callback. Returning a boolean is now deprecated._
|
||||
|
||||
_Changed in Synapse v1.132.0: Added the `room_config` argument. Callbacks that only expect a single `user_id` argument are still supported._
|
||||
|
||||
```python
|
||||
async def user_may_create_room(user_id: str) -> Union["synapse.module_api.NOT_SPAM", "synapse.module_api.errors.Codes", bool]
|
||||
async def user_may_create_room(user_id: str, room_config: synapse.module_api.JsonDict) -> Union["synapse.module_api.NOT_SPAM", "synapse.module_api.errors.Codes", bool]
|
||||
```
|
||||
|
||||
Called when processing a room creation request.
|
||||
|
||||
The arguments passed to this callback are:
|
||||
|
||||
* `user_id`: The Matrix user ID of the user (e.g. `@alice:example.com`).
|
||||
* `room_config`: The contents of the body of a [/createRoom request](https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3createroom) as a dictionary.
|
||||
|
||||
The callback must return one of:
|
||||
- `synapse.module_api.NOT_SPAM`, to allow the operation. Other callbacks may still
|
||||
decide to reject it.
|
||||
@@ -239,6 +246,41 @@ be used. If this happens, Synapse will not call any of the subsequent implementa
|
||||
this callback.
|
||||
|
||||
|
||||
### `user_may_send_state_event`
|
||||
|
||||
_First introduced in Synapse v1.132.0_
|
||||
|
||||
```python
|
||||
async def user_may_send_state_event(user_id: str, room_id: str, event_type: str, state_key: str, content: JsonDict) -> Union["synapse.module_api.NOT_SPAM", "synapse.module_api.errors.Codes"]
|
||||
```
|
||||
|
||||
**<span style="color:red">
|
||||
Caution: This callback is currently experimental . The method signature or behaviour
|
||||
may change without notice.
|
||||
</span>**
|
||||
|
||||
Called when processing a request to [send state events](https://spec.matrix.org/latest/client-server-api/#put_matrixclientv3roomsroomidstateeventtypestatekey) to a room.
|
||||
|
||||
The arguments passed to this callback are:
|
||||
|
||||
* `user_id`: The Matrix user ID of the user (e.g. `@alice:example.com`) sending the state event.
|
||||
* `room_id`: The ID of the room that the requested state event is being sent to.
|
||||
* `event_type`: The requested type of event.
|
||||
* `state_key`: The requested state key.
|
||||
* `content`: The requested event contents.
|
||||
|
||||
The callback must return one of:
|
||||
- `synapse.module_api.NOT_SPAM`, to allow the operation. Other callbacks may still
|
||||
decide to reject it.
|
||||
- `synapse.module_api.errors.Codes` to reject the operation with an error code. In case
|
||||
of doubt, `synapse.module_api.errors.Codes.FORBIDDEN` is a good error code.
|
||||
|
||||
If multiple modules implement this callback, they will be considered in order. If a
|
||||
callback returns `synapse.module_api.NOT_SPAM`, Synapse falls through to the next one.
|
||||
The value of the first callback that does not return `synapse.module_api.NOT_SPAM` will
|
||||
be used. If this happens, Synapse will not call any of the subsequent implementations of
|
||||
this callback.
|
||||
|
||||
|
||||
### `check_username_for_spam`
|
||||
|
||||
|
||||
@@ -5,10 +5,10 @@ It is recommended to put a reverse proxy such as
|
||||
[Apache](https://httpd.apache.org/docs/current/mod/mod_proxy_http.html),
|
||||
[Caddy](https://caddyserver.com/docs/quick-starts/reverse-proxy),
|
||||
[HAProxy](https://www.haproxy.org/) or
|
||||
[relayd](https://man.openbsd.org/relayd.8) in front of Synapse. One advantage
|
||||
of doing so is that it means that you can expose the default https port
|
||||
(443) to Matrix clients without needing to run Synapse with root
|
||||
privileges.
|
||||
[relayd](https://man.openbsd.org/relayd.8) in front of Synapse.
|
||||
This has the advantage of being able to expose the default HTTPS port (443) to Matrix
|
||||
clients without requiring Synapse to bind to a privileged port (port numbers less than
|
||||
1024), avoiding the need for `CAP_NET_BIND_SERVICE` or running as root.
|
||||
|
||||
You should configure your reverse proxy to forward requests to `/_matrix` or
|
||||
`/_synapse/client` to Synapse, and have it set the `X-Forwarded-For` and
|
||||
|
||||
@@ -63,7 +63,7 @@ class ExampleSpamChecker:
|
||||
async def user_may_invite(self, inviter_userid, invitee_userid, room_id):
|
||||
return True # allow all invites
|
||||
|
||||
async def user_may_create_room(self, userid):
|
||||
async def user_may_create_room(self, userid, room_config):
|
||||
return True # allow all room creations
|
||||
|
||||
async def user_may_create_room_alias(self, userid, room_alias):
|
||||
|
||||
@@ -762,6 +762,24 @@ Example configuration:
|
||||
max_event_delay_duration: 24h
|
||||
```
|
||||
---
|
||||
### `user_types`
|
||||
|
||||
Configuration settings related to the user types feature.
|
||||
|
||||
This setting has the following sub-options:
|
||||
* `default_user_type`: The default user type to use for registering new users when no value has been specified.
|
||||
Defaults to none.
|
||||
* `extra_user_types`: Array of additional user types to allow. These are treated as real users. Defaults to [].
|
||||
|
||||
Example configuration:
|
||||
```yaml
|
||||
user_types:
|
||||
default_user_type: "custom"
|
||||
extra_user_types:
|
||||
- "custom"
|
||||
- "custom2"
|
||||
```
|
||||
|
||||
## Homeserver blocking
|
||||
|
||||
Useful options for Synapse admins.
|
||||
|
||||
@@ -97,7 +97,7 @@ module-name = "synapse.synapse_rust"
|
||||
|
||||
[tool.poetry]
|
||||
name = "matrix-synapse"
|
||||
version = "1.130.0"
|
||||
version = "1.132.0rc1"
|
||||
description = "Homeserver for the Matrix decentralised comms protocol"
|
||||
authors = ["Matrix.org Team and Contributors <packages@matrix.org>"]
|
||||
license = "AGPL-3.0-or-later"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
$schema: https://element-hq.github.io/synapse/latest/schema/v1/meta.schema.json
|
||||
$id: https://element-hq.github.io/synapse/v1.131/schema/synapse-config.schema.json
|
||||
$id: https://element-hq.github.io/synapse/schema/synapse/v1.132/synapse-config.schema.json
|
||||
type: object
|
||||
properties:
|
||||
modules:
|
||||
|
||||
@@ -229,6 +229,7 @@ test_packages=(
|
||||
./tests/msc3902
|
||||
./tests/msc3967
|
||||
./tests/msc4140
|
||||
./tests/msc4155
|
||||
)
|
||||
|
||||
# Enable dirty runs, so tests will reuse the same container where possible.
|
||||
|
||||
@@ -185,12 +185,18 @@ ServerNoticeLimitReached: Final = "m.server_notice.usage_limit_reached"
|
||||
|
||||
class UserTypes:
|
||||
"""Allows for user type specific behaviour. With the benefit of hindsight
|
||||
'admin' and 'guest' users should also be UserTypes. Normal users are type None
|
||||
'admin' and 'guest' users should also be UserTypes. Extra user types can be
|
||||
added in the configuration. Normal users are type None or one of the extra
|
||||
user types (if configured).
|
||||
"""
|
||||
|
||||
SUPPORT: Final = "support"
|
||||
BOT: Final = "bot"
|
||||
ALL_USER_TYPES: Final = (SUPPORT, BOT)
|
||||
ALL_BUILTIN_USER_TYPES: Final = (SUPPORT, BOT)
|
||||
"""
|
||||
The user types that are built-in to Synapse. Extra user types can be
|
||||
added in the configuration.
|
||||
"""
|
||||
|
||||
|
||||
class RelationTypes:
|
||||
@@ -280,6 +286,10 @@ class AccountDataTypes:
|
||||
IGNORED_USER_LIST: Final = "m.ignored_user_list"
|
||||
TAG: Final = "m.tag"
|
||||
PUSH_RULES: Final = "m.push_rules"
|
||||
# MSC4155: Invite filtering
|
||||
MSC4155_INVITE_PERMISSION_CONFIG: Final = (
|
||||
"org.matrix.msc4155.invite_permission_config"
|
||||
)
|
||||
|
||||
|
||||
class HistoryVisibility:
|
||||
|
||||
@@ -137,6 +137,9 @@ class Codes(str, Enum):
|
||||
PROFILE_TOO_LARGE = "M_PROFILE_TOO_LARGE"
|
||||
KEY_TOO_LARGE = "M_KEY_TOO_LARGE"
|
||||
|
||||
# Part of MSC4155
|
||||
INVITE_BLOCKED = "ORG.MATRIX.MSC4155.M_INVITE_BLOCKED"
|
||||
|
||||
|
||||
class CodeMessageException(RuntimeError):
|
||||
"""An exception with integer code, a message string attributes and optional headers.
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
#
|
||||
#
|
||||
|
||||
from typing import Dict, Hashable, Optional, Tuple
|
||||
from typing import TYPE_CHECKING, Dict, Hashable, Optional, Tuple
|
||||
|
||||
from synapse.api.errors import LimitExceededError
|
||||
from synapse.config.ratelimiting import RatelimitSettings
|
||||
@@ -28,6 +28,12 @@ from synapse.storage.databases.main import DataStore
|
||||
from synapse.types import Requester
|
||||
from synapse.util import Clock
|
||||
|
||||
if TYPE_CHECKING:
|
||||
# To avoid circular imports:
|
||||
from synapse.module_api.callbacks.ratelimit_callbacks import (
|
||||
RatelimitModuleApiCallbacks,
|
||||
)
|
||||
|
||||
|
||||
class Ratelimiter:
|
||||
"""
|
||||
@@ -72,12 +78,14 @@ class Ratelimiter:
|
||||
store: DataStore,
|
||||
clock: Clock,
|
||||
cfg: RatelimitSettings,
|
||||
ratelimit_callbacks: Optional["RatelimitModuleApiCallbacks"] = None,
|
||||
):
|
||||
self.clock = clock
|
||||
self.rate_hz = cfg.per_second
|
||||
self.burst_count = cfg.burst_count
|
||||
self.store = store
|
||||
self._limiter_name = cfg.key
|
||||
self._ratelimit_callbacks = ratelimit_callbacks
|
||||
|
||||
# A dictionary representing the token buckets tracked by this rate
|
||||
# limiter. Each entry maps a key of arbitrary type to a tuple representing:
|
||||
@@ -165,6 +173,20 @@ class Ratelimiter:
|
||||
if override and not override.messages_per_second:
|
||||
return True, -1.0
|
||||
|
||||
if requester and self._ratelimit_callbacks:
|
||||
# Check if the user has a custom rate limit for this specific limiter
|
||||
# as returned by the module API.
|
||||
module_override = (
|
||||
await self._ratelimit_callbacks.get_ratelimit_override_for_user(
|
||||
requester.user.to_string(),
|
||||
self._limiter_name,
|
||||
)
|
||||
)
|
||||
|
||||
if module_override:
|
||||
rate_hz = module_override.per_second
|
||||
burst_count = module_override.burst_count
|
||||
|
||||
# Override default values if set
|
||||
time_now_s = _time_now_s if _time_now_s is not None else self.clock.time()
|
||||
rate_hz = rate_hz if rate_hz is not None else self.rate_hz
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
||||
#
|
||||
# Copyright 2015, 2016 OpenMarket Ltd
|
||||
# Copyright (C) 2023 New Vector, Ltd
|
||||
# Copyright (C) 2023, 2025 New Vector, Ltd
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
@@ -70,6 +70,8 @@ from typing import (
|
||||
Tuple,
|
||||
)
|
||||
|
||||
from twisted.internet.interfaces import IDelayedCall
|
||||
|
||||
from synapse.appservice import (
|
||||
ApplicationService,
|
||||
ApplicationServiceState,
|
||||
@@ -450,6 +452,20 @@ class _TransactionController:
|
||||
recoverer.recover()
|
||||
logger.info("Now %i active recoverers", len(self.recoverers))
|
||||
|
||||
def force_retry(self, service: ApplicationService) -> None:
|
||||
"""Forces a Recoverer to attempt delivery of transations immediately.
|
||||
|
||||
Args:
|
||||
service:
|
||||
"""
|
||||
recoverer = self.recoverers.get(service.id)
|
||||
if not recoverer:
|
||||
# No need to force a retry on a happy AS.
|
||||
logger.info(f"{service.id} is not in recovery, not forcing retry")
|
||||
return
|
||||
|
||||
recoverer.force_retry()
|
||||
|
||||
async def _is_service_up(self, service: ApplicationService) -> bool:
|
||||
state = await self.store.get_appservice_state(service)
|
||||
return state == ApplicationServiceState.UP or state is None
|
||||
@@ -482,11 +498,12 @@ class _Recoverer:
|
||||
self.service = service
|
||||
self.callback = callback
|
||||
self.backoff_counter = 1
|
||||
self.scheduled_recovery: Optional[IDelayedCall] = None
|
||||
|
||||
def recover(self) -> None:
|
||||
delay = 2**self.backoff_counter
|
||||
logger.info("Scheduling retries on %s in %fs", self.service.id, delay)
|
||||
self.clock.call_later(
|
||||
self.scheduled_recovery = self.clock.call_later(
|
||||
delay, run_as_background_process, "as-recoverer", self.retry
|
||||
)
|
||||
|
||||
@@ -496,6 +513,21 @@ class _Recoverer:
|
||||
self.backoff_counter += 1
|
||||
self.recover()
|
||||
|
||||
def force_retry(self) -> None:
|
||||
"""Cancels the existing timer and forces an immediate retry in the background.
|
||||
|
||||
Args:
|
||||
service:
|
||||
"""
|
||||
# Prevent the existing backoff from occuring
|
||||
if self.scheduled_recovery:
|
||||
self.clock.cancel_call_later(self.scheduled_recovery)
|
||||
# Run a retry, which will resechedule a recovery if it fails.
|
||||
run_as_background_process(
|
||||
"retry",
|
||||
self.retry,
|
||||
)
|
||||
|
||||
async def retry(self) -> None:
|
||||
logger.info("Starting retries on %s", self.service.id)
|
||||
try:
|
||||
|
||||
@@ -59,6 +59,7 @@ from synapse.config import ( # noqa: F401
|
||||
tls,
|
||||
tracer,
|
||||
user_directory,
|
||||
user_types,
|
||||
voip,
|
||||
workers,
|
||||
)
|
||||
@@ -122,6 +123,7 @@ class RootConfig:
|
||||
retention: retention.RetentionConfig
|
||||
background_updates: background_updates.BackgroundUpdateConfig
|
||||
auto_accept_invites: auto_accept_invites.AutoAcceptInvitesConfig
|
||||
user_types: user_types.UserTypesConfig
|
||||
|
||||
config_classes: List[Type["Config"]] = ...
|
||||
config_files: List[str]
|
||||
|
||||
@@ -566,3 +566,6 @@ class ExperimentalConfig(Config):
|
||||
"msc4263_limit_key_queries_to_users_who_share_rooms",
|
||||
False,
|
||||
)
|
||||
|
||||
# MSC4155: Invite filtering
|
||||
self.msc4155_enabled: bool = experimental.get("msc4155_enabled", False)
|
||||
|
||||
@@ -94,5 +94,21 @@ class FederationConfig(Config):
|
||||
2**62,
|
||||
)
|
||||
|
||||
def is_domain_allowed_according_to_federation_whitelist(self, domain: str) -> bool:
|
||||
"""
|
||||
Returns whether a domain is allowed according to the federation whitelist. If a
|
||||
federation whitelist is not set, all domains are allowed.
|
||||
|
||||
Args:
|
||||
domain: The domain to test.
|
||||
|
||||
Returns:
|
||||
True if the domain is allowed or if a whitelist is not set, False otherwise.
|
||||
"""
|
||||
if self.federation_domain_whitelist is None:
|
||||
return True
|
||||
|
||||
return domain in self.federation_domain_whitelist
|
||||
|
||||
|
||||
_METRICS_FOR_DOMAINS_SCHEMA = {"type": "array", "items": {"type": "string"}}
|
||||
|
||||
@@ -59,6 +59,7 @@ from .third_party_event_rules import ThirdPartyRulesConfig
|
||||
from .tls import TlsConfig
|
||||
from .tracer import TracerConfig
|
||||
from .user_directory import UserDirectoryConfig
|
||||
from .user_types import UserTypesConfig
|
||||
from .voip import VoipConfig
|
||||
from .workers import WorkerConfig
|
||||
|
||||
@@ -107,4 +108,5 @@ class HomeServerConfig(RootConfig):
|
||||
ExperimentalConfig,
|
||||
BackgroundUpdateConfig,
|
||||
AutoAcceptInvitesConfig,
|
||||
UserTypesConfig,
|
||||
]
|
||||
|
||||
44
synapse/config/user_types.py
Normal file
44
synapse/config/user_types.py
Normal file
@@ -0,0 +1,44 @@
|
||||
#
|
||||
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
||||
#
|
||||
# Copyright (C) 2025 New Vector, Ltd
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# See the GNU Affero General Public License for more details:
|
||||
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
||||
#
|
||||
|
||||
from typing import Any, List, Optional
|
||||
|
||||
from synapse.api.constants import UserTypes
|
||||
from synapse.types import JsonDict
|
||||
|
||||
from ._base import Config, ConfigError
|
||||
|
||||
|
||||
class UserTypesConfig(Config):
|
||||
section = "user_types"
|
||||
|
||||
def read_config(self, config: JsonDict, **kwargs: Any) -> None:
|
||||
user_types: JsonDict = config.get("user_types", {})
|
||||
|
||||
self.default_user_type: Optional[str] = user_types.get(
|
||||
"default_user_type", None
|
||||
)
|
||||
self.extra_user_types: List[str] = user_types.get("extra_user_types", [])
|
||||
|
||||
all_user_types: List[str] = []
|
||||
all_user_types.extend(UserTypes.ALL_BUILTIN_USER_TYPES)
|
||||
all_user_types.extend(self.extra_user_types)
|
||||
|
||||
self.all_user_types = all_user_types
|
||||
|
||||
if self.default_user_type is not None:
|
||||
if self.default_user_type not in all_user_types:
|
||||
raise ConfigError(
|
||||
f"Default user type {self.default_user_type} is not in the list of all user types: {all_user_types}"
|
||||
)
|
||||
@@ -342,6 +342,8 @@ class _DestinationWakeupQueue:
|
||||
destination, _ = self.queue.popitem(last=False)
|
||||
|
||||
queue = self.sender._get_per_destination_queue(destination)
|
||||
if queue is None:
|
||||
continue
|
||||
|
||||
if not queue._new_data_to_send:
|
||||
# The per destination queue has already been woken up.
|
||||
@@ -436,12 +438,23 @@ class FederationSender(AbstractFederationSender):
|
||||
self._wake_destinations_needing_catchup,
|
||||
)
|
||||
|
||||
def _get_per_destination_queue(self, destination: str) -> PerDestinationQueue:
|
||||
def _get_per_destination_queue(
|
||||
self, destination: str
|
||||
) -> Optional[PerDestinationQueue]:
|
||||
"""Get or create a PerDestinationQueue for the given destination
|
||||
|
||||
Args:
|
||||
destination: server_name of remote server
|
||||
|
||||
Returns:
|
||||
None if the destination is not allowed by the federation whitelist.
|
||||
Otherwise a PerDestinationQueue for this destination.
|
||||
"""
|
||||
if not self.hs.config.federation.is_domain_allowed_according_to_federation_whitelist(
|
||||
destination
|
||||
):
|
||||
return None
|
||||
|
||||
queue = self._per_destination_queues.get(destination)
|
||||
if not queue:
|
||||
queue = PerDestinationQueue(self.hs, self._transaction_manager, destination)
|
||||
@@ -718,6 +731,16 @@ class FederationSender(AbstractFederationSender):
|
||||
# track the fact that we have a PDU for these destinations,
|
||||
# to allow us to perform catch-up later on if the remote is unreachable
|
||||
# for a while.
|
||||
# Filter out any destinations not present in the federation_domain_whitelist, if
|
||||
# the whitelist exists. These destinations should not be sent to so let's not
|
||||
# waste time or space keeping track of events destined for them.
|
||||
destinations = [
|
||||
d
|
||||
for d in destinations
|
||||
if self.hs.config.federation.is_domain_allowed_according_to_federation_whitelist(
|
||||
d
|
||||
)
|
||||
]
|
||||
await self.store.store_destination_rooms_entries(
|
||||
destinations,
|
||||
pdu.room_id,
|
||||
@@ -732,7 +755,12 @@ class FederationSender(AbstractFederationSender):
|
||||
)
|
||||
|
||||
for destination in destinations:
|
||||
self._get_per_destination_queue(destination).send_pdu(pdu)
|
||||
queue = self._get_per_destination_queue(destination)
|
||||
# We expect `queue` to not be None as we already filtered out
|
||||
# non-whitelisted destinations above.
|
||||
assert queue is not None
|
||||
|
||||
queue.send_pdu(pdu)
|
||||
|
||||
async def send_read_receipt(self, receipt: ReadReceipt) -> None:
|
||||
"""Send a RR to any other servers in the room
|
||||
@@ -841,12 +869,16 @@ class FederationSender(AbstractFederationSender):
|
||||
for domain in immediate_domains:
|
||||
# Add to destination queue and wake the destination up
|
||||
queue = self._get_per_destination_queue(domain)
|
||||
if queue is None:
|
||||
continue
|
||||
queue.queue_read_receipt(receipt)
|
||||
queue.attempt_new_transaction()
|
||||
|
||||
for domain in delay_domains:
|
||||
# Add to destination queue...
|
||||
queue = self._get_per_destination_queue(domain)
|
||||
if queue is None:
|
||||
continue
|
||||
queue.queue_read_receipt(receipt)
|
||||
|
||||
# ... and schedule the destination to be woken up.
|
||||
@@ -882,9 +914,10 @@ class FederationSender(AbstractFederationSender):
|
||||
if self.is_mine_server_name(destination):
|
||||
continue
|
||||
|
||||
self._get_per_destination_queue(destination).send_presence(
|
||||
states, start_loop=False
|
||||
)
|
||||
queue = self._get_per_destination_queue(destination)
|
||||
if queue is None:
|
||||
continue
|
||||
queue.send_presence(states, start_loop=False)
|
||||
|
||||
self._destination_wakeup_queue.add_to_queue(destination)
|
||||
|
||||
@@ -934,6 +967,8 @@ class FederationSender(AbstractFederationSender):
|
||||
return
|
||||
|
||||
queue = self._get_per_destination_queue(edu.destination)
|
||||
if queue is None:
|
||||
return
|
||||
if key:
|
||||
queue.send_keyed_edu(edu, key)
|
||||
else:
|
||||
@@ -958,9 +993,15 @@ class FederationSender(AbstractFederationSender):
|
||||
|
||||
for destination in destinations:
|
||||
if immediate:
|
||||
self._get_per_destination_queue(destination).attempt_new_transaction()
|
||||
queue = self._get_per_destination_queue(destination)
|
||||
if queue is None:
|
||||
continue
|
||||
queue.attempt_new_transaction()
|
||||
else:
|
||||
self._get_per_destination_queue(destination).mark_new_data()
|
||||
queue = self._get_per_destination_queue(destination)
|
||||
if queue is None:
|
||||
continue
|
||||
queue.mark_new_data()
|
||||
self._destination_wakeup_queue.add_to_queue(destination)
|
||||
|
||||
def wake_destination(self, destination: str) -> None:
|
||||
@@ -979,7 +1020,9 @@ class FederationSender(AbstractFederationSender):
|
||||
):
|
||||
return
|
||||
|
||||
self._get_per_destination_queue(destination).attempt_new_transaction()
|
||||
queue = self._get_per_destination_queue(destination)
|
||||
if queue is not None:
|
||||
queue.attempt_new_transaction()
|
||||
|
||||
@staticmethod
|
||||
def get_current_token() -> int:
|
||||
@@ -1024,6 +1067,9 @@ class FederationSender(AbstractFederationSender):
|
||||
d
|
||||
for d in destinations_to_wake
|
||||
if self._federation_shard_config.should_handle(self._instance_name, d)
|
||||
and self.hs.config.federation.is_domain_allowed_according_to_federation_whitelist(
|
||||
d
|
||||
)
|
||||
]
|
||||
|
||||
for destination in destinations_to_wake:
|
||||
|
||||
@@ -78,6 +78,7 @@ from synapse.replication.http.federation import (
|
||||
ReplicationStoreRoomOnOutlierMembershipRestServlet,
|
||||
)
|
||||
from synapse.storage.databases.main.events_worker import EventRedactBehaviour
|
||||
from synapse.storage.invite_rule import InviteRule
|
||||
from synapse.types import JsonDict, StrCollection, get_domain_from_id
|
||||
from synapse.types.state import StateFilter
|
||||
from synapse.util.async_helpers import Linearizer
|
||||
@@ -1089,6 +1090,20 @@ class FederationHandler:
|
||||
if event.state_key == self._server_notices_mxid:
|
||||
raise SynapseError(HTTPStatus.FORBIDDEN, "Cannot invite this user")
|
||||
|
||||
# check the invitee's configuration and apply rules
|
||||
invite_config = await self.store.get_invite_config_for_user(event.state_key)
|
||||
rule = invite_config.get_invite_rule(event.sender)
|
||||
if rule == InviteRule.BLOCK:
|
||||
logger.info(
|
||||
f"Automatically rejecting invite from {event.sender} due to the invite filtering rules of {event.state_key}"
|
||||
)
|
||||
raise SynapseError(
|
||||
403,
|
||||
"You are not permitted to invite this user.",
|
||||
errcode=Codes.INVITE_BLOCKED,
|
||||
)
|
||||
# InviteRule.IGNORE is handled at the sync layer
|
||||
|
||||
# We retrieve the room member handler here as to not cause a cyclic dependency
|
||||
member_handler = self.hs.get_room_member_handler()
|
||||
# We don't rate limit based on room ID, as that should be done by
|
||||
|
||||
@@ -115,6 +115,7 @@ class RegistrationHandler:
|
||||
self._user_consent_version = self.hs.config.consent.user_consent_version
|
||||
self._server_notices_mxid = hs.config.servernotices.server_notices_mxid
|
||||
self._server_name = hs.hostname
|
||||
self._user_types_config = hs.config.user_types
|
||||
|
||||
self._spam_checker_module_callbacks = hs.get_module_api_callbacks().spam_checker
|
||||
|
||||
@@ -306,6 +307,9 @@ class RegistrationHandler:
|
||||
elif default_display_name is None:
|
||||
default_display_name = localpart
|
||||
|
||||
if user_type is None:
|
||||
user_type = self._user_types_config.default_user_type
|
||||
|
||||
await self.register_with_store(
|
||||
user_id=user_id,
|
||||
password_hash=password_hash,
|
||||
|
||||
@@ -468,17 +468,6 @@ class RoomCreationHandler:
|
||||
"""
|
||||
user_id = requester.user.to_string()
|
||||
|
||||
spam_check = await self._spam_checker_module_callbacks.user_may_create_room(
|
||||
user_id
|
||||
)
|
||||
if spam_check != self._spam_checker_module_callbacks.NOT_SPAM:
|
||||
raise SynapseError(
|
||||
403,
|
||||
"You are not permitted to create rooms",
|
||||
errcode=spam_check[0],
|
||||
additional_fields=spam_check[1],
|
||||
)
|
||||
|
||||
creation_content: JsonDict = {
|
||||
"room_version": new_room_version.identifier,
|
||||
"predecessor": {"room_id": old_room_id, "event_id": tombstone_event_id},
|
||||
@@ -585,6 +574,24 @@ class RoomCreationHandler:
|
||||
if current_power_level_int < needed_power_level:
|
||||
user_power_levels[user_id] = needed_power_level
|
||||
|
||||
# We construct what the body of a call to /createRoom would look like for passing
|
||||
# to the spam checker. We don't include a preset here, as we expect the
|
||||
# initial state to contain everything we need.
|
||||
spam_check = await self._spam_checker_module_callbacks.user_may_create_room(
|
||||
user_id,
|
||||
{
|
||||
"creation_content": creation_content,
|
||||
"initial_state": list(initial_state.items()),
|
||||
},
|
||||
)
|
||||
if spam_check != self._spam_checker_module_callbacks.NOT_SPAM:
|
||||
raise SynapseError(
|
||||
403,
|
||||
"You are not permitted to create rooms",
|
||||
errcode=spam_check[0],
|
||||
additional_fields=spam_check[1],
|
||||
)
|
||||
|
||||
await self._send_events_for_new_room(
|
||||
requester,
|
||||
new_room_id,
|
||||
@@ -786,7 +793,7 @@ class RoomCreationHandler:
|
||||
|
||||
if not is_requester_admin:
|
||||
spam_check = await self._spam_checker_module_callbacks.user_may_create_room(
|
||||
user_id
|
||||
user_id, config
|
||||
)
|
||||
if spam_check != self._spam_checker_module_callbacks.NOT_SPAM:
|
||||
raise SynapseError(
|
||||
|
||||
@@ -53,6 +53,7 @@ from synapse.metrics import event_processing_positions
|
||||
from synapse.metrics.background_process_metrics import run_as_background_process
|
||||
from synapse.replication.http.push import ReplicationCopyPusherRestServlet
|
||||
from synapse.storage.databases.main.state_deltas import StateDelta
|
||||
from synapse.storage.invite_rule import InviteRule
|
||||
from synapse.types import (
|
||||
JsonDict,
|
||||
Requester,
|
||||
@@ -158,6 +159,7 @@ class RoomMemberHandler(metaclass=abc.ABCMeta):
|
||||
store=self.store,
|
||||
clock=self.clock,
|
||||
cfg=hs.config.ratelimiting.rc_invites_per_room,
|
||||
ratelimit_callbacks=hs.get_module_api_callbacks().ratelimit,
|
||||
)
|
||||
|
||||
# Ratelimiter for invites, keyed by recipient (across all rooms, all
|
||||
@@ -166,6 +168,7 @@ class RoomMemberHandler(metaclass=abc.ABCMeta):
|
||||
store=self.store,
|
||||
clock=self.clock,
|
||||
cfg=hs.config.ratelimiting.rc_invites_per_user,
|
||||
ratelimit_callbacks=hs.get_module_api_callbacks().ratelimit,
|
||||
)
|
||||
|
||||
# Ratelimiter for invites, keyed by issuer (across all rooms, all
|
||||
@@ -174,6 +177,7 @@ class RoomMemberHandler(metaclass=abc.ABCMeta):
|
||||
store=self.store,
|
||||
clock=self.clock,
|
||||
cfg=hs.config.ratelimiting.rc_invites_per_issuer,
|
||||
ratelimit_callbacks=hs.get_module_api_callbacks().ratelimit,
|
||||
)
|
||||
|
||||
self._third_party_invite_limiter = Ratelimiter(
|
||||
@@ -912,6 +916,21 @@ class RoomMemberHandler(metaclass=abc.ABCMeta):
|
||||
additional_fields=block_invite_result[1],
|
||||
)
|
||||
|
||||
# check the invitee's configuration and apply rules. Admins on the server can bypass.
|
||||
if not is_requester_admin:
|
||||
invite_config = await self.store.get_invite_config_for_user(target_id)
|
||||
rule = invite_config.get_invite_rule(requester.user.to_string())
|
||||
if rule == InviteRule.BLOCK:
|
||||
logger.info(
|
||||
f"Automatically rejecting invite from {target_id} due to the the invite filtering rules of {requester.user}"
|
||||
)
|
||||
raise SynapseError(
|
||||
403,
|
||||
"You are not permitted to invite this user.",
|
||||
errcode=Codes.INVITE_BLOCKED,
|
||||
)
|
||||
# InviteRule.IGNORE is handled at the sync layer.
|
||||
|
||||
# An empty prev_events list is allowed as long as the auth_event_ids are present
|
||||
if prev_event_ids is not None:
|
||||
return await self._local_membership_update(
|
||||
|
||||
@@ -701,7 +701,7 @@ class RoomSummaryHandler:
|
||||
# The API doesn't return the room version so assume that a
|
||||
# join rule of knock is valid.
|
||||
if (
|
||||
room.get("join_rule")
|
||||
room.get("join_rule", JoinRules.PUBLIC)
|
||||
in (JoinRules.PUBLIC, JoinRules.KNOCK, JoinRules.KNOCK_RESTRICTED)
|
||||
or room.get("world_readable") is True
|
||||
):
|
||||
|
||||
@@ -49,6 +49,7 @@ from synapse.storage.databases.main.state import (
|
||||
Sentinel as StateSentinel,
|
||||
)
|
||||
from synapse.storage.databases.main.stream import CurrentStateDeltaMembership
|
||||
from synapse.storage.invite_rule import InviteRule
|
||||
from synapse.storage.roommember import (
|
||||
RoomsForUser,
|
||||
RoomsForUserSlidingSync,
|
||||
@@ -278,6 +279,7 @@ class SlidingSyncRoomLists:
|
||||
|
||||
# Remove invites from ignored users
|
||||
ignored_users = await self.store.ignored_users(user_id)
|
||||
invite_config = await self.store.get_invite_config_for_user(user_id)
|
||||
if ignored_users:
|
||||
# FIXME: It would be nice to avoid this copy but since
|
||||
# `get_sliding_sync_rooms_for_user_from_membership_snapshots` is cached, it
|
||||
@@ -292,7 +294,14 @@ class SlidingSyncRoomLists:
|
||||
room_for_user_sliding_sync = room_membership_for_user_map[room_id]
|
||||
if (
|
||||
room_for_user_sliding_sync.membership == Membership.INVITE
|
||||
and room_for_user_sliding_sync.sender in ignored_users
|
||||
and room_for_user_sliding_sync.sender
|
||||
and (
|
||||
room_for_user_sliding_sync.sender in ignored_users
|
||||
or invite_config.get_invite_rule(
|
||||
room_for_user_sliding_sync.sender
|
||||
)
|
||||
== InviteRule.IGNORE
|
||||
)
|
||||
):
|
||||
room_membership_for_user_map.pop(room_id, None)
|
||||
|
||||
|
||||
@@ -66,6 +66,7 @@ from synapse.logging.opentracing import (
|
||||
from synapse.storage.databases.main.event_push_actions import RoomNotifCounts
|
||||
from synapse.storage.databases.main.roommember import extract_heroes_from_room_summary
|
||||
from synapse.storage.databases.main.stream import PaginateFunction
|
||||
from synapse.storage.invite_rule import InviteRule
|
||||
from synapse.storage.roommember import MemberSummary
|
||||
from synapse.types import (
|
||||
DeviceListUpdates,
|
||||
@@ -2549,6 +2550,7 @@ class SyncHandler:
|
||||
room_entries: List[RoomSyncResultBuilder] = []
|
||||
invited: List[InvitedSyncResult] = []
|
||||
knocked: List[KnockedSyncResult] = []
|
||||
invite_config = await self.store.get_invite_config_for_user(user_id)
|
||||
for room_id, events in mem_change_events_by_room_id.items():
|
||||
# The body of this loop will add this room to at least one of the five lists
|
||||
# above. Things get messy if you've e.g. joined, left, joined then left the
|
||||
@@ -2631,7 +2633,11 @@ class SyncHandler:
|
||||
# Only bother if we're still currently invited
|
||||
should_invite = last_non_join.membership == Membership.INVITE
|
||||
if should_invite:
|
||||
if last_non_join.sender not in ignored_users:
|
||||
if (
|
||||
last_non_join.sender not in ignored_users
|
||||
and invite_config.get_invite_rule(last_non_join.sender)
|
||||
!= InviteRule.IGNORE
|
||||
):
|
||||
invite_room_sync = InvitedSyncResult(room_id, invite=last_non_join)
|
||||
if invite_room_sync:
|
||||
invited.append(invite_room_sync)
|
||||
@@ -2786,6 +2792,7 @@ class SyncHandler:
|
||||
membership_list=Membership.LIST,
|
||||
excluded_rooms=sync_result_builder.excluded_room_ids,
|
||||
)
|
||||
invite_config = await self.store.get_invite_config_for_user(user_id)
|
||||
|
||||
room_entries = []
|
||||
invited = []
|
||||
@@ -2811,6 +2818,8 @@ class SyncHandler:
|
||||
elif event.membership == Membership.INVITE:
|
||||
if event.sender in ignored_users:
|
||||
continue
|
||||
if invite_config.get_invite_rule(event.sender) == InviteRule.IGNORE:
|
||||
continue
|
||||
invite = await self.store.get_event(event.event_id)
|
||||
invited.append(InvitedSyncResult(room_id=event.room_id, invite=invite))
|
||||
elif event.membership == Membership.KNOCK:
|
||||
|
||||
@@ -90,6 +90,14 @@ from synapse.module_api.callbacks.account_validity_callbacks import (
|
||||
ON_USER_LOGIN_CALLBACK,
|
||||
ON_USER_REGISTRATION_CALLBACK,
|
||||
)
|
||||
from synapse.module_api.callbacks.media_repository_callbacks import (
|
||||
GET_MEDIA_CONFIG_FOR_USER_CALLBACK,
|
||||
IS_USER_ALLOWED_TO_UPLOAD_MEDIA_OF_SIZE_CALLBACK,
|
||||
)
|
||||
from synapse.module_api.callbacks.ratelimit_callbacks import (
|
||||
GET_RATELIMIT_OVERRIDE_FOR_USER_CALLBACK,
|
||||
RatelimitOverride,
|
||||
)
|
||||
from synapse.module_api.callbacks.spamchecker_callbacks import (
|
||||
CHECK_EVENT_FOR_SPAM_CALLBACK,
|
||||
CHECK_LOGIN_FOR_SPAM_CALLBACK,
|
||||
@@ -103,6 +111,7 @@ from synapse.module_api.callbacks.spamchecker_callbacks import (
|
||||
USER_MAY_JOIN_ROOM_CALLBACK,
|
||||
USER_MAY_PUBLISH_ROOM_CALLBACK,
|
||||
USER_MAY_SEND_3PID_INVITE_CALLBACK,
|
||||
USER_MAY_SEND_STATE_EVENT_CALLBACK,
|
||||
SpamCheckerModuleApiCallbacks,
|
||||
)
|
||||
from synapse.module_api.callbacks.third_party_event_rules_callbacks import (
|
||||
@@ -189,6 +198,7 @@ __all__ = [
|
||||
"ProfileInfo",
|
||||
"RoomAlias",
|
||||
"UserProfile",
|
||||
"RatelimitOverride",
|
||||
]
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -311,6 +321,7 @@ class ModuleApi:
|
||||
USER_MAY_CREATE_ROOM_ALIAS_CALLBACK
|
||||
] = None,
|
||||
user_may_publish_room: Optional[USER_MAY_PUBLISH_ROOM_CALLBACK] = None,
|
||||
user_may_send_state_event: Optional[USER_MAY_SEND_STATE_EVENT_CALLBACK] = None,
|
||||
check_username_for_spam: Optional[CHECK_USERNAME_FOR_SPAM_CALLBACK] = None,
|
||||
check_registration_for_spam: Optional[
|
||||
CHECK_REGISTRATION_FOR_SPAM_CALLBACK
|
||||
@@ -335,6 +346,7 @@ class ModuleApi:
|
||||
check_registration_for_spam=check_registration_for_spam,
|
||||
check_media_file_for_spam=check_media_file_for_spam,
|
||||
check_login_for_spam=check_login_for_spam,
|
||||
user_may_send_state_event=user_may_send_state_event,
|
||||
)
|
||||
|
||||
def register_account_validity_callbacks(
|
||||
@@ -360,6 +372,36 @@ class ModuleApi:
|
||||
on_legacy_admin_request=on_legacy_admin_request,
|
||||
)
|
||||
|
||||
def register_ratelimit_callbacks(
|
||||
self,
|
||||
*,
|
||||
get_ratelimit_override_for_user: Optional[
|
||||
GET_RATELIMIT_OVERRIDE_FOR_USER_CALLBACK
|
||||
] = None,
|
||||
) -> None:
|
||||
"""Registers callbacks for ratelimit capabilities.
|
||||
Added in Synapse v1.132.0.
|
||||
"""
|
||||
return self._callbacks.ratelimit.register_callbacks(
|
||||
get_ratelimit_override_for_user=get_ratelimit_override_for_user,
|
||||
)
|
||||
|
||||
def register_media_repository_callbacks(
|
||||
self,
|
||||
*,
|
||||
get_media_config_for_user: Optional[GET_MEDIA_CONFIG_FOR_USER_CALLBACK] = None,
|
||||
is_user_allowed_to_upload_media_of_size: Optional[
|
||||
IS_USER_ALLOWED_TO_UPLOAD_MEDIA_OF_SIZE_CALLBACK
|
||||
] = None,
|
||||
) -> None:
|
||||
"""Registers callbacks for media repository capabilities.
|
||||
Added in Synapse v1.132.0.
|
||||
"""
|
||||
return self._callbacks.media_repository.register_callbacks(
|
||||
get_media_config_for_user=get_media_config_for_user,
|
||||
is_user_allowed_to_upload_media_of_size=is_user_allowed_to_upload_media_of_size,
|
||||
)
|
||||
|
||||
def register_third_party_rules_callbacks(
|
||||
self,
|
||||
*,
|
||||
|
||||
@@ -27,6 +27,12 @@ if TYPE_CHECKING:
|
||||
from synapse.module_api.callbacks.account_validity_callbacks import (
|
||||
AccountValidityModuleApiCallbacks,
|
||||
)
|
||||
from synapse.module_api.callbacks.media_repository_callbacks import (
|
||||
MediaRepositoryModuleApiCallbacks,
|
||||
)
|
||||
from synapse.module_api.callbacks.ratelimit_callbacks import (
|
||||
RatelimitModuleApiCallbacks,
|
||||
)
|
||||
from synapse.module_api.callbacks.spamchecker_callbacks import (
|
||||
SpamCheckerModuleApiCallbacks,
|
||||
)
|
||||
@@ -38,5 +44,7 @@ from synapse.module_api.callbacks.third_party_event_rules_callbacks import (
|
||||
class ModuleApiCallbacks:
|
||||
def __init__(self, hs: "HomeServer") -> None:
|
||||
self.account_validity = AccountValidityModuleApiCallbacks()
|
||||
self.media_repository = MediaRepositoryModuleApiCallbacks(hs)
|
||||
self.ratelimit = RatelimitModuleApiCallbacks(hs)
|
||||
self.spam_checker = SpamCheckerModuleApiCallbacks(hs)
|
||||
self.third_party_event_rules = ThirdPartyEventRulesModuleApiCallbacks(hs)
|
||||
|
||||
76
synapse/module_api/callbacks/media_repository_callbacks.py
Normal file
76
synapse/module_api/callbacks/media_repository_callbacks.py
Normal file
@@ -0,0 +1,76 @@
|
||||
#
|
||||
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
||||
#
|
||||
# Copyright (C) 2025 New Vector, Ltd
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# See the GNU Affero General Public License for more details:
|
||||
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
||||
#
|
||||
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Awaitable, Callable, List, Optional
|
||||
|
||||
from synapse.types import JsonDict
|
||||
from synapse.util.async_helpers import delay_cancellation
|
||||
from synapse.util.metrics import Measure
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from synapse.server import HomeServer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
GET_MEDIA_CONFIG_FOR_USER_CALLBACK = Callable[[str], Awaitable[Optional[JsonDict]]]
|
||||
|
||||
IS_USER_ALLOWED_TO_UPLOAD_MEDIA_OF_SIZE_CALLBACK = Callable[[str, int], Awaitable[bool]]
|
||||
|
||||
|
||||
class MediaRepositoryModuleApiCallbacks:
|
||||
def __init__(self, hs: "HomeServer") -> None:
|
||||
self.clock = hs.get_clock()
|
||||
self._get_media_config_for_user_callbacks: List[
|
||||
GET_MEDIA_CONFIG_FOR_USER_CALLBACK
|
||||
] = []
|
||||
self._is_user_allowed_to_upload_media_of_size_callbacks: List[
|
||||
IS_USER_ALLOWED_TO_UPLOAD_MEDIA_OF_SIZE_CALLBACK
|
||||
] = []
|
||||
|
||||
def register_callbacks(
|
||||
self,
|
||||
get_media_config_for_user: Optional[GET_MEDIA_CONFIG_FOR_USER_CALLBACK] = None,
|
||||
is_user_allowed_to_upload_media_of_size: Optional[
|
||||
IS_USER_ALLOWED_TO_UPLOAD_MEDIA_OF_SIZE_CALLBACK
|
||||
] = None,
|
||||
) -> None:
|
||||
"""Register callbacks from module for each hook."""
|
||||
if get_media_config_for_user is not None:
|
||||
self._get_media_config_for_user_callbacks.append(get_media_config_for_user)
|
||||
|
||||
if is_user_allowed_to_upload_media_of_size is not None:
|
||||
self._is_user_allowed_to_upload_media_of_size_callbacks.append(
|
||||
is_user_allowed_to_upload_media_of_size
|
||||
)
|
||||
|
||||
async def get_media_config_for_user(self, user_id: str) -> Optional[JsonDict]:
|
||||
for callback in self._get_media_config_for_user_callbacks:
|
||||
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
||||
res: Optional[JsonDict] = await delay_cancellation(callback(user_id))
|
||||
if res:
|
||||
return res
|
||||
|
||||
return None
|
||||
|
||||
async def is_user_allowed_to_upload_media_of_size(
|
||||
self, user_id: str, size: int
|
||||
) -> bool:
|
||||
for callback in self._is_user_allowed_to_upload_media_of_size_callbacks:
|
||||
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
||||
res: bool = await delay_cancellation(callback(user_id, size))
|
||||
if not res:
|
||||
return res
|
||||
|
||||
return True
|
||||
74
synapse/module_api/callbacks/ratelimit_callbacks.py
Normal file
74
synapse/module_api/callbacks/ratelimit_callbacks.py
Normal file
@@ -0,0 +1,74 @@
|
||||
#
|
||||
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
||||
#
|
||||
# Copyright (C) 2025 New Vector, Ltd
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# See the GNU Affero General Public License for more details:
|
||||
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
||||
#
|
||||
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Awaitable, Callable, List, Optional
|
||||
|
||||
import attr
|
||||
|
||||
from synapse.util.async_helpers import delay_cancellation
|
||||
from synapse.util.metrics import Measure
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from synapse.server import HomeServer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@attr.s(auto_attribs=True)
|
||||
class RatelimitOverride:
|
||||
"""Represents a ratelimit being overridden."""
|
||||
|
||||
per_second: float
|
||||
"""The number of actions that can be performed in a second. `0.0` means that ratelimiting is disabled."""
|
||||
burst_count: int
|
||||
"""How many actions that can be performed before being limited."""
|
||||
|
||||
|
||||
GET_RATELIMIT_OVERRIDE_FOR_USER_CALLBACK = Callable[
|
||||
[str, str], Awaitable[Optional[RatelimitOverride]]
|
||||
]
|
||||
|
||||
|
||||
class RatelimitModuleApiCallbacks:
|
||||
def __init__(self, hs: "HomeServer") -> None:
|
||||
self.clock = hs.get_clock()
|
||||
self._get_ratelimit_override_for_user_callbacks: List[
|
||||
GET_RATELIMIT_OVERRIDE_FOR_USER_CALLBACK
|
||||
] = []
|
||||
|
||||
def register_callbacks(
|
||||
self,
|
||||
get_ratelimit_override_for_user: Optional[
|
||||
GET_RATELIMIT_OVERRIDE_FOR_USER_CALLBACK
|
||||
] = None,
|
||||
) -> None:
|
||||
"""Register callbacks from module for each hook."""
|
||||
if get_ratelimit_override_for_user is not None:
|
||||
self._get_ratelimit_override_for_user_callbacks.append(
|
||||
get_ratelimit_override_for_user
|
||||
)
|
||||
|
||||
async def get_ratelimit_override_for_user(
|
||||
self, user_id: str, limiter_name: str
|
||||
) -> Optional[RatelimitOverride]:
|
||||
for callback in self._get_ratelimit_override_for_user_callbacks:
|
||||
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
||||
res: Optional[RatelimitOverride] = await delay_cancellation(
|
||||
callback(user_id, limiter_name)
|
||||
)
|
||||
if res:
|
||||
return res
|
||||
|
||||
return None
|
||||
@@ -22,6 +22,7 @@
|
||||
import functools
|
||||
import inspect
|
||||
import logging
|
||||
from copy import deepcopy
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Any,
|
||||
@@ -120,20 +121,24 @@ USER_MAY_SEND_3PID_INVITE_CALLBACK = Callable[
|
||||
]
|
||||
],
|
||||
]
|
||||
USER_MAY_CREATE_ROOM_CALLBACK = Callable[
|
||||
[str],
|
||||
Awaitable[
|
||||
Union[
|
||||
Literal["NOT_SPAM"],
|
||||
Codes,
|
||||
# Highly experimental, not officially part of the spamchecker API, may
|
||||
# disappear without warning depending on the results of ongoing
|
||||
# experiments.
|
||||
# Use this to return additional information as part of an error.
|
||||
Tuple[Codes, JsonDict],
|
||||
# Deprecated
|
||||
bool,
|
||||
]
|
||||
USER_MAY_CREATE_ROOM_CALLBACK_RETURN_VALUE = Union[
|
||||
Literal["NOT_SPAM"],
|
||||
Codes,
|
||||
# Highly experimental, not officially part of the spamchecker API, may
|
||||
# disappear without warning depending on the results of ongoing
|
||||
# experiments.
|
||||
# Use this to return additional information as part of an error.
|
||||
Tuple[Codes, JsonDict],
|
||||
# Deprecated
|
||||
bool,
|
||||
]
|
||||
USER_MAY_CREATE_ROOM_CALLBACK = Union[
|
||||
Callable[
|
||||
[str, JsonDict],
|
||||
Awaitable[USER_MAY_CREATE_ROOM_CALLBACK_RETURN_VALUE],
|
||||
],
|
||||
Callable[ # Single argument variant for backwards compatibility
|
||||
[str], Awaitable[USER_MAY_CREATE_ROOM_CALLBACK_RETURN_VALUE]
|
||||
],
|
||||
]
|
||||
USER_MAY_CREATE_ROOM_ALIAS_CALLBACK = Callable[
|
||||
@@ -168,6 +173,20 @@ USER_MAY_PUBLISH_ROOM_CALLBACK = Callable[
|
||||
]
|
||||
],
|
||||
]
|
||||
USER_MAY_SEND_STATE_EVENT_CALLBACK = Callable[
|
||||
[str, str, str, str, JsonDict],
|
||||
Awaitable[
|
||||
Union[
|
||||
Literal["NOT_SPAM"],
|
||||
Codes,
|
||||
# Highly experimental, not officially part of the spamchecker API, may
|
||||
# disappear without warning depending on the results of ongoing
|
||||
# experiments.
|
||||
# Use this to return additional information as part of an error.
|
||||
Tuple[Codes, JsonDict],
|
||||
]
|
||||
],
|
||||
]
|
||||
CHECK_USERNAME_FOR_SPAM_CALLBACK = Union[
|
||||
Callable[[UserProfile], Awaitable[bool]],
|
||||
Callable[[UserProfile, str], Awaitable[bool]],
|
||||
@@ -332,6 +351,9 @@ class SpamCheckerModuleApiCallbacks:
|
||||
USER_MAY_SEND_3PID_INVITE_CALLBACK
|
||||
] = []
|
||||
self._user_may_create_room_callbacks: List[USER_MAY_CREATE_ROOM_CALLBACK] = []
|
||||
self._user_may_send_state_event_callbacks: List[
|
||||
USER_MAY_SEND_STATE_EVENT_CALLBACK
|
||||
] = []
|
||||
self._user_may_create_room_alias_callbacks: List[
|
||||
USER_MAY_CREATE_ROOM_ALIAS_CALLBACK
|
||||
] = []
|
||||
@@ -367,6 +389,7 @@ class SpamCheckerModuleApiCallbacks:
|
||||
] = None,
|
||||
check_media_file_for_spam: Optional[CHECK_MEDIA_FILE_FOR_SPAM_CALLBACK] = None,
|
||||
check_login_for_spam: Optional[CHECK_LOGIN_FOR_SPAM_CALLBACK] = None,
|
||||
user_may_send_state_event: Optional[USER_MAY_SEND_STATE_EVENT_CALLBACK] = None,
|
||||
) -> None:
|
||||
"""Register callbacks from module for each hook."""
|
||||
if check_event_for_spam is not None:
|
||||
@@ -391,6 +414,11 @@ class SpamCheckerModuleApiCallbacks:
|
||||
if user_may_create_room is not None:
|
||||
self._user_may_create_room_callbacks.append(user_may_create_room)
|
||||
|
||||
if user_may_send_state_event is not None:
|
||||
self._user_may_send_state_event_callbacks.append(
|
||||
user_may_send_state_event,
|
||||
)
|
||||
|
||||
if user_may_create_room_alias is not None:
|
||||
self._user_may_create_room_alias_callbacks.append(
|
||||
user_may_create_room_alias,
|
||||
@@ -622,16 +650,41 @@ class SpamCheckerModuleApiCallbacks:
|
||||
return self.NOT_SPAM
|
||||
|
||||
async def user_may_create_room(
|
||||
self, userid: str
|
||||
self, userid: str, room_config: JsonDict
|
||||
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
|
||||
"""Checks if a given user may create a room
|
||||
|
||||
Args:
|
||||
userid: The ID of the user attempting to create a room
|
||||
room_config: The room creation configuration which is the body of the /createRoom request
|
||||
"""
|
||||
for callback in self._user_may_create_room_callbacks:
|
||||
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
||||
res = await delay_cancellation(callback(userid))
|
||||
checker_args = inspect.signature(callback)
|
||||
# Also ensure backwards compatibility with spam checker callbacks
|
||||
# that don't expect the room_config argument.
|
||||
if len(checker_args.parameters) == 2:
|
||||
callback_with_requester_id = cast(
|
||||
Callable[
|
||||
[str, JsonDict],
|
||||
Awaitable[USER_MAY_CREATE_ROOM_CALLBACK_RETURN_VALUE],
|
||||
],
|
||||
callback,
|
||||
)
|
||||
# We make a copy of the config to ensure the spam checker cannot modify it.
|
||||
res = await delay_cancellation(
|
||||
callback_with_requester_id(userid, deepcopy(room_config))
|
||||
)
|
||||
else:
|
||||
callback_without_requester_id = cast(
|
||||
Callable[
|
||||
[str], Awaitable[USER_MAY_CREATE_ROOM_CALLBACK_RETURN_VALUE]
|
||||
],
|
||||
callback,
|
||||
)
|
||||
res = await delay_cancellation(
|
||||
callback_without_requester_id(userid)
|
||||
)
|
||||
if res is True or res is self.NOT_SPAM:
|
||||
continue
|
||||
elif res is False:
|
||||
@@ -653,6 +706,40 @@ class SpamCheckerModuleApiCallbacks:
|
||||
|
||||
return self.NOT_SPAM
|
||||
|
||||
async def user_may_send_state_event(
|
||||
self,
|
||||
user_id: str,
|
||||
room_id: str,
|
||||
event_type: str,
|
||||
state_key: str,
|
||||
content: JsonDict,
|
||||
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
|
||||
"""Checks if a given user may create a room with a given visibility
|
||||
Args:
|
||||
user_id: The ID of the user attempting to create a room
|
||||
room_id: The ID of the room that the event will be sent to
|
||||
event_type: The type of the state event
|
||||
state_key: The state key of the state event
|
||||
content: The content of the state event
|
||||
"""
|
||||
for callback in self._user_may_send_state_event_callbacks:
|
||||
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
||||
# We make a copy of the content to ensure that the spam checker cannot modify it.
|
||||
res = await delay_cancellation(
|
||||
callback(user_id, room_id, event_type, state_key, deepcopy(content))
|
||||
)
|
||||
if res is self.NOT_SPAM:
|
||||
continue
|
||||
elif isinstance(res, synapse.api.errors.Codes):
|
||||
return res, {}
|
||||
else:
|
||||
logger.warning(
|
||||
"Module returned invalid value, rejecting room creation as spam"
|
||||
)
|
||||
return synapse.api.errors.Codes.FORBIDDEN, {}
|
||||
|
||||
return self.NOT_SPAM
|
||||
|
||||
async def user_may_create_room_alias(
|
||||
self, userid: str, room_alias: RoomAlias
|
||||
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
|
||||
|
||||
@@ -52,6 +52,7 @@ from synapse.events.snapshot import EventContext
|
||||
from synapse.logging.context import make_deferred_yieldable, run_in_background
|
||||
from synapse.state import POWER_KEY
|
||||
from synapse.storage.databases.main.roommember import EventIdMembership
|
||||
from synapse.storage.invite_rule import InviteRule
|
||||
from synapse.storage.roommember import ProfileInfo
|
||||
from synapse.synapse_rust.push import FilteredPushRules, PushRuleEvaluator
|
||||
from synapse.types import JsonValue
|
||||
@@ -191,9 +192,17 @@ class BulkPushRuleEvaluator:
|
||||
|
||||
# if this event is an invite event, we may need to run rules for the user
|
||||
# who's been invited, otherwise they won't get told they've been invited
|
||||
if event.type == EventTypes.Member and event.membership == Membership.INVITE:
|
||||
if (
|
||||
event.is_state()
|
||||
and event.type == EventTypes.Member
|
||||
and event.membership == Membership.INVITE
|
||||
):
|
||||
invited = event.state_key
|
||||
if invited and self.hs.is_mine_id(invited) and invited not in local_users:
|
||||
invite_config = await self.store.get_invite_config_for_user(invited)
|
||||
if invite_config.get_invite_rule(event.sender) != InviteRule.ALLOW:
|
||||
# Invite was blocked or ignored, never notify.
|
||||
return {}
|
||||
if self.hs.is_mine_id(invited) and invited not in local_users:
|
||||
local_users.append(invited)
|
||||
|
||||
if not local_users:
|
||||
|
||||
@@ -28,7 +28,7 @@ from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
|
||||
import attr
|
||||
|
||||
from synapse._pydantic_compat import StrictBool, StrictInt, StrictStr
|
||||
from synapse.api.constants import Direction, UserTypes
|
||||
from synapse.api.constants import Direction
|
||||
from synapse.api.errors import Codes, NotFoundError, SynapseError
|
||||
from synapse.http.servlet import (
|
||||
RestServlet,
|
||||
@@ -230,6 +230,7 @@ class UserRestServletV2(RestServlet):
|
||||
self.registration_handler = hs.get_registration_handler()
|
||||
self.pusher_pool = hs.get_pusherpool()
|
||||
self._msc3866_enabled = hs.config.experimental.msc3866.enabled
|
||||
self._all_user_types = hs.config.user_types.all_user_types
|
||||
|
||||
async def on_GET(
|
||||
self, request: SynapseRequest, user_id: str
|
||||
@@ -277,7 +278,7 @@ class UserRestServletV2(RestServlet):
|
||||
assert_params_in_dict(external_id, ["auth_provider", "external_id"])
|
||||
|
||||
user_type = body.get("user_type", None)
|
||||
if user_type is not None and user_type not in UserTypes.ALL_USER_TYPES:
|
||||
if user_type is not None and user_type not in self._all_user_types:
|
||||
raise SynapseError(HTTPStatus.BAD_REQUEST, "Invalid user type")
|
||||
|
||||
set_admin_to = body.get("admin", False)
|
||||
@@ -524,6 +525,7 @@ class UserRegisterServlet(RestServlet):
|
||||
self.reactor = hs.get_reactor()
|
||||
self.nonces: Dict[str, int] = {}
|
||||
self.hs = hs
|
||||
self._all_user_types = hs.config.user_types.all_user_types
|
||||
|
||||
def _clear_old_nonces(self) -> None:
|
||||
"""
|
||||
@@ -605,7 +607,7 @@ class UserRegisterServlet(RestServlet):
|
||||
user_type = body.get("user_type", None)
|
||||
displayname = body.get("displayname", None)
|
||||
|
||||
if user_type is not None and user_type not in UserTypes.ALL_USER_TYPES:
|
||||
if user_type is not None and user_type not in self._all_user_types:
|
||||
raise SynapseError(HTTPStatus.BAD_REQUEST, "Invalid user type")
|
||||
|
||||
if "mac" not in body:
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
||||
#
|
||||
# Copyright 2023 Tulir Asokan
|
||||
# Copyright (C) 2023 New Vector, Ltd
|
||||
# Copyright (C) 2023, 2025 New Vector, Ltd
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
@@ -53,6 +53,7 @@ class AppservicePingRestServlet(RestServlet):
|
||||
def __init__(self, hs: "HomeServer"):
|
||||
super().__init__()
|
||||
self.as_api = hs.get_application_service_api()
|
||||
self.scheduler = hs.get_application_service_scheduler()
|
||||
self.auth = hs.get_auth()
|
||||
|
||||
async def on_POST(
|
||||
@@ -85,6 +86,10 @@ class AppservicePingRestServlet(RestServlet):
|
||||
start = time.monotonic()
|
||||
try:
|
||||
await self.as_api.ping(requester.app_service, txn_id)
|
||||
|
||||
# We got a OK response, so if the AS needs to be recovered then lets recover it now.
|
||||
# This sets off a task in the background and so is safe to execute and forget.
|
||||
self.scheduler.txn_ctrl.force_retry(requester.app_service)
|
||||
except RequestTimedOutError as e:
|
||||
raise SynapseError(
|
||||
HTTPStatus.GATEWAY_TIMEOUT,
|
||||
|
||||
@@ -102,10 +102,17 @@ class MediaConfigResource(RestServlet):
|
||||
self.clock = hs.get_clock()
|
||||
self.auth = hs.get_auth()
|
||||
self.limits_dict = {"m.upload.size": config.media.max_upload_size}
|
||||
self.media_repository_callbacks = hs.get_module_api_callbacks().media_repository
|
||||
|
||||
async def on_GET(self, request: SynapseRequest) -> None:
|
||||
await self.auth.get_user_by_req(request)
|
||||
respond_with_json(request, 200, self.limits_dict, send_cors=True)
|
||||
requester = await self.auth.get_user_by_req(request)
|
||||
user_specific_config = (
|
||||
await self.media_repository_callbacks.get_media_config_for_user(
|
||||
requester.user.to_string(),
|
||||
)
|
||||
)
|
||||
response = user_specific_config if user_specific_config else self.limits_dict
|
||||
respond_with_json(request, 200, response, send_cors=True)
|
||||
|
||||
|
||||
class ThumbnailResource(RestServlet):
|
||||
|
||||
@@ -198,6 +198,7 @@ class RoomStateEventRestServlet(RestServlet):
|
||||
self.delayed_events_handler = hs.get_delayed_events_handler()
|
||||
self.auth = hs.get_auth()
|
||||
self._max_event_delay_ms = hs.config.server.max_event_delay_ms
|
||||
self._spam_checker_module_callbacks = hs.get_module_api_callbacks().spam_checker
|
||||
|
||||
def register(self, http_server: HttpServer) -> None:
|
||||
# /rooms/$roomid/state/$eventtype
|
||||
@@ -289,6 +290,25 @@ class RoomStateEventRestServlet(RestServlet):
|
||||
|
||||
content = parse_json_object_from_request(request)
|
||||
|
||||
is_requester_admin = await self.auth.is_server_admin(requester)
|
||||
if not is_requester_admin:
|
||||
spam_check = (
|
||||
await self._spam_checker_module_callbacks.user_may_send_state_event(
|
||||
user_id=requester.user.to_string(),
|
||||
room_id=room_id,
|
||||
event_type=event_type,
|
||||
state_key=state_key,
|
||||
content=content,
|
||||
)
|
||||
)
|
||||
if spam_check != self._spam_checker_module_callbacks.NOT_SPAM:
|
||||
raise SynapseError(
|
||||
403,
|
||||
"You are not permitted to send the state event",
|
||||
errcode=spam_check[0],
|
||||
additional_fields=spam_check[1],
|
||||
)
|
||||
|
||||
origin_server_ts = None
|
||||
if requester.app_service:
|
||||
origin_server_ts = parse_integer(request, "ts")
|
||||
|
||||
@@ -174,6 +174,8 @@ class VersionsRestServlet(RestServlet):
|
||||
"org.matrix.simplified_msc3575": msc3575_enabled,
|
||||
# Arbitrary key-value profile fields.
|
||||
"uk.tcpip.msc4133": self.config.experimental.msc4133_enabled,
|
||||
# MSC4155: Invite filtering
|
||||
"org.matrix.msc4155": self.config.experimental.msc4155_enabled,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
@@ -40,7 +40,14 @@ class MediaConfigResource(RestServlet):
|
||||
self.clock = hs.get_clock()
|
||||
self.auth = hs.get_auth()
|
||||
self.limits_dict = {"m.upload.size": config.media.max_upload_size}
|
||||
self.media_repository_callbacks = hs.get_module_api_callbacks().media_repository
|
||||
|
||||
async def on_GET(self, request: SynapseRequest) -> None:
|
||||
await self.auth.get_user_by_req(request)
|
||||
respond_with_json(request, 200, self.limits_dict, send_cors=True)
|
||||
requester = await self.auth.get_user_by_req(request)
|
||||
user_specific_config = (
|
||||
await self.media_repository_callbacks.get_media_config_for_user(
|
||||
requester.user.to_string()
|
||||
)
|
||||
)
|
||||
response = user_specific_config if user_specific_config else self.limits_dict
|
||||
respond_with_json(request, 200, response, send_cors=True)
|
||||
|
||||
@@ -50,9 +50,12 @@ class BaseUploadServlet(RestServlet):
|
||||
self.server_name = hs.hostname
|
||||
self.auth = hs.get_auth()
|
||||
self.max_upload_size = hs.config.media.max_upload_size
|
||||
self._media_repository_callbacks = (
|
||||
hs.get_module_api_callbacks().media_repository
|
||||
)
|
||||
|
||||
def _get_file_metadata(
|
||||
self, request: SynapseRequest
|
||||
async def _get_file_metadata(
|
||||
self, request: SynapseRequest, user_id: str
|
||||
) -> Tuple[int, Optional[str], str]:
|
||||
raw_content_length = request.getHeader("Content-Length")
|
||||
if raw_content_length is None:
|
||||
@@ -67,7 +70,14 @@ class BaseUploadServlet(RestServlet):
|
||||
code=413,
|
||||
errcode=Codes.TOO_LARGE,
|
||||
)
|
||||
|
||||
if not await self._media_repository_callbacks.is_user_allowed_to_upload_media_of_size(
|
||||
user_id, content_length
|
||||
):
|
||||
raise SynapseError(
|
||||
msg="Upload request body is too large",
|
||||
code=413,
|
||||
errcode=Codes.TOO_LARGE,
|
||||
)
|
||||
args: Dict[bytes, List[bytes]] = request.args # type: ignore
|
||||
upload_name_bytes = parse_bytes_from_args(args, "filename")
|
||||
if upload_name_bytes:
|
||||
@@ -104,7 +114,9 @@ class UploadServlet(BaseUploadServlet):
|
||||
|
||||
async def on_POST(self, request: SynapseRequest) -> None:
|
||||
requester = await self.auth.get_user_by_req(request)
|
||||
content_length, upload_name, media_type = self._get_file_metadata(request)
|
||||
content_length, upload_name, media_type = await self._get_file_metadata(
|
||||
request, requester.user.to_string()
|
||||
)
|
||||
|
||||
try:
|
||||
content: IO = request.content # type: ignore
|
||||
@@ -152,7 +164,9 @@ class AsyncUploadServlet(BaseUploadServlet):
|
||||
|
||||
async with lock:
|
||||
await self.media_repo.verify_can_upload(media_id, requester.user)
|
||||
content_length, upload_name, media_type = self._get_file_metadata(request)
|
||||
content_length, upload_name, media_type = await self._get_file_metadata(
|
||||
request, requester.user.to_string()
|
||||
)
|
||||
|
||||
try:
|
||||
content: IO = request.content # type: ignore
|
||||
|
||||
@@ -34,6 +34,7 @@ from typing import (
|
||||
)
|
||||
|
||||
from synapse.api.constants import AccountDataTypes
|
||||
from synapse.api.errors import Codes, SynapseError
|
||||
from synapse.replication.tcp.streams import AccountDataStream
|
||||
from synapse.storage._base import db_to_json
|
||||
from synapse.storage.database import (
|
||||
@@ -43,6 +44,7 @@ from synapse.storage.database import (
|
||||
)
|
||||
from synapse.storage.databases.main.cache import CacheInvalidationWorkerStore
|
||||
from synapse.storage.databases.main.push_rule import PushRulesWorkerStore
|
||||
from synapse.storage.invite_rule import InviteRulesConfig
|
||||
from synapse.storage.util.id_generators import MultiWriterIdGenerator
|
||||
from synapse.types import JsonDict, JsonMapping
|
||||
from synapse.util import json_encoder
|
||||
@@ -102,6 +104,8 @@ class AccountDataWorkerStore(PushRulesWorkerStore, CacheInvalidationWorkerStore)
|
||||
self._delete_account_data_for_deactivated_users,
|
||||
)
|
||||
|
||||
self._msc4155_enabled = hs.config.experimental.msc4155_enabled
|
||||
|
||||
def get_max_account_data_stream_id(self) -> int:
|
||||
"""Get the current max stream ID for account data stream
|
||||
|
||||
@@ -557,6 +561,23 @@ class AccountDataWorkerStore(PushRulesWorkerStore, CacheInvalidationWorkerStore)
|
||||
)
|
||||
)
|
||||
|
||||
async def get_invite_config_for_user(self, user_id: str) -> InviteRulesConfig:
|
||||
"""
|
||||
Get the invite configuration for the current user.
|
||||
|
||||
Args:
|
||||
user_id:
|
||||
"""
|
||||
|
||||
if not self._msc4155_enabled:
|
||||
# This equates to allowing all invites, as if the setting was off.
|
||||
return InviteRulesConfig(None)
|
||||
|
||||
data = await self.get_global_account_data_by_type_for_user(
|
||||
user_id, AccountDataTypes.MSC4155_INVITE_PERMISSION_CONFIG
|
||||
)
|
||||
return InviteRulesConfig(data)
|
||||
|
||||
def process_replication_rows(
|
||||
self,
|
||||
stream_name: str,
|
||||
@@ -760,6 +781,9 @@ class AccountDataWorkerStore(PushRulesWorkerStore, CacheInvalidationWorkerStore)
|
||||
else:
|
||||
currently_ignored_users = set()
|
||||
|
||||
if user_id in currently_ignored_users:
|
||||
raise SynapseError(400, "You cannot ignore yourself", Codes.INVALID_PARAM)
|
||||
|
||||
# If the data has not changed, nothing to do.
|
||||
if previously_ignored_users == currently_ignored_users:
|
||||
return
|
||||
|
||||
@@ -583,7 +583,9 @@ class RegistrationWorkerStore(CacheInvalidationWorkerStore):
|
||||
|
||||
await self.db_pool.runInteraction("set_shadow_banned", set_shadow_banned_txn)
|
||||
|
||||
async def set_user_type(self, user: UserID, user_type: Optional[UserTypes]) -> None:
|
||||
async def set_user_type(
|
||||
self, user: UserID, user_type: Optional[Union[UserTypes, str]]
|
||||
) -> None:
|
||||
"""Sets the user type.
|
||||
|
||||
Args:
|
||||
@@ -683,7 +685,7 @@ class RegistrationWorkerStore(CacheInvalidationWorkerStore):
|
||||
retcol="user_type",
|
||||
allow_none=True,
|
||||
)
|
||||
return res is None
|
||||
return res is None or res not in [UserTypes.BOT, UserTypes.SUPPORT]
|
||||
|
||||
def is_support_user_txn(self, txn: LoggingTransaction, user_id: str) -> bool:
|
||||
res = self.db_pool.simple_select_one_onecol_txn(
|
||||
@@ -959,10 +961,12 @@ class RegistrationWorkerStore(CacheInvalidationWorkerStore):
|
||||
return await self.db_pool.runInteraction("count_users", _count_users)
|
||||
|
||||
async def count_real_users(self) -> int:
|
||||
"""Counts all users without a special user_type registered on the homeserver."""
|
||||
"""Counts all users without the bot or support user_types registered on the homeserver."""
|
||||
|
||||
def _count_users(txn: LoggingTransaction) -> int:
|
||||
txn.execute("SELECT COUNT(*) FROM users where user_type is null")
|
||||
txn.execute(
|
||||
f"SELECT COUNT(*) FROM users WHERE user_type IS NULL OR user_type NOT IN ('{UserTypes.BOT}', '{UserTypes.SUPPORT}')"
|
||||
)
|
||||
row = txn.fetchone()
|
||||
assert row is not None
|
||||
return row[0]
|
||||
@@ -2545,7 +2549,8 @@ class RegistrationStore(StatsStore, RegistrationBackgroundUpdateStore):
|
||||
the user, setting their displayname to the given value
|
||||
admin: is an admin user?
|
||||
user_type: type of user. One of the values from api.constants.UserTypes,
|
||||
or None for a normal user.
|
||||
a custom value set in the configuration file, or None for a normal
|
||||
user.
|
||||
shadow_banned: Whether the user is shadow-banned, i.e. they may be
|
||||
told their requests succeeded but we ignore them.
|
||||
approved: Whether to consider the user has already been approved by an
|
||||
|
||||
@@ -77,6 +77,8 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
||||
class RatelimitOverride:
|
||||
# n.b. elsewhere in Synapse messages_per_second is represented as a float, but it is
|
||||
# an integer in the database
|
||||
messages_per_second: int
|
||||
burst_count: int
|
||||
|
||||
|
||||
@@ -86,10 +86,10 @@ class TransactionWorkerStore(CacheInvalidationWorkerStore):
|
||||
@wrap_as_background_process("cleanup_transactions")
|
||||
async def _cleanup_transactions(self) -> None:
|
||||
now = self._clock.time_msec()
|
||||
month_ago = now - 30 * 24 * 60 * 60 * 1000
|
||||
day_ago = now - 24 * 60 * 60 * 1000
|
||||
|
||||
def _cleanup_transactions_txn(txn: LoggingTransaction) -> None:
|
||||
txn.execute("DELETE FROM received_transactions WHERE ts < ?", (month_ago,))
|
||||
txn.execute("DELETE FROM received_transactions WHERE ts < ?", (day_ago,))
|
||||
|
||||
await self.db_pool.runInteraction(
|
||||
"_cleanup_transactions", _cleanup_transactions_txn
|
||||
|
||||
@@ -43,6 +43,7 @@ try:
|
||||
|
||||
USE_ICU = True
|
||||
except ModuleNotFoundError:
|
||||
# except ModuleNotFoundError:
|
||||
USE_ICU = False
|
||||
|
||||
from synapse.api.errors import StoreError
|
||||
|
||||
110
synapse/storage/invite_rule.py
Normal file
110
synapse/storage/invite_rule.py
Normal file
@@ -0,0 +1,110 @@
|
||||
import logging
|
||||
from enum import Enum
|
||||
from typing import Optional, Pattern
|
||||
|
||||
from matrix_common.regex import glob_to_regex
|
||||
|
||||
from synapse.types import JsonMapping, UserID
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class InviteRule(Enum):
|
||||
"""Enum to define the action taken when an invite matches a rule."""
|
||||
|
||||
ALLOW = "allow"
|
||||
BLOCK = "block"
|
||||
IGNORE = "ignore"
|
||||
|
||||
|
||||
class InviteRulesConfig:
|
||||
"""Class to determine if a given user permits an invite from another user, and the action to take."""
|
||||
|
||||
def __init__(self, account_data: Optional[JsonMapping]):
|
||||
self.allowed_users: list[Pattern[str]] = []
|
||||
self.ignored_users: list[Pattern[str]] = []
|
||||
self.blocked_users: list[Pattern[str]] = []
|
||||
|
||||
self.allowed_servers: list[Pattern[str]] = []
|
||||
self.ignored_servers: list[Pattern[str]] = []
|
||||
self.blocked_servers: list[Pattern[str]] = []
|
||||
|
||||
def process_field(
|
||||
values: Optional[list[str]],
|
||||
ruleset: list[Pattern[str]],
|
||||
rule: InviteRule,
|
||||
) -> None:
|
||||
if isinstance(values, list):
|
||||
for value in values:
|
||||
if isinstance(value, str) and len(value) > 0:
|
||||
# User IDs cannot exceed 255 bytes. Don't process large, potentially
|
||||
# expensive glob patterns.
|
||||
if len(value) > 255:
|
||||
logger.debug(
|
||||
"Ignoring invite config glob pattern that is >255 bytes: {value}"
|
||||
)
|
||||
continue
|
||||
|
||||
try:
|
||||
ruleset.append(glob_to_regex(value))
|
||||
except Exception as e:
|
||||
# If for whatever reason we can't process this, just ignore it.
|
||||
logger.debug(
|
||||
f"Could not process '{value}' field of invite rule config, ignoring: {e}"
|
||||
)
|
||||
|
||||
if account_data:
|
||||
process_field(
|
||||
account_data.get("allowed_users"), self.allowed_users, InviteRule.ALLOW
|
||||
)
|
||||
process_field(
|
||||
account_data.get("ignored_users"), self.ignored_users, InviteRule.IGNORE
|
||||
)
|
||||
process_field(
|
||||
account_data.get("blocked_users"), self.blocked_users, InviteRule.BLOCK
|
||||
)
|
||||
process_field(
|
||||
account_data.get("allowed_servers"),
|
||||
self.allowed_servers,
|
||||
InviteRule.ALLOW,
|
||||
)
|
||||
process_field(
|
||||
account_data.get("ignored_servers"),
|
||||
self.ignored_servers,
|
||||
InviteRule.IGNORE,
|
||||
)
|
||||
process_field(
|
||||
account_data.get("blocked_servers"),
|
||||
self.blocked_servers,
|
||||
InviteRule.BLOCK,
|
||||
)
|
||||
|
||||
def get_invite_rule(self, user_id: str) -> InviteRule:
|
||||
"""Get the invite rule that matches this user. Will return InviteRule.ALLOW if no rules match
|
||||
|
||||
Args:
|
||||
user_id: The user ID of the inviting user.
|
||||
|
||||
"""
|
||||
user = UserID.from_string(user_id)
|
||||
# The order here is important. We always process user rules before server rules
|
||||
# and we always process in the order of Allow, Ignore, Block.
|
||||
for patterns, rule in [
|
||||
(self.allowed_users, InviteRule.ALLOW),
|
||||
(self.ignored_users, InviteRule.IGNORE),
|
||||
(self.blocked_users, InviteRule.BLOCK),
|
||||
]:
|
||||
for regex in patterns:
|
||||
if regex.match(user_id):
|
||||
return rule
|
||||
|
||||
for patterns, rule in [
|
||||
(self.allowed_servers, InviteRule.ALLOW),
|
||||
(self.ignored_servers, InviteRule.IGNORE),
|
||||
(self.blocked_servers, InviteRule.BLOCK),
|
||||
]:
|
||||
for regex in patterns:
|
||||
if regex.match(user.domain):
|
||||
return rule
|
||||
|
||||
return InviteRule.ALLOW
|
||||
@@ -1,6 +1,10 @@
|
||||
from typing import Optional
|
||||
|
||||
from synapse.api.ratelimiting import LimitExceededError, Ratelimiter
|
||||
from synapse.appservice import ApplicationService
|
||||
from synapse.config.ratelimiting import RatelimitSettings
|
||||
from synapse.module_api import RatelimitOverride
|
||||
from synapse.module_api.callbacks.ratelimit_callbacks import RatelimitModuleApiCallbacks
|
||||
from synapse.types import create_requester
|
||||
|
||||
from tests import unittest
|
||||
@@ -440,3 +444,49 @@ class TestRatelimiter(unittest.HomeserverTestCase):
|
||||
limiter.can_do_action(requester=None, key="a", _time_now_s=20.0)
|
||||
)
|
||||
self.assertTrue(success)
|
||||
|
||||
def test_get_ratelimit_override_for_user_callback(self) -> None:
|
||||
test_user_id = "@user:test"
|
||||
test_limiter_name = "name"
|
||||
callbacks = RatelimitModuleApiCallbacks(self.hs)
|
||||
requester = create_requester(test_user_id)
|
||||
limiter = Ratelimiter(
|
||||
store=self.hs.get_datastores().main,
|
||||
clock=self.clock,
|
||||
cfg=RatelimitSettings(
|
||||
test_limiter_name,
|
||||
per_second=0.1,
|
||||
burst_count=3,
|
||||
),
|
||||
ratelimit_callbacks=callbacks,
|
||||
)
|
||||
|
||||
# Observe four actions, exceeding the burst_count.
|
||||
limiter.record_action(requester=requester, n_actions=4, _time_now_s=0.0)
|
||||
|
||||
# We should be prevented from taking a new action now.
|
||||
success, _ = self.get_success_or_raise(
|
||||
limiter.can_do_action(requester=requester, _time_now_s=0.0)
|
||||
)
|
||||
self.assertFalse(success)
|
||||
|
||||
# Now register a callback that overrides the ratelimit for this user
|
||||
# and limiter name.
|
||||
async def get_ratelimit_override_for_user(
|
||||
user_id: str, limiter_name: str
|
||||
) -> Optional[RatelimitOverride]:
|
||||
if user_id == test_user_id:
|
||||
return RatelimitOverride(
|
||||
per_second=0.1,
|
||||
burst_count=10,
|
||||
)
|
||||
return None
|
||||
|
||||
callbacks.register_callbacks(
|
||||
get_ratelimit_override_for_user=get_ratelimit_override_for_user
|
||||
)
|
||||
|
||||
success, _ = self.get_success_or_raise(
|
||||
limiter.can_do_action(requester=requester, _time_now_s=0.0)
|
||||
)
|
||||
self.assertTrue(success)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
||||
#
|
||||
# Copyright 2015, 2016 OpenMarket Ltd
|
||||
# Copyright (C) 2023 New Vector, Ltd
|
||||
# Copyright (C) 2023, 2025 New Vector, Ltd
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
@@ -234,6 +234,41 @@ class ApplicationServiceSchedulerRecovererTestCase(unittest.TestCase):
|
||||
self.assertEqual(1, txn.complete.call_count)
|
||||
self.callback.assert_called_once_with(self.recoverer)
|
||||
|
||||
def test_recover_force_retry(self) -> None:
|
||||
txn = Mock()
|
||||
txns = [txn, None]
|
||||
pop_txn = False
|
||||
|
||||
def take_txn(
|
||||
*args: object, **kwargs: object
|
||||
) -> "defer.Deferred[Optional[Mock]]":
|
||||
if pop_txn:
|
||||
return defer.succeed(txns.pop(0))
|
||||
else:
|
||||
return defer.succeed(txn)
|
||||
|
||||
self.store.get_oldest_unsent_txn = Mock(side_effect=take_txn)
|
||||
|
||||
# Start the recovery, and then fail the first attempt.
|
||||
self.recoverer.recover()
|
||||
self.assertEqual(0, self.store.get_oldest_unsent_txn.call_count)
|
||||
txn.send = AsyncMock(return_value=False)
|
||||
txn.complete = AsyncMock(return_value=None)
|
||||
self.clock.advance_time(2)
|
||||
self.assertEqual(1, txn.send.call_count)
|
||||
self.assertEqual(0, txn.complete.call_count)
|
||||
self.assertEqual(0, self.callback.call_count)
|
||||
|
||||
# Now allow the send to succeed, and force a retry.
|
||||
pop_txn = True # returns the txn the first time, then no more.
|
||||
txn.send = AsyncMock(return_value=True) # successfully send the txn
|
||||
self.recoverer.force_retry()
|
||||
self.assertEqual(1, txn.send.call_count) # new mock reset call count
|
||||
self.assertEqual(1, txn.complete.call_count)
|
||||
|
||||
# Ensure we call the callback to say we're done!
|
||||
self.callback.assert_called_once_with(self.recoverer)
|
||||
|
||||
|
||||
# Corresponds to synapse.appservice.scheduler._TransactionController.send
|
||||
TxnCtrlArgs: TypeAlias = """
|
||||
|
||||
@@ -738,6 +738,41 @@ class RegistrationTestCase(unittest.HomeserverTestCase):
|
||||
self.handler.register_user(localpart="bobflimflob", auth_provider_id="saml")
|
||||
)
|
||||
|
||||
def test_register_default_user_type(self) -> None:
|
||||
"""Test that the default user type is none when registering a user."""
|
||||
user_id = self.get_success(self.handler.register_user(localpart="user"))
|
||||
user_info = self.get_success(self.store.get_user_by_id(user_id))
|
||||
assert user_info is not None
|
||||
self.assertEqual(user_info.user_type, None)
|
||||
|
||||
def test_register_extra_user_types_valid(self) -> None:
|
||||
"""
|
||||
Test that the specified user type is set correctly when registering a user.
|
||||
n.b. No validation is done on the user type, so this test
|
||||
is only to ensure that the user type can be set to any value.
|
||||
"""
|
||||
user_id = self.get_success(
|
||||
self.handler.register_user(localpart="user", user_type="anyvalue")
|
||||
)
|
||||
user_info = self.get_success(self.store.get_user_by_id(user_id))
|
||||
assert user_info is not None
|
||||
self.assertEqual(user_info.user_type, "anyvalue")
|
||||
|
||||
@override_config(
|
||||
{
|
||||
"user_types": {
|
||||
"extra_user_types": ["extra1", "extra2"],
|
||||
"default_user_type": "extra1",
|
||||
}
|
||||
}
|
||||
)
|
||||
def test_register_extra_user_types_with_default(self) -> None:
|
||||
"""Test that the default_user_type in config is set correctly when registering a user."""
|
||||
user_id = self.get_success(self.handler.register_user(localpart="user"))
|
||||
user_info = self.get_success(self.store.get_user_by_id(user_id))
|
||||
assert user_info is not None
|
||||
self.assertEqual(user_info.user_type, "extra1")
|
||||
|
||||
async def get_or_create_user(
|
||||
self,
|
||||
requester: Requester,
|
||||
|
||||
@@ -5,10 +5,13 @@ from twisted.test.proto_helpers import MemoryReactor
|
||||
import synapse.rest.admin
|
||||
import synapse.rest.client.login
|
||||
import synapse.rest.client.room
|
||||
from synapse.api.constants import EventTypes, Membership
|
||||
from synapse.api.constants import AccountDataTypes, EventTypes, Membership
|
||||
from synapse.api.errors import Codes, LimitExceededError, SynapseError
|
||||
from synapse.crypto.event_signing import add_hashes_and_signatures
|
||||
from synapse.events import FrozenEventV3
|
||||
from synapse.federation.federation_base import (
|
||||
event_from_pdu_json,
|
||||
)
|
||||
from synapse.federation.federation_client import SendJoinResult
|
||||
from synapse.server import HomeServer
|
||||
from synapse.types import UserID, create_requester
|
||||
@@ -453,3 +456,165 @@ class RoomMemberMasterHandlerTestCase(HomeserverTestCase):
|
||||
new_count = rows[0][0]
|
||||
|
||||
self.assertEqual(initial_count, new_count)
|
||||
|
||||
|
||||
class TestInviteFiltering(FederatingHomeserverTestCase):
|
||||
servlets = [
|
||||
synapse.rest.admin.register_servlets,
|
||||
synapse.rest.client.login.register_servlets,
|
||||
synapse.rest.client.room.register_servlets,
|
||||
]
|
||||
|
||||
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
||||
self.handler = hs.get_room_member_handler()
|
||||
self.fed_handler = hs.get_federation_handler()
|
||||
self.store = hs.get_datastores().main
|
||||
|
||||
# Create three users.
|
||||
self.alice = self.register_user("alice", "pass")
|
||||
self.alice_token = self.login("alice", "pass")
|
||||
self.bob = self.register_user("bob", "pass")
|
||||
self.bob_token = self.login("bob", "pass")
|
||||
|
||||
@override_config({"experimental_features": {"msc4155_enabled": True}})
|
||||
def test_misc4155_block_invite_local(self) -> None:
|
||||
"""Test that MSC4155 will block a user from being invited to a room"""
|
||||
room_id = self.helper.create_room_as(self.alice, tok=self.alice_token)
|
||||
|
||||
self.get_success(
|
||||
self.store.add_account_data_for_user(
|
||||
self.bob,
|
||||
AccountDataTypes.MSC4155_INVITE_PERMISSION_CONFIG,
|
||||
{
|
||||
"blocked_users": [self.alice],
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
f = self.get_failure(
|
||||
self.handler.update_membership(
|
||||
requester=create_requester(self.alice),
|
||||
target=UserID.from_string(self.bob),
|
||||
room_id=room_id,
|
||||
action=Membership.INVITE,
|
||||
),
|
||||
SynapseError,
|
||||
).value
|
||||
self.assertEqual(f.code, 403)
|
||||
self.assertEqual(f.errcode, "ORG.MATRIX.MSC4155.M_INVITE_BLOCKED")
|
||||
|
||||
@override_config({"experimental_features": {"msc4155_enabled": False}})
|
||||
def test_msc4155_disabled_allow_invite_local(self) -> None:
|
||||
"""Test that MSC4155 will block a user from being invited to a room"""
|
||||
room_id = self.helper.create_room_as(self.alice, tok=self.alice_token)
|
||||
|
||||
self.get_success(
|
||||
self.store.add_account_data_for_user(
|
||||
self.bob,
|
||||
AccountDataTypes.MSC4155_INVITE_PERMISSION_CONFIG,
|
||||
{
|
||||
"blocked_users": [self.alice],
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
self.get_success(
|
||||
self.handler.update_membership(
|
||||
requester=create_requester(self.alice),
|
||||
target=UserID.from_string(self.bob),
|
||||
room_id=room_id,
|
||||
action=Membership.INVITE,
|
||||
),
|
||||
)
|
||||
|
||||
@override_config({"experimental_features": {"msc4155_enabled": True}})
|
||||
def test_msc4155_block_invite_remote(self) -> None:
|
||||
"""Test that MSC4155 will block a remote user from being invited to a room"""
|
||||
# A remote user who sends the invite
|
||||
remote_server = "otherserver"
|
||||
remote_user = "@otheruser:" + remote_server
|
||||
|
||||
self.get_success(
|
||||
self.store.add_account_data_for_user(
|
||||
self.bob,
|
||||
AccountDataTypes.MSC4155_INVITE_PERMISSION_CONFIG,
|
||||
{"blocked_users": [remote_user]},
|
||||
)
|
||||
)
|
||||
|
||||
room_id = self.helper.create_room_as(
|
||||
room_creator=self.alice, tok=self.alice_token
|
||||
)
|
||||
room_version = self.get_success(self.store.get_room_version(room_id))
|
||||
|
||||
invite_event = event_from_pdu_json(
|
||||
{
|
||||
"type": EventTypes.Member,
|
||||
"content": {"membership": "invite"},
|
||||
"room_id": room_id,
|
||||
"sender": remote_user,
|
||||
"state_key": self.bob,
|
||||
"depth": 32,
|
||||
"prev_events": [],
|
||||
"auth_events": [],
|
||||
"origin_server_ts": self.clock.time_msec(),
|
||||
},
|
||||
room_version,
|
||||
)
|
||||
|
||||
f = self.get_failure(
|
||||
self.fed_handler.on_invite_request(
|
||||
remote_server,
|
||||
invite_event,
|
||||
invite_event.room_version,
|
||||
),
|
||||
SynapseError,
|
||||
).value
|
||||
self.assertEqual(f.code, 403)
|
||||
self.assertEqual(f.errcode, "ORG.MATRIX.MSC4155.M_INVITE_BLOCKED")
|
||||
|
||||
@override_config({"experimental_features": {"msc4155_enabled": True}})
|
||||
def test_msc4155_block_invite_remote_server(self) -> None:
|
||||
"""Test that MSC4155 will block a remote server's user from being invited to a room"""
|
||||
# A remote user who sends the invite
|
||||
remote_server = "otherserver"
|
||||
remote_user = "@otheruser:" + remote_server
|
||||
|
||||
self.get_success(
|
||||
self.store.add_account_data_for_user(
|
||||
self.bob,
|
||||
AccountDataTypes.MSC4155_INVITE_PERMISSION_CONFIG,
|
||||
{"blocked_servers": [remote_server]},
|
||||
)
|
||||
)
|
||||
|
||||
room_id = self.helper.create_room_as(
|
||||
room_creator=self.alice, tok=self.alice_token
|
||||
)
|
||||
room_version = self.get_success(self.store.get_room_version(room_id))
|
||||
|
||||
invite_event = event_from_pdu_json(
|
||||
{
|
||||
"type": EventTypes.Member,
|
||||
"content": {"membership": "invite"},
|
||||
"room_id": room_id,
|
||||
"sender": remote_user,
|
||||
"state_key": self.bob,
|
||||
"depth": 32,
|
||||
"prev_events": [],
|
||||
"auth_events": [],
|
||||
"origin_server_ts": self.clock.time_msec(),
|
||||
},
|
||||
room_version,
|
||||
)
|
||||
|
||||
f = self.get_failure(
|
||||
self.fed_handler.on_invite_request(
|
||||
remote_server,
|
||||
invite_event,
|
||||
invite_event.room_version,
|
||||
),
|
||||
SynapseError,
|
||||
).value
|
||||
self.assertEqual(f.code, 403)
|
||||
self.assertEqual(f.errcode, "ORG.MATRIX.MSC4155.M_INVITE_BLOCKED")
|
||||
|
||||
@@ -1360,3 +1360,42 @@ class MediaHashesTestCase(unittest.HomeserverTestCase):
|
||||
store_media.sha256,
|
||||
SMALL_PNG_SHA256,
|
||||
)
|
||||
|
||||
|
||||
class MediaRepoSizeModuleCallbackTestCase(unittest.HomeserverTestCase):
|
||||
servlets = [
|
||||
login.register_servlets,
|
||||
admin.register_servlets,
|
||||
]
|
||||
|
||||
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
||||
self.user = self.register_user("user", "pass")
|
||||
self.tok = self.login("user", "pass")
|
||||
self.mock_result = True # Allow all uploads by default
|
||||
|
||||
hs.get_module_api().register_media_repository_callbacks(
|
||||
is_user_allowed_to_upload_media_of_size=self.is_user_allowed_to_upload_media_of_size,
|
||||
)
|
||||
|
||||
def create_resource_dict(self) -> Dict[str, Resource]:
|
||||
resources = super().create_resource_dict()
|
||||
resources["/_matrix/media"] = self.hs.get_media_repository_resource()
|
||||
return resources
|
||||
|
||||
async def is_user_allowed_to_upload_media_of_size(
|
||||
self, user_id: str, size: int
|
||||
) -> bool:
|
||||
self.last_user_id = user_id
|
||||
self.last_size = size
|
||||
return self.mock_result
|
||||
|
||||
def test_upload_allowed(self) -> None:
|
||||
self.helper.upload_media(SMALL_PNG, tok=self.tok, expect_code=200)
|
||||
assert self.last_user_id == self.user
|
||||
assert self.last_size == len(SMALL_PNG)
|
||||
|
||||
def test_upload_not_allowed(self) -> None:
|
||||
self.mock_result = False
|
||||
self.helper.upload_media(SMALL_PNG, tok=self.tok, expect_code=413)
|
||||
assert self.last_user_id == self.user
|
||||
assert self.last_size == len(SMALL_PNG)
|
||||
|
||||
244
tests/module_api/test_spamchecker.py
Normal file
244
tests/module_api/test_spamchecker.py
Normal file
@@ -0,0 +1,244 @@
|
||||
#
|
||||
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
||||
#
|
||||
# Copyright (C) 2025 New Vector, Ltd
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# See the GNU Affero General Public License for more details:
|
||||
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
||||
#
|
||||
#
|
||||
from typing import Literal, Union
|
||||
|
||||
from twisted.test.proto_helpers import MemoryReactor
|
||||
|
||||
from synapse.config.server import DEFAULT_ROOM_VERSION
|
||||
from synapse.rest import admin, login, room, room_upgrade_rest_servlet
|
||||
from synapse.server import HomeServer
|
||||
from synapse.types import Codes, JsonDict
|
||||
from synapse.util import Clock
|
||||
|
||||
from tests.server import FakeChannel
|
||||
from tests.unittest import HomeserverTestCase
|
||||
|
||||
|
||||
class SpamCheckerTestCase(HomeserverTestCase):
|
||||
servlets = [
|
||||
room.register_servlets,
|
||||
admin.register_servlets,
|
||||
login.register_servlets,
|
||||
room_upgrade_rest_servlet.register_servlets,
|
||||
]
|
||||
|
||||
def prepare(
|
||||
self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
|
||||
) -> None:
|
||||
self._module_api = homeserver.get_module_api()
|
||||
self.user_id = self.register_user("user", "password")
|
||||
self.token = self.login("user", "password")
|
||||
|
||||
def create_room(self, content: JsonDict) -> FakeChannel:
|
||||
channel = self.make_request(
|
||||
"POST",
|
||||
"/_matrix/client/r0/createRoom",
|
||||
content,
|
||||
access_token=self.token,
|
||||
)
|
||||
|
||||
return channel
|
||||
|
||||
def test_may_user_create_room(self) -> None:
|
||||
"""Test that the may_user_create_room callback is called when a user
|
||||
creates a room, and that it receives the correct parameters.
|
||||
"""
|
||||
|
||||
async def user_may_create_room(
|
||||
user_id: str, room_config: JsonDict
|
||||
) -> Union[Literal["NOT_SPAM"], Codes]:
|
||||
self.last_room_config = room_config
|
||||
self.last_user_id = user_id
|
||||
return "NOT_SPAM"
|
||||
|
||||
self._module_api.register_spam_checker_callbacks(
|
||||
user_may_create_room=user_may_create_room
|
||||
)
|
||||
|
||||
channel = self.create_room({"foo": "baa"})
|
||||
self.assertEqual(channel.code, 200)
|
||||
self.assertEqual(self.last_user_id, self.user_id)
|
||||
self.assertEqual(self.last_room_config["foo"], "baa")
|
||||
|
||||
def test_may_user_create_room_on_upgrade(self) -> None:
|
||||
"""Test that the may_user_create_room callback is called when a room is upgraded."""
|
||||
|
||||
# First, create a room to upgrade.
|
||||
channel = self.create_room({"topic": "foo"})
|
||||
self.assertEqual(channel.code, 200)
|
||||
room_id = channel.json_body["room_id"]
|
||||
|
||||
async def user_may_create_room(
|
||||
user_id: str, room_config: JsonDict
|
||||
) -> Union[Literal["NOT_SPAM"], Codes]:
|
||||
self.last_room_config = room_config
|
||||
self.last_user_id = user_id
|
||||
return "NOT_SPAM"
|
||||
|
||||
# Register the callback for spam checking.
|
||||
self._module_api.register_spam_checker_callbacks(
|
||||
user_may_create_room=user_may_create_room
|
||||
)
|
||||
|
||||
# Now upgrade the room.
|
||||
channel = self.make_request(
|
||||
"POST",
|
||||
f"/_matrix/client/r0/rooms/{room_id}/upgrade",
|
||||
# This will upgrade a room to the same version, but that's fine.
|
||||
content={"new_version": DEFAULT_ROOM_VERSION},
|
||||
access_token=self.token,
|
||||
)
|
||||
|
||||
# Check that the callback was called and the room was upgraded.
|
||||
self.assertEqual(channel.code, 200)
|
||||
self.assertEqual(self.last_user_id, self.user_id)
|
||||
# Check that the initial state received by callback contains the topic event.
|
||||
self.assertTrue(
|
||||
any(
|
||||
event[0][0] == "m.room.topic" and event[1].get("topic") == "foo"
|
||||
for event in self.last_room_config["initial_state"]
|
||||
)
|
||||
)
|
||||
|
||||
def test_may_user_create_room_disallowed(self) -> None:
|
||||
"""Test that the codes response from may_user_create_room callback is respected
|
||||
and returned via the API.
|
||||
"""
|
||||
|
||||
async def user_may_create_room(
|
||||
user_id: str, room_config: JsonDict
|
||||
) -> Union[Literal["NOT_SPAM"], Codes]:
|
||||
self.last_room_config = room_config
|
||||
self.last_user_id = user_id
|
||||
return Codes.UNAUTHORIZED
|
||||
|
||||
self._module_api.register_spam_checker_callbacks(
|
||||
user_may_create_room=user_may_create_room
|
||||
)
|
||||
|
||||
channel = self.create_room({"foo": "baa"})
|
||||
self.assertEqual(channel.code, 403)
|
||||
self.assertEqual(channel.json_body["errcode"], Codes.UNAUTHORIZED)
|
||||
self.assertEqual(self.last_user_id, self.user_id)
|
||||
self.assertEqual(self.last_room_config["foo"], "baa")
|
||||
|
||||
def test_may_user_create_room_compatibility(self) -> None:
|
||||
"""Test that the may_user_create_room callback is called when a user
|
||||
creates a room for a module that uses the old callback signature
|
||||
(without the `room_config` parameter)
|
||||
"""
|
||||
|
||||
async def user_may_create_room(
|
||||
user_id: str,
|
||||
) -> Union[Literal["NOT_SPAM"], Codes]:
|
||||
self.last_user_id = user_id
|
||||
return "NOT_SPAM"
|
||||
|
||||
self._module_api.register_spam_checker_callbacks(
|
||||
user_may_create_room=user_may_create_room
|
||||
)
|
||||
|
||||
channel = self.create_room({"foo": "baa"})
|
||||
self.assertEqual(channel.code, 200)
|
||||
self.assertEqual(self.last_user_id, self.user_id)
|
||||
|
||||
def test_user_may_send_state_event(self) -> None:
|
||||
"""Test that the user_may_send_state_event callback is called when a state event
|
||||
is sent, and that it receives the correct parameters.
|
||||
"""
|
||||
|
||||
async def user_may_send_state_event(
|
||||
user_id: str,
|
||||
room_id: str,
|
||||
event_type: str,
|
||||
state_key: str,
|
||||
content: JsonDict,
|
||||
) -> Union[Literal["NOT_SPAM"], Codes]:
|
||||
self.last_user_id = user_id
|
||||
self.last_room_id = room_id
|
||||
self.last_event_type = event_type
|
||||
self.last_state_key = state_key
|
||||
self.last_content = content
|
||||
return "NOT_SPAM"
|
||||
|
||||
self._module_api.register_spam_checker_callbacks(
|
||||
user_may_send_state_event=user_may_send_state_event
|
||||
)
|
||||
|
||||
channel = self.create_room({})
|
||||
self.assertEqual(channel.code, 200)
|
||||
|
||||
room_id = channel.json_body["room_id"]
|
||||
|
||||
event_type = "test.event.type"
|
||||
state_key = "test.state.key"
|
||||
channel = self.make_request(
|
||||
"PUT",
|
||||
"/_matrix/client/r0/rooms/%s/state/%s/%s"
|
||||
% (
|
||||
room_id,
|
||||
event_type,
|
||||
state_key,
|
||||
),
|
||||
content={"foo": "bar"},
|
||||
access_token=self.token,
|
||||
)
|
||||
|
||||
self.assertEqual(channel.code, 200)
|
||||
self.assertEqual(self.last_user_id, self.user_id)
|
||||
self.assertEqual(self.last_room_id, room_id)
|
||||
self.assertEqual(self.last_event_type, event_type)
|
||||
self.assertEqual(self.last_state_key, state_key)
|
||||
self.assertEqual(self.last_content, {"foo": "bar"})
|
||||
|
||||
def test_user_may_send_state_event_disallows(self) -> None:
|
||||
"""Test that the user_may_send_state_event callback is called when a state event
|
||||
is sent, and that the response is honoured.
|
||||
"""
|
||||
|
||||
async def user_may_send_state_event(
|
||||
user_id: str,
|
||||
room_id: str,
|
||||
event_type: str,
|
||||
state_key: str,
|
||||
content: JsonDict,
|
||||
) -> Union[Literal["NOT_SPAM"], Codes]:
|
||||
return Codes.FORBIDDEN
|
||||
|
||||
self._module_api.register_spam_checker_callbacks(
|
||||
user_may_send_state_event=user_may_send_state_event
|
||||
)
|
||||
|
||||
channel = self.create_room({})
|
||||
self.assertEqual(channel.code, 200)
|
||||
|
||||
room_id = channel.json_body["room_id"]
|
||||
|
||||
event_type = "test.event.type"
|
||||
state_key = "test.state.key"
|
||||
channel = self.make_request(
|
||||
"PUT",
|
||||
"/_matrix/client/r0/rooms/%s/state/%s/%s"
|
||||
% (
|
||||
room_id,
|
||||
event_type,
|
||||
state_key,
|
||||
),
|
||||
content={"foo": "bar"},
|
||||
access_token=self.token,
|
||||
)
|
||||
|
||||
self.assertEqual(channel.code, 403)
|
||||
self.assertEqual(channel.json_body["errcode"], Codes.FORBIDDEN)
|
||||
@@ -328,6 +328,61 @@ class UserRegisterTestCase(unittest.HomeserverTestCase):
|
||||
self.assertEqual(400, channel.code, msg=channel.json_body)
|
||||
self.assertEqual("Invalid user type", channel.json_body["error"])
|
||||
|
||||
@override_config(
|
||||
{
|
||||
"user_types": {
|
||||
"extra_user_types": ["extra1", "extra2"],
|
||||
}
|
||||
}
|
||||
)
|
||||
def test_extra_user_type(self) -> None:
|
||||
"""
|
||||
Check that the extra user type can be used when registering a user.
|
||||
"""
|
||||
|
||||
def nonce_mac(user_type: str) -> tuple[str, str]:
|
||||
"""
|
||||
Get a nonce and the expected HMAC for that nonce.
|
||||
"""
|
||||
channel = self.make_request("GET", self.url)
|
||||
nonce = channel.json_body["nonce"]
|
||||
|
||||
want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
|
||||
want_mac.update(
|
||||
nonce.encode("ascii")
|
||||
+ b"\x00alice\x00abc123\x00notadmin\x00"
|
||||
+ user_type.encode("ascii")
|
||||
)
|
||||
want_mac_str = want_mac.hexdigest()
|
||||
|
||||
return nonce, want_mac_str
|
||||
|
||||
nonce, mac = nonce_mac("extra1")
|
||||
# Valid user_type
|
||||
body = {
|
||||
"nonce": nonce,
|
||||
"username": "alice",
|
||||
"password": "abc123",
|
||||
"user_type": "extra1",
|
||||
"mac": mac,
|
||||
}
|
||||
channel = self.make_request("POST", self.url, body)
|
||||
self.assertEqual(200, channel.code, msg=channel.json_body)
|
||||
|
||||
nonce, mac = nonce_mac("extra3")
|
||||
# Invalid user_type
|
||||
body = {
|
||||
"nonce": nonce,
|
||||
"username": "alice",
|
||||
"password": "abc123",
|
||||
"user_type": "extra3",
|
||||
"mac": mac,
|
||||
}
|
||||
channel = self.make_request("POST", self.url, body)
|
||||
|
||||
self.assertEqual(400, channel.code, msg=channel.json_body)
|
||||
self.assertEqual("Invalid user type", channel.json_body["error"])
|
||||
|
||||
def test_displayname(self) -> None:
|
||||
"""
|
||||
Test that displayname of new user is set
|
||||
@@ -1186,6 +1241,80 @@ class UsersListTestCase(unittest.HomeserverTestCase):
|
||||
not_user_types=["custom"],
|
||||
)
|
||||
|
||||
@override_config(
|
||||
{
|
||||
"user_types": {
|
||||
"extra_user_types": ["extra1", "extra2"],
|
||||
}
|
||||
}
|
||||
)
|
||||
def test_filter_not_user_types_with_extra(self) -> None:
|
||||
"""Tests that the endpoint handles the not_user_types param when extra_user_types are configured"""
|
||||
|
||||
regular_user_id = self.register_user("normalo", "secret")
|
||||
|
||||
extra1_user_id = self.register_user("extra1", "secret")
|
||||
self.make_request(
|
||||
"PUT",
|
||||
"/_synapse/admin/v2/users/" + urllib.parse.quote(extra1_user_id),
|
||||
{"user_type": "extra1"},
|
||||
access_token=self.admin_user_tok,
|
||||
)
|
||||
|
||||
def test_user_type(
|
||||
expected_user_ids: List[str], not_user_types: Optional[List[str]] = None
|
||||
) -> None:
|
||||
"""Runs a test for the not_user_types param
|
||||
Args:
|
||||
expected_user_ids: Ids of the users that are expected to be returned
|
||||
not_user_types: List of values for the not_user_types param
|
||||
"""
|
||||
|
||||
user_type_query = ""
|
||||
|
||||
if not_user_types is not None:
|
||||
user_type_query = "&".join(
|
||||
[f"not_user_type={u}" for u in not_user_types]
|
||||
)
|
||||
|
||||
test_url = f"{self.url}?{user_type_query}"
|
||||
channel = self.make_request(
|
||||
"GET",
|
||||
test_url,
|
||||
access_token=self.admin_user_tok,
|
||||
)
|
||||
|
||||
self.assertEqual(200, channel.code)
|
||||
self.assertEqual(channel.json_body["total"], len(expected_user_ids))
|
||||
self.assertEqual(
|
||||
expected_user_ids,
|
||||
[u["name"] for u in channel.json_body["users"]],
|
||||
)
|
||||
|
||||
# Request without user_types → all users expected
|
||||
test_user_type([self.admin_user, extra1_user_id, regular_user_id])
|
||||
|
||||
# Request and exclude extra1 user type
|
||||
test_user_type(
|
||||
[self.admin_user, regular_user_id],
|
||||
not_user_types=["extra1"],
|
||||
)
|
||||
|
||||
# Request and exclude extra1 and extra2 user types
|
||||
test_user_type(
|
||||
[self.admin_user, regular_user_id],
|
||||
not_user_types=["extra1", "extra2"],
|
||||
)
|
||||
|
||||
# Request and exclude empty user types → only expected the extra1 user
|
||||
test_user_type([extra1_user_id], not_user_types=[""])
|
||||
|
||||
# Request and exclude an unregistered type → expect all users
|
||||
test_user_type(
|
||||
[self.admin_user, extra1_user_id, regular_user_id],
|
||||
not_user_types=["extra3"],
|
||||
)
|
||||
|
||||
def test_erasure_status(self) -> None:
|
||||
# Create a new user.
|
||||
user_id = self.register_user("eraseme", "eraseme")
|
||||
@@ -2977,56 +3106,66 @@ class UserRestTestCase(unittest.HomeserverTestCase):
|
||||
self.assertEqual("@user:test", channel.json_body["name"])
|
||||
self.assertTrue(channel.json_body["admin"])
|
||||
|
||||
def set_user_type(self, user_type: Optional[str]) -> None:
|
||||
# Set to user_type
|
||||
channel = self.make_request(
|
||||
"PUT",
|
||||
self.url_other_user,
|
||||
access_token=self.admin_user_tok,
|
||||
content={"user_type": user_type},
|
||||
)
|
||||
|
||||
self.assertEqual(200, channel.code, msg=channel.json_body)
|
||||
self.assertEqual("@user:test", channel.json_body["name"])
|
||||
self.assertEqual(user_type, channel.json_body["user_type"])
|
||||
|
||||
# Get user
|
||||
channel = self.make_request(
|
||||
"GET",
|
||||
self.url_other_user,
|
||||
access_token=self.admin_user_tok,
|
||||
)
|
||||
|
||||
self.assertEqual(200, channel.code, msg=channel.json_body)
|
||||
self.assertEqual("@user:test", channel.json_body["name"])
|
||||
self.assertEqual(user_type, channel.json_body["user_type"])
|
||||
|
||||
def test_set_user_type(self) -> None:
|
||||
"""
|
||||
Test changing user type.
|
||||
"""
|
||||
|
||||
# Set to support type
|
||||
channel = self.make_request(
|
||||
"PUT",
|
||||
self.url_other_user,
|
||||
access_token=self.admin_user_tok,
|
||||
content={"user_type": UserTypes.SUPPORT},
|
||||
)
|
||||
|
||||
self.assertEqual(200, channel.code, msg=channel.json_body)
|
||||
self.assertEqual("@user:test", channel.json_body["name"])
|
||||
self.assertEqual(UserTypes.SUPPORT, channel.json_body["user_type"])
|
||||
|
||||
# Get user
|
||||
channel = self.make_request(
|
||||
"GET",
|
||||
self.url_other_user,
|
||||
access_token=self.admin_user_tok,
|
||||
)
|
||||
|
||||
self.assertEqual(200, channel.code, msg=channel.json_body)
|
||||
self.assertEqual("@user:test", channel.json_body["name"])
|
||||
self.assertEqual(UserTypes.SUPPORT, channel.json_body["user_type"])
|
||||
self.set_user_type(UserTypes.SUPPORT)
|
||||
|
||||
# Change back to a regular user
|
||||
self.set_user_type(None)
|
||||
|
||||
@override_config({"user_types": {"extra_user_types": ["extra1", "extra2"]}})
|
||||
def test_set_user_type_with_extras(self) -> None:
|
||||
"""
|
||||
Test changing user type with extra_user_types configured.
|
||||
"""
|
||||
|
||||
# Check that we can still set to support type
|
||||
self.set_user_type(UserTypes.SUPPORT)
|
||||
|
||||
# Check that we can set to an extra user type
|
||||
self.set_user_type("extra2")
|
||||
|
||||
# Change back to a regular user
|
||||
self.set_user_type(None)
|
||||
|
||||
# Try setting to invalid type
|
||||
channel = self.make_request(
|
||||
"PUT",
|
||||
self.url_other_user,
|
||||
access_token=self.admin_user_tok,
|
||||
content={"user_type": None},
|
||||
content={"user_type": "extra3"},
|
||||
)
|
||||
|
||||
self.assertEqual(200, channel.code, msg=channel.json_body)
|
||||
self.assertEqual("@user:test", channel.json_body["name"])
|
||||
self.assertIsNone(channel.json_body["user_type"])
|
||||
|
||||
# Get user
|
||||
channel = self.make_request(
|
||||
"GET",
|
||||
self.url_other_user,
|
||||
access_token=self.admin_user_tok,
|
||||
)
|
||||
|
||||
self.assertEqual(200, channel.code, msg=channel.json_body)
|
||||
self.assertEqual("@user:test", channel.json_body["name"])
|
||||
self.assertIsNone(channel.json_body["user_type"])
|
||||
self.assertEqual(400, channel.code, msg=channel.json_body)
|
||||
self.assertEqual("Invalid user type", channel.json_body["error"])
|
||||
|
||||
def test_accidental_deactivation_prevention(self) -> None:
|
||||
"""
|
||||
|
||||
@@ -1618,6 +1618,63 @@ class MediaConfigTest(unittest.HomeserverTestCase):
|
||||
)
|
||||
|
||||
|
||||
class MediaConfigModuleCallbackTestCase(unittest.HomeserverTestCase):
|
||||
servlets = [
|
||||
media.register_servlets,
|
||||
admin.register_servlets,
|
||||
login.register_servlets,
|
||||
]
|
||||
|
||||
def make_homeserver(
|
||||
self, reactor: ThreadedMemoryReactorClock, clock: Clock
|
||||
) -> HomeServer:
|
||||
config = self.default_config()
|
||||
|
||||
self.storage_path = self.mktemp()
|
||||
self.media_store_path = self.mktemp()
|
||||
os.mkdir(self.storage_path)
|
||||
os.mkdir(self.media_store_path)
|
||||
config["media_store_path"] = self.media_store_path
|
||||
|
||||
provider_config = {
|
||||
"module": "synapse.media.storage_provider.FileStorageProviderBackend",
|
||||
"store_local": True,
|
||||
"store_synchronous": False,
|
||||
"store_remote": True,
|
||||
"config": {"directory": self.storage_path},
|
||||
}
|
||||
|
||||
config["media_storage_providers"] = [provider_config]
|
||||
|
||||
return self.setup_test_homeserver(config=config)
|
||||
|
||||
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
||||
self.user = self.register_user("user", "password")
|
||||
self.tok = self.login("user", "password")
|
||||
|
||||
hs.get_module_api().register_media_repository_callbacks(
|
||||
get_media_config_for_user=self.get_media_config_for_user,
|
||||
)
|
||||
|
||||
async def get_media_config_for_user(
|
||||
self,
|
||||
user_id: str,
|
||||
) -> Optional[JsonDict]:
|
||||
# We echo back the user_id and set a custom upload size.
|
||||
return {"m.upload.size": 1024, "user_id": user_id}
|
||||
|
||||
def test_media_config(self) -> None:
|
||||
channel = self.make_request(
|
||||
"GET",
|
||||
"/_matrix/client/v1/media/config",
|
||||
shorthand=False,
|
||||
access_token=self.tok,
|
||||
)
|
||||
self.assertEqual(channel.code, 200)
|
||||
self.assertEqual(channel.json_body["m.upload.size"], 1024)
|
||||
self.assertEqual(channel.json_body["user_id"], self.user)
|
||||
|
||||
|
||||
class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
|
||||
servlets = [
|
||||
media.register_servlets,
|
||||
|
||||
@@ -24,6 +24,7 @@ from typing import Iterable, Optional, Set
|
||||
from twisted.test.proto_helpers import MemoryReactor
|
||||
|
||||
from synapse.api.constants import AccountDataTypes
|
||||
from synapse.api.errors import Codes, SynapseError
|
||||
from synapse.server import HomeServer
|
||||
from synapse.util import Clock
|
||||
|
||||
@@ -93,6 +94,20 @@ class IgnoredUsersTestCase(unittest.HomeserverTestCase):
|
||||
# Check the removed user.
|
||||
self.assert_ignorers("@another:remote", {self.user})
|
||||
|
||||
def test_ignoring_self_fails(self) -> None:
|
||||
"""Ensure users cannot add themselves to the ignored list."""
|
||||
|
||||
f = self.get_failure(
|
||||
self.store.add_account_data_for_user(
|
||||
self.user,
|
||||
AccountDataTypes.IGNORED_USER_LIST,
|
||||
{"ignored_users": {self.user: {}}},
|
||||
),
|
||||
SynapseError,
|
||||
).value
|
||||
self.assertEqual(f.code, 400)
|
||||
self.assertEqual(f.errcode, Codes.INVALID_PARAM)
|
||||
|
||||
def test_caching(self) -> None:
|
||||
"""Ensure that caching works properly between different users."""
|
||||
# The first user ignores a user.
|
||||
|
||||
167
tests/storage/test_invite_rule.py
Normal file
167
tests/storage/test_invite_rule.py
Normal file
@@ -0,0 +1,167 @@
|
||||
from synapse.storage.invite_rule import InviteRule, InviteRulesConfig
|
||||
from synapse.types import UserID
|
||||
|
||||
from tests import unittest
|
||||
|
||||
regular_user = UserID.from_string("@test:example.org")
|
||||
allowed_user = UserID.from_string("@allowed:allow.example.org")
|
||||
blocked_user = UserID.from_string("@blocked:block.example.org")
|
||||
ignored_user = UserID.from_string("@ignored:ignore.example.org")
|
||||
|
||||
|
||||
class InviteFilterTestCase(unittest.TestCase):
|
||||
def test_empty(self) -> None:
|
||||
"""Permit by default"""
|
||||
config = InviteRulesConfig(None)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(regular_user.to_string()), InviteRule.ALLOW
|
||||
)
|
||||
|
||||
def test_ignore_invalid(self) -> None:
|
||||
"""Invalid strings are ignored"""
|
||||
config = InviteRulesConfig({"blocked_users": ["not a user"]})
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(blocked_user.to_string()), InviteRule.ALLOW
|
||||
)
|
||||
|
||||
def test_user_blocked(self) -> None:
|
||||
"""Permit all, except explicitly blocked users"""
|
||||
config = InviteRulesConfig({"blocked_users": [blocked_user.to_string()]})
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(blocked_user.to_string()), InviteRule.BLOCK
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(regular_user.to_string()), InviteRule.ALLOW
|
||||
)
|
||||
|
||||
def test_user_ignored(self) -> None:
|
||||
"""Permit all, except explicitly ignored users"""
|
||||
config = InviteRulesConfig({"ignored_users": [ignored_user.to_string()]})
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(ignored_user.to_string()), InviteRule.IGNORE
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(regular_user.to_string()), InviteRule.ALLOW
|
||||
)
|
||||
|
||||
def test_user_precedence(self) -> None:
|
||||
"""Always take allowed over ignored, ignored over blocked, and then block."""
|
||||
config = InviteRulesConfig(
|
||||
{
|
||||
"allowed_users": [allowed_user.to_string()],
|
||||
"ignored_users": [allowed_user.to_string(), ignored_user.to_string()],
|
||||
"blocked_users": [
|
||||
allowed_user.to_string(),
|
||||
ignored_user.to_string(),
|
||||
blocked_user.to_string(),
|
||||
],
|
||||
}
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(allowed_user.to_string()), InviteRule.ALLOW
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(ignored_user.to_string()), InviteRule.IGNORE
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(blocked_user.to_string()), InviteRule.BLOCK
|
||||
)
|
||||
|
||||
def test_server_blocked(self) -> None:
|
||||
"""Block all users on the server except those allowed."""
|
||||
user_on_same_server = UserID("blocked", allowed_user.domain)
|
||||
config = InviteRulesConfig(
|
||||
{
|
||||
"allowed_users": [allowed_user.to_string()],
|
||||
"blocked_servers": [allowed_user.domain],
|
||||
}
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(allowed_user.to_string()), InviteRule.ALLOW
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(user_on_same_server.to_string()), InviteRule.BLOCK
|
||||
)
|
||||
|
||||
def test_server_ignored(self) -> None:
|
||||
"""Ignore all users on the server except those allowed."""
|
||||
user_on_same_server = UserID("ignored", allowed_user.domain)
|
||||
config = InviteRulesConfig(
|
||||
{
|
||||
"allowed_users": [allowed_user.to_string()],
|
||||
"ignored_servers": [allowed_user.domain],
|
||||
}
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(allowed_user.to_string()), InviteRule.ALLOW
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(user_on_same_server.to_string()), InviteRule.IGNORE
|
||||
)
|
||||
|
||||
def test_server_allow(self) -> None:
|
||||
"""Allow all from a server except explictly blocked or ignored users."""
|
||||
blocked_user_on_same_server = UserID("blocked", allowed_user.domain)
|
||||
ignored_user_on_same_server = UserID("ignored", allowed_user.domain)
|
||||
allowed_user_on_same_server = UserID("another", allowed_user.domain)
|
||||
config = InviteRulesConfig(
|
||||
{
|
||||
"ignored_users": [ignored_user_on_same_server.to_string()],
|
||||
"blocked_users": [blocked_user_on_same_server.to_string()],
|
||||
"allowed_servers": [allowed_user.to_string()],
|
||||
}
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(allowed_user.to_string()), InviteRule.ALLOW
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(allowed_user_on_same_server.to_string()),
|
||||
InviteRule.ALLOW,
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(blocked_user_on_same_server.to_string()),
|
||||
InviteRule.BLOCK,
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(ignored_user_on_same_server.to_string()),
|
||||
InviteRule.IGNORE,
|
||||
)
|
||||
|
||||
def test_server_precedence(self) -> None:
|
||||
"""Always take allowed over ignored, ignored over blocked, and then block."""
|
||||
config = InviteRulesConfig(
|
||||
{
|
||||
"allowed_servers": [allowed_user.domain],
|
||||
"ignored_servers": [allowed_user.domain, ignored_user.domain],
|
||||
"blocked_servers": [
|
||||
allowed_user.domain,
|
||||
ignored_user.domain,
|
||||
blocked_user.domain,
|
||||
],
|
||||
}
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(allowed_user.to_string()), InviteRule.ALLOW
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(ignored_user.to_string()), InviteRule.IGNORE
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(blocked_user.to_string()), InviteRule.BLOCK
|
||||
)
|
||||
|
||||
def test_server_glob(self) -> None:
|
||||
"""Test that glob patterns match"""
|
||||
config = InviteRulesConfig({"blocked_servers": ["*.example.org"]})
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(allowed_user.to_string()), InviteRule.BLOCK
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(ignored_user.to_string()), InviteRule.BLOCK
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(blocked_user.to_string()), InviteRule.BLOCK
|
||||
)
|
||||
self.assertEqual(
|
||||
config.get_invite_rule(regular_user.to_string()), InviteRule.ALLOW
|
||||
)
|
||||
@@ -112,11 +112,11 @@ class TestTaskScheduler(HomeserverTestCase):
|
||||
|
||||
# At this point, there should be MAX_CONCURRENT_RUNNING_TASKS active tasks and
|
||||
# one scheduled task.
|
||||
self.assertEquals(
|
||||
self.assertEqual(
|
||||
len(get_tasks_of_status(TaskStatus.ACTIVE)),
|
||||
TaskScheduler.MAX_CONCURRENT_RUNNING_TASKS,
|
||||
)
|
||||
self.assertEquals(
|
||||
self.assertEqual(
|
||||
len(get_tasks_of_status(TaskStatus.SCHEDULED)),
|
||||
1,
|
||||
)
|
||||
@@ -126,17 +126,17 @@ class TestTaskScheduler(HomeserverTestCase):
|
||||
|
||||
# Check that MAX_CONCURRENT_RUNNING_TASKS tasks have run and that one
|
||||
# is still scheduled.
|
||||
self.assertEquals(
|
||||
self.assertEqual(
|
||||
len(get_tasks_of_status(TaskStatus.COMPLETE)),
|
||||
TaskScheduler.MAX_CONCURRENT_RUNNING_TASKS,
|
||||
)
|
||||
scheduled_tasks = get_tasks_of_status(TaskStatus.SCHEDULED)
|
||||
self.assertEquals(len(scheduled_tasks), 1)
|
||||
self.assertEqual(len(scheduled_tasks), 1)
|
||||
|
||||
# The scheduled task should start 0.1s after the first of the active tasks
|
||||
# finishes
|
||||
self.reactor.advance(0.1)
|
||||
self.assertEquals(len(get_tasks_of_status(TaskStatus.ACTIVE)), 1)
|
||||
self.assertEqual(len(get_tasks_of_status(TaskStatus.ACTIVE)), 1)
|
||||
|
||||
# ... and should finally complete after another second
|
||||
self.reactor.advance(1)
|
||||
@@ -144,7 +144,7 @@ class TestTaskScheduler(HomeserverTestCase):
|
||||
self.task_scheduler.get_task(scheduled_tasks[0].id)
|
||||
)
|
||||
assert prev_scheduled_task is not None
|
||||
self.assertEquals(
|
||||
self.assertEqual(
|
||||
prev_scheduled_task.status,
|
||||
TaskStatus.COMPLETE,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user