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

Commit 6d02eb2

Browse files
authored
Fix startup failure with localdb_enabled: False (#8937)
1 parent 1619802 commit 6d02eb2

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

changelog.d/8937.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug introduced in Synapse v1.24.0 which would cause an exception on startup if both `enabled` and `localdb_enabled` were set to `False` in the `password_config` setting of the configuration file.

synapse/handlers/auth.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -198,27 +198,25 @@ def __init__(self, hs: "HomeServer"):
198198
self._password_enabled = hs.config.password_enabled
199199
self._password_localdb_enabled = hs.config.password_localdb_enabled
200200

201-
# we keep this as a list despite the O(N^2) implication so that we can
202-
# keep PASSWORD first and avoid confusing clients which pick the first
203-
# type in the list. (NB that the spec doesn't require us to do so and
204-
# clients which favour types that they don't understand over those that
205-
# they do are technically broken)
206-
207201
# start out by assuming PASSWORD is enabled; we will remove it later if not.
208-
login_types = []
202+
login_types = set()
209203
if self._password_localdb_enabled:
210-
login_types.append(LoginType.PASSWORD)
204+
login_types.add(LoginType.PASSWORD)
211205

212206
for provider in self.password_providers:
213-
if hasattr(provider, "get_supported_login_types"):
214-
for t in provider.get_supported_login_types().keys():
215-
if t not in login_types:
216-
login_types.append(t)
207+
login_types.update(provider.get_supported_login_types().keys())
217208

218209
if not self._password_enabled:
210+
login_types.discard(LoginType.PASSWORD)
211+
212+
# Some clients just pick the first type in the list. In this case, we want
213+
# them to use PASSWORD (rather than token or whatever), so we want to make sure
214+
# that comes first, where it's present.
215+
self._supported_login_types = []
216+
if LoginType.PASSWORD in login_types:
217+
self._supported_login_types.append(LoginType.PASSWORD)
219218
login_types.remove(LoginType.PASSWORD)
220-
221-
self._supported_login_types = login_types
219+
self._supported_login_types.extend(login_types)
222220

223221
# Ratelimiter for failed auth during UIA. Uses same ratelimit config
224222
# as per `rc_login.failed_attempts`.

tests/handlers/test_password_providers.py

+23
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,29 @@ def test_custom_auth_password_disabled(self):
430430
self.assertEqual(channel.code, 400, channel.result)
431431
mock_password_provider.check_auth.assert_not_called()
432432

433+
@override_config(
434+
{
435+
**providers_config(CustomAuthProvider),
436+
"password_config": {"enabled": False, "localdb_enabled": False},
437+
}
438+
)
439+
def test_custom_auth_password_disabled_localdb_enabled(self):
440+
"""Check the localdb_enabled == enabled == False
441+
442+
Regression test for https://github.com/matrix-org/synapse/issues/8914: check
443+
that setting *both* `localdb_enabled` *and* `password: enabled` to False doesn't
444+
cause an exception.
445+
"""
446+
self.register_user("localuser", "localpass")
447+
448+
flows = self._get_login_flows()
449+
self.assertEqual(flows, [{"type": "test.login_type"}] + ADDITIONAL_LOGIN_FLOWS)
450+
451+
# login shouldn't work and should be rejected with a 400 ("unknown login type")
452+
channel = self._send_password_login("localuser", "localpass")
453+
self.assertEqual(channel.code, 400, channel.result)
454+
mock_password_provider.check_auth.assert_not_called()
455+
433456
@override_config(
434457
{
435458
**providers_config(PasswordCustomAuthProvider),

0 commit comments

Comments
 (0)