verify hostname is valid before letting user save it

This commit is contained in:
Daniel Gultsch 2020-08-01 15:03:20 +02:00
parent 637c0cb15a
commit 129f43a349
2 changed files with 20 additions and 7 deletions

View file

@ -33,6 +33,8 @@ import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.Toast; import android.widget.Toast;
import com.google.common.base.CharMatcher;
import org.openintents.openpgp.util.OpenPgpUtils; import org.openintents.openpgp.util.OpenPgpUtils;
import java.net.URL; import java.net.URL;
@ -69,13 +71,13 @@ import eu.siacs.conversations.utils.TorServiceUtils;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.utils.XmppUri; import eu.siacs.conversations.utils.XmppUri;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.Jid;
import eu.siacs.conversations.xmpp.OnKeyStatusUpdated; import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist; import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.XmppConnection.Features; import eu.siacs.conversations.xmpp.XmppConnection.Features;
import eu.siacs.conversations.xmpp.forms.Data; import eu.siacs.conversations.xmpp.forms.Data;
import eu.siacs.conversations.xmpp.pep.Avatar; import eu.siacs.conversations.xmpp.pep.Avatar;
import eu.siacs.conversations.xmpp.Jid;
public class EditAccountActivity extends OmemoActivity implements OnAccountUpdate, OnUpdateBlocklist, public class EditAccountActivity extends OmemoActivity implements OnAccountUpdate, OnUpdateBlocklist,
OnKeyStatusUpdated, OnCaptchaRequested, KeyChainAliasCallback, XmppConnectionService.OnShowErrorToast, XmppConnectionService.OnMamPreferencesFetched { OnKeyStatusUpdated, OnCaptchaRequested, KeyChainAliasCallback, XmppConnectionService.OnShowErrorToast, XmppConnectionService.OnMamPreferencesFetched {
@ -215,12 +217,12 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
removeErrorsOnAllBut(binding.accountJidLayout); removeErrorsOnAllBut(binding.accountJidLayout);
return; return;
} }
String hostname = null; final String hostname;
int numericPort = 5222; int numericPort = 5222;
if (mShowOptions) { if (mShowOptions) {
hostname = binding.hostname.getText().toString().replaceAll("\\s", ""); hostname = CharMatcher.whitespace().removeFrom(binding.hostname.getText());
final String port = binding.port.getText().toString().replaceAll("\\s", ""); final String port = CharMatcher.whitespace().removeFrom(binding.port.getText());
if (hostname.contains(" ")) { if (Resolver.invalidHostname(hostname)) {
binding.hostnameLayout.setError(getString(R.string.not_valid_hostname)); binding.hostnameLayout.setError(getString(R.string.not_valid_hostname));
binding.hostname.requestFocus(); binding.hostname.requestFocus();
removeErrorsOnAllBut(binding.hostnameLayout); removeErrorsOnAllBut(binding.hostnameLayout);
@ -243,6 +245,8 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
return; return;
} }
} }
} else {
hostname = null;
} }
if (jid.getLocal() == null) { if (jid.getLocal() == null) {

View file

@ -74,8 +74,8 @@ public class Resolver {
} }
} }
public static List<Result> fromHardCoded(String hostname, int port) { public static List<Result> fromHardCoded(final String hostname, final int port) {
Result result = new Result(); final Result result = new Result();
result.hostname = DNSName.from(hostname); result.hostname = DNSName.from(hostname);
result.port = port; result.port = port;
result.directTls = useDirectTls(port); result.directTls = useDirectTls(port);
@ -83,6 +83,15 @@ public class Resolver {
return Collections.singletonList(result); return Collections.singletonList(result);
} }
public static boolean invalidHostname(final String hostname) {
try {
DNSName.from(hostname);
return false;
} catch (IllegalArgumentException e) {
return true;
}
}
public static boolean useDirectTls(final int port) { public static boolean useDirectTls(final int port) {
return port == 443 || port == 5223; return port == 443 || port == 5223;