{"id":44147,"date":"2019-05-17T11:40:55","date_gmt":"2019-05-17T18:40:55","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/xamarin\/?p=44147"},"modified":"2020-04-26T22:23:16","modified_gmt":"2020-04-27T05:23:16","slug":"cleartext-http-android-network-security","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/cleartext-http-android-network-security\/","title":{"rendered":"Managing HTTP &#038; Cleartext Traffic on Android with Network Security Configuration"},"content":{"rendered":"<p>Did you know that starting with Android 9 (API level 28) cleartext(non-HTTPS) support is disabled by default? It is always recommended to make connections over HTTPS to ensure that any web communication is secure. This policy may have an impact on your development cycle if your app needs to download an image or file on a server hasn&#8217;t been configured for HTTPS. Also, you may just be trying to debug your application locally and don&#8217;t want to install development certs. You may have strong business requirements that all web traffic on all versions of Android is always HTTPS. This is where the new Network Security Configuration feature of Android comes in, to help us finely tune network traffic security in our app.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/swimlane-secure-chat-app.png\" alt=\"\" width=\"636\" height=\"300\" class=\"aligncenter size-full wp-image-44148\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/swimlane-secure-chat-app.png 636w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/swimlane-secure-chat-app-300x142.png 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/p>\n<h2>When does Cleartext apply?<\/h2>\n<p>Cleartext is disabled by default on Android 9 (Pie, API 28) devices when your application is set to target and compile against Android 9. On the project&#8217;s properties you will find the SDK you are compiling against under Application:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/Pie.png\" alt=\"\" width=\"701\" height=\"193\" class=\"aligncenter size-full wp-image-44150\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/Pie.png 701w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/Pie-300x83.png 300w\" sizes=\"(max-width: 701px) 100vw, 701px\" \/><\/p>\n<p>Inside of your Android Manifest options you will find the Target Framework that can be set to Android 9:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/Pie2-1.png\" alt=\"\" width=\"437\" height=\"127\" class=\"aligncenter size-full wp-image-44153\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/Pie2-1.png 437w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/Pie2-1-300x87.png 300w\" sizes=\"(max-width: 437px) 100vw, 437px\" \/><\/p>\n<h2>Network Security Config<\/h2>\n<p>To configure security options, you will create a new xml file under Resources\/xml named <b>network_security_config.xml<\/b>. <\/p>\n<p><img decoding=\"async\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/xmlfile.png\" alt=\"\" width=\"290\" height=\"419\" class=\"aligncenter size-full wp-image-44154\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/xmlfile.png 290w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/xmlfile-208x300.png 208w\" sizes=\"(max-width: 290px) 100vw, 290px\" \/><\/p>\n<p>The following configuration will enable cleartext web traffic to be allowed in our app for specific domains and IP addresses:<\/p>\n<pre><code>\r\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;network-security-config&gt;\r\n  &lt;domain-config cleartextTrafficPermitted=\"true\"&gt;\r\n    &lt;domain includeSubdomains=\"true\"&gt;10.0.2.2&lt;\/domain&gt; &lt;!-- Debug port --&gt;\r\n    &lt;domain includeSubdomains=\"true\"&gt;xamarin.com&lt;\/domain&gt;\r\n  &lt;\/domain-config&gt;\r\n&lt;\/network-security-config&gt;\r\n<\/code><\/pre>\n<p>You can strengthen the security of your app by also restricting cleartext traffic on all versions of Android regardless of the compile and target framework. This is accomplished by setting <b>cleartextTrafficPermitted<\/b> to <b>false<\/b>. Enabling this will restrict any traffic that is non-HTTPS at all times.<\/p>\n<h2>Configure Application Manifest<\/h2>\n<p>The last thing that needs to be done is to configure the <b>networkSecurityConfig<\/b> property on the <b>application<\/b> node in the Android Manifest:<\/p>\n<pre><code>\r\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;manifest&gt;\r\n    &lt;application android:networkSecurityConfig=\"@xml\/network_security_config\"&gt;\r\n        ...\r\n    &lt;\/application&gt;\r\n&lt;\/manifest&gt;\r\n<\/code><\/pre>\n<p>That&#8217;s it! Now the application is completely configured to allow or restrict cleartext during web requests.<\/p>\n<h2>Learn More<\/h2>\n<p>Network security configuration can do a lot more than just allow or restrict cleartext traffic in Android applications. It can configure trust anchors, debug-only overrides, certificate pinning, and more. Be sure to read through the <a href=\"https:\/\/developer.android.com\/training\/articles\/security-config\">Android developers documentation<\/a> for a full guide. To enable cleartext traffic in iOS applications, you will want to take a look at our <a href=\"https:\/\/docs.microsoft.com\/xamarin\/ios\/app-fundamentals\/ats?WT.mc_id=docs-xamarinblog-jamont\">App Transport Security(ATS) documentation<\/a> for a full walkthrough. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enhance security by enforcing HTTPS on all of your Android applications web request and easily adjust them for development purposes with Network Security Configuration.<\/p>\n","protected":false},"author":544,"featured_media":44149,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[313,2],"tags":[5251,978],"class_list":["post-44147","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-developers","tag-http","tag-security"],"acf":[],"blog_post_summary":"<p>Enhance security by enforcing HTTPS on all of your Android applications web request and easily adjust them for development purposes with Network Security Configuration.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/44147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=44147"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/44147\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/44149"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=44147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=44147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=44147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}