Skip to content

Commit 446e76e

Browse files
committed
fix: insecure requests on android
1 parent 9fc396c commit 446e76e

2 files changed

Lines changed: 117 additions & 1 deletion

File tree

app.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@
111111
}
112112
],
113113
"expo-font",
114-
"expo-router"
114+
"expo-router",
115+
"./plugins/withInsecureHost"
115116
]
116117
}
117118
}

plugins/withInsecureHost.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const { withMainApplication, withDangerousMod } = require('expo/config-plugins');
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
// TAMU's server ships an incomplete cert chain (missing intermediate), which
6+
// Android's OkHttp rejects as "unable to find valid certification path" and RN
7+
// surfaces as "TypeError: Network request failed". We can't fix the upstream
8+
// server, so we bypass TLS validation for that single host. Everything else
9+
// (including the auth.maroonrides.app token flow) keeps full validation.
10+
const INSECURE_HOST = 'aggiespirit.ts.tamu.edu';
11+
12+
const kotlin = (pkg) => `package ${pkg}
13+
14+
import com.facebook.react.modules.network.OkHttpClientFactory
15+
import com.facebook.react.modules.network.OkHttpClientProvider
16+
import okhttp3.OkHttpClient
17+
import java.security.KeyStore
18+
import java.security.cert.CertificateException
19+
import java.security.cert.X509Certificate
20+
import javax.net.ssl.HttpsURLConnection
21+
import javax.net.ssl.SSLContext
22+
import javax.net.ssl.TrustManager
23+
import javax.net.ssl.TrustManagerFactory
24+
import javax.net.ssl.X509TrustManager
25+
26+
object MaroonHttpConfig {
27+
private const val INSECURE_HOST = "${INSECURE_HOST}"
28+
29+
fun install() {
30+
val defaultTrustManager = defaultTrustManager()
31+
32+
val trustManager = object : X509TrustManager {
33+
override fun checkClientTrusted(chain: Array<out X509Certificate>, authType: String) =
34+
defaultTrustManager.checkClientTrusted(chain, authType)
35+
36+
override fun checkServerTrusted(chain: Array<out X509Certificate>, authType: String) {
37+
try {
38+
defaultTrustManager.checkServerTrusted(chain, authType)
39+
} catch (e: CertificateException) {
40+
if (!leafClaimsHost(chain.firstOrNull(), INSECURE_HOST)) throw e
41+
}
42+
}
43+
44+
override fun getAcceptedIssuers(): Array<X509Certificate> =
45+
defaultTrustManager.acceptedIssuers
46+
}
47+
48+
val sslContext = SSLContext.getInstance("TLS")
49+
sslContext.init(null, arrayOf<TrustManager>(trustManager), null)
50+
51+
val defaultVerifier = HttpsURLConnection.getDefaultHostnameVerifier()
52+
53+
OkHttpClientProvider.setOkHttpClientFactory(object : OkHttpClientFactory {
54+
override fun createNewNetworkModuleClient(): OkHttpClient =
55+
OkHttpClientProvider.createClientBuilder()
56+
.sslSocketFactory(sslContext.socketFactory, trustManager)
57+
.hostnameVerifier { hostname, session ->
58+
hostname == INSECURE_HOST || defaultVerifier.verify(hostname, session)
59+
}
60+
.build()
61+
})
62+
}
63+
64+
private fun defaultTrustManager(): X509TrustManager {
65+
val tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
66+
tmf.init(null as KeyStore?)
67+
return tmf.trustManagers.first { it is X509TrustManager } as X509TrustManager
68+
}
69+
70+
private fun leafClaimsHost(cert: X509Certificate?, host: String): Boolean {
71+
if (cert == null) return false
72+
cert.subjectAlternativeNames?.forEach { entry ->
73+
val value = entry.getOrNull(1) as? String ?: return@forEach
74+
if (value.equals(host, ignoreCase = true)) return true
75+
}
76+
return cert.subjectDN?.name?.contains("CN=$host", ignoreCase = true) == true
77+
}
78+
}
79+
`;
80+
81+
const withInsecureHost = (config) => {
82+
config = withDangerousMod(config, [
83+
'android',
84+
async (cfg) => {
85+
const pkg = cfg.android?.package;
86+
if (!pkg) throw new Error('withInsecureHost: android.package is not set');
87+
88+
const dir = path.join(
89+
cfg.modRequest.platformProjectRoot,
90+
'app/src/main/java',
91+
pkg.replace(/\./g, '/'),
92+
);
93+
fs.mkdirSync(dir, { recursive: true });
94+
fs.writeFileSync(path.join(dir, 'MaroonHttpConfig.kt'), kotlin(pkg));
95+
return cfg;
96+
},
97+
]);
98+
99+
config = withMainApplication(config, (cfg) => {
100+
const src = cfg.modResults.contents;
101+
const anchor = 'SoLoader.init(this, OpenSourceMergedSoMapping)';
102+
103+
if (!src.includes('MaroonHttpConfig.install()')) {
104+
cfg.modResults.contents = src.replace(
105+
anchor,
106+
`${anchor}\n MaroonHttpConfig.install()`,
107+
);
108+
}
109+
return cfg;
110+
});
111+
112+
return config;
113+
};
114+
115+
module.exports = withInsecureHost;

0 commit comments

Comments
 (0)