浏览代码

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 年前
父节点
当前提交
ae805f8d05
没有帐户链接到提交者的电子邮件
共有 1 个文件被更改,包括 14 次插入1 次删除
  1. 14
    1
      android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java

+ 14
- 1
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java 查看文件

@@ -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