library/network: the no-redirect client rebuilds when the trust bundle changes
- Commit
f509b01017050fac803ce4187ab34905920af0ab- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 09:58 UTC (5 days ago)
- Parents
5af5eed
PooledHttpEngine cached two clients but tracked trust state for only one: the redirect client records builtFactory/ Manager/SystemTrust and rebuilds on mismatch, while the no-redirect branch returned its cached client unconditionally. init(SYSTEM) then init(FIRST_PARTY) with execute(followRedirects=false) kept issuing requests through SYSTEM trust, so a chain outside the FIRST_PARTY roots was accepted that should be rejected; the reverse sequence kept FIRST_PARTY and rejected valid system chains. Toggling the per-call useSystemTrust flag had the same effect. The no-redirect client now mirrors the redirect client's bookkeeping (factory, manager, system-trust) and rebuilds on any mismatch; unchanged trust still reuses the shared pool. Three regression tests cover bundle swaps in both directions and the flag toggle, asserting instance rebuild and the SSL factory identity (failed against the restored old logic). Retry gating, fail-closed bundle init, static mirror freshness and redirect handling were audited and cleared. Verified: :library:network:testDebugUnitTest 101 tests pass; lint no issues (ktlint/detekt findings are the module backlog on untouched code).
Diff
library/network/src/main/java/com/vayunmathur/library/network/PooledHttpEngine.kt
diff --git a/library/network/src/main/java/com/vayunmathur/library/network/PooledHttpEngine.kt b/library/network/src/main/java/com/vayunmathur/library/network/PooledHttpEngine.ktindex 8217dd1223..9089160247 100644--- a/library/network/src/main/java/com/vayunmathur/library/network/PooledHttpEngine.kt+++ b/library/network/src/main/java/com/vayunmathur/library/network/PooledHttpEngine.kt@@ -87,6 +87,16 @@ internal object PooledHttpEngine { @Volatile private var builtSystemTrust = false + /** The trust state [noRedirectClient] was built for; a mismatch rebuilds it (see [clientFor]). */+ @Volatile+ private var builtNoRedirectFactory: SSLSocketFactory? = null++ @Volatile+ private var builtNoRedirectManager: X509TrustManager? = null++ @Volatile+ private var builtNoRedirectSystemTrust = false+ /** * Whether this engine can serve a call with [explicitFactory]: only when it is null (use the * client's trust) — a bare factory without its manager cannot join the pool.@@ -198,7 +208,7 @@ internal object PooledHttpEngine { * client-level setting in OkHttp, so the suppressing caller gets its own shared instance * rather than a per-call build. */- private fun clientFor(useSystemTrust: Boolean, followRedirects: Boolean): OkHttpClient {+ internal fun clientFor(useSystemTrust: Boolean, followRedirects: Boolean): OkHttpClient { val factory = if (useSystemTrust) null else NetworkClient.defaultSslSocketFactory val manager = if (useSystemTrust) null else NetworkClient.defaultTrustManager synchronized(lock) {@@ -216,9 +226,18 @@ internal object PooledHttpEngine { builtSystemTrust = useSystemTrust return built }- noRedirectClient?.let { return it }+ val currentNoRedirect = noRedirectClient+ val noRedirectTrustMatches = factory === builtNoRedirectFactory &&+ manager === builtNoRedirectManager &&+ useSystemTrust == builtNoRedirectSystemTrust+ if (currentNoRedirect != null && noRedirectTrustMatches) {+ return currentNoRedirect+ } val built = buildClient(factory, manager, false) noRedirectClient = built+ builtNoRedirectFactory = factory+ builtNoRedirectManager = manager+ builtNoRedirectSystemTrust = useSystemTrust return built } }@@ -257,6 +276,9 @@ internal object PooledHttpEngine { builtFactory = null builtManager = null builtSystemTrust = false+ builtNoRedirectFactory = null+ builtNoRedirectManager = null+ builtNoRedirectSystemTrust = false lastUsedPooled = false } }
library/network/src/test/java/com/vayunmathur/library/network/PooledHttpEngineTrustCacheTest.kt
diff --git a/library/network/src/test/java/com/vayunmathur/library/network/PooledHttpEngineTrustCacheTest.kt b/library/network/src/test/java/com/vayunmathur/library/network/PooledHttpEngineTrustCacheTest.ktnew file mode 100644index 0000000000..f0c0757cef--- /dev/null+++ b/library/network/src/test/java/com/vayunmathur/library/network/PooledHttpEngineTrustCacheTest.kt@@ -0,0 +1,90 @@+package com.vayunmathur.library.network++import android.content.Context+import androidx.test.core.app.ApplicationProvider+import kotlin.test.AfterTest+import kotlin.test.BeforeTest+import kotlin.test.Test+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/**+ * Regression: the no-redirect pooled client must rebuild when trust changes.+ * It previously returned its first-built instance unconditionally, so a later+ * `NetworkClient.init` with a narrower bundle (or SYSTEM) never took effect+ * for `execute(followRedirects = false)` — chains the new bundle should+ * reject kept validating, and chains it should accept kept failing.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class PooledHttpEngineTrustCacheTest {++ private val context = ApplicationProvider.getApplicationContext<Context>()++ @BeforeTest+ fun setUp() {+ PooledHttpEngine.resetForTests()+ }++ @AfterTest+ fun tearDown() {+ PooledHttpEngine.resetForTests()+ NetworkClient.init(context, TrustBundle.SYSTEM)+ BundledTrust.clearCache()+ }++ @Test+ fun noRedirectClientRebuildsWhenNarrowBundleReplacesSystem() {+ NetworkClient.init(context, TrustBundle.SYSTEM)+ val systemClient = PooledHttpEngine.clientFor(useSystemTrust = false, followRedirects = false)++ NetworkClient.init(context, TrustBundle.FIRST_PARTY)+ val narrowClient = PooledHttpEngine.clientFor(useSystemTrust = false, followRedirects = false)++ assertTrue(systemClient !== narrowClient, "no-redirect client kept the SYSTEM-trust instance")+ assertTrue(+ narrowClient.sslSocketFactory === NetworkClient.defaultSslSocketFactory,+ "no-redirect client is not pinned to the FIRST_PARTY factory",+ )+ assertTrue(+ systemClient.sslSocketFactory !== NetworkClient.defaultSslSocketFactory,+ "SYSTEM client unexpectedly shares the FIRST_PARTY factory",+ )+ // Same trust state keeps the shared pool instead of rebuilding per call.+ assertTrue(+ narrowClient === PooledHttpEngine.clientFor(useSystemTrust = false, followRedirects = false),+ "unchanged trust must reuse the no-redirect client",+ )+ }++ @Test+ fun noRedirectClientRebuildsWhenSystemReplacesNarrowBundle() {+ NetworkClient.init(context, TrustBundle.FIRST_PARTY)+ val narrowFactory = NetworkClient.defaultSslSocketFactory+ val narrowClient = PooledHttpEngine.clientFor(useSystemTrust = false, followRedirects = false)+ assertTrue(narrowClient.sslSocketFactory === narrowFactory)++ NetworkClient.init(context, TrustBundle.SYSTEM)+ val systemClient = PooledHttpEngine.clientFor(useSystemTrust = false, followRedirects = false)++ assertTrue(narrowClient !== systemClient, "no-redirect client kept the FIRST_PARTY instance")+ assertTrue(+ systemClient.sslSocketFactory !== narrowFactory,+ "no-redirect client still pins the FIRST_PARTY factory after SYSTEM init",+ )+ }++ @Test+ fun noRedirectClientRebuildsWhenUseSystemTrustToggles() {+ NetworkClient.init(context, TrustBundle.FIRST_PARTY)+ val narrowFactory = NetworkClient.defaultSslSocketFactory+ val narrowClient = PooledHttpEngine.clientFor(useSystemTrust = false, followRedirects = false)++ val systemClient = PooledHttpEngine.clientFor(useSystemTrust = true, followRedirects = false)++ assertTrue(narrowClient !== systemClient, "useSystemTrust=true kept the narrow-trust client")+ assertTrue(systemClient.sslSocketFactory !== narrowFactory)+ }+}