-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
KTOR-6182 Support throwing UnknownServiceException
if there is no cleartext traffic permitted
#3725
base: main
Are you sure you want to change the base?
Changes from 4 commits
7d3fb01
1641149
f0733ef
ced616f
044d2b6
54df7e7
9ff3467
e8dd071
cb1e09c
ed199d2
e048727
140c37c
a4a1c11
4273c1e
93c5751
f32ce93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2014-2023 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.client.engine.cio | ||
|
||
import java.lang.reflect.InvocationTargetException | ||
|
||
internal actual object Platform { | ||
/** | ||
* This explicit check avoids activating in Android Studio with Android specific classes available when running plugins inside the IDE. | ||
*/ | ||
private val isAndroid: Boolean = System.getProperty("java.vm.name") == "Dalvik" | ||
|
||
actual fun isCleartextTrafficPermitted(hostname: String): Boolean { | ||
if (!isAndroid) return true | ||
return try { | ||
val networkPolicyClass = Class.forName("android.security.NetworkSecurityPolicy") | ||
val getInstanceMethod = networkPolicyClass.getMethod("getInstance").apply { isAccessible = true } | ||
val networkSecurityPolicy = getInstanceMethod.invoke(null) | ||
api24IsCleartextTrafficPermitted(hostname, networkPolicyClass, networkSecurityPolicy) | ||
} catch (_: ClassNotFoundException) { | ||
true | ||
} catch (_: NoSuchMethodException) { | ||
true | ||
} catch (e: IllegalAccessException) { | ||
throw AssertionError("unable to determine cleartext support", e) | ||
} catch (e: IllegalArgumentException) { | ||
throw AssertionError("unable to determine cleartext support", e) | ||
} catch (e: InvocationTargetException) { | ||
throw AssertionError("unable to determine cleartext support", e) | ||
} | ||
} | ||
|
||
@Throws(InvocationTargetException::class, IllegalAccessException::class) | ||
private fun api24IsCleartextTrafficPermitted( | ||
hostname: String, | ||
networkPolicyClass: Class<*>, | ||
networkSecurityPolicy: Any | ||
): Boolean = try { | ||
val isCleartextTrafficPermittedMethod = networkPolicyClass | ||
.getMethod("isCleartextTrafficPermitted", String::class.java) | ||
.apply { isAccessible = true } | ||
isCleartextTrafficPermittedMethod.invoke(networkSecurityPolicy, hostname) as Boolean | ||
} catch (_: NoSuchMethodException) { | ||
api23IsCleartextTrafficPermitted(networkPolicyClass, networkSecurityPolicy) | ||
} | ||
|
||
@Throws(InvocationTargetException::class, IllegalAccessException::class) | ||
private fun api23IsCleartextTrafficPermitted( | ||
networkPolicyClass: Class<*>, | ||
networkSecurityPolicy: Any | ||
): Boolean = try { | ||
val isCleartextTrafficPermittedMethod = networkPolicyClass | ||
.getMethod("isCleartextTrafficPermitted") | ||
.apply { isAccessible = true } | ||
isCleartextTrafficPermittedMethod.invoke(networkSecurityPolicy) as Boolean | ||
} catch (_: NoSuchMethodException) { | ||
true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ import io.ktor.util.* | |
import io.ktor.util.date.* | ||
import io.ktor.utils.io.* | ||
import io.ktor.utils.io.core.* | ||
import io.ktor.utils.io.errors.UnknownServiceException | ||
import kotlinx.atomicfu.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.channels.* | ||
|
@@ -199,6 +200,12 @@ internal class Endpoint( | |
connections.incrementAndGet() | ||
|
||
try { | ||
if (!Platform.isCleartextTrafficPermitted(host)) { | ||
throw UnknownServiceException( | ||
"CLEARTEXT communication to $host not permitted by network security policy" | ||
) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not very sure if we should put this check here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can do it earlier, before creating connection. Maybe even in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we can, but I see this check had been seated at a simular position in Okhttp, not very sure why. @yschimke Can you take a look? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know enough about CIO to comment, is this HTTP only here? Or could it be HTTPS also? Probably worth adding tests to validate the behaviour for both cases. Some extra context for OkHttp. We position it
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be HTTP or HTTPS here. Thanks for your explanation, especially for the second point. |
||
|
||
repeat(connectAttempts) { | ||
val address = InetSocketAddress(host, port) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright 2014-2023 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.client.engine.cio | ||
|
||
internal expect object Platform { | ||
|
||
fun isCleartextTrafficPermitted(hostname: String): Boolean | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright 2014-2023 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.client.engine.cio | ||
|
||
internal actual object Platform { | ||
actual fun isCleartextTrafficPermitted(hostname: String): Boolean = true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These aren't the full story for OkHttp.
We compile against robolectric android-all in order to call these APIs directly.
https://github.com/square/okhttp/blob/okhttp_4x/okhttp/src/main/kotlin/okhttp3/internal/platform/AndroidPlatform.kt
It's way simpler than the reflection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I borrowed these code from the old implementation, cause I see there is no Android Runtime applied in CIO module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for OkHttp. It's a compile only dependency