From 982c944bfac8a458b1c4b1a44a2920b36f8c0fdb Mon Sep 17 00:00:00 2001 From: Robozinho <65715921+RobozinhoD@users.noreply.github.com> Date: Sat, 13 Jul 2024 18:33:29 -0300 Subject: [PATCH] restrict deck option links --- .../java/com/ichi2/anki/pages/DeckOptions.kt | 23 +++++++++++++++++-- .../com/ichi2/anki/pages/PageWebViewClient.kt | 5 ++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/pages/DeckOptions.kt b/AnkiDroid/src/main/java/com/ichi2/anki/pages/DeckOptions.kt index 8b3e73c031b9..04025203200e 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/pages/DeckOptions.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/pages/DeckOptions.kt @@ -18,6 +18,8 @@ package com.ichi2.anki.pages import android.content.Context import android.content.Intent import android.os.Bundle +import android.webkit.WebResourceRequest +import android.webkit.WebView import androidx.activity.OnBackPressedCallback import androidx.fragment.app.FragmentActivity import anki.collection.OpChanges @@ -25,6 +27,7 @@ import com.ichi2.anki.CollectionManager import com.ichi2.anki.OnPageFinishedCallback import com.ichi2.anki.R import com.ichi2.anki.dialogs.DiscardChangesDialog +import com.ichi2.anki.utils.openUrl import com.ichi2.anki.withProgress import com.ichi2.annotations.NeedsTest import com.ichi2.libanki.undoableOp @@ -60,11 +63,27 @@ class DeckOptions : PageFragment() { override fun onCreateWebViewClient(savedInstanceState: Bundle?): PageWebViewClient { requireActivity().onBackPressedDispatcher.addCallback(this, onBackSaveCallback) requireActivity().onBackPressedDispatcher.addCallback(this, onBackCallback) - return PageWebViewClient().apply { - onPageFinishedCallback = OnPageFinishedCallback { view -> + + return object : PageWebViewClient() { + private val allowedHosts = listOf(Regex("^(?:.+\\.)?ankiweb\\.net\$")) + + override var onPageFinishedCallback: OnPageFinishedCallback? = OnPageFinishedCallback { view -> Timber.v("canGoBack: %b", view.canGoBack()) onBackCallback.isEnabled = view.canGoBack() } + + override fun shouldOverrideUrlLoading( + view: WebView, + request: WebResourceRequest + ): Boolean { + val host = request.url.host + return if (host != null && allowedHosts.any { it.matches(host) }) { + super.shouldOverrideUrlLoading(view, request) + } else { + openUrl(request.url!!) + true + } + } } } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/pages/PageWebViewClient.kt b/AnkiDroid/src/main/java/com/ichi2/anki/pages/PageWebViewClient.kt index db825f8ffbf4..9a7ef5e291bc 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/pages/PageWebViewClient.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/pages/PageWebViewClient.kt @@ -38,7 +38,7 @@ open class PageWebViewClient : WebViewClient() { /** Wait for the provided promise to complete before showing the WebView */ open val promiseToWaitFor: String? = null - var onPageFinishedCallback: OnPageFinishedCallback? = null + open var onPageFinishedCallback: OnPageFinishedCallback? = null override fun shouldInterceptRequest( view: WebView, @@ -108,8 +108,7 @@ open class PageWebViewClient : WebViewClient() { return super.shouldOverrideUrlLoading(view, url) } - override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { - if (view == null || request == null) return super.shouldOverrideUrlLoading(view, request) + override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { if (handleUrl(view, request.url.toString())) { return true }