Merge pull request #2294 from element-hq/feature/bma/fix

Ensure login and password exclude `\n`
This commit is contained in:
Benoit Marty 2024-01-26 10:11:51 +01:00 committed by GitHub
commit 8a2490f1e8
2 changed files with 20 additions and 9 deletions

1
changelog.d/2263.bugfix Normal file
View file

@ -0,0 +1 @@
Ensure login and password exclude `\n`

View file

@ -194,15 +194,17 @@ private fun LoginForm(
.onTabOrEnterKeyFocusNext(focusManager) .onTabOrEnterKeyFocusNext(focusManager)
.testTag(TestTags.loginEmailUsername) .testTag(TestTags.loginEmailUsername)
.autofill(autofillTypes = listOf(AutofillType.Username), onFill = { .autofill(autofillTypes = listOf(AutofillType.Username), onFill = {
loginFieldState = it val sanitized = it.sanitize()
eventSink(LoginPasswordEvents.SetLogin(it)) loginFieldState = sanitized
eventSink(LoginPasswordEvents.SetLogin(sanitized))
}), }),
placeholder = { placeholder = {
Text(text = stringResource(CommonStrings.common_username)) Text(text = stringResource(CommonStrings.common_username))
}, },
onValueChange = { onValueChange = {
loginFieldState = it val sanitized = it.sanitize()
eventSink(LoginPasswordEvents.SetLogin(it)) loginFieldState = sanitized
eventSink(LoginPasswordEvents.SetLogin(sanitized))
}, },
keyboardOptions = KeyboardOptions( keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Email, keyboardType = KeyboardType.Email,
@ -224,7 +226,6 @@ private fun LoginForm(
null null
}, },
) )
var passwordVisible by remember { mutableStateOf(false) } var passwordVisible by remember { mutableStateOf(false) }
if (state.loginAction is AsyncData.Loading) { if (state.loginAction is AsyncData.Loading) {
// Ensure password is hidden when user submits the form // Ensure password is hidden when user submits the form
@ -239,12 +240,14 @@ private fun LoginForm(
.onTabOrEnterKeyFocusNext(focusManager) .onTabOrEnterKeyFocusNext(focusManager)
.testTag(TestTags.loginPassword) .testTag(TestTags.loginPassword)
.autofill(autofillTypes = listOf(AutofillType.Password), onFill = { .autofill(autofillTypes = listOf(AutofillType.Password), onFill = {
passwordFieldState = it val sanitized = it.sanitize()
eventSink(LoginPasswordEvents.SetPassword(it)) passwordFieldState = sanitized
eventSink(LoginPasswordEvents.SetPassword(sanitized))
}), }),
onValueChange = { onValueChange = {
passwordFieldState = it val sanitized = it.sanitize()
eventSink(LoginPasswordEvents.SetPassword(it)) passwordFieldState = sanitized
eventSink(LoginPasswordEvents.SetPassword(sanitized))
}, },
placeholder = { placeholder = {
Text(text = stringResource(CommonStrings.common_password)) Text(text = stringResource(CommonStrings.common_password))
@ -272,6 +275,13 @@ private fun LoginForm(
} }
} }
/**
* Ensure that the string does not contain any new line characters, which can happen when pasting values.
*/
private fun String.sanitize(): String {
return replace("\n", "")
}
@Composable @Composable
private fun LoginErrorDialog(error: Throwable, onDismiss: () -> Unit) { private fun LoginErrorDialog(error: Throwable, onDismiss: () -> Unit) {
ErrorDialog( ErrorDialog(