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

Commit a743bf4

Browse files
authored
Port the ThirdPartyEventRules module interface to the new generic interface (#10386)
Port the third-party event rules interface to the generic module interface introduced in v1.37.0
1 parent f3ac9c6 commit a743bf4

File tree

12 files changed

+403
-108
lines changed

12 files changed

+403
-108
lines changed

changelog.d/10386.removal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The third-party event rules module interface is deprecated in favour of the generic module interface introduced in Synapse v1.37.0. See the [upgrade notes](https://matrix-org.github.io/synapse/latest/upgrade.html#upgrading-to-v1390) for more information.

docs/modules.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ The arguments passed to this callback are:
186186
```python
187187
async def check_media_file_for_spam(
188188
file_wrapper: "synapse.rest.media.v1.media_storage.ReadableFileWrapper",
189-
file_info: "synapse.rest.media.v1._base.FileInfo"
189+
file_info: "synapse.rest.media.v1._base.FileInfo",
190190
) -> bool
191191
```
192192

@@ -223,6 +223,66 @@ Called after successfully registering a user, in case the module needs to perfor
223223
operations to keep track of them. (e.g. add them to a database table). The user is
224224
represented by their Matrix user ID.
225225

226+
#### Third party rules callbacks
227+
228+
Third party rules callbacks allow module developers to add extra checks to verify the
229+
validity of incoming events. Third party event rules callbacks can be registered using
230+
the module API's `register_third_party_rules_callbacks` method.
231+
232+
The available third party rules callbacks are:
233+
234+
```python
235+
async def check_event_allowed(
236+
event: "synapse.events.EventBase",
237+
state_events: "synapse.types.StateMap",
238+
) -> Tuple[bool, Optional[dict]]
239+
```
240+
241+
**<span style="color:red">
242+
This callback is very experimental and can and will break without notice. Module developers
243+
are encouraged to implement `check_event_for_spam` from the spam checker category instead.
244+
</span>**
245+
246+
Called when processing any incoming event, with the event and a `StateMap`
247+
representing the current state of the room the event is being sent into. A `StateMap` is
248+
a dictionary that maps tuples containing an event type and a state key to the
249+
corresponding state event. For example retrieving the room's `m.room.create` event from
250+
the `state_events` argument would look like this: `state_events.get(("m.room.create", ""))`.
251+
The module must return a boolean indicating whether the event can be allowed.
252+
253+
Note that this callback function processes incoming events coming via federation
254+
traffic (on top of client traffic). This means denying an event might cause the local
255+
copy of the room's history to diverge from that of remote servers. This may cause
256+
federation issues in the room. It is strongly recommended to only deny events using this
257+
callback function if the sender is a local user, or in a private federation in which all
258+
servers are using the same module, with the same configuration.
259+
260+
If the boolean returned by the module is `True`, it may also tell Synapse to replace the
261+
event with new data by returning the new event's data as a dictionary. In order to do
262+
that, it is recommended the module calls `event.get_dict()` to get the current event as a
263+
dictionary, and modify the returned dictionary accordingly.
264+
265+
Note that replacing the event only works for events sent by local users, not for events
266+
received over federation.
267+
268+
```python
269+
async def on_create_room(
270+
requester: "synapse.types.Requester",
271+
request_content: dict,
272+
is_requester_admin: bool,
273+
) -> None
274+
```
275+
276+
Called when processing a room creation request, with the `Requester` object for the user
277+
performing the request, a dictionary representing the room creation request's JSON body
278+
(see [the spec](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-createroom)
279+
for a list of possible parameters), and a boolean indicating whether the user performing
280+
the request is a server admin.
281+
282+
Modules can modify the `request_content` (by e.g. adding events to its `initial_state`),
283+
or deny the room's creation by raising a `module_api.errors.SynapseError`.
284+
285+
226286
### Porting an existing module that uses the old interface
227287

228288
In order to port a module that uses Synapse's old module interface, its author needs to:

docs/sample_config.yaml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,19 +2654,6 @@ stats:
26542654
# action: allow
26552655

26562656

2657-
# Server admins can define a Python module that implements extra rules for
2658-
# allowing or denying incoming events. In order to work, this module needs to
2659-
# override the methods defined in synapse/events/third_party_rules.py.
2660-
#
2661-
# This feature is designed to be used in closed federations only, where each
2662-
# participating server enforces the same rules.
2663-
#
2664-
#third_party_event_rules:
2665-
# module: "my_custom_project.SuperRulesSet"
2666-
# config:
2667-
# example_option: 'things'
2668-
2669-
26702657
## Opentracing ##
26712658

26722659
# These settings enable opentracing, which implements distributed tracing.

docs/upgrade.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ process, for example:
8686
```
8787

8888

89+
# Upgrading to v1.39.0
90+
91+
## Deprecation of the current third-party rules module interface
92+
93+
The current third-party rules module interface is deprecated in favour of the new generic
94+
modules system introduced in Synapse v1.37.0. Authors of third-party rules modules can refer
95+
to [this documentation](modules.md#porting-an-existing-module-that-uses-the-old-interface)
96+
to update their modules. Synapse administrators can refer to [this documentation](modules.md#using-modules)
97+
to update their configuration once the modules they are using have been updated.
98+
99+
We plan to remove support for the current third-party rules interface in September 2021.
100+
101+
89102
# Upgrading to v1.38.0
90103

91104
## Re-indexing of `events` table on Postgres databases

synapse/app/_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from synapse.config.homeserver import HomeServerConfig
3939
from synapse.crypto import context_factory
4040
from synapse.events.spamcheck import load_legacy_spam_checkers
41+
from synapse.events.third_party_rules import load_legacy_third_party_event_rules
4142
from synapse.logging.context import PreserveLoggingContext
4243
from synapse.metrics.background_process_metrics import wrap_as_background_process
4344
from synapse.metrics.jemalloc import setup_jemalloc_stats
@@ -368,6 +369,7 @@ def run_sighup(*args, **kwargs):
368369
module(config=config, api=module_api)
369370

370371
load_legacy_spam_checkers(hs)
372+
load_legacy_third_party_event_rules(hs)
371373

372374
# If we've configured an expiry time for caches, start the background job now.
373375
setup_expire_lru_cache_entries(hs)

synapse/config/third_party_event_rules.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,3 @@ def read_config(self, config, **kwargs):
2828
self.third_party_event_rules = load_module(
2929
provider, ("third_party_event_rules",)
3030
)
31-
32-
def generate_config_section(self, **kwargs):
33-
return """\
34-
# Server admins can define a Python module that implements extra rules for
35-
# allowing or denying incoming events. In order to work, this module needs to
36-
# override the methods defined in synapse/events/third_party_rules.py.
37-
#
38-
# This feature is designed to be used in closed federations only, where each
39-
# participating server enforces the same rules.
40-
#
41-
#third_party_event_rules:
42-
# module: "my_custom_project.SuperRulesSet"
43-
# config:
44-
# example_option: 'things'
45-
"""

0 commit comments

Comments
 (0)