Check if the search element is blocked after Autofill popup click

This commit is contained in:
J-Jamet
2020-06-17 22:49:09 +02:00
parent e12f008b92
commit bd9f2c4757
3 changed files with 41 additions and 33 deletions

View File

@@ -26,26 +26,43 @@ import android.content.Intent
import android.content.IntentSender import android.content.IntentSender
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.widget.Toast
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.kunzisoft.keepass.R import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.autofill.AutofillHelper import com.kunzisoft.keepass.autofill.AutofillHelper
import com.kunzisoft.keepass.autofill.KeeAutofillService
import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.search.SearchHelper import com.kunzisoft.keepass.database.search.SearchHelper
import com.kunzisoft.keepass.model.SearchInfo import com.kunzisoft.keepass.model.SearchInfo
import com.kunzisoft.keepass.settings.PreferencesUtil
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
class AutofillLauncherActivity : AppCompatActivity() { class AutofillLauncherActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
// Pass extra for Autofill (EXTRA_ASSIST_STRUCTURE)
val assistStructure = AutofillHelper.retrieveAssistStructure(intent)
if (assistStructure != null) {
// Build search param // Build search param
val searchInfo = SearchInfo().apply { val searchInfo = SearchInfo().apply {
applicationId = intent.getStringExtra(KEY_SEARCH_APPLICATION_ID) applicationId = intent.getStringExtra(KEY_SEARCH_APPLICATION_ID)
webDomain = intent.getStringExtra(KEY_SEARCH_DOMAIN) webDomain = intent.getStringExtra(KEY_SEARCH_DOMAIN)
} }
// Pass extra for Autofill (EXTRA_ASSIST_STRUCTURE)
val assistStructure = AutofillHelper.retrieveAssistStructure(intent)
if (assistStructure == null) {
setResult(Activity.RESULT_CANCELED)
finish()
} else if (!KeeAutofillService.searchAllowedFor(searchInfo.applicationId,
PreferencesUtil.applicationIdBlocklist(this))
|| !KeeAutofillService.searchAllowedFor(searchInfo.webDomain,
PreferencesUtil.webDomainBlocklist(this))) {
// If item not allowed, show a toast
Toast.makeText(this.applicationContext, R.string.autofill_block_restart, Toast.LENGTH_LONG).show()
setResult(Activity.RESULT_CANCELED)
finish()
} else {
// If database is open // If database is open
SearchHelper.checkAutoSearchInfo(this, SearchHelper.checkAutoSearchInfo(this,
Database.getInstance(), Database.getInstance(),
@@ -69,9 +86,6 @@ class AutofillLauncherActivity : AppCompatActivity() {
searchInfo) searchInfo)
} }
) )
} else {
setResult(Activity.RESULT_CANCELED)
finish()
} }
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)

View File

@@ -76,14 +76,14 @@ abstract class SpecialModeActivity : StylishActivity() {
val webDomain = searchInfo?.webDomain val webDomain = searchInfo?.webDomain
val applicationId = searchInfo?.applicationId val applicationId = searchInfo?.applicationId
if (webDomain != null) { if (webDomain != null) {
PreferencesUtil.addWebDomainToBlocklist(this@SpecialModeActivity, PreferencesUtil.addWebDomainToBlocklist(this,
webDomain) webDomain)
} else if (applicationId != null) { } else if (applicationId != null) {
PreferencesUtil.addApplicationIdToBlocklist(this@SpecialModeActivity, PreferencesUtil.addApplicationIdToBlocklist(this,
applicationId) applicationId)
} }
onCancelSpecialMode() onCancelSpecialMode()
Toast.makeText(this@SpecialModeActivity, Toast.makeText(this.applicationContext,
R.string.autofill_block_restart, R.string.autofill_block_restart,
Toast.LENGTH_LONG).show() Toast.LENGTH_LONG).show()
} }

View File

@@ -57,27 +57,8 @@ class KeeAutofillService : AutofillService() {
StructureParser(latestStructure).parse()?.let { parseResult -> StructureParser(latestStructure).parse()?.let { parseResult ->
// Build search info only if applicationId or webDomain are not blocked // Build search info only if applicationId or webDomain are not blocked
var searchAllowed = true if (searchAllowedFor(parseResult.applicationId, applicationIdBlocklist)
parseResult.applicationId?.let { appId -> && searchAllowedFor(parseResult.domain, webDomainBlocklist)) {
if (applicationIdBlocklist?.any { appIdBlocked ->
appId.contains(appIdBlocked)
} == true
) {
searchAllowed = false
Log.d(TAG, "Autofill not allowed for $appId")
}
}
parseResult.domain?.let { domain ->
if (webDomainBlocklist?.any { webDomainBlocked ->
domain.contains(webDomainBlocked)
} == true
) {
searchAllowed = false
Log.d(TAG, "Autofill not allowed for $domain")
}
}
if (searchAllowed) {
val searchInfo = SearchInfo().apply { val searchInfo = SearchInfo().apply {
applicationId = parseResult.applicationId applicationId = parseResult.applicationId
webDomain = parseResult.domain webDomain = parseResult.domain
@@ -150,5 +131,18 @@ class KeeAutofillService : AutofillService() {
companion object { companion object {
private val TAG = KeeAutofillService::class.java.name private val TAG = KeeAutofillService::class.java.name
fun searchAllowedFor(element: String?, blockList: Set<String>?): Boolean {
element?.let { elementNotNull ->
if (blockList?.any { appIdBlocked ->
elementNotNull.contains(appIdBlocked)
} == true
) {
Log.d(TAG, "Autofill not allowed for $elementNotNull")
return false
}
}
return true
}
} }
} }