From 8cbde5c422b7fd213f8384add5444b307a32b6ff Mon Sep 17 00:00:00 2001 From: SatoshiQaziMuhammed Date: Fri, 6 Nov 2026 02:41:43 +0400 Subject: [PATCH] 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. --- common/src/main/res/values-ku/strings.xml | 4 ++ common/src/main/res/values-tr/strings.xml | 4 ++ common/src/main/res/values/strings.xml | 4 ++ .../browser/main/DAppBrowserFragment.kt | 39 ++++++++++++ .../utils/tabs/models/BrowserTabSession.kt | 28 +++++++++ .../web3/webview/KurdExternalContent.kt | 63 +++++++++++++++++++ .../main/res/layout/fragment_dapp_browser.xml | 41 ++++++++++++ .../web3/webview/KurdExternalContentTest.kt | 48 ++++++++++++++ 8 files changed, 231 insertions(+) create mode 100644 feature-dapp-impl/src/main/java/io/novafoundation/nova/feature_dapp_impl/web3/webview/KurdExternalContent.kt create mode 100644 feature-dapp-impl/src/test/java/io/novafoundation/nova/feature_dapp_impl/web3/webview/KurdExternalContentTest.kt diff --git a/common/src/main/res/values-ku/strings.xml b/common/src/main/res/values-ku/strings.xml index eec3c75d..b1fc82d1 100644 --- a/common/src/main/res/values-ku/strings.xml +++ b/common/src/main/res/values-ku/strings.xml @@ -2172,4 +2172,8 @@ Navê tevahî kontrol bike. Ev nav ji aliyê dewletê ve nehatiye dayîn. Ev nav bi zincîrê nehat kontrolkirin. Tenê îmze bike heke tu bawer î ku ev malpera kê ye. Guhertoyeke nûtir nehat dîtin — guhertoya li zincîrê tê nîşandan + Ev malper bi servereke derve ve girê dide (%s). Ew beş nehat barkirin. + vîdeo an deng + wêne + naverok diff --git a/common/src/main/res/values-tr/strings.xml b/common/src/main/res/values-tr/strings.xml index dc4b13b7..55bd6b24 100644 --- a/common/src/main/res/values-tr/strings.xml +++ b/common/src/main/res/values-tr/strings.xml @@ -2169,4 +2169,8 @@ Referral kısmına bu adresi yapıştır: Adın tamamını kontrol et. Bu ad devlet tarafından verilmedi. Bu ad zincirde denetlenemedi. Yalnızca sitenin kime ait olduğundan eminsen imzala. Daha yeni sürüm bulunamadı — zincirdeki sürüm gösteriliyor + Bu site dış bir sunucuya bağlanıyor (%s). O bölüm yüklenmedi. + video veya ses + görseller + içerik diff --git a/common/src/main/res/values/strings.xml b/common/src/main/res/values/strings.xml index 736db432..46fde249 100644 --- a/common/src/main/res/values/strings.xml +++ b/common/src/main/res/values/strings.xml @@ -2897,4 +2897,8 @@ Check the full name. This name was not given by the state. This name could not be checked on chain. Sign only if you are sure whose site this is. No newer version found — showing the version on chain + This site connects to an external server (%s). That part was not loaded. + video or audio + images + content diff --git a/feature-dapp-impl/src/main/java/io/novafoundation/nova/feature_dapp_impl/presentation/browser/main/DAppBrowserFragment.kt b/feature-dapp-impl/src/main/java/io/novafoundation/nova/feature_dapp_impl/presentation/browser/main/DAppBrowserFragment.kt index 03c7f8ec..d4424871 100644 --- a/feature-dapp-impl/src/main/java/io/novafoundation/nova/feature_dapp_impl/presentation/browser/main/DAppBrowserFragment.kt +++ b/feature-dapp-impl/src/main/java/io/novafoundation/nova/feature_dapp_impl/presentation/browser/main/DAppBrowserFragment.kt @@ -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= OVERFLOW_TABS_COUNT) { @@ -268,6 +274,16 @@ class DAppBrowserFragment : BaseFragment = emptySet() + + /** What was blocked when the reader closed the note; it comes back only if something new is. */ + private var kurdExternalDismissed: Set = 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>(emptySet()) + + /** What the `.kurd` document shown now could not load from servers outside its name. */ + val kurdBlocked: StateFlow> = _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() diff --git a/feature-dapp-impl/src/main/java/io/novafoundation/nova/feature_dapp_impl/web3/webview/KurdExternalContent.kt b/feature-dapp-impl/src/main/java/io/novafoundation/nova/feature_dapp_impl/web3/webview/KurdExternalContent.kt new file mode 100644 index 00000000..0ba7a729 --- /dev/null +++ b/feature-dapp-impl/src/main/java/io/novafoundation/nova/feature_dapp_impl/web3/webview/KurdExternalContent.kt @@ -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() +} diff --git a/feature-dapp-impl/src/main/res/layout/fragment_dapp_browser.xml b/feature-dapp-impl/src/main/res/layout/fragment_dapp_browser.xml index 63b6457b..1527b342 100644 --- a/feature-dapp-impl/src/main/res/layout/fragment_dapp_browser.xml +++ b/feature-dapp-impl/src/main/res/layout/fragment_dapp_browser.xml @@ -92,6 +92,47 @@ android:textColor="@color/text_warning" /> + + + + + + + + +