Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a StrictMode noteSlowCall for SSL init #8339

Merged
merged 9 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package okhttp.android.test

import android.os.StrictMode
import android.os.StrictMode.ThreadPolicy
import android.os.strictmode.Violation
import assertk.assertThat
import assertk.assertions.hasSize
import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.internal.platform.Platform
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.parallel.Isolated

@Isolated
class StrictModeTest {
private val violations = mutableListOf<Violation>()

@AfterEach
fun cleanup() {
StrictMode.setThreadPolicy(
ThreadPolicy.Builder()
.permitAll()
.build(),
)
}

@Test
fun testInit() {
Platform.resetForTests()

applyStrictMode()

// Not currently safe
// See https://github.com/square/okhttp/pull/8248
OkHttpClient()

assertThat(violations).hasSize(1)
assertThat(violations[0].message).isEqualTo("newSSLContext")
}

@Test
fun testNewCall() {
Platform.resetForTests()

val client = OkHttpClient()

applyStrictMode()

// Safe on main
client.newCall(Request("https://google.com/robots.txt".toHttpUrl()))

assertThat(violations).isEmpty()
}

private fun applyStrictMode() {
StrictMode.setThreadPolicy(
ThreadPolicy.Builder()
.detectCustomSlowCalls()
.penaltyListener({ it.run() }) {
violations.add(it)
}
Comment on lines +65 to +67
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. When was this added? I would assume after our minSdk, so it should really have an @SdkSuppress(minSdk=something) on the class.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check, also why it's not failing if so.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, just the tests. And not running because of mannodermaus/android-junit5#225 and a minSdk 26 requirement for tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. JUnit 5 continues to prove that it's the wrong choice for any project.

.build(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package okhttp3.internal.platform
import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.os.StrictMode
import android.security.NetworkSecurityPolicy
import android.util.CloseGuard
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocket
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.X509TrustManager
Expand All @@ -32,6 +34,7 @@ import okhttp3.internal.platform.android.BouncyCastleSocketAdapter
import okhttp3.internal.platform.android.ConscryptSocketAdapter
import okhttp3.internal.platform.android.DeferredSocketAdapter
import okhttp3.internal.tls.CertificateChainCleaner
import okhttp3.internal.tls.TrustRootIndex

/** Android 10+ (API 29+). */
@SuppressSignatureCheck
Expand All @@ -51,6 +54,18 @@ class Android10Platform : Platform(), ContextAwarePlatform {
socketAdapters.find { it.matchesSocketFactory(sslSocketFactory) }
?.trustManager(sslSocketFactory)

override fun newSSLContext(): SSLContext {
StrictMode.noteSlowCall("newSSLContext")

return super.newSSLContext()
}

override fun buildTrustRootIndex(trustManager: X509TrustManager): TrustRootIndex {
StrictMode.noteSlowCall("buildTrustRootIndex")

return super.buildTrustRootIndex(trustManager)
}

override fun configureTlsExtensions(
sslSocket: SSLSocket,
hostname: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package okhttp3.internal.platform

import android.content.Context
import android.os.Build
import android.os.StrictMode
import android.security.NetworkSecurityPolicy
import android.util.Log
import java.io.IOException
Expand All @@ -26,6 +27,7 @@ import java.net.InetSocketAddress
import java.net.Socket
import java.security.cert.TrustAnchor
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocket
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.X509TrustManager
Expand Down Expand Up @@ -74,6 +76,12 @@ class AndroidPlatform : Platform(), ContextAwarePlatform {
}
}

override fun newSSLContext(): SSLContext {
StrictMode.noteSlowCall("newSSLContext")

return super.newSSLContext()
}

override fun trustManager(sslSocketFactory: SSLSocketFactory): X509TrustManager? =
socketAdapters.find { it.matchesSocketFactory(sslSocketFactory) }
?.trustManager(sslSocketFactory)
Expand Down Expand Up @@ -104,6 +112,8 @@ class AndroidPlatform : Platform(), ContextAwarePlatform {

override fun buildTrustRootIndex(trustManager: X509TrustManager): TrustRootIndex =
try {
StrictMode.noteSlowCall("buildTrustRootIndex")

// From org.conscrypt.TrustManagerImpl, we want the method with this signature:
// private TrustAnchor findTrustAnchorByIssuerAndSignature(X509Certificate lastCert);
val method =
Expand Down
Loading