{"id":11444,"date":"2013-07-29T13:06:00","date_gmt":"2013-07-29T12:06:00","guid":{"rendered":"http:\/\/touk.pl\/blog\/?guid=21e01a4d3b71fd7f2d47949e9f99129a"},"modified":"2022-08-02T14:47:15","modified_gmt":"2022-08-02T12:47:15","slug":"phonegap-cordova-and-cross-domain-ssl-request-problem-on-android","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2013\/07\/29\/phonegap-cordova-and-cross-domain-ssl-request-problem-on-android\/","title":{"rendered":"Phonegap \/ Cordova and cross domain ssl request problem on android."},"content":{"rendered":"<p>In one app I have participated, there was a use case:<br \/>\n* User fill up a form.<br \/>\n* User submit the form.<br \/>\n* System send data via https to server and show a response.During development there wasn\u2019t any problem, but when we were going to release production version then some unsuspected situation occurred. I prepare the production version accordingly with standard flow for Android environment:<\/p>\n<ul>\n<li>ant release<\/li>\n<li>align<\/li>\n<li>signingDuring conduct tests on that version, every time I try to submit the form, a connection error appear. In that situation, at the first you should check whitelist in cordova settings. Every URL you want to connect to, must be explicit type in:\n<p>res\/xml\/cordova.xml<br \/>\nIf whitelist looks fine, the error is most likely caused by inner implementation of Android System. The Android WebView does not allow by default self-signed SSL certs. When app is debug-signed the SSL error is ignored, but if app is release-signed connection to untrusted services is blocked.<\/li>\n<\/ul>\n<h3 id=\"workaround\"><span style=\"font-size: x-large\">Workaround<\/span><\/h3>\n<div><span style=\"font-size: x-large\">\u00a0<\/span><\/div>\n<p>You have to remember that secure connection to service with self-signed certificate is risky and unrecommended. But if you know what you are doing there is some workaround of the security problem. Behavior of method<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">CordovaWebViewClient.onReceivedSslErrormust be changed.\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Thus add new class extended CordovaWebViewClient and override \u2018onReceivedSslError\u2019. I strongly suggest to implement custom onReceiveSslError as secure as possible. I know that the problem occours when app try connect to example.domain.com and in spite of self signed certificate the domain is trusted, so only for that case the SslError is ignored.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class MyWebViewClient extends CordovaWebViewClient {\r\n\r\n   private static final String TAG = MyWebViewClient.class.getName();\r\n   private static final String AVAILABLE_SLL_CN\r\n= \"example.domain.com\";\r\n\r\n   public MyWebViewClient(DroidGap ctx) {\r\n       super(ctx);\r\n   }\r\n\r\n   @Override\r\n   public void onReceivedSslError(WebView view,\r\nSslErrorHandler handler,\r\nandroid.net.http.SslError error) {\r\n\r\nString errorSourceCName = error.getCertificate().\r\ngetIssuedTo().getCName();\r\n\r\n       if( AVAILABLE_SLL_CN.equals(errorSourceCName) ) {\r\n           Log.i(TAG, \"Detect ssl connection error: \" +\r\nerror.toString() +\r\n\u00e2\u20ac\u017e so the error is ignored\u00e2\u20ac\u009d);\r\n\r\n           handler.proceed();\r\n           return;\r\n       }\r\n\r\n       super.onReceivedSslError(view, handler, error);\r\n   }\r\n}Next step is forcing yours app to \u00c2 use custom implementation of WebViewClient. public class Start extends DroidGap\r\n{\r\n   private static final String TAG = Start.class.getName();\r\n\r\n   @Override\r\n   public void onCreate(Bundle savedInstanceState)\r\n   {\r\n       super.onCreate(savedInstanceState);\r\n       super.setIntegerProperty(\"splashscreen\", R.drawable.splash);\r\n       super.init();\r\n\r\n       MyWebViewClient myWebViewClient = new MyWebViewClient(this);\r\n       myWebViewClient.setWebView(this.appView);\r\n\r\n       this.appView.setWebViewClient(myWebViewClient);\r\n       \r\n\/\/ yours code\r\n\r\n   }\r\n}<\/pre>\n<p>That is all ypu have to do if minSdk of yours app is greater or equals 8. In older version of Android there is no class android.net.http.SslErrorSo in class MyCordovaWebViewClient class there are errors because compliator doesn\u00e2\u20ac\u2122t see SslError class. Fortunately Android is(was) open source, so it is easy to find source of the class. There is no inpediments to \u00e2\u20ac\u02dcupgrade\u00e2\u20ac\u2122 app and just add the file to project. I suggest to keep original packages. Thus after all operations the source tree looks like:<\/p>\n<table class=\"tr-caption-container\" style=\"margin-left: auto;margin-right: auto;text-align: center\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\n<tbody>\n<tr>\n<td style=\"text-align: center\"><a style=\"margin-left: auto;margin-right: auto\" href=\"http:\/\/2.bp.blogspot.com\/-Bk9U3BxOkkw\/UaW4xvBt2fI\/AAAAAAAAAME\/yLSMVUqtYIU\/s1600\/sslAndroidSourceTree.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/2.bp.blogspot.com\/-Bk9U3BxOkkw\/UaW4xvBt2fI\/AAAAAAAAAME\/yLSMVUqtYIU\/s1600\/sslAndroidSourceTree.png\" width=\"320\" height=\"144\" border=\"0\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td class=\"tr-caption\" style=\"text-align: center\">Class SslError placed in source tree.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Now the app created in release mode can connect via https to services with self-signed SSl certificates.<\/p>\n","protected":false},"excerpt":{"rendered":"In one app I have participated, there was a use case: \n\nUser fill up a form.\nUser submit the form.\nSystem send data via https to server and show a response.\n\nDuring development there wasn&rsquo;t any problem, but when we were going to release production version then some unsuspected situation occurred. I prepare the production version accordingly with standard flow for Android environment:   \n\nant release\nalign\nsigning\n\nDuring conduct tests on that version, every time I try to submit the form, a connection error appear. In that situation, at the first you should check whitelist in cordova settings. Every URL you want to connect to, must be explicit type in:   \nres\/xml\/cordova.xml\nIf whitelist looks fine, the error is most likely caused by inner implementation of Android System. The Android WebView does not allow by default self-signed SSL certs. When app is debug-signed the SSL error is ignored, but if app is release-signed connection to untrusted services is blocked.   \nWorkaround\n\nYou have to remember that secure connection to service with self-signed certificate is risky and unrecommended. But if you know what you are doing there is some workaround of the security problem. Behavior of method  \nCordovaWebViewClient.onReceivedSslError\nmust be changed.\nThus add new class extended CordovaWebViewClient and override &lsquo;onReceivedSslError&rsquo;. I strongly suggest to implement custom onReceiveSslError as secure as possible. I know that the problem occours when app try connect to example.domain.com and in spite of self signed certificate  the domain is trusted, so only for that case the SslError is ignored. \npublic class MyWebViewClient extends CordovaWebViewClient {&nbsp;&nbsp;&nbsp;private static final String TAG = MyWebViewClient.class.getName();&nbsp;&nbsp;&nbsp;private static final String AVAILABLE_SLL_CN                                = \"example.domain.com\";&nbsp;&nbsp;&nbsp;public MyWebViewClient(DroidGap ctx) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(ctx);&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;@Override&nbsp;&nbsp;&nbsp;public void onReceivedSslError(WebView view,                                   SslErrorHandler handler,                                   android.net.http.SslError error) {       String errorSourceCName = error.getCertificate().                                  getIssuedTo().getCName();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( AVAILABLE_SLL_CN.equals(errorSourceCName) ) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.i(TAG, \"Detect ssl connection error: \" +                        error.toString() +                        &bdquo; so the error is ignored&rdquo;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handler.proceed();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onReceivedSslError(view, handler, error);&nbsp;&nbsp;&nbsp;}}\nNext step is forcing yours app to &nbsp;use custom implementation of WebViewClient.  \npublic class Start extends DroidGap{&nbsp;&nbsp;&nbsp;private static final String TAG = Start.class.getName();&nbsp;&nbsp;&nbsp;@Override&nbsp;&nbsp;&nbsp;public void onCreate(Bundle savedInstanceState)&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onCreate(savedInstanceState);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.setIntegerProperty(\"splashscreen\", R.drawable.splash);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.init();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyWebViewClient myWebViewClient = new MyWebViewClient(this);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myWebViewClient.setWebView(this.appView);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.appView.setWebViewClient(myWebViewClient);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;       \/\/ yours code&nbsp;&nbsp;&nbsp;}}\nThat is all ypu have to do if minSdk of yours app is greater or equals 8.  In older version of Android there is no class \nandroid.net.http.SslError\nSo in class MyCordovaWebViewClient class there are errors because compliator doesn&rsquo;t see SslError class. Fortunately Android is(was) open source, so it is easy to find source of the class. There is no inpediments to &lsquo;upgrade&rsquo; app and just add the file to project. I suggest to keep original packages. Thus after all operations the source tree looks like:\n\n\n\n\n\n\nClass SslError placed in source tree.&nbsp;\n\n\n\n&nbsp;Now the app created in release mode can connect via https to services with self-signed SSl certificates.\n","protected":false},"author":47,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[445,255],"class_list":{"0":"post-11444","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-android","8":"tag-mobile"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=11444"}],"version-history":[{"count":12,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11444\/revisions"}],"predecessor-version":[{"id":14865,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11444\/revisions\/14865"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=11444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=11444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=11444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}