Skip to content
TypeScript

fix(mobile): trust user-installed Android CA certificates (#25730)

Julius Marminge36aea583bb65
diff --git a/apps/mobile/app.config.ts b/apps/mobile/app.config.ts
index 980da9c83c3..6ea260eae4d 100644
--- a/apps/mobile/app.config.ts
+++ b/apps/mobile/app.config.ts
@@ -425,7 +425,7 @@ const config: ExpoConfig = {
["expo-sensors", { motionPermission: false }],
// Mods run in reverse order; expo-widgets must create and wipe the target before assets are added.
...(!isIosPersonalTeamBuild ? ["./plugins/withWidgetLogoAsset.cjs", widgetsPlugin] : []),
- "./plugins/withAndroidCleartextTraffic.cjs",
+ "./plugins/withAndroidNetworkSecurityConfig.cjs",
"./plugins/withAndroidGradleHeap.cjs",
"./plugins/withAndroidInputBackground.cjs",
"./plugins/withAndroidModernPopupMenu.cjs",
diff --git a/apps/mobile/plugins/withAndroidCleartextTraffic.cjs b/apps/mobile/plugins/withAndroidCleartextTraffic.cjs
deleted file mode 100644
index f612f751e91..00000000000
--- a/apps/mobile/plugins/withAndroidCleartextTraffic.cjs
+++ /dev/null
@@ -1,18 +0,0 @@
-const { withAndroidManifest } = require("expo/config-plugins");
-
-module.exports = function withAndroidCleartextTraffic(config) {
- return withAndroidManifest(config, (nextConfig) => {
- const application = nextConfig.modResults.manifest.application?.[0];
-
- if (application == null) {
- throw new Error(
- "AndroidManifest.xml is missing the application element required for cleartext traffic configuration.",
- );
- }
-
- application.$ ??= {};
- application.$["android:usesCleartextTraffic"] = "true";
-
- return nextConfig;
- });
-};
diff --git a/apps/mobile/plugins/withAndroidNetworkSecurityConfig.cjs b/apps/mobile/plugins/withAndroidNetworkSecurityConfig.cjs
new file mode 100644
index 00000000000..90fea189e8d
--- /dev/null
+++ b/apps/mobile/plugins/withAndroidNetworkSecurityConfig.cjs
@@ -0,0 +1,56 @@
+const fs = require("node:fs");
+const path = require("node:path");
+const { withAndroidManifest, withDangerousMod } = require("expo/config-plugins");
+
+// Environments behind a private CA installed in the user store must work like they do in Chrome.
+// Android ignores usesCleartextTraffic once a network security config exists, so permit it here.
+const NETWORK_SECURITY_CONFIG = `<?xml version="1.0" encoding="utf-8"?>
+<network-security-config>
+ <base-config cleartextTrafficPermitted="true">
+ <trust-anchors>
+ <certificates src="system" />
+ <certificates src="user" />
+ </trust-anchors>
+ </base-config>
+</network-security-config>
+`;
+
+function withNetworkSecurityConfigManifest(config) {
+ return withAndroidManifest(config, (nextConfig) => {
+ const application = nextConfig.modResults.manifest.application?.[0];
+
+ if (application == null) {
+ throw new Error(
+ "AndroidManifest.xml is missing the application element required for network security configuration.",
+ );
+ }
+
+ application.$ ??= {};
+ application.$["android:networkSecurityConfig"] = "@xml/network_security_config";
+
+ return nextConfig;
+ });
+}
+
+function withNetworkSecurityConfigResource(config) {
+ return withDangerousMod(config, [
+ "android",
+ async (config) => {
+ const xmlDir = path.join(
+ config.modRequest.platformProjectRoot,
+ "app",
+ "src",
+ "main",
+ "res",
+ "xml",
+ );
+ fs.mkdirSync(xmlDir, { recursive: true });
+ fs.writeFileSync(path.join(xmlDir, "network_security_config.xml"), NETWORK_SECURITY_CONFIG);
+ return config;
+ },
+ ]);
+}
+
+module.exports = function withAndroidNetworkSecurityConfig(config) {
+ return withNetworkSecurityConfigResource(withNetworkSecurityConfigManifest(config));
+};
diff --git a/docs/user/remote-access.md b/docs/user/remote-access.md
index 2f7a8018766..d1bfe3710d2 100644
--- a/docs/user/remote-access.md
+++ b/docs/user/remote-access.md
@@ -62,6 +62,10 @@ On a computer with the desktop app, you can open the printed `t3code://pair`
app link instead. The desktop app asks you to confirm the server address, then
adds it.
+If your HTTPS endpoint uses a certificate from your own certificate authority,
+install that CA on Android as a **CA certificate** in its security settings; the
+Android app then trusts it.
+
Pairing authorizes that device for future connections. Use a fresh one-time link
for each new device; you do not need the original token to reconnect. Links
created in Settings can only be copied from the client that created them while