Browse Source

add explicit SSL error handling to RNCWebViewClient

Minh Pham 4 years ago
parent
commit
79d313c132
No account linked to committer's email address

+ 46
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

@@ -10,6 +10,7 @@ import android.content.pm.PackageManager;
10 10
 import android.graphics.Bitmap;
11 11
 import android.graphics.Color;
12 12
 import android.Manifest;
13
+import android.net.http.SslError;
13 14
 import android.net.Uri;
14 15
 import android.os.Build;
15 16
 import android.os.Environment;
@@ -27,6 +28,7 @@ import android.webkit.DownloadListener;
27 28
 import android.webkit.GeolocationPermissions;
28 29
 import android.webkit.JavascriptInterface;
29 30
 import android.webkit.PermissionRequest;
31
+import android.webkit.SslErrorHandler;
30 32
 import android.webkit.URLUtil;
31 33
 import android.webkit.ValueCallback;
32 34
 import android.webkit.WebChromeClient;
@@ -759,6 +761,50 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
759 761
       return this.shouldOverrideUrlLoading(view, url);
760 762
     }
761 763
 
764
+    @Override
765
+    public void onReceivedSslError(final WebView webView, final SslErrorHandler handler, final SslError error) {
766
+        handler.proceed();
767
+
768
+        int code = error.getPrimaryError();
769
+        String failingUrl = error.getUrl();
770
+        String description = "";
771
+
772
+        // https://developer.android.com/reference/android/net/http/SslError.html
773
+        switch (code) {
774
+          case SslError.SSL_DATE_INVALID:
775
+            description = "The date of the certificate is invalid";
776
+            break;
777
+          case SslError.SSL_EXPIRED:
778
+            description = "The certificate has expired";
779
+            break;
780
+          case SslError.SSL_IDMISMATCH:
781
+            description = "Hostname mismatch";
782
+            break;
783
+          case SslError.SSL_INVALID:
784
+            description = "A generic error occurred";
785
+            break;
786
+          case SslError.SSL_MAX_ERROR:
787
+            description = "The number of different SSL errors.";
788
+            break;
789
+          case SslError.SSL_NOTYETVALID:
790
+            description = "The certificate is not yet valid";
791
+            break;
792
+          case SslError.SSL_UNTRUSTED:
793
+            description = "The certificate authority is not trusted";
794
+            break;
795
+          default: 
796
+            description = "Unknown SSL Error";
797
+            break;
798
+        }
799
+
800
+        this.onReceivedError(
801
+          webView,
802
+          code,
803
+          description,
804
+          failingUrl
805
+        );
806
+    }
807
+
762 808
     @Override
763 809
     public void onReceivedError(
764 810
       WebView webView,