How to Limit an HTTP Exception to One Domain in Android
If one integration temporarily needs HTTP, its exception should have a clear, verifiable boundary. Network Security Configuration supports domain rules, but XML alone is not enough: you need to inspect the configuration in the build and test the behavior of the networking mechanism in use. We’ll use a wholly fictional integration on test domains you control to show where to allow HTTP, how to confirm it is blocked outside the exception, and when to remove it. A successful HTTPS request and a rejected HTTP request are tested separately here: one result does not prove the other.
Define the exception’s boundary first
Imagine a fictional Android product with a temporary integration at legacy.qa.example.test. All names below represent test hosts that your team must reproduce in its own controlled environment. Use synthetic data only. The goal is to allow HTTP for exactly this host while keeping HTTP blocked for its subdomain child.legacy.qa.example.test, its parent qa.example.test, and a separate host, control.example.test.
Before configuring anything, record the expected results for each address. The boundary here separates the exact name of the permitted host from all other destinations. Testing should establish which connections the app actually allows in the selected build, rather than merely confirm that a particular line exists in the source files.
Set a baseline block and a separate domain rule
Network Security Configuration is connected through android:networkSecurityConfig in the manifest’s application element. For example, the value @xml/network_security_config points to the configuration’s XML resource. In that resource, define a base-config with cleartextTrafficPermitted=false and a separate sibling domain-config with cleartextTrafficPermitted=true. Inside the domain rule, specify legacy.qa.example.test and includeSubdomains=false.
base-config applies to destinations not covered by a domain-config. With includeSubdomains=false, the hostname must match exactly. Setting it to true extends the exception to all levels of subdomains. Where domain rules overlap, the most specific rule applies—the one with the longest matching domain. Check additional and nested rules as well, because they can change the expected result.
For apps targeting API 28 or higher, the default base configuration blocks cleartext and uses system certificate authorities. In this proposed test, the block is set explicitly so that the intended setting is visible when reviewing the XML.
Inspect the configuration in the release build
Gradle merges the project’s manifest files into a single manifest included in the app. Inspecting one source file therefore does not confirm the final configuration. Open Merged Manifest for the variant being tested: this view shows the merge result and information about the contributing files.
Check that android:networkSecurityConfig references the expected resource, then inspect the corresponding XML in the build output. Acceptance criteria: the reference points to the correct configuration, the baseline block remains in place, the permitting rule covers only the selected host, and there are no additional permissions. Repeat this check separately for the app’s release variant.
Compare HTTPS, permitted HTTP, and blocked HTTP
The following sequence is an original editorial test, not an official Android procedure. Send requests through the networking mechanism the app actually uses. First, retrieve the expected synthetic response from your own test server over HTTPS. This provides a positive control for the availability of the selected scenario.
Next, test HTTP to the host covered by the exception and to the three addresses outside it. For the permitted case, correlate the response with your own server’s logs and the test request identifier. For blocked cases, require diagnostic evidence that the rejection specifically resulted from the cleartext policy, correlated with server logs. A DNS error, TLS error, timeout, or missing server log entry does not, by itself, prove that HTTP was blocked.
- HTTPS: the expected synthetic response was received, confirming the selected test scenario.
- HTTP to legacy.qa.example.test: the test server accepted the request in accordance with the expected exception.
- HTTP to the subdomain, parent domain, and control domain: rejection by the cleartext policy was confirmed; ambiguous failures require another check.
- If the scenario changes state, compare the synthetic data before and after the request: a successful HTTP response alone does not confirm the change.
Check the networking library and subdomain coverage
Do not assume that every HTTP library enforces the restrictions in the same way. The documentation specifically warns that Ktor may not enforce these restrictions. Correct XML therefore does not replace observing the actual behavior of the mechanism in use.
If the integration genuinely needs subdomains, extend the proposed editorial test separately: set includeSubdomains=true, test a child host and a nested child host, and retain a negative HTTP control outside that tree. Results from this test cannot be applied to a configuration that requires an exact hostname match.
Account for localhost and the limits of the results
Starting with Android 17, API 37, localhost receives an implicit configuration allowing cleartext when no explicit configuration is present. Loopback should therefore not be your only negative control. Check separately for an explicit localhost configuration and record the actual scope of the test.
In the report, specify the build variant, device, networking mechanism, and addresses tested. These observations confirm the behavior of a particular scenario under the stated conditions. They do not guarantee the security of every connection the app makes, nor do they justify treating any network failure as a successful HTTP block.
Set a lifetime for the exception
The first priority is to remove unnecessary permissions and obtain unambiguous results from the control requests. Then create a task to move the fictional integration to HTTPS: assign an owner and define the condition for removing the domain exception. After removing it, repeat the successful HTTPS control and confirm that HTTP is also rejected for the formerly exempt host.
If your product has a website or web component, start with a free audit of that website or web product from CheckMyApp. Treat the Android configuration review as a separate engineering task, using the acceptance criteria and limits described above.