Android WebView ignores certificate errors

Ignoring Android WebView certificate errors

Description

WebView calls WebViewClient.onReceivedSslError for recoverable certificate errors. Calling handler.proceed() allows the affected resource request to continue despite the error, so a page impersonating the server may appear legitimate.

Potential impact

  • Interception or modification of HTTPS traffic.
  • Disclosure of login credentials and session tokens.
  • Injection of malicious scripts.

Remediation

  • On a certificate error, call handler.cancel() to stop loading the page.
  • If a development server uses a test certificate authority (CA), trust it only in debug builds with Network Security Config’s debug-overrides. If you require certificate pinning, configure it in Network Security Config.

Examples

Before

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

After

java
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.cancel();
}

References