fix: Possible crash if given url is wrong

This commit is contained in:
LooKeR 2025-06-23 20:22:56 +05:30
parent 4223cea6d7
commit 53df86b72a
No known key found for this signature in database
GPG Key ID: 6B59369FDB608FB9

View File

@ -27,6 +27,7 @@ import com.looker.droidify.ui.Message
import com.looker.droidify.ui.MessageDialog import com.looker.droidify.ui.MessageDialog
import com.looker.droidify.ui.ScreenFragment import com.looker.droidify.ui.ScreenFragment
import com.looker.droidify.utility.common.extension.clipboardManager import com.looker.droidify.utility.common.extension.clipboardManager
import com.looker.droidify.utility.common.extension.exceptCancellation
import com.looker.droidify.utility.common.extension.get import com.looker.droidify.utility.common.extension.get
import com.looker.droidify.utility.common.extension.getMutatedIcon import com.looker.droidify.utility.common.extension.getMutatedIcon
import com.looker.droidify.utility.common.nullIfEmpty import com.looker.droidify.utility.common.nullIfEmpty
@ -136,7 +137,7 @@ class EditRepositoryFragment() : ScreenFragment() {
Selection.setSelection( Selection.setSelection(
text, text,
realPosition(outputString, inputStart), realPosition(outputString, inputStart),
realPosition(outputString, inputEnd) realPosition(outputString, inputEnd),
) )
} }
} }
@ -155,7 +156,7 @@ class EditRepositoryFragment() : ScreenFragment() {
Pair( Pair(
uri.buildUpon().path(uri.path?.pathCropped).query(null).fragment(null) uri.buildUpon().path(uri.path?.pathCropped).query(null).fragment(null)
.build().toString(), .build().toString(),
fingerprintText fingerprintText,
) )
} catch (e: Exception) { } catch (e: Exception) {
Pair(null, null) Pair(null, null)
@ -171,7 +172,7 @@ class EditRepositoryFragment() : ScreenFragment() {
setEndIconOnClickListener { setEndIconOnClickListener {
SelectMirrorDialog(mirrors).show( SelectMirrorDialog(mirrors).show(
childFragmentManager, childFragmentManager,
SelectMirrorDialog::class.java.name SelectMirrorDialog::class.java.name,
) )
} }
} }
@ -189,7 +190,7 @@ class EditRepositoryFragment() : ScreenFragment() {
if (index >= 0) { if (index >= 0) {
Pair( Pair(
it.substring(0, index), it.substring(0, index),
it.substring(index + 1) it.substring(index + 1),
) )
} else { } else {
null null
@ -319,7 +320,7 @@ class EditRepositoryFragment() : ScreenFragment() {
return if (endsWith != null) { return if (endsWith != null) {
cropped.substring( cropped.substring(
0, 0,
cropped.length - endsWith.length - 1 cropped.length - endsWith.length - 1,
) )
} else { } else {
cropped cropped
@ -330,12 +331,12 @@ class EditRepositoryFragment() : ScreenFragment() {
val uri = try { val uri = try {
val uri = URI(address) val uri = URI(address)
if (uri.isAbsolute) uri.normalize() else null if (uri.isAbsolute) uri.normalize() else null
} catch (e: URISyntaxException) { } catch (_: URISyntaxException) {
return null return null
} }
return try { return try {
uri?.toURL()?.toURI()?.toString()?.removeSuffix("/") uri?.toURL()?.toURI()?.toString()?.removeSuffix("/")
} catch (e: URISyntaxException) { } catch (_: URISyntaxException) {
null null
} }
} }
@ -346,7 +347,10 @@ class EditRepositoryFragment() : ScreenFragment() {
private fun onSaveRepositoryClick(check: Boolean) { private fun onSaveRepositoryClick(check: Boolean) {
if (!checkInProgress) { if (!checkInProgress) {
val address = normalizeAddress(binding.address.text.toString())!! val address = normalizeAddress(binding.address.text.toString()) ?: kotlin.run {
failedAddressCheck()
return
}
val fingerprint = binding.fingerprint.text.toString().replace(" ", "") val fingerprint = binding.fingerprint.text.toString().replace(" ", "")
val username = binding.username.text.toString().nullIfEmpty() val username = binding.username.text.toString().nullIfEmpty()
val password = binding.password.text.toString().nullIfEmpty() val password = binding.password.text.toString().nullIfEmpty()
@ -354,7 +358,7 @@ class EditRepositoryFragment() : ScreenFragment() {
password?.let { p -> password?.let { p ->
Base64.encodeToString( Base64.encodeToString(
"$u:$p".toByteArray(Charset.defaultCharset()), "$u:$p".toByteArray(Charset.defaultCharset()),
Base64.NO_WRAP Base64.NO_WRAP,
) )
} }
}?.let { "Basic $it" }.orEmpty() }?.let { "Basic $it" }.orEmpty()
@ -364,7 +368,7 @@ class EditRepositoryFragment() : ScreenFragment() {
val resultAddress = try { val resultAddress = try {
checkAddress(address, authentication) checkAddress(address, authentication)
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.exceptCancellation()
failedAddressCheck() failedAddressCheck()
null null
} }
@ -378,7 +382,7 @@ class EditRepositoryFragment() : ScreenFragment() {
onSaveRepositoryProceedInvalidate( onSaveRepositoryProceedInvalidate(
resultAddress, resultAddress,
fingerprint, fingerprint,
authentication authentication,
) )
} else { } else {
invalidateState() invalidateState()
@ -393,7 +397,7 @@ class EditRepositoryFragment() : ScreenFragment() {
private suspend fun checkAddress( private suspend fun checkAddress(
rawAddress: String, rawAddress: String,
authentication: String authentication: String,
): String? = coroutineScope { ): String? = coroutineScope {
checkInProgress = true checkInProgress = true
invalidateState() invalidateState()
@ -403,7 +407,7 @@ class EditRepositoryFragment() : ScreenFragment() {
.forEach { address -> .forEach { address ->
val response = downloader.headCall( val response = downloader.headCall(
url = "$address/index-v1.jar", url = "$address/index-v1.jar",
headers = { authentication(authentication) } headers = { authentication(authentication) },
) )
if (response is NetworkResponse.Success) return@coroutineScope address if (response is NetworkResponse.Success) return@coroutineScope address
} }
@ -413,7 +417,7 @@ class EditRepositoryFragment() : ScreenFragment() {
private fun onSaveRepositoryProceedInvalidate( private fun onSaveRepositoryProceedInvalidate(
address: String, address: String,
fingerprint: String, fingerprint: String,
authentication: String authentication: String,
) { ) {
val binder = syncConnection.binder val binder = syncConnection.binder
if (binder != null) { if (binder != null) {
@ -442,7 +446,7 @@ class EditRepositoryFragment() : ScreenFragment() {
Snackbar.make( Snackbar.make(
requireView(), requireView(),
R.string.repository_unreachable, R.string.repository_unreachable,
Snackbar.LENGTH_SHORT Snackbar.LENGTH_SHORT,
).show() ).show()
} }