Skip to content

Commit

Permalink
Remove embedded dataset and add worker to download data
Browse files Browse the repository at this point in the history
  • Loading branch information
CrisBarreiro committed Jan 15, 2025
1 parent 39d1235 commit ac60f1f
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 470,365 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {

implementation KotlinX.coroutines.android
implementation AndroidX.core.ktx
implementation AndroidX.work.runtimeKtx
implementation Google.dagger

implementation project(path: ':common-utils')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2025 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.malicioussiteprotection.impl

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import androidx.work.BackoffPolicy
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import com.duckduckgo.anvil.annotations.ContributesWorker
import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.malicioussiteprotection.impl.data.MaliciousSiteRepository
import com.squareup.anvil.annotations.ContributesMultibinding
import dagger.SingleInstanceIn
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlinx.coroutines.withContext

@ContributesWorker(AppScope::class)
class MaliciousSiteProtectionFiltersUpdateWorker(
context: Context,
workerParameters: WorkerParameters,
) : CoroutineWorker(context, workerParameters) {
@Inject
lateinit var maliciousSiteRepository: MaliciousSiteRepository

@Inject
lateinit var dispatcherProvider: DispatcherProvider

@Inject
lateinit var maliciousSiteProtectionFeature: MaliciousSiteProtectionRCFeature

override suspend fun doWork(): Result {
return withContext(dispatcherProvider.io()) {
if (maliciousSiteProtectionFeature.isFeatureEnabled().not()) {
return@withContext Result.success()
}
try {
maliciousSiteRepository.loadFilters()
return@withContext Result.success()
} catch (e: Exception) {
return@withContext Result.retry()
}
}
}
}

@ContributesMultibinding(
scope = AppScope::class,
boundType = MainProcessLifecycleObserver::class,
)
@SingleInstanceIn(AppScope::class)
class MaliciousSiteProtectionFiltersUpdateWorkerScheduler @Inject constructor(
private val workManager: WorkManager,
private val maliciousSiteProtectionFeature: MaliciousSiteProtectionRCFeature,

) : MainProcessLifecycleObserver {

override fun onCreate(owner: LifecycleOwner) {
val workerRequest = PeriodicWorkRequestBuilder<MaliciousSiteProtectionFiltersUpdateWorker>(
maliciousSiteProtectionFeature.getFilterSetUpdateFrequency(),
TimeUnit.MINUTES,
)
.addTag(MALICIOUS_SITE_PROTECTION_FILTERS_UPDATE_WORKER_TAG)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.MINUTES)
.build()
workManager.enqueueUniquePeriodicWork(MALICIOUS_SITE_PROTECTION_FILTERS_UPDATE_WORKER_TAG, ExistingPeriodicWorkPolicy.UPDATE, workerRequest)
}

companion object {
private const val MALICIOUS_SITE_PROTECTION_FILTERS_UPDATE_WORKER_TAG = "MALICIOUS_SITE_PROTECTION_FILTERS_UPDATE_WORKER_TAG"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2025 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.malicioussiteprotection.impl

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import androidx.work.BackoffPolicy
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import com.duckduckgo.anvil.annotations.ContributesWorker
import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.malicioussiteprotection.impl.data.MaliciousSiteRepository
import com.squareup.anvil.annotations.ContributesMultibinding
import dagger.SingleInstanceIn
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlinx.coroutines.withContext

@ContributesWorker(AppScope::class)
class MaliciousSiteProtectionHashPrefixesUpdateWorker(
context: Context,
workerParameters: WorkerParameters,
) : CoroutineWorker(context, workerParameters) {
@Inject
lateinit var maliciousSiteRepository: MaliciousSiteRepository

@Inject
lateinit var dispatcherProvider: DispatcherProvider

@Inject
lateinit var maliciousSiteProtectionFeature: MaliciousSiteProtectionRCFeature

override suspend fun doWork(): Result {
return withContext(dispatcherProvider.io()) {
if (maliciousSiteProtectionFeature.isFeatureEnabled().not()) {
return@withContext Result.success()
}
try {
maliciousSiteRepository.loadHashPrefixes()
return@withContext Result.success()
} catch (e: Exception) {
return@withContext Result.retry()
}
}
}
}

@ContributesMultibinding(
scope = AppScope::class,
boundType = MainProcessLifecycleObserver::class,
)
@SingleInstanceIn(AppScope::class)
class MaliciousSiteProtectionHashPrefixesUpdateWorkerScheduler @Inject constructor(
private val workManager: WorkManager,
private val maliciousSiteProtectionFeature: MaliciousSiteProtectionRCFeature,

) : MainProcessLifecycleObserver {

override fun onCreate(owner: LifecycleOwner) {
val workerRequest = PeriodicWorkRequestBuilder<MaliciousSiteProtectionHashPrefixesUpdateWorker>(
maliciousSiteProtectionFeature.getHashPrefixUpdateFrequency(),
TimeUnit.MINUTES,
)
.addTag(MALICIOUS_SITE_PROTECTION_HASH_PREFIXES_UPDATE_WORKER_TAG)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.MINUTES)
.build()
workManager.enqueueUniquePeriodicWork(
MALICIOUS_SITE_PROTECTION_HASH_PREFIXES_UPDATE_WORKER_TAG,
ExistingPeriodicWorkPolicy.UPDATE,
workerRequest,
)
}

companion object {
private const val MALICIOUS_SITE_PROTECTION_HASH_PREFIXES_UPDATE_WORKER_TAG = "MALICIOUS_SITE_PROTECTION_HASH_PREFIXES_UPDATE_WORKER_TAG"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,96 @@

package com.duckduckgo.malicioussiteprotection.impl.data

import com.duckduckgo.app.di.AppCoroutineScope
import com.duckduckgo.app.di.IsMainProcess
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.malicioussiteprotection.impl.MaliciousSiteProtectionFeature
import com.duckduckgo.malicioussiteprotection.impl.data.db.MaliciousSiteDao
import com.duckduckgo.malicioussiteprotection.impl.data.embedded.MaliciousSiteProtectionEmbeddedDataProvider
import com.duckduckgo.malicioussiteprotection.impl.data.network.FilterSetResponse
import com.duckduckgo.malicioussiteprotection.impl.data.network.HashPrefixResponse
import com.duckduckgo.malicioussiteprotection.impl.data.network.MaliciousSiteService
import com.duckduckgo.malicioussiteprotection.impl.models.Feed.MALWARE
import com.duckduckgo.malicioussiteprotection.impl.models.Feed.PHISHING
import com.duckduckgo.malicioussiteprotection.impl.models.Filter
import com.duckduckgo.malicioussiteprotection.impl.models.FilterSetWithRevision.MalwareFilterSetWithRevision
import com.duckduckgo.malicioussiteprotection.impl.models.FilterSetWithRevision.PhishingFilterSetWithRevision
import com.duckduckgo.malicioussiteprotection.impl.models.HashPrefixesWithRevision.MalwareHashPrefixesWithRevision
import com.duckduckgo.malicioussiteprotection.impl.models.HashPrefixesWithRevision.PhishingHashPrefixesWithRevision
import com.duckduckgo.malicioussiteprotection.impl.models.Match
import com.duckduckgo.malicioussiteprotection.impl.models.Type
import com.squareup.anvil.annotations.ContributesBinding
import dagger.SingleInstanceIn
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import timber.log.Timber

interface MaliciousSiteRepository {
suspend fun containsHashPrefix(hashPrefix: String): Boolean
suspend fun getFilters(hash: String): List<Filter>?
suspend fun matches(hashPrefix: String): List<Match>
suspend fun loadFilters()
suspend fun loadHashPrefixes()
}

@ContributesBinding(AppScope::class)
@SingleInstanceIn(AppScope::class)
class RealMaliciousSiteRepository @Inject constructor(
private val dataProvider: MaliciousSiteProtectionEmbeddedDataProvider,
private val maliciousSiteDao: MaliciousSiteDao,
@IsMainProcess private val isMainProcess: Boolean,
maliciousSiteProtectionFeature: MaliciousSiteProtectionFeature,
private val maliciousSiteService: MaliciousSiteService,
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
dispatcherProvider: DispatcherProvider,
) : MaliciousSiteRepository {

init {
if (isMainProcess) {
appCoroutineScope.launch(dispatcherProvider.io()) {
if (maliciousSiteProtectionFeature.self().isEnabled()) {
loadEmbeddedData()
override suspend fun loadFilters() {
try {
val networkRevision = maliciousSiteService.getRevision().revision

(maliciousSiteDao.getLatestRevision()?.filter { it.type == Type.FILTER_SET.name } ?: listOf()).let {
val phishingFilterSetRevision = it.firstOrNull() { it.feed == PHISHING.name }?.revision ?: 0
val phishingFilterSet: FilterSetResponse? = if (networkRevision > phishingFilterSetRevision) {
maliciousSiteService.getPhishingFilterSet(phishingFilterSetRevision)
} else {
null
}
val malwareFilterSetRevision = it.firstOrNull() { it.feed == MALWARE.name }?.revision ?: 0
val malwareFilterSet: FilterSetResponse? = if (networkRevision > malwareFilterSetRevision) {
maliciousSiteService.getMalwareFilterSet(malwareFilterSetRevision)
} else {
null
}

maliciousSiteDao.updateFilters(
phishingFilterSet?.let { PhishingFilterSetWithRevision(it.insert, it.delete, it.revision, it.replace) },
)
maliciousSiteDao.updateFilters(malwareFilterSet?.let { MalwareFilterSetWithRevision(it.insert, it.delete, it.revision, it.replace) })
}
} catch (e: Exception) {
Timber.e(e, "Failed to download malicious site protection list")
}
}

private suspend fun loadEmbeddedData() {
val embeddedPhishingHashPrefixes = dataProvider.loadEmbeddedPhishingHashPrefixes()
override suspend fun loadHashPrefixes() {
try {
val networkRevision = maliciousSiteService.getRevision().revision

val embeddedPhishingFilterSet = dataProvider.loadEmbeddedPhishingFilterSet()

val embeddedMalwareHashPrefixes = dataProvider.loadEmbeddedMalwareHashPrefixes()

val embeddedMalwareFilterSet = dataProvider.loadEmbeddedMalwareFilterSet()

// TODO (cbarreiro): Once we have the download scheduler, we should check the revision and update the data accordingly
(maliciousSiteDao.getLatestRevision()?.filter { it.type == Type.HASH_PREFIXES.name } ?: listOf()).let {
val phishingHashPrefixesRevision = it.firstOrNull() { it.feed == PHISHING.name }?.revision ?: 0
val phishingHashPrefixes: HashPrefixResponse? = if (networkRevision > phishingHashPrefixesRevision) {
maliciousSiteService.getPhishingHashPrefixes(phishingHashPrefixesRevision)
} else {
null
}
val malwareHashPrefixesRevision = it.firstOrNull() { it.feed == MALWARE.name }?.revision ?: 0
val malwareHashPrefixes: HashPrefixResponse? = if (networkRevision > malwareHashPrefixesRevision) {
maliciousSiteService.getMalwareHashPrefixes(malwareHashPrefixesRevision)
} else {
null
}

maliciousSiteDao.insertData(
phishingFilterSetRevision = embeddedPhishingFilterSet?.revision,
malwareFilterSetRevision = embeddedMalwareFilterSet?.revision,
phishingHashPrefixesRevision = embeddedPhishingHashPrefixes?.revision,
malwareHashPrefixesRevision = embeddedMalwareHashPrefixes?.revision,
phishingHashPrefixes = embeddedPhishingHashPrefixes?.insert?.toSet() ?: setOf(),
phishingFilterSet = embeddedPhishingFilterSet?.insert ?: setOf(),
malwareHashPrefixes = embeddedMalwareHashPrefixes?.insert?.toSet() ?: setOf(),
malwareFilterSet = embeddedMalwareFilterSet?.insert ?: setOf(),
)
maliciousSiteDao.updateHashPrefixes(
phishingHashPrefixes?.let { PhishingHashPrefixesWithRevision(it.insert, it.delete, it.revision, it.replace) },
)
maliciousSiteDao.updateHashPrefixes(
malwareHashPrefixes?.let { MalwareHashPrefixesWithRevision(it.insert, it.delete, it.revision, it.replace) },
)
}
} catch (e: Exception) {
Timber.e(e, "Failed to download malicious site protection list")
}
}

override suspend fun containsHashPrefix(hashPrefix: String): Boolean {
Expand All @@ -96,24 +122,9 @@ class RealMaliciousSiteRepository @Inject constructor(

override suspend fun matches(hashPrefix: String): List<Match> {
return try {
maliciousSiteService.getMatches(hashPrefix).matches.also {
Timber.d("\uD83D\uDFE2 Cris: Fetched $it matches for hash prefix $hashPrefix")
}
maliciousSiteService.getMatches(hashPrefix).matches
} catch (e: Exception) {
Timber.d("\uD83D\uDD34 Cris: Failed to fetch matches for hash prefix $hashPrefix")
listOf()
}
}
}

data class Match(
val hostname: String,
val url: String,
val regex: String,
val hash: String,
)

data class Filter(
val hash: String,
val regex: String,
)
Loading

0 comments on commit ac60f1f

Please sign in to comment.