Merge pull request #14203 from nextcloud/nmc/userAccountExceptionFix

Check added for StringIndexOutOfBoundException
This commit is contained in:
Alper Öztürk 2024-12-20 09:43:29 +01:00 committed by GitHub
commit 878d8accf9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -113,25 +113,78 @@ public class UserAccountManagerImpl implements UserAccountManager {
@Override
public boolean exists(Account account) {
Account[] nextcloudAccounts = getAccounts();
try {
if (account == null) {
Log_OC.d(TAG, "account is null");
return false;
}
Account[] nextcloudAccounts = getAccounts();
if (nextcloudAccounts.length == 0) {
Log_OC.d(TAG, "nextcloudAccounts are empty");
return false;
}
if (account.name.isEmpty()) {
Log_OC.d(TAG, "account name is empty");
return false;
}
if (account != null && account.name != null) {
int lastAtPos = account.name.lastIndexOf('@');
if (lastAtPos == -1) {
Log_OC.d(TAG, "lastAtPos cannot be found");
return false;
}
boolean isLastAtPosInBoundsForHostAndPort = lastAtPos + 1 < account.name.length();
if (!isLastAtPosInBoundsForHostAndPort) {
Log_OC.d(TAG, "lastAtPos not in bounds");
return false;
}
String hostAndPort = account.name.substring(lastAtPos + 1);
String username = account.name.substring(0, lastAtPos);
if (hostAndPort.isEmpty() || username.isEmpty()) {
Log_OC.d(TAG, "hostAndPort or username is empty");
return false;
}
String otherHostAndPort;
String otherUsername;
for (Account otherAccount : nextcloudAccounts) {
// Skip null accounts or accounts with null names
if (otherAccount == null || otherAccount.name.isEmpty()) {
continue;
}
lastAtPos = otherAccount.name.lastIndexOf('@');
// Skip invalid account names
if (lastAtPos == -1) {
continue;
}
boolean isLastAtPosInBoundsForOtherHostAndPort = lastAtPos + 1 < otherAccount.name.length();
if (!isLastAtPosInBoundsForOtherHostAndPort) {
continue;
}
otherHostAndPort = otherAccount.name.substring(lastAtPos + 1);
otherUsername = otherAccount.name.substring(0, lastAtPos);
if (otherHostAndPort.equals(hostAndPort) &&
otherUsername.equalsIgnoreCase(username)) {
return true;
}
}
return false;
} catch (Exception e) {
Log_OC.d(TAG, "Exception caught at UserAccountManagerImpl.exists(): " + e);
return false;
}
return false;
}
@Override