From dc432ccaa57215f7e77c5dd34bcd78789198a259 Mon Sep 17 00:00:00 2001 From: AL-Session <160798022+AL-Session@users.noreply.github.com> Date: Thu, 6 Feb 2025 16:32:59 +1100 Subject: [PATCH] Recovery password linebreak / double space removal - QA feedback (#918) * Filtered any control characters or double-spaces from copied recovery password mnemonic * Fix typo in comment * Single char comment alignment adjustment * Comment phrasing adjustment * Fixed double space removal (single char adjustment) * Simplified mnemonic filtering regex * Fixed regex --------- Co-authored-by: alansley --- .../RecoveryPasswordViewModel.kt | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/recoverypassword/RecoveryPasswordViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/recoverypassword/RecoveryPasswordViewModel.kt index e6af49ff19..f97daa9319 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/recoverypassword/RecoveryPasswordViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/recoverypassword/RecoveryPasswordViewModel.kt @@ -27,15 +27,6 @@ class RecoveryPasswordViewModel @Inject constructor( ): AndroidViewModel(application) { val prefs = AppTextSecurePreferences(application) - // Regex to remove any spurious characters from our recovery password mnemonic. - // The regex matches are: - // - "\r" - carriage return, - // - "\n" - newline, - // - "\u2028" - unicode line separator, - // - "\u2029" - unicode paragraph separator, - // - "|\s{2,}" - two or more consecutive spaces. - val linebreakRemovalRegex = Regex("""[\r\n\u2028\u2029]+|\s{2,}""") - val seed = MutableStateFlow(null) val mnemonic = seed.filterNotNull() .map { @@ -50,8 +41,13 @@ class RecoveryPasswordViewModel @Inject constructor( fun copyMnemonic() { prefs.setHasViewedSeed(true) - // Ensure that our mnemonic words are separated by single spaces only without any control characters - val normalisedMnemonic = mnemonic.value.replace(linebreakRemovalRegex, " ") + // Ensure that our mnemonic words are separated by single spaces only without any excessive + // whitespace or control characters via: + // - Replacing all control chars (\p{Cc}) or Unicode separators (\p{Z}) with a single space, then + // - Trimming all leading & trailing spaces. + val normalisedMnemonic = mnemonic.value + .replace(Regex("[\\p{Cc}\\p{Z}]+"), " ") + .trim() ClipData.newPlainText("Seed", normalisedMnemonic) .let(application.clipboard::setPrimaryClip)