From 74a6ed82b31d340b62e6bd255f21e8946ffe6db6 Mon Sep 17 00:00:00 2001 From: David Woloszyn Date: Fri, 5 Apr 2024 10:37:59 +1100 Subject: [PATCH] MDL-80849 communication_matrix: Prevent numeric usernames --- .../provider/matrix/classes/matrix_user_manager.php | 11 +++++++++++ .../matrix/tests/matrix_user_manager_test.php | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/communication/provider/matrix/classes/matrix_user_manager.php b/communication/provider/matrix/classes/matrix_user_manager.php index 38323c70f0a..2dd4041f8b0 100644 --- a/communication/provider/matrix/classes/matrix_user_manager.php +++ b/communication/provider/matrix/classes/matrix_user_manager.php @@ -24,6 +24,12 @@ namespace communication_matrix; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class matrix_user_manager { + + /** + * Prefix for Matrix usernames when they are detected as numeric. + */ + const MATRIX_USER_PREFIX = 'user'; + /** * Gets matrix user id from moodle. * @@ -56,6 +62,11 @@ class matrix_user_manager { $username = preg_replace('/[@#$%^&*()+{}|<>?!,]/i', '.', $username); $username = ltrim(rtrim($username, '.'), '.'); + // Matrix/Synapse servers will not allow numeric usernames. + if (is_numeric($username)) { + $username = self::MATRIX_USER_PREFIX . $username; + } + $homeserver = self::get_formatted_matrix_home_server(); return "@{$username}:{$homeserver}"; diff --git a/communication/provider/matrix/tests/matrix_user_manager_test.php b/communication/provider/matrix/tests/matrix_user_manager_test.php index 00877c4b9ba..559e435e417 100644 --- a/communication/provider/matrix/tests/matrix_user_manager_test.php +++ b/communication/provider/matrix/tests/matrix_user_manager_test.php @@ -126,6 +126,11 @@ class matrix_user_manager_test extends \advanced_testcase { 'colin.creavey', '@colin.creavey:matrix.example.org', ], + 'numeric username' => [ + 'https://matrix.example.org', + '123456', + '@' . matrix_user_manager::MATRIX_USER_PREFIX . '123456:matrix.example.org', + ], ]; }