android - 如何解决 "Remediation for JavaScript Interface Injection Vulnerability"?

标签 android android-webview android-security

Google 要求我解决 https://support.google.com/faqs/answer/9095419在我的 Android 应用程序中,这基本上意味着不对通过 HTTP 加载的网页使用 JavaScript 注入(inject)机制。

不使用此机制(选项 1)对我不起作用。将 android:usesCleartextTraffic 设置为 false 也不起作用,因为应用程序在其他地方使用非 HTTPS 流量。所以这给我留下了“你可以确保任何受影响的 WebView 不会通过 loadUrl 加载任何具有 HTTP 方案的 URL”——我很乐意这样做,因为我的应用程序只使用 file:///URL 将内容加载到 WebView ,这在安全方面应该很好。但是,我需要如何编写 shouldOverrideUrlLoading 方法的代码,以便 Google 的检查器识别出我只使用了 file:///URL?

注意这个问题不同于Remediation for JavaScript Interface Injection Vulnerability (因为我很清楚被问到的是什么)和In Android, JavaScript Interface Injection Vulnerability (因为我没有使用 HTTP,而是使用 file:///URL)。

编辑:添加我的 shouldOverrideUrlLoading 方法。 (这不是整个方法,而是它的重要部分。)

    @Override
    public boolean shouldOverrideUrlLoading (WebView browser, String url) {
        if (url.startsWith("file:///")) {
            // This is my web site, so do not override; let my WebView load the page
            browser.loadUrl(url);
            return true;
        }

        // Otherwise, the link is not for a page on my site, or is an entirely different kind of URI
        // (like tel:, geo: or mailto:), so launch another Activity that handles URLs
        act.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        return true;
    }

最佳答案

我还没有找到以满足谷歌代码检查器的方式将 file://URLs 与 Assets 一起使用的方法。虽然这可以解决问题,但我仍然不清楚人们可能需要如何对其进行编码。

我最终做的是通过 WebView.evaluateJavascript 方法调用 JavaScript 方法,这解决了我眼前的问题。当从 WebViewClient.onPageFinished 中调用时,页面已完成加载,因此所有元素都可以访问。虽然对我的情况不重要,但此方法也可以向 Java 代码返回一个值。因此,虽然它不是 JavascriptInterface 的一般替代品,但它解决了它的一些用例。

关于android - 如何解决 "Remediation for JavaScript Interface Injection Vulnerability"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53705818/

相关文章:

android - 将 Android View 附加到布局/从布局中分离

android - 在 webview android 中加载 URL 的问题

android - 有没有办法在 Android 上隐藏/混淆 .realm 文件?

java - 为什么在 Google Play 商店上传应用程序时得到 SslErrorHandler?

android - Google Play 警告 : WebViewClient. onReceivedSslError 处理程序

java - Android - 首次运行首选项初始化

Android 2.2 webview搜索问题

Android如何画线?

Android:使用ArrayAdapter的强引用循环

android - 如何在 sqlite-android 中存储和检索图像