From 7636b4d7a82c0ac7e4b237f081f4d8eec7ab167e Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 4 Oct 2021 12:40:43 +0200 Subject: [PATCH] limit alias length in candidateAliasFromRoomName() --- .../org/matrix/android/sdk/api/MatrixPatterns.kt | 14 ++++++++------ .../features/spaces/create/CreateSpaceViewModel.kt | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixPatterns.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixPatterns.kt index 5134c0d538..034efc2683 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixPatterns.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixPatterns.kt @@ -162,12 +162,14 @@ object MatrixPatterns { return order != null && order.length < 50 && order matches ORDER_STRING_REGEX } - fun candidateAliasFromRoomName(name: String): String { - return Regex("\\s").replace(name.lowercase(), "_").let { - "[^a-z0-9._%#@=+-]".toRegex().replace(it, "") - }.let { alias -> - if (alias.length > 255) alias.substring(0, 255) else alias - } + fun candidateAliasFromRoomName(roomName: String, domain: String): String { + return roomName.lowercase() + // Replace spaces by '_' + .let { Regex("\\s").replace(it, "_") } + // Remove all invalid chars + .let { "[^a-z0-9._%#@=+-]".toRegex().replace(it, "") } + // limit length + .substring(0, MatrixConstants.maxAliasLocalPartLength(domain)) } /** diff --git a/vector/src/main/java/im/vector/app/features/spaces/create/CreateSpaceViewModel.kt b/vector/src/main/java/im/vector/app/features/spaces/create/CreateSpaceViewModel.kt index e6ead2294e..4495ee31a1 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/create/CreateSpaceViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/create/CreateSpaceViewModel.kt @@ -134,7 +134,7 @@ class CreateSpaceViewModel @AssistedInject constructor( ) } else { val tentativeAlias = - MatrixPatterns.candidateAliasFromRoomName(action.name) + MatrixPatterns.candidateAliasFromRoomName(action.name, homeServerName) copy( nameInlineError = null, name = action.name,