From fc27b0ce99c47f0e73b8b811e2ad1aa022a2bdc0 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Wed, 13 Apr 2022 20:09:56 -0400 Subject: [PATCH] Validate server URLs --- app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt | 9 ++++++++- app/src/main/java/io/heckel/ntfy/ui/UserFragment.kt | 3 ++- app/src/main/java/io/heckel/ntfy/util/Util.kt | 2 +- fastlane/metadata/android/en-US/changelog/26.txt | 3 +++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt index bcb3e4f..ac6e241 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt @@ -1,7 +1,6 @@ package io.heckel.ntfy.ui import android.Manifest -import android.annotation.SuppressLint import android.app.AlertDialog import android.content.ClipData import android.content.ClipboardManager @@ -10,6 +9,7 @@ import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.text.TextUtils +import android.widget.Button import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.Keep @@ -297,6 +297,13 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere return repository.getDefaultBaseUrl() } } + defaultBaseUrl?.setOnBindEditTextListener { editText -> + editText.addTextChangedListener(AfterChangedTextWatcher { + val okayButton: Button = editText.rootView.findViewById(android.R.id.button1) + val value = editText.text.toString() + okayButton.isEnabled = value.isEmpty() || validUrl(value) + }) + } defaultBaseUrl?.summaryProvider = Preference.SummaryProvider { pref -> if (TextUtils.isEmpty(pref.text)) { getString(R.string.settings_general_default_base_url_default_summary, appBaseUrl) diff --git a/app/src/main/java/io/heckel/ntfy/ui/UserFragment.kt b/app/src/main/java/io/heckel/ntfy/ui/UserFragment.kt index 4f4daed..7dd8bb9 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/UserFragment.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/UserFragment.kt @@ -16,6 +16,7 @@ import androidx.fragment.app.DialogFragment import com.google.android.material.textfield.TextInputEditText import io.heckel.ntfy.R import io.heckel.ntfy.db.User +import io.heckel.ntfy.util.validUrl class UserFragment : DialogFragment() { private var user: User? = null @@ -167,7 +168,7 @@ class UserFragment : DialogFragment() { val username = usernameView.text?.toString() ?: "" val password = passwordView.text?.toString() ?: "" if (user == null) { - positiveButton.isEnabled = (baseUrl.startsWith("http://") || baseUrl.startsWith("https://")) + positiveButton.isEnabled = validUrl(baseUrl) && !baseUrlsInUse.contains(baseUrl) && username.isNotEmpty() && password.isNotEmpty() } else { diff --git a/app/src/main/java/io/heckel/ntfy/util/Util.kt b/app/src/main/java/io/heckel/ntfy/util/Util.kt index 7df8718..f915215 100644 --- a/app/src/main/java/io/heckel/ntfy/util/Util.kt +++ b/app/src/main/java/io/heckel/ntfy/util/Util.kt @@ -71,7 +71,7 @@ fun validTopic(topic: String): Boolean { } fun validUrl(url: String): Boolean { - return "^https?://.+".toRegex().matches(url) + return "^https?://\\S+".toRegex().matches(url) } fun formatDateShort(timestampSecs: Long): String { diff --git a/fastlane/metadata/android/en-US/changelog/26.txt b/fastlane/metadata/android/en-US/changelog/26.txt index d371c2c..1f44066 100644 --- a/fastlane/metadata/android/en-US/changelog/26.txt +++ b/fastlane/metadata/android/en-US/changelog/26.txt @@ -1,5 +1,8 @@ Features: * Support for ntfy:// deep links (#20, thanks to @Copephobia for reporting) +Bugs: +* Validate URLs when changing default server and server in user management (#193, thanks to @StoyanDimitrov) + Translations: * Japanese (thanks to @shak)