Google 播放安全警报 - 您的应用正在使用不安全的 HostnameVerifier 实现

Google Play Security Alert - Your app is using an unsafe implementation of the HostnameVerifier

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

您的应用正在使用 HostnameVerifier. And refer a link to Google Play Help Center 文章的不安全实现,以了解有关漏洞修复和截止日期的详细信息。

下面是我的代码。

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

任何人都可以举例说明,我应该做些什么来修复这个警告?

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

此代码有效地从您的连接中移除了 HTTPS 的保护。你需要删除它。

禁用主机名验证允许网络上的任何人通过执行中间人攻击来查看和篡改您的网络流量。

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

Your app is using an unsafe implementation of HostnameVerifier. Please see this Google Help Center article for details, including the deadline for fixing the vulnerability. Im not using HostnameVerifier and not calling setDefaultHostnameVerifier. Moreover - Im using OKHTTP lib for http-requests. I hope that defining TrustManager will solve this issue.

因为我不是 subclassing HostnameVerifier 或调用 setDefaultHostnameVerifier() 我假设它依赖于某些第 3 方库。由于我无法检测到这样的库,我想我会尝试使用以下代码

添加 class
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
}

最后 Google 说:

SECURITY SCAN COMPLETE
No known vulnerabilities were detected for APK 158.

但是我不确定是什么代码生成的,HostNameVerifieronReceivedSslError()mWebView.setWebViewClient。注意:HostNameVerifier.setDefaultHostnameVerifier() 不应该 return true 总是像在你的代码中那样!它必须实施一些逻辑来检查它是否与 SSL 和 return true 或 false 一切正常。这是必不可少的。

请检查我的代码我只验证了我的应用程序使用的域。在您的代码中,您必须验证您的应用程序使用的所有域。 我已经使用了我的服务器和 Fabric.com 所以我的代码在

下面
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
    @Override
    public boolean verify(String hostname, SSLSession arg1) {
        if (hostname.equalsIgnoreCase("api.my.com") || 
            hostname.equalsIgnoreCase("api.crashlytics.com") || 
            hostname.equalsIgnoreCase("settings.crashlytics.com")) {
            return true;
        } else {
            return false;
        }
    }
});
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
    @Override
    public boolean verify(String hostname, SSLSession arg1) {
        if (!hostname.equalsIgnoreCase("www.asdasdad.com"))
            return true;
        else
            return false;
    }
});

有效

在 google 上运行控制台,转到发布管理 -> Select apk 版本 -> 安全选项卡。在那里,您将看到该 apk 的安全问题列表以及代码中的 class,这些问题可能会导致该安全问题。

如果您使用 Braintree 进行 Paypal 支付。

Unsafe implementation of the HostnameVerifier interface - Google policy violation 也可能是由于最新的 Braintree 凭据更新引起的。

请在 build.gradle(Project)

中更新以下凭据和 URL

旧凭证

url  "https://cardinalcommerce.bintray.com/android"
username 'braintree-team-sdk@cardinalcommerce'
password '220cc9476025679c4e5c843666c27d97cfb0f951'

新凭据

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url "https://cardinalcommerceprod.jfrog.io/artifactory/android"
            credentials {
                username 'braintree_team_sdk'
                password 'AKCp8jQcoDy2hxSWhDAUQKXLDPDx6NYRkqrgFLRc3qDrayg6rrCbJpsKKyMwaykVL8FWusJpp'
            }
        }
    }
}

如果您使用的是 Google Play Services Gradle 插件,您还需要将此添加到 build.gradle 以避免依赖项解析问题:

components.all {
    allVariants {
        withDependencies { deps ->
            deps.each { dep ->
                if (dep.group == 'net.minidev' && dep.name =='json-smart') {
                    dep.version {
                        prefer "2.3"
                    }
                    dep.because "resolving dependencies issue"
                }
            }
        }
    }
}

Whosebug:

更多信息:https://developer.paypal.com/braintree/docs/guides/3d-secure/client-side/android/v3#generate-a-client-token

相关 Git 主题涉及: https://github.com/braintree/braintree-android-drop-in/issues/219