2016-12-02 320 views
4

最近我的一個應用從Google Play收到安全警報,如下所示。Google Play安全警報 - 您的應用正在使用HostnameVerifier的不安全實施

您的應用正在使用不安全的HostnameVerifier實施。請參閱Google Play Help Center文章的鏈接以獲取有關修復漏洞和截止日期的詳細信息。

以下是我的代碼。

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

任何人都可以用例子來解釋一下,我應該怎麼做才能解決這個警告?

+0

我也有同樣的問題。請列出您正在使用的所有庫。我可以與我比較,找出哪一個是造成問題。 –

+0

難道是Glide lib?這裏同樣存在問題,但根本沒有在項目中使用HostnameVerifier。無法理解,如果我沒有任何此類實現,我如何更改「我的自定義HostnameVerifier以使其返回false」? – Stan

+0

@Stan我沒有使用Glide lib。我已經添加了HostnameVerifier的代碼。 –

回答

2

同樣在這裏 - 不安全的主機名驗證檢測在APK

您的應用使用不安全的實現的HostnameVerifier的。有關詳細信息,請參閱此Google幫助中心文章,其中包括 修復漏洞的截止日期,請參閱 。我沒有使用HostnameVerifier 而不是調用setDefaultHostnameVerifier。此外 - 我使用OKHTTP lib用於http請求。我希望定義TrustManager將解決 這個問題。

由於我不是繼承HostnameVerifier或致電setDefaultHostnameVerifier()我認爲它依賴於一些第三方庫。既然不能檢測這樣的lib我想我會嘗試添加一類具有下面的代碼

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。這是至關重要的。

+0

感謝您的有用答案,但如何在發佈之前檢查APK以確保警告是爲了避免。通過僅上傳apk到Alpha或Beta來發送AFAIK的 –

+0

。這種方式的應用程序將不會提供給普通用戶。 – Stan

+0

是否需要在服務器上進行ssl認證。我只使用http。沒有ssl認證 – Abhinav

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

此代碼有效地消除了HTTPS對連接的保護。你需要刪除它。

禁用主機名驗證允許網絡中的任何人通過執行中間人攻擊來查看和篡改您的網絡流量。

2

請檢查我的代碼我只驗證了我的應用使用的域。在您的代碼中,您必須驗證您的應用使用的所有域。 我已經使用我的服務器和面料。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; 
     } 
    } 
}); 
0
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() 
    { 
     @Override 
     public boolean verify(String hostname,SSLSession arg1) 
     { 
      if (! hostname.equalsIgnoreCase("www.asdasdad.com")) 
       return true; 
      else 
       return false; 
     } 
    }); 

它的工作原理

+1

當hostname不等於域名時,它肯定會工作bcos,你會返回true! –

+0

yeap!谷歌想要這個。只有控制權 –

0

在谷歌玩控制檯去發佈管理 - >選擇APK版本 - >安全選項卡。在那裏你會看到該apk的安全問題列表,以及代碼中導致安全問題的類。

enter image description here

相關問題