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

Commit 3c41d87

Browse files
authored
Add restrictions by default to open registration in Synapse (#12091)
1 parent 7ca8ee6 commit 3c41d87

File tree

7 files changed

+67
-4
lines changed

7 files changed

+67
-4
lines changed

changelog.d/12091.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refuse to start if registration is enabled without email, captcha, or token-based verification unless new config flag `enable_registration_without_verification` is set.

demo/start.sh

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ for port in 8080 8081 8082; do
3838
printf '\n\n# Customisation made by demo/start.sh\n\n'
3939
echo "public_baseurl: http://localhost:$port/"
4040
echo 'enable_registration: true'
41+
echo 'enable_registration_without_verification: true'
4142
echo ''
4243

4344
# Warning, this heredoc depends on the interaction of tabs and spaces.

docs/sample_config.yaml

+9-1
Original file line numberDiff line numberDiff line change
@@ -1218,10 +1218,18 @@ oembed:
12181218
# Registration can be rate-limited using the parameters in the "Ratelimiting"
12191219
# section of this file.
12201220

1221-
# Enable registration for new users.
1221+
# Enable registration for new users. Defaults to 'false'. It is highly recommended that if you enable registration,
1222+
# you use either captcha, email, or token-based verification to verify that new users are not bots. In order to enable registration
1223+
# without any verification, you must also set `enable_registration_without_verification`, found below.
12221224
#
12231225
#enable_registration: false
12241226

1227+
# Enable registration without email or captcha verification. Note: this option is *not* recommended,
1228+
# as registration without verification is a known vector for spam and abuse. Defaults to false. Has no effect
1229+
# unless `enable_registration` is also enabled.
1230+
#
1231+
#enable_registration_without_verification: true
1232+
12251233
# Time that a user's session remains valid for, after they log in.
12261234
#
12271235
# Note that this is not currently compatible with guest logins.

docs/upgrade.md

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ for more information and instructions on how to fix a database with incorrect va
108108

109109
# Upgrading to v1.55.0
110110

111+
## Open registration without verification is now disabled by default
112+
113+
Synapse will refuse to start if registration is enabled without email, captcha, or token-based verification unless the new config
114+
flag `enable_registration_without_verification` is set to "true".
115+
116+
111117
## `synctl` script has been moved
112118

113119
The `synctl` script

synapse/app/homeserver.py

+17
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,23 @@ def setup(config_options: List[str]) -> SynapseHomeServer:
351351
if config.server.gc_seconds:
352352
synapse.metrics.MIN_TIME_BETWEEN_GCS = config.server.gc_seconds
353353

354+
if (
355+
config.registration.enable_registration
356+
and not config.registration.enable_registration_without_verification
357+
):
358+
if (
359+
not config.captcha.enable_registration_captcha
360+
and not config.registration.registrations_require_3pid
361+
and not config.registration.registration_requires_token
362+
):
363+
364+
raise ConfigError(
365+
"You have enabled open registration without any verification. This is a known vector for "
366+
"spam and abuse. If you would like to allow public registration, please consider adding email, "
367+
"captcha, or token-based verification. Otherwise this check can be removed by setting the "
368+
"`enable_registration_without_verification` config option to `true`."
369+
)
370+
354371
hs = SynapseHomeServer(
355372
config.server.server_name,
356373
config=config,

synapse/config/registration.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def read_config(self, config, **kwargs):
3333
str(config["disable_registration"])
3434
)
3535

36+
self.enable_registration_without_verification = strtobool(
37+
str(config.get("enable_registration_without_verification", False))
38+
)
39+
3640
self.registrations_require_3pid = config.get("registrations_require_3pid", [])
3741
self.allowed_local_3pids = config.get("allowed_local_3pids", [])
3842
self.enable_3pid_lookup = config.get("enable_3pid_lookup", True)
@@ -207,10 +211,18 @@ def generate_config_section(self, generate_secrets=False, **kwargs):
207211
# Registration can be rate-limited using the parameters in the "Ratelimiting"
208212
# section of this file.
209213
210-
# Enable registration for new users.
214+
# Enable registration for new users. Defaults to 'false'. It is highly recommended that if you enable registration,
215+
# you use either captcha, email, or token-based verification to verify that new users are not bots. In order to enable registration
216+
# without any verification, you must also set `enable_registration_without_verification`, found below.
211217
#
212218
#enable_registration: false
213219
220+
# Enable registration without email or captcha verification. Note: this option is *not* recommended,
221+
# as registration without verification is a known vector for spam and abuse. Defaults to false. Has no effect
222+
# unless `enable_registration` is also enabled.
223+
#
224+
#enable_registration_without_verification: true
225+
214226
# Time that a user's session remains valid for, after they log in.
215227
#
216228
# Note that this is not currently compatible with guest logins.

tests/config/test_registration_config.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
import synapse.app.homeserver
1416
from synapse.config import ConfigError
1517
from synapse.config.homeserver import HomeServerConfig
1618

17-
from tests.unittest import TestCase
19+
from tests.config.utils import ConfigFileTestCase
1820
from tests.utils import default_config
1921

2022

21-
class RegistrationConfigTestCase(TestCase):
23+
class RegistrationConfigTestCase(ConfigFileTestCase):
2224
def test_session_lifetime_must_not_be_exceeded_by_smaller_lifetimes(self):
2325
"""
2426
session_lifetime should logically be larger than, or at least as large as,
@@ -76,3 +78,19 @@ def test_session_lifetime_must_not_be_exceeded_by_smaller_lifetimes(self):
7678
HomeServerConfig().parse_config_dict(
7779
{"session_lifetime": "31m", "refresh_token_lifetime": "31m", **config_dict}
7880
)
81+
82+
def test_refuse_to_start_if_open_registration_and_no_verification(self):
83+
self.generate_config()
84+
self.add_lines_to_config(
85+
[
86+
" ",
87+
"enable_registration: true",
88+
"registrations_require_3pid: []",
89+
"enable_registration_captcha: false",
90+
"registration_requires_token: false",
91+
]
92+
)
93+
94+
# Test that allowing open registration without verification raises an error
95+
with self.assertRaises(ConfigError):
96+
synapse.app.homeserver.setup(["-c", self.config_file])

0 commit comments

Comments
 (0)