From 5f3f6661b7188d1496399c8d8685202b9033da78 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 26 Sep 2019 16:24:41 +0200 Subject: [PATCH 1/2] Remove URI verification --- .../keepass/activities/PasswordActivity.kt | 13 +- .../activities/helpers/OpenFileHelper.kt | 9 +- .../com/kunzisoft/keepass/utils/UriUtil.kt | 120 +----------------- 3 files changed, 9 insertions(+), 133 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/PasswordActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/PasswordActivity.kt index 11f6b5045..bfb85ab32 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/PasswordActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/PasswordActivity.kt @@ -190,17 +190,10 @@ class PasswordActivity : StylishActivity() { // If is a view intent val action = intent.action - if (action != null && action == VIEW_INTENT) { - - val databaseUriRetrieve = intent.data - // Stop activity here if we can't verify database URI - if (!UriUtil.verifyFileUri(databaseUriRetrieve)) { - Log.e(TAG, "File URI not validate") - finish() - } - databaseUri = databaseUriRetrieve + if (action != null + && action == VIEW_INTENT) { + databaseUri = intent.data keyFileUri = UriUtil.getUriFromIntent(intent, KEY_KEYFILE) - } else { databaseUri = intent.getParcelableExtra(KEY_FILENAME) keyFileUri = intent.getParcelableExtra(KEY_KEYFILE) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/helpers/OpenFileHelper.kt b/app/src/main/java/com/kunzisoft/keepass/activities/helpers/OpenFileHelper.kt index 48f283491..deac3ee02 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/helpers/OpenFileHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/helpers/OpenFileHelper.kt @@ -177,22 +177,19 @@ class OpenFileHelper { GET_CONTENT, OPEN_DOC -> { if (resultCode == RESULT_OK) { if (data != null) { - var uri = data.data + val uri = data.data if (uri != null) { try { // try to persist read and write permissions if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity?.contentResolver?.apply { - takePersistableUriPermission(uri!!, Intent.FLAG_GRANT_READ_URI_PERMISSION) - takePersistableUriPermission(uri!!, Intent.FLAG_GRANT_WRITE_URI_PERMISSION) + takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION) + takePersistableUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION) } } } catch (e: Exception) { // nop } - if (requestCode == GET_CONTENT) { - uri = UriUtil.translateUri(activity!!, uri) - } keyFileCallback?.invoke(uri) } } diff --git a/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt b/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt index 92f0dba58..617a6c1c9 100644 --- a/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt +++ b/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt @@ -35,89 +35,6 @@ import java.io.InputStream object UriUtil { - /** - * Many android apps respond with non-writeable content URIs that correspond to files. - * This will attempt to translate the content URIs to file URIs when possible/appropriate - * @param uri - * @return - */ - fun translateUri(ctx: Context, uri: Uri): Uri { - var currentUri = uri - // StorageAF provides nice URIs - if (hasWritableContentUri(currentUri)) { - return currentUri - } - - val scheme = currentUri.scheme - if (scheme == null || scheme.isEmpty()) { - return currentUri - } - - var filepath: String? = null - - try { - // Use content resolver to try and find the file - if (scheme.equals("content", ignoreCase = true)) { - val cursor = ctx.contentResolver.query(currentUri, arrayOf(android.provider.MediaStore.Images.ImageColumns.DATA), null, null, null) - if (cursor != null) { - cursor.moveToFirst() - filepath = cursor.getString(0) - cursor.close() - if (!isValidFilePath(filepath)) { - filepath = null - } - } - } - - // Try using the URI path as a straight file - if (filepath == null || filepath.isEmpty()) { - filepath = currentUri.encodedPath - if (!isValidFilePath(filepath)) { - filepath = null - } - } - } catch (e: Exception) { - filepath = null - } - // Fall back to URI if this fails. - - // Update the file to a file URI - if (filepath != null && filepath.isNotEmpty()) { - val b = Uri.Builder() - currentUri = b.scheme("file").authority("").path(filepath).build() - } - - return currentUri - } - - private fun isValidFilePath(filepath: String?): Boolean { - if (filepath == null || filepath.isEmpty()) { - return false - } - val file = File(filepath) - return file.exists() && file.canRead() - } - - /** - * Whitelist for known content providers that support writing - * @param uri - * @return - */ - private fun hasWritableContentUri(uri: Uri): Boolean { - val scheme = uri.scheme - if (scheme == null || scheme.isEmpty()) { - return false - } - if (!scheme.equals("content", ignoreCase = true)) { - return false - } - when (uri.authority) { - "com.google.android.apps.docs.storage" -> return true - } - - return false - } - @Throws(FileNotFoundException::class) fun getUriInputStream(contentResolver: ContentResolver, uri: Uri?): InputStream? { if (uri == null) @@ -132,38 +49,9 @@ object UriUtil { } } - fun verifyFileUri(fileUri: Uri?): Boolean { - - if (fileUri == null || fileUri == Uri.EMPTY) - return false - - val scheme = fileUri.scheme - return when { - scheme == null || scheme.isEmpty() -> { - false - } - scheme.equals("file", ignoreCase = true) -> { - val filePath = fileUri.path - if (filePath == null || filePath.isEmpty()) - false - else { - File(filePath).exists() - } - } - scheme.equals("content", ignoreCase = true) -> { - true - } - else -> false - } - } - fun parse(stringUri: String?): Uri? { return if (stringUri?.isNotEmpty() == true) { - val uriParsed = Uri.parse(stringUri) - if (verifyFileUri(uriParsed)) - uriParsed - else - null + Uri.parse(stringUri) } else null } @@ -193,18 +81,16 @@ object UriUtil { return null } - @Throws(ActivityNotFoundException::class) fun gotoUrl(context: Context, url: String?) { try { if (url != null && url.isNotEmpty()) { - context.startActivity(Intent(Intent.ACTION_VIEW, parse(url))) + context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) } - } catch (e: ActivityNotFoundException) { + } catch (e: Exception) { Toast.makeText(context, R.string.no_url_handler, Toast.LENGTH_LONG).show() } } - @Throws(ActivityNotFoundException::class) fun gotoUrl(context: Context, resId: Int) { gotoUrl(context, context.getString(resId)) } From 4100258476f112641d9fce8f065da2b2bb088255 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 26 Sep 2019 16:26:41 +0200 Subject: [PATCH 2/2] Remove unused import --- app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt b/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt index 617a6c1c9..c70b6630f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt +++ b/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt @@ -19,7 +19,6 @@ */ package com.kunzisoft.keepass.utils -import android.content.ActivityNotFoundException import android.content.ContentResolver import android.content.Context import android.content.Intent @@ -27,7 +26,6 @@ import android.net.Uri import android.os.Build import android.widget.Toast import com.kunzisoft.keepass.R -import java.io.File import java.io.FileInputStream import java.io.FileNotFoundException import java.io.InputStream