Bladeren bron

Update SSL error handling for Android WebView

Update SSL error handling to call onReceivedError() only on top-level navigations. This prevents iframes and other subresources from causing user-visible SSL error messages. The desired behavior is only to have top-level navigations show user-visible error messages. All other requests should be cancelled automatically with no user-visible error message.
Alesandro Ortiz 4 jaren geleden
bovenliggende
commit
ae805f8d05
No account linked to committer's email address
1 gewijzigde bestanden met toevoegingen van 14 en 1 verwijderingen
  1. 14
    1
      android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java

+ 14
- 1
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java Bestand weergeven

@@ -801,10 +801,23 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
801 801
 
802 802
     @Override
803 803
     public void onReceivedSslError(final WebView webView, final SslErrorHandler handler, final SslError error) {
804
+        // WebView.getUrl() will return the top-level window URL.
805
+        // If a top-level navigation triggers this error handler, the top-level URL will be the failing URL (not the current URL).
806
+        // This is desired behavior. We later use these values to determine whether the request is a top-level navigation or a subresource request.
807
+        String topWindowUrl = webView.getUrl();
808
+        String failingUrl = error.getUrl();
809
+        
810
+        // Cancel request after obtaining top-level URL.
811
+        // If request is cancelled before obtaining top-level URL, undesired behavior may occur.
812
+        // Undesired behavior: Return value of WebView.getUrl() may be the current URL instead of the failing URL.
804 813
         handler.cancel();
805 814
 
815
+        if (!topWindowUrl.equalsIgnoreCase(failingUrl)) {
816
+          // If error is not due to top-level navigation, then do not call onReceivedError()
817
+          return;
818
+        }
819
+
806 820
         int code = error.getPrimaryError();
807
-        String failingUrl = error.getUrl();
808 821
         String description = "";
809 822
         String descriptionPrefix = "SSL error: ";
810 823