android - WebView : how to avoid security alert from Google Play upon implementation of onReceivedSslError

标签 android google-play android-webview android-security sslerrorhandler

我有一个链接将在 WebView 中打开。问题是它无法打开,直到我像这样覆盖 onReceivedSslError:

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed();
}

我收到了来自 Google Play 的安全警告:

Security alert Your application has an unsafe implementation of the WebViewClient.onReceivedSslError handler. Specifically, the implementation ignores all SSL certificate validation errors, making your app vulnerable to man-in-the-middle attacks. An attacker could change the affected WebView's content, read transmitted data (such as login credentials), and execute code inside the app using JavaScript.

To properly handle SSL certificate validation, change your code to invoke SslErrorHandler.proceed() whenever the certificate presented by the server meets your expectations, and invoke SslErrorHandler.cancel() otherwise. An email alert containing the affected app(s) and class(es) has been sent to your developer account address.

Please address this vulnerability as soon as possible and increment the version number of the upgraded APK. For more information about the SSL error handler, please see our documentation in the Developer Help Center. For other technical questions, you can post to https://www.stackoverflow.com/questions and use the tags “android-security” and “SslErrorHandler.” If you are using a 3rd party library that’s responsible for this, please notify the 3rd party and work with them to address the issue.

To confirm that you've upgraded correctly, upload the updated version to the Developer Console and check back after five hours. If the app hasn't been correctly upgraded, we will display a warning.

Please note, while these specific issues may not affect every app that uses WebView SSL, it's best to stay up to date on all security patches. Apps with vulnerabilities that expose users to risk of compromise may be considered dangerous products in violation of the Content Policy and section 4.4 of the Developer Distribution Agreement.

Please ensure all apps published are compliant with the Developer Distribution Agreement and Content Policy. If you have questions or concerns, please contact our support team through the Google Play Developer Help Center.

如果我删除 onReceivedSslError (handler.proceed()),则页面将无法打开。

有什么方法可以在 WebView 中打开页面并避免安全警报?

最佳答案

To properly handle SSL certificate validation, change your code to invoke SslErrorHandler.proceed() whenever the certificate presented by the server meets your expectations, and invoke SslErrorHandler.cancel() otherwise.

正如电子邮件所说,onReceivedSslError 应该处理用户将要访问带有无效证书的页面,例如通知对话框。你不应该直接进行。

例如,我添加了一个警告对话框让用户确认并且谷歌似乎不再显示警告。


@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(R.string.notification_error_ssl_cert_invalid);
    builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            handler.proceed();
        }
    });
    builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            handler.cancel();
        }
    });
    final AlertDialog dialog = builder.create();
    dialog.show();
}

关于电子邮件的更多解释。

Specifically, the implementation ignores all SSL certificate validation errors, making your app vulnerable to man-in-the-middle attacks.

电子邮件说默认工具忽略了一个重要的 SSL 安全问题。所以我们需要在我们自己的使用 WebView 的应用程序中处理它。使用警告对话框通知用户是一种简单的方法。

关于android - WebView : how to avoid security alert from Google Play upon implementation of onReceivedSslError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43534582/

相关文章:

java - 用于发送带有附件的电子邮件的 Android Intent

java - Android 声音媒体播放器 IllegalStateException 并且没有声音

android - 从 MenuItem 获取 ID 并转到下一个 Activity

java - WebView.addJavascriptInterface 不起作用

android - 如何在我的 apk 上添加 SSL 证书?

java - Android RunOnUIThread 不会永久改变 View 可见性

android - 如何要求 android.permission.CALL_PHONE 但仍安装在平板电脑上?

android - 0 支持设备在 Google Play 开发者控制台

android - 在通过 Android 管理 api 控制的完全托管设备中启用内部应用程序共享

android - 如何从广播接收器访问我的主要 Activity 中的 webview?