mirror of
https://github.com/pezkuwichain/pezkuwi-wallet-android.git
synced 2026-11-20 01:26:11 +00:00
Tell the reader when a .kurd page could not load a part from elsewhere (#38)
A .kurd page may load only from its own name, so a site built to pull a video, an image or a script from another server has that part missing, with nothing saying why. A script the wallet adds at the start of every .kurd document reports the page's own policy violations; the browser shows a note naming what kind of content was not loaded, until the reader closes it or leaves the page. Only the top frame of the .kurd page shown is heard, and only a block of a server other than the page's own counts: inline script, eval and data URLs are the page's doing, not a server it relies on. A page can post the report itself, but only to have the note shown about itself.
This commit is contained in:
1 parent
425079281c
commit
8cbde5c422
8 files changed
+231
No files matched your search
+39
@@ -45,12 +45,14 @@ import io.novafoundation.nova.feature_dapp_impl.presentation.common.favourites.s
|
||||
import io.novafoundation.nova.feature_dapp_impl.utils.tabs.models.BrowserTabSession
|
||||
import io.novafoundation.nova.feature_dapp_impl.utils.tabs.models.SessionCallback
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.CompoundWeb3Injector
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.KurdExternalContent
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.Web3ChromeClient
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.Web3WebViewClient
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.WebViewHolder
|
||||
import io.novafoundation.nova.feature_external_sign_api.presentation.externalSign.AuthorizeDappBottomSheet
|
||||
import io.novafoundation.nova.runtime.names.KurdName
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
private const val OVERFLOW_TABS_COUNT = 100
|
||||
@@ -252,6 +254,10 @@ class DAppBrowserFragment : BaseFragment<DAppBrowserViewModel, FragmentDappBrows
|
||||
|
||||
binder.dappBrowserKurdNotice.kurdNoticeSecondary.setOnClickListener { viewModel.kurdOpenAnyway() }
|
||||
binder.dappBrowserKurdOnChainVersionRetry.setOnClickListener { viewModel.kurdRetry() }
|
||||
binder.dappBrowserKurdExternalClose.setOnClickListener {
|
||||
kurdExternalDismissed = kurdBlocked
|
||||
showKurdExternal()
|
||||
}
|
||||
|
||||
viewModel.tabsCountFlow.observe {
|
||||
if (it >= OVERFLOW_TABS_COUNT) {
|
||||
@@ -268,6 +274,16 @@ class DAppBrowserFragment : BaseFragment<DAppBrowserViewModel, FragmentDappBrows
|
||||
clearProgress()
|
||||
session.attachToHost(createChromeClient(), this)
|
||||
webViewHolder.set(session.webView)
|
||||
|
||||
kurdBlockedJob?.cancel()
|
||||
kurdBlockedJob = viewLifecycleOwner.lifecycleScope.launch {
|
||||
session.kurdBlocked.collect {
|
||||
kurdBlocked = it
|
||||
// A new document starts empty; a note closed on the page before does not carry over
|
||||
if (it.isEmpty()) kurdExternalDismissed = emptySet()
|
||||
showKurdExternal()
|
||||
}
|
||||
}
|
||||
webViewClient = session.webViewClient
|
||||
|
||||
binder.dappBrowserWebViewContainer.removeAllViews()
|
||||
@@ -364,6 +380,28 @@ class DAppBrowserFragment : BaseFragment<DAppBrowserViewModel, FragmentDappBrows
|
||||
|
||||
private var lastKurdPage: KurdPageUi? = null
|
||||
|
||||
private var kurdBlockedJob: Job? = null
|
||||
private var kurdBlocked: Set<KurdExternalContent.Kind> = emptySet()
|
||||
|
||||
/** What was blocked when the reader closed the note; it comes back only if something new is. */
|
||||
private var kurdExternalDismissed: Set<KurdExternalContent.Kind> = emptySet()
|
||||
|
||||
/** Content an open `.kurd` page could not load from servers outside its name. */
|
||||
private fun showKurdExternal() {
|
||||
val shown = lastKurdPage is KurdPageUi.Open && kurdBlocked.isNotEmpty() && !kurdExternalDismissed.containsAll(kurdBlocked)
|
||||
binder.dappBrowserKurdExternal.setVisible(shown)
|
||||
if (!shown) return
|
||||
|
||||
val kinds = kurdBlocked.sorted().joinToString(", ") { kind ->
|
||||
when (kind) {
|
||||
KurdExternalContent.Kind.MEDIA -> getString(R.string.dapp_kurd_external_media)
|
||||
KurdExternalContent.Kind.IMAGE -> getString(R.string.dapp_kurd_external_image)
|
||||
KurdExternalContent.Kind.OTHER -> getString(R.string.dapp_kurd_external_other)
|
||||
}
|
||||
}
|
||||
binder.dappBrowserKurdExternalText.text = getString(R.string.dapp_kurd_external_note, kinds)
|
||||
}
|
||||
|
||||
private fun showKurdBadge(page: KurdPageUi?) {
|
||||
binder.dappBrowserAddressBar.showKurdBadge((page as? KurdPageUi.Open)?.badge) { viewModel.kurdBadgeClicked() }
|
||||
}
|
||||
@@ -372,6 +410,7 @@ class DAppBrowserFragment : BaseFragment<DAppBrowserViewModel, FragmentDappBrows
|
||||
private fun showKurdPage(page: KurdPageUi?) {
|
||||
lastKurdPage = page
|
||||
showKurdBadge(page)
|
||||
showKurdExternal()
|
||||
binder.dappBrowserKurdOnChainVersion.setVisible((page as? KurdPageUi.Open)?.onChainVersion == true)
|
||||
|
||||
val notice = binder.dappBrowserKurdNotice
|
||||
|
||||
+28
@@ -2,6 +2,8 @@ package io.novafoundation.nova.feature_dapp_impl.utils.tabs.models
|
||||
|
||||
import io.novafoundation.nova.runtime.names.content.KurdSiteServer
|
||||
import android.graphics.Bitmap
|
||||
import androidx.webkit.WebViewCompat
|
||||
import androidx.webkit.WebViewFeature
|
||||
import android.webkit.WebResourceRequest
|
||||
import android.webkit.WebView
|
||||
import io.novafoundation.nova.common.resources.ContextManager
|
||||
@@ -10,12 +12,16 @@ import io.novafoundation.nova.feature_dapp_impl.utils.integrityCheck.IntegrityCh
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.PageCallback
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.Web3ChromeClient
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.CompoundWeb3Injector
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.KurdExternalContent
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.WebViewWeb3JavaScriptInterface
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.Web3WebViewClient
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.injectWeb3
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.uninjectWeb3
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.withContext
|
||||
@@ -66,8 +72,14 @@ class BrowserTabSession(
|
||||
|
||||
private var sessionCallback: SessionCallback? = null
|
||||
|
||||
private val _kurdBlocked = MutableStateFlow<Set<KurdExternalContent.Kind>>(emptySet())
|
||||
|
||||
/** What the `.kurd` document shown now could not load from servers outside its name. */
|
||||
val kurdBlocked: StateFlow<Set<KurdExternalContent.Kind>> = _kurdBlocked
|
||||
|
||||
init {
|
||||
webView.injectWeb3(webViewClient)
|
||||
listenForKurdBlockedContent()
|
||||
// Before the first load: the bridge is only exposed to documents loaded after it is added
|
||||
compoundWeb3Injector.initialInject(webView)
|
||||
webView.loadUrl(startUrl)
|
||||
@@ -94,6 +106,7 @@ class BrowserTabSession(
|
||||
}
|
||||
|
||||
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
|
||||
_kurdBlocked.value = emptySet()
|
||||
sessionCallback?.onPageStarted(view, url, favicon)
|
||||
}
|
||||
|
||||
@@ -110,6 +123,21 @@ class BrowserTabSession(
|
||||
integrityCheckJSBridge.onPageFinished()
|
||||
}
|
||||
|
||||
/** Without both features the note is simply never shown: the content stays blocked either way. */
|
||||
private fun listenForKurdBlockedContent() {
|
||||
val supported = WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER) &&
|
||||
WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)
|
||||
if (!supported) return
|
||||
|
||||
WebViewCompat.addWebMessageListener(webView, KurdExternalContent.LISTENER_NAME, setOf("*")) { _, message, sourceOrigin, isMainFrame, _ ->
|
||||
// A report still in flight from the page before must not land on the one shown now
|
||||
if (!WebViewWeb3JavaScriptInterface.sameOrigin(sourceOrigin.toString(), webView.url.orEmpty())) return@addWebMessageListener
|
||||
val kind = message.data?.let { KurdExternalContent.kindOf(it, sourceOrigin.toString(), isMainFrame) } ?: return@addWebMessageListener
|
||||
_kurdBlocked.value = _kurdBlocked.value + kind
|
||||
}
|
||||
WebViewCompat.addDocumentStartJavaScript(webView, KurdExternalContent.DOCUMENT_START_SCRIPT, setOf("*"))
|
||||
}
|
||||
|
||||
fun destroy() {
|
||||
cancel() // Cancel coroutine scope
|
||||
integrityCheckJSBridge.onDestroy()
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package io.novafoundation.nova.feature_dapp_impl.web3.webview
|
||||
|
||||
import com.google.gson.JsonParser
|
||||
import io.novafoundation.nova.runtime.names.KurdName
|
||||
import java.net.URI
|
||||
|
||||
/**
|
||||
* What a `.kurd` page tried to load from outside its own name. The wallet's policy lets such a page
|
||||
* load only from itself (`KurdSiteServer.CONTENT_SECURITY_POLICY`), so a site built to fetch a video
|
||||
* or a font from a server elsewhere has that part missing. Reported to the reader, so a hole in the
|
||||
* page has a reason.
|
||||
*/
|
||||
object KurdExternalContent {
|
||||
|
||||
enum class Kind { MEDIA, IMAGE, OTHER }
|
||||
|
||||
/** The page global the document-start script reports through. */
|
||||
const val LISTENER_NAME = "Pezkuwi_kurdBlocked"
|
||||
|
||||
/**
|
||||
* Runs at the start of every document, before the page's own scripts, and is not subject to the
|
||||
* page's policy. Only a `.kurd` document listens; each directive is reported once per server.
|
||||
*/
|
||||
val DOCUMENT_START_SCRIPT = """
|
||||
(function () {
|
||||
var host = location.hostname.toLowerCase();
|
||||
if (host !== 'kurd' && host.slice(-5) !== '.kurd') return;
|
||||
var listener = window.$LISTENER_NAME;
|
||||
if (!listener) return;
|
||||
var seen = {};
|
||||
document.addEventListener('securitypolicyviolation', function (event) {
|
||||
var key = event.effectiveDirective + ' ' + event.blockedURI.split('/').slice(0, 3).join('/');
|
||||
if (seen[key]) return;
|
||||
seen[key] = true;
|
||||
listener.postMessage(JSON.stringify({ directive: event.effectiveDirective, uri: event.blockedURI }));
|
||||
});
|
||||
})();
|
||||
""".trimIndent()
|
||||
|
||||
/**
|
||||
* The kind of content a violation report from [sourceOrigin] says was blocked, or null when it is
|
||||
* not a `.kurd` top frame reporting a server other than its own (inline script, `eval`, a
|
||||
* malformed report). A page can send this itself, but only to have a note shown about itself.
|
||||
*/
|
||||
fun kindOf(message: String, sourceOrigin: String, isMainFrame: Boolean): Kind? = runCatching {
|
||||
if (!isMainFrame) return null
|
||||
val pageHost = URI(sourceOrigin).host?.lowercase() ?: return null
|
||||
if (!KurdName.isKurdHost(pageHost)) return null
|
||||
|
||||
val report = JsonParser.parseString(message).asJsonObject
|
||||
val directive = report.get("directive")?.asString ?: return null
|
||||
val blocked = URI(report.get("uri")?.asString ?: return null)
|
||||
// `inline`, `eval`, `data` and the like carry no host
|
||||
val blockedHost = blocked.host?.lowercase() ?: return null
|
||||
if (blockedHost == pageHost) return null
|
||||
|
||||
when (directive) {
|
||||
"media-src" -> Kind.MEDIA
|
||||
"img-src" -> Kind.IMAGE
|
||||
else -> Kind.OTHER
|
||||
}
|
||||
}.getOrNull()
|
||||
}
|
||||
@@ -92,6 +92,47 @@
|
||||
android:textColor="@color/text_warning" />
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/dappBrowserKurdExternal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@color/secondary_screen_background"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="4dp"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintBottom_toTopOf="@+id/dappBrowserBottomNavigation">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="18dp"
|
||||
android:layout_height="18dp"
|
||||
android:importantForAccessibility="no"
|
||||
android:src="@drawable/ic_warning_filled"
|
||||
app:tint="@color/icon_warning" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/dappBrowserKurdExternalText"
|
||||
style="@style/TextAppearance.NovaFoundation.Regular.Footnote"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="10dp"
|
||||
android:layout_marginVertical="12dp"
|
||||
android:layout_weight="1"
|
||||
android:textColor="@color/text_secondary"
|
||||
tools:text="@string/dapp_kurd_external_note" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/dappBrowserKurdExternalClose"
|
||||
android:layout_width="44dp"
|
||||
android:layout_height="44dp"
|
||||
android:background="?attr/selectableItemBackgroundBorderless"
|
||||
android:contentDescription="@string/common_close"
|
||||
android:padding="13dp"
|
||||
android:src="@drawable/ic_close"
|
||||
app:tint="@color/icon_secondary" />
|
||||
</LinearLayout>
|
||||
|
||||
<include
|
||||
android:id="@+id/dappBrowserKurdNotice"
|
||||
layout="@layout/view_kurd_notice"
|
||||
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package io.novafoundation.nova.feature_dapp_impl.web3.webview
|
||||
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.KurdExternalContent.Kind
|
||||
import io.novafoundation.nova.feature_dapp_impl.web3.webview.KurdExternalContent.kindOf
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
|
||||
class KurdExternalContentTest {
|
||||
|
||||
private fun report(directive: String, uri: String) = """{"directive":"$directive","uri":"$uri"}"""
|
||||
|
||||
@Test
|
||||
fun `a server elsewhere is reported by what it would have served`() {
|
||||
assertEquals(Kind.MEDIA, kindOf(report("media-src", "https://video.example/a.mp4"), "https://dks-news.kurd", isMainFrame = true))
|
||||
assertEquals(Kind.IMAGE, kindOf(report("img-src", "https://cdn.example/x.png"), "https://dks-news.kurd", isMainFrame = true))
|
||||
assertEquals(Kind.OTHER, kindOf(report("connect-src", "wss://feed.example/"), "https://dks-news.kurd", isMainFrame = true))
|
||||
assertEquals(Kind.OTHER, kindOf(report("script-src-elem", "https://cdn.example/a.js"), "https://serok.gov.kurd", isMainFrame = true))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `another kurd name is a server elsewhere too`() {
|
||||
assertEquals(Kind.IMAGE, kindOf(report("img-src", "https://other.kurd/x.png"), "https://dks.kurd", isMainFrame = true))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a block that is not about a server is not`() {
|
||||
// Inline script, eval and data: URLs are the page's own doing, not a server it relies on
|
||||
assertNull(kindOf(report("script-src-elem", "inline"), "https://dks.kurd", isMainFrame = true))
|
||||
assertNull(kindOf(report("script-src", "eval"), "https://dks.kurd", isMainFrame = true))
|
||||
assertNull(kindOf(report("img-src", "data"), "https://dks.kurd", isMainFrame = true))
|
||||
assertNull(kindOf(report("img-src", "https://dks.kurd/x.png"), "https://dks.kurd", isMainFrame = true))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `only a kurd top frame is heard`() {
|
||||
assertNull(kindOf(report("img-src", "https://cdn.example/x.png"), "https://dks.kurd", isMainFrame = false))
|
||||
assertNull(kindOf(report("img-src", "https://cdn.example/x.png"), "https://app.pezkuwichain.io", isMainFrame = true))
|
||||
assertNull(kindOf(report("img-src", "https://cdn.example/x.png"), "https://dks.kurd.example", isMainFrame = true))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a malformed report is dropped`() {
|
||||
assertNull(kindOf("not json", "https://dks.kurd", isMainFrame = true))
|
||||
assertNull(kindOf("""{"directive":"img-src"}""", "https://dks.kurd", isMainFrame = true))
|
||||
assertNull(kindOf("""{"uri":"https://cdn.example/"}""", "https://dks.kurd", isMainFrame = true))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user