Skip to content

Commit

Permalink
Validate server URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Heckel committed Apr 14, 2022
1 parent 2b0fa4f commit fc27b0c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
9 changes: 8 additions & 1 deletion app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<EditTextPreference> { pref ->
if (TextUtils.isEmpty(pref.text)) {
getString(R.string.settings_general_default_base_url_default_summary, appBaseUrl)
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/io/heckel/ntfy/ui/UserFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/io/heckel/ntfy/util/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions fastlane/metadata/android/en-US/changelog/26.txt
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit fc27b0c

Please sign in to comment.