WebUIActivity WebView navigates itself for links outside the module UI

オープン 初心者向け
#1,697 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 4 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
84/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
android, kotlin
領域
mobile, security

調査の方向性

app/src/main/java/me/bmax/apatch/ui/WebUIActivity.ktから始めて、WebViewClientの設定、特にshouldInterceptRequestとdoUpdateVisitedHistoryを読みます。トップレベルナビゲーションの処理を追加し、モジュールアセットのオリジンはWebView内にとどまり、外部リンクはACTION_VIEW経由で開くようにします。ActivityNotFoundExceptionも処理してください。モジュールのページが引き続き内部で読み込まれ、外部リンクはWebViewの外で開かれ、未対応のリンクでクラッシュしなければ完了です。

索引モデルが issue の本文から書いたものです。

説明

enhancement
Is your request related to a specific issue? | 你的请求是否与某个问题相关?

While reading through WebUIActivity (app/src/main/java/me/bmax/apatch/ui/WebUIActivity.kt) I noticed that the module WebView has no say over where it navigates. The module's webroot is served through a WebViewAssetLoader on https://mui.kernelsu.org/, and the WebViewClient overrides shouldInterceptRequest and doUpdateVisitedHistory, but not shouldOverrideUrlLoading:

val webViewClient = object : WebViewClient() {
    override fun shouldInterceptRequest(
        view: WebView,
        request: WebResourceRequest
    ): WebResourceResponse? {
        ...
        return webViewAssetLoader.shouldInterceptRequest(url)
    }

    override fun doUpdateVisitedHistory(view: WebView?, url: String?, isReload: Boolean) {
        webCanGoBack = view?.canGoBack() == true
        super.doUpdateVisitedHistory(view, url, isReload)
    }
}

The asset loader only answers for mui.kernelsu.org. For any other host it returns null, so the WebView fetches the page from the network. When a module page contains an ordinary link to another site (project page, docs, donation link) and the user taps it, the WebView navigates itself to that site:

  1. The remote page is shown inside the module screen. There is no address bar, so the user cannot tell they have left the module UI.
  2. The ksu object added with addJavascriptInterface(webViewInterface, "ksu") stays attached to the WebView, so the remote page can call ksu.exec(...) and ksu.spawn(...) just like the module's own page.
  3. Non http(s) links (mailto:, tel:, intent:) are not handed to another app. The WebView tries to load them itself and fails.

The second point is the one that matters. A module author is trusted, but a site the module happens to link to is not, and an http:// link can be modified in transit.

Describe the solution you'd like | 描述你想要的解决方案

Override shouldOverrideUrlLoading so only the asset loader origin stays in the WebView and every other top level navigation is sent out via Intent.ACTION_VIEW, wrapped in a try/catch for ActivityNotFoundException:

override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
    if (!request.isForMainFrame) return false
    val url = request.url
    if (url.scheme == "https" && url.host == "mui.kernelsu.org") return false
    try {
        startActivity(Intent(Intent.ACTION_VIEW, url))
    } catch (_: ActivityNotFoundException) {
    }
    return true
}

Intent and ActivityNotFoundException are already imported in this file. Module pages keep working as before, and outside links open in the browser, where the ksu bridge does not exist.

Describe the alternatives you've considered | 描述您考虑过的备选方案

I didn't consider any other alternatives

Other information | 其他信息

The same kind of gap was fixed in Nextcloud Notes in nextcloud/notes-android#3192, where the editor WebView now keeps links to its own server and sends everything else out.

主要言語
Kotlin
スター
8.2k
フォーク
809
平均マージ
7日 17時間
マージ済み PR(30日)
1

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

bmax121/APatch のほかの issue

bmax121/APatch の issue をすべて見る

似ている issue

Kotlin の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。