From 16c8ab5517363137d9d231ecbf7d8e02dc2b4325 Mon Sep 17 00:00:00 2001 From: Callum Brown Date: Wed, 22 Sep 2021 17:20:43 +0100 Subject: [PATCH 1/3] Allow `.` and `~` chars in registration tokens See https://github.com/matrix-org/matrix-doc/pull/3231/commits/79939f955b34113404b5d363a981f790aa7ec0e7 Signed-off-by: Callum Brown --- changelog.d/10887.bugfix | 1 + synapse/rest/admin/registration_tokens.py | 2 +- tests/rest/admin/test_registration_tokens.py | 6 ++++-- 3 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 changelog.d/10887.bugfix diff --git a/changelog.d/10887.bugfix b/changelog.d/10887.bugfix new file mode 100644 index 000000000000..2d4ac4af5985 --- /dev/null +++ b/changelog.d/10887.bugfix @@ -0,0 +1 @@ +Allow `.` and `~` characters when creating registration tokens as per the [change to MSC3231](https://github.com/matrix-org/matrix-doc/pull/3231/commits/79939f955b34113404b5d363a981f790aa7ec0e7). diff --git a/synapse/rest/admin/registration_tokens.py b/synapse/rest/admin/registration_tokens.py index 5a1c929d85c4..aba48f6e7bab 100644 --- a/synapse/rest/admin/registration_tokens.py +++ b/synapse/rest/admin/registration_tokens.py @@ -113,7 +113,7 @@ def __init__(self, hs: "HomeServer"): self.store = hs.get_datastore() self.clock = hs.get_clock() # A string of all the characters allowed to be in a registration_token - self.allowed_chars = string.ascii_letters + string.digits + "-_" + self.allowed_chars = string.ascii_letters + string.digits + "._~-" self.allowed_chars_set = set(self.allowed_chars) async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]: diff --git a/tests/rest/admin/test_registration_tokens.py b/tests/rest/admin/test_registration_tokens.py index 4927321e5a4f..fe0db25a0e48 100644 --- a/tests/rest/admin/test_registration_tokens.py +++ b/tests/rest/admin/test_registration_tokens.py @@ -95,8 +95,10 @@ def test_create_using_defaults(self): def test_create_specifying_fields(self): """Create a token specifying the value of all fields.""" + # As many of the allowed characters as possible with length <= 64 + token = "adefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._~-" data = { - "token": "abcd", + "token": token, "uses_allowed": 1, "expiry_time": self.clock.time_msec() + 1000000, } @@ -109,7 +111,7 @@ def test_create_specifying_fields(self): ) self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) - self.assertEqual(channel.json_body["token"], "abcd") + self.assertEqual(channel.json_body["token"], token) self.assertEqual(channel.json_body["uses_allowed"], 1) self.assertEqual(channel.json_body["expiry_time"], data["expiry_time"]) self.assertEqual(channel.json_body["pending"], 0) From 5a280e3bfc4f557df8d7805ccdbc385a45cbbc89 Mon Sep 17 00:00:00 2001 From: Callum Brown Date: Thu, 23 Sep 2021 12:34:31 +0100 Subject: [PATCH 2/3] Fix unit test Signed-off-by: Callum Brown --- tests/rest/admin/test_registration_tokens.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rest/admin/test_registration_tokens.py b/tests/rest/admin/test_registration_tokens.py index fe0db25a0e48..9bac423ae048 100644 --- a/tests/rest/admin/test_registration_tokens.py +++ b/tests/rest/admin/test_registration_tokens.py @@ -195,7 +195,7 @@ def test_create_unable_to_generate_token(self): """Check right error is raised when server can't generate unique token.""" # Create all possible single character tokens tokens = [] - for c in string.ascii_letters + string.digits + "-_": + for c in string.ascii_letters + string.digits + "._~-": tokens.append( { "token": c, From f1b42bb09963fed0dca6d373d34913c98bde5dfe Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 23 Sep 2021 13:34:35 -0400 Subject: [PATCH 3/3] Update changelog. --- changelog.d/10887.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/10887.bugfix b/changelog.d/10887.bugfix index 2d4ac4af5985..2d1f67489a89 100644 --- a/changelog.d/10887.bugfix +++ b/changelog.d/10887.bugfix @@ -1 +1 @@ -Allow `.` and `~` characters when creating registration tokens as per the [change to MSC3231](https://github.com/matrix-org/matrix-doc/pull/3231/commits/79939f955b34113404b5d363a981f790aa7ec0e7). +Allow the `.` and `~` characters when creating registration tokens as per the change to [MSC3231](https://github.com/matrix-org/matrix-doc/pull/3231).