Warm tip: This article is reproduced from serverfault.com, please click

android-Google Play安全警报

(android - Google Play Security Alert)

发布于 2016-12-02 09:10:52

最近,我的一个应用收到了来自Google Play的安全警报,如下所示。

你的应用正在使用HostnameVerifier的不安全实现并指向Google Play帮助中心文章的链接,以获取有关漏洞修复和期限的详细信息。

下面是我的代码。

HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){ 
    public boolean verify(String arg0, SSLSession arg1) {
        return true;
}}); 

任何人都可以举一个例子来解释,我应该做些什么改变来解决这个警告?

Questioner
Priyank Patel
Viewed
0
44.4k 2017-03-15 22:03:08

此处相同-在APK中检测到不安全的主机名验证程序

你的应用使用了不安全的HostnameVerifier实现。请参阅此Google帮助中心文章以了解详细信息,包括修复漏洞的截止日期。我没有使用HostnameVerifier,也没有调用setDefaultHostnameVerifier。此外-我使用OKHTTP库进行HTTP请求。我希望定义TrustManager可以解决此问题。

由于我不是子类化HostnameVerifier或调用setDefaultHostnameVerifier()所以我认为它依赖于某些第三方库。由于我无法检测到这样的库,我想我将尝试使用以下代码添加一个类

HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
    public boolean verify(final String hostname, final SSLSession session) {
        if (/* check if SSL is really valid */)
            return true;
        else
            return false;
    }
});

到我的项目中,然后查看它是否可以解决问题。
所以我做到了,此外,我对每个webView添加了覆盖方法

@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
    // the main thing is to show dialog informing user
    // that SSL cert is invalid and prompt him to continue without 
    // protection: handler.proceed();
    // or cancel: handler.cancel();
    String message;
    switch(error.getPrimaryError()) {
        case SslError.SSL_DATE_INVALID:
            message = ResHelper.getString(R.string.ssl_cert_error_date_invalid);
            break;
        case SslError.SSL_EXPIRED:
            message = ResHelper.getString(R.string.ssl_cert_error_expired);
            break;
        case SslError.SSL_IDMISMATCH:
            message = ResHelper.getString(R.string.ssl_cert_error_idmismatch);
            break;
        case SslError.SSL_INVALID:
            message = ResHelper.getString(R.string.ssl_cert_error_invalid);
            break;
        case SslError.SSL_NOTYETVALID:
            message = ResHelper.getString(R.string.ssl_cert_error_not_yet_valid);
            break;
        case SslError.SSL_UNTRUSTED:
            message = ResHelper.getString(R.string.ssl_cert_error_untrusted);
            break;
        default:
            message = ResHelper.getString(R.string.ssl_cert_error_cert_invalid);
    }
    mSSLConnectionDialog = new MaterialDialog.Builder(getParentActivity())
            .title(R.string.ssl_cert_error_title)
            .content(message)
            .positiveText(R.string.continue_button)
            .negativeText(R.string.cancel_button)
            .titleColorRes(R.color.black)
            .positiveColorRes(R.color.main_red)
            .contentColorRes(R.color.comment_grey)
            .backgroundColorRes(R.color.sides_menu_gray)
            .onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                    mSSLConnectionDialog.dismiss();
                    handler.proceed();
                }
            })
            .onNegative(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                    handler.cancel();
                }
            })
            .build();
    mSSLConnectionDialog.show(); 
}

mWebView.setWebViewClient(new WebViewClient() {
... // other corresponding overridden methods
}

最后,谷歌说:

安全扫描
已完成未检测到APK 158的已知漏洞。

但是我不确定是由什么代码组成的,HostNameVerifieronReceivedSslError()由决定的mWebView.setWebViewClient注意:HostNameVerifier.setDefaultHostnameVerifier()不应该true总是像代码中那样返回它必须实现一些逻辑以检查SSL一切正常,然后返回true或false。这是必不可少的。