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

Commit c955e37

Browse files
authored
Fix wrapping of legacy check_registration_for_spam (#10238)
Fixes #10234
1 parent 9ec45ac commit c955e37

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

changelog.d/10238.removal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The current spam checker interface is deprecated in favour of a new generic modules system. See the [upgrade notes](https://github.com/matrix-org/synapse/blob/master/UPGRADE.rst#deprecation-of-the-current-spam-checker-interface) for more information on how to update to the new system.

synapse/events/spamcheck.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ def async_wrapper(f: Optional[Callable]) -> Optional[Callable[..., Awaitable]]:
109109
if f is None:
110110
return None
111111

112+
wrapped_func = f
113+
112114
if f.__name__ == "check_registration_for_spam":
113115
checker_args = inspect.signature(f)
114116
if len(checker_args.parameters) == 3:
@@ -133,19 +135,18 @@ def wrapper(
133135
request_info,
134136
)
135137

136-
f = wrapper
138+
wrapped_func = wrapper
137139
elif len(checker_args.parameters) != 4:
138140
raise RuntimeError(
139141
"Bad signature for callback check_registration_for_spam",
140142
)
141143

142144
def run(*args, **kwargs):
143-
# We've already made sure f is not None above, but mypy doesn't do well
144-
# across function boundaries so we need to tell it f is definitely not
145-
# None.
146-
assert f is not None
145+
# mypy doesn't do well across function boundaries so we need to tell it
146+
# wrapped_func is definitely not None.
147+
assert wrapped_func is not None
147148

148-
return maybe_awaitable(f(*args, **kwargs))
149+
return maybe_awaitable(wrapped_func(*args, **kwargs))
149150

150151
return run
151152

tests/handlers/test_register.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from synapse.api.auth import Auth
1818
from synapse.api.constants import UserTypes
1919
from synapse.api.errors import Codes, ResourceLimitError, SynapseError
20+
from synapse.events.spamcheck import load_legacy_spam_checkers
2021
from synapse.spam_checker_api import RegistrationBehaviour
2122
from synapse.types import RoomAlias, UserID, create_requester
2223

@@ -79,6 +80,39 @@ async def check_registration_for_spam(
7980
return RegistrationBehaviour.ALLOW
8081

8182

83+
class TestLegacyRegistrationSpamChecker:
84+
def __init__(self, config, api):
85+
pass
86+
87+
async def check_registration_for_spam(
88+
self,
89+
email_threepid,
90+
username,
91+
request_info,
92+
):
93+
pass
94+
95+
96+
class LegacyAllowAll(TestLegacyRegistrationSpamChecker):
97+
async def check_registration_for_spam(
98+
self,
99+
email_threepid,
100+
username,
101+
request_info,
102+
):
103+
return RegistrationBehaviour.ALLOW
104+
105+
106+
class LegacyDenyAll(TestLegacyRegistrationSpamChecker):
107+
async def check_registration_for_spam(
108+
self,
109+
email_threepid,
110+
username,
111+
request_info,
112+
):
113+
return RegistrationBehaviour.DENY
114+
115+
82116
class RegistrationTestCase(unittest.HomeserverTestCase):
83117
"""Tests the RegistrationHandler."""
84118

@@ -95,6 +129,8 @@ def make_homeserver(self, reactor, clock):
95129

96130
hs = self.setup_test_homeserver(config=hs_config)
97131

132+
load_legacy_spam_checkers(hs)
133+
98134
module_api = hs.get_module_api()
99135
for module, config in hs.config.modules.loaded_modules:
100136
module(config=config, api=module_api)
@@ -535,6 +571,46 @@ def test_spam_checker_deny(self):
535571
"""A spam checker can deny registration, which results in an error."""
536572
self.get_failure(self.handler.register_user(localpart="user"), SynapseError)
537573

574+
@override_config(
575+
{
576+
"spam_checker": [
577+
{
578+
"module": TestSpamChecker.__module__ + ".LegacyAllowAll",
579+
}
580+
]
581+
}
582+
)
583+
def test_spam_checker_legacy_allow(self):
584+
"""Tests that a legacy spam checker implementing the legacy 3-arg version of the
585+
check_registration_for_spam callback is correctly called.
586+
587+
In this test and the following one we test both success and failure to make sure
588+
any failure comes from the spam checker (and not something else failing in the
589+
call stack) and any success comes from the spam checker (and not because a
590+
misconfiguration prevented it from being loaded).
591+
"""
592+
self.get_success(self.handler.register_user(localpart="user"))
593+
594+
@override_config(
595+
{
596+
"spam_checker": [
597+
{
598+
"module": TestSpamChecker.__module__ + ".LegacyDenyAll",
599+
}
600+
]
601+
}
602+
)
603+
def test_spam_checker_legacy_deny(self):
604+
"""Tests that a legacy spam checker implementing the legacy 3-arg version of the
605+
check_registration_for_spam callback is correctly called.
606+
607+
In this test and the previous one we test both success and failure to make sure
608+
any failure comes from the spam checker (and not something else failing in the
609+
call stack) and any success comes from the spam checker (and not because a
610+
misconfiguration prevented it from being loaded).
611+
"""
612+
self.get_failure(self.handler.register_user(localpart="user"), SynapseError)
613+
538614
@override_config(
539615
{
540616
"modules": [

0 commit comments

Comments
 (0)