-
Notifications
You must be signed in to change notification settings - Fork 928
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make maliciousSiteProtection webView agnostic
- Loading branch information
1 parent
7f8a206
commit 13cfb3a
Showing
10 changed files
with
318 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...ain/java/com/duckduckgo/app/browser/webview/RealMaliciousSiteBlockerWebViewIntegration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright (c) 2024 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.app.browser.webview | ||
|
||
import android.net.Uri | ||
import android.webkit.WebResourceRequest | ||
import android.webkit.WebResourceResponse | ||
import androidx.annotation.VisibleForTesting | ||
import androidx.core.net.toUri | ||
import com.duckduckgo.browser.api.MaliciousSiteBlockerWebViewIntegration | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.malicioussiteprotection.api.MaliciousSiteProtection | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import java.net.URLDecoder | ||
import javax.inject.Inject | ||
import timber.log.Timber | ||
|
||
@ContributesBinding(AppScope::class) | ||
class RealMaliciousSiteBlockerWebViewIntegration @Inject constructor( | ||
private val maliciousSiteProtection: MaliciousSiteProtection, | ||
) : MaliciousSiteBlockerWebViewIntegration { | ||
|
||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
val processedUrls = mutableListOf<String>() | ||
|
||
override suspend fun shouldIntercept( | ||
request: WebResourceRequest, | ||
documentUri: Uri?, | ||
onSiteBlockedAsync: () -> Unit, | ||
): WebResourceResponse? { | ||
if (!maliciousSiteProtection.isFeatureEnabled) { | ||
return null | ||
} | ||
val url = request.url.let { | ||
if (it.fragment != null) { | ||
it.buildUpon().fragment(null).build() | ||
} else { | ||
it | ||
} | ||
} | ||
|
||
val decodedUrl = URLDecoder.decode(url.toString(), "UTF-8").lowercase() | ||
|
||
if (processedUrls.contains(decodedUrl)) { | ||
processedUrls.remove(decodedUrl) | ||
Timber.tag("PhishingAndMalwareDetector").d("Already intercepted, skipping $decodedUrl") | ||
return null | ||
} | ||
|
||
if (request.isForMainFrame && decodedUrl.toUri() == documentUri) { | ||
if (maliciousSiteProtection.isMalicious(decodedUrl.toUri(), onSiteBlockedAsync)) { | ||
return WebResourceResponse(null, null, null) | ||
} | ||
processedUrls.add(decodedUrl) | ||
} else if (isForIframe(request) && documentUri?.host == request.requestHeaders["Referer"]?.toUri()?.host) { | ||
if (maliciousSiteProtection.isMalicious(decodedUrl.toUri(), onSiteBlockedAsync)) { | ||
return WebResourceResponse(null, null, null) | ||
} | ||
processedUrls.add(decodedUrl) | ||
} | ||
return null | ||
} | ||
|
||
override suspend fun shouldOverrideUrlLoading( | ||
url: Uri, | ||
webViewUrl: Uri?, | ||
isForMainFrame: Boolean, | ||
onSiteBlockedAsync: () -> Unit, | ||
): Boolean { | ||
if (!maliciousSiteProtection.isFeatureEnabled) { | ||
return false | ||
} | ||
val decodedUrl = URLDecoder.decode(url.toString(), "UTF-8").lowercase() | ||
|
||
if (processedUrls.contains(decodedUrl)) { | ||
processedUrls.remove(decodedUrl) | ||
Timber.tag("PhishingAndMalwareDetector").d("Already intercepted, skipping $decodedUrl") | ||
return false | ||
} | ||
|
||
if (isForMainFrame && decodedUrl.toUri() == webViewUrl) { | ||
if (maliciousSiteProtection.isMalicious(decodedUrl.toUri(), onSiteBlockedAsync)) { | ||
return true | ||
} | ||
processedUrls.add(decodedUrl) | ||
} | ||
return false | ||
} | ||
|
||
private fun isForIframe(request: WebResourceRequest) = request.requestHeaders["Sec-Fetch-Dest"] == "iframe" || | ||
request.url.path?.contains("/embed/") == true || | ||
request.url.path?.contains("/iframe/") == true || | ||
request.requestHeaders["Accept"]?.contains("text/html") == true | ||
|
||
override fun onPageLoadStarted() { | ||
processedUrls.clear() | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
...java/com/duckduckgo/app/browser/webview/RealMaliciousSiteBlockerWebViewIntegrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package com.duckduckgo.app.browser.webview | ||
|
||
import android.webkit.WebResourceRequest | ||
import androidx.core.net.toUri | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.duckduckgo.malicioussiteprotection.api.MaliciousSiteProtection | ||
import junit.framework.TestCase.assertTrue | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Assert.assertNull | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito.mock | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.whenever | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class RealMaliciousSiteBlockerWebViewIntegrationTest { | ||
|
||
private lateinit var testee: RealMaliciousSiteBlockerWebViewIntegration | ||
private val maliciousSiteProtection: MaliciousSiteProtection = mock(MaliciousSiteProtection::class.java) | ||
private val maliciousUri = "http://malicious.com".toUri() | ||
private val exampleUri = "http://example.com".toUri() | ||
|
||
@Before | ||
fun setup() { | ||
testee = RealMaliciousSiteBlockerWebViewIntegration(maliciousSiteProtection) | ||
whenever(maliciousSiteProtection.isFeatureEnabled).thenReturn(true) | ||
} | ||
|
||
@Test | ||
fun `shouldOverrideUrlLoading returns false when feature is disabled`() = runTest { | ||
whenever(maliciousSiteProtection.isFeatureEnabled).thenReturn(false) | ||
|
||
val result = testee.shouldOverrideUrlLoading(exampleUri, null, true) {} | ||
assertFalse(result) | ||
} | ||
|
||
@Test | ||
fun `shouldInterceptRequest returns null when feature is disabled`() = runTest { | ||
val request = mock(WebResourceRequest::class.java) | ||
whenever(request.url).thenReturn(exampleUri) | ||
whenever(maliciousSiteProtection.isFeatureEnabled).thenReturn(false) | ||
|
||
val result = testee.shouldIntercept(request, null) {} | ||
assertNull(result) | ||
} | ||
|
||
@Test | ||
fun `shouldOverrideUrlLoading returns false when url is already processed`() = runTest { | ||
testee.processedUrls.add(exampleUri.toString()) | ||
|
||
val result = testee.shouldOverrideUrlLoading(exampleUri, exampleUri, true) {} | ||
assertFalse(result) | ||
} | ||
|
||
@Test | ||
fun `shouldInterceptRequest returns result when feature is enabled, is malicious, and is mainframe`() = runTest { | ||
val request = mock(WebResourceRequest::class.java) | ||
whenever(request.url).thenReturn(maliciousUri) | ||
whenever(request.isForMainFrame).thenReturn(true) | ||
whenever(maliciousSiteProtection.isMalicious(any(), any())).thenReturn(true) | ||
|
||
val result = testee.shouldIntercept(request, maliciousUri) {} | ||
assertNotNull(result) | ||
} | ||
|
||
@Test | ||
fun `shouldInterceptRequest returns result when feature is enabled, is malicious, and is iframe`() = runTest { | ||
val request = mock(WebResourceRequest::class.java) | ||
whenever(request.url).thenReturn(maliciousUri) | ||
whenever(request.isForMainFrame).thenReturn(true) | ||
whenever(request.requestHeaders).thenReturn(mapOf("Sec-Fetch-Dest" to "iframe")) | ||
whenever(maliciousSiteProtection.isMalicious(any(), any())).thenReturn(true) | ||
|
||
val result = testee.shouldIntercept(request, maliciousUri) {} | ||
assertNotNull(result) | ||
} | ||
|
||
@Test | ||
fun `shouldInterceptRequest returns null when feature is enabled, is malicious, and is not mainframe nor iframe`() = runTest { | ||
val request = mock(WebResourceRequest::class.java) | ||
whenever(request.url).thenReturn(maliciousUri) | ||
whenever(request.isForMainFrame).thenReturn(false) | ||
whenever(maliciousSiteProtection.isMalicious(any(), any())).thenReturn(true) | ||
|
||
val result = testee.shouldIntercept(request, maliciousUri) {} | ||
assertNull(result) | ||
} | ||
|
||
@Test | ||
fun `shouldOverride returns false when feature is enabled, is malicious, and is not mainframe`() = runTest { | ||
whenever(maliciousSiteProtection.isMalicious(any(), any())).thenReturn(true) | ||
|
||
val result = testee.shouldOverrideUrlLoading(maliciousUri, maliciousUri, false) {} | ||
assertFalse(result) | ||
} | ||
|
||
@Test | ||
fun `shouldOverride returns true when feature is enabled, is malicious, and is mainframe`() = runTest { | ||
whenever(maliciousSiteProtection.isMalicious(any(), any())).thenReturn(true) | ||
|
||
val result = testee.shouldOverrideUrlLoading(maliciousUri, maliciousUri, true) {} | ||
assertTrue(result) | ||
} | ||
|
||
@Test | ||
fun `shouldOverride returns true when feature is enabled, is malicious, and not mainframe nor iframe`() = runTest { | ||
whenever(maliciousSiteProtection.isMalicious(any(), any())).thenReturn(true) | ||
|
||
val result = testee.shouldOverrideUrlLoading(maliciousUri, maliciousUri, false) {} | ||
assertFalse(result) | ||
} | ||
|
||
@Test | ||
fun `shouldOverride returns true when feature is enabled, is malicious, and is mainframe but webView has different host`() = runTest { | ||
whenever(maliciousSiteProtection.isMalicious(any(), any())).thenReturn(true) | ||
|
||
val result = testee.shouldOverrideUrlLoading(maliciousUri, exampleUri, true) {} | ||
assertFalse(result) | ||
} | ||
|
||
@Test | ||
fun `onPageLoadStarted clears processedUrls`() = runTest { | ||
testee.processedUrls.add(exampleUri.toString()) | ||
testee.onPageLoadStarted() | ||
assertTrue(testee.processedUrls.isEmpty()) | ||
} | ||
} |
Oops, something went wrong.