Limit allowed characters in display names

This commit is contained in:
Niels Andriesse 2019-10-21 16:28:05 +11:00
parent f7e01688b0
commit 6d0858cf35
2 changed files with 10 additions and 0 deletions

View File

@ -67,6 +67,8 @@ import java.io.IOException;
import java.security.SecureRandom;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
@ -227,9 +229,14 @@ public class CreateProfileActivity extends BaseActionBarActivity implements Inje
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
Pattern pattern = Pattern.compile("[a-zA-Z0-9_]+");
Matcher matcher = pattern.matcher(s.toString());
if (s.toString().isEmpty()) {
name.getInput().setError("Invalid");
finishButton.setEnabled(false);
} else if (!matcher.matches()) {
name.getInput().setError("Invalid (a-z, A-Z, 0-9 and _ only)");
finishButton.setEnabled(false);
} else if (s.toString().getBytes().length > ProfileCipher.NAME_PADDED_LENGTH) {
name.getInput().setError(getString(R.string.CreateProfileActivity_too_long));
finishButton.setEnabled(false);

View File

@ -27,6 +27,9 @@ class DisplayNameActivity : BaseActionBarActivity() {
if (name.isEmpty()) {
return nameEditText.input.setError("Invalid")
}
if (!name.matches(Regex("[a-zA-Z0-9_]+"))) {
return nameEditText.input.setError("Invalid (a-z, A-Z, 0-9 and _ only)")
}
if (name.toByteArray().size > ProfileCipher.NAME_PADDED_LENGTH) {
return nameEditText.input.setError("Too Long")
} else {