From 129f43a34991ea85c64f14e4874879fe7ddb1738 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 1 Aug 2020 15:03:20 +0200 Subject: [PATCH] verify hostname is valid before letting user save it --- .../conversations/ui/EditAccountActivity.java | 14 +++++++++----- .../eu/siacs/conversations/utils/Resolver.java | 13 +++++++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java index d192fa0c6..92d6f2093 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java @@ -33,6 +33,8 @@ import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; +import com.google.common.base.CharMatcher; + import org.openintents.openpgp.util.OpenPgpUtils; 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.XmppUri; import eu.siacs.conversations.xml.Element; +import eu.siacs.conversations.xmpp.Jid; import eu.siacs.conversations.xmpp.OnKeyStatusUpdated; import eu.siacs.conversations.xmpp.OnUpdateBlocklist; import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.XmppConnection.Features; import eu.siacs.conversations.xmpp.forms.Data; import eu.siacs.conversations.xmpp.pep.Avatar; -import eu.siacs.conversations.xmpp.Jid; public class EditAccountActivity extends OmemoActivity implements OnAccountUpdate, OnUpdateBlocklist, OnKeyStatusUpdated, OnCaptchaRequested, KeyChainAliasCallback, XmppConnectionService.OnShowErrorToast, XmppConnectionService.OnMamPreferencesFetched { @@ -215,12 +217,12 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat removeErrorsOnAllBut(binding.accountJidLayout); return; } - String hostname = null; + final String hostname; int numericPort = 5222; if (mShowOptions) { - hostname = binding.hostname.getText().toString().replaceAll("\\s", ""); - final String port = binding.port.getText().toString().replaceAll("\\s", ""); - if (hostname.contains(" ")) { + hostname = CharMatcher.whitespace().removeFrom(binding.hostname.getText()); + final String port = CharMatcher.whitespace().removeFrom(binding.port.getText()); + if (Resolver.invalidHostname(hostname)) { binding.hostnameLayout.setError(getString(R.string.not_valid_hostname)); binding.hostname.requestFocus(); removeErrorsOnAllBut(binding.hostnameLayout); @@ -243,6 +245,8 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat return; } } + } else { + hostname = null; } if (jid.getLocal() == null) { diff --git a/src/main/java/eu/siacs/conversations/utils/Resolver.java b/src/main/java/eu/siacs/conversations/utils/Resolver.java index b7e866a80..2afec6225 100644 --- a/src/main/java/eu/siacs/conversations/utils/Resolver.java +++ b/src/main/java/eu/siacs/conversations/utils/Resolver.java @@ -74,8 +74,8 @@ public class Resolver { } } - public static List fromHardCoded(String hostname, int port) { - Result result = new Result(); + public static List fromHardCoded(final String hostname, final int port) { + final Result result = new Result(); result.hostname = DNSName.from(hostname); result.port = port; result.directTls = useDirectTls(port); @@ -83,6 +83,15 @@ public class Resolver { 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) { return port == 443 || port == 5223;