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 6 commits
7d3fb01
1641149
f0733ef
ced616f
044d2b6
54df7e7
9ff3467
e8dd071
cb1e09c
ed199d2
e048727
140c37c
a4a1c11
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,4 @@ | ||
public final class io/ktor/client/plugins/platform/PlatformingKt { | ||
public static final fun getPlatforming ()Lio/ktor/client/plugins/api/ClientPlugin; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
description = "Ktor client platform support" | ||
|
||
useJdkVersionForJvmTests(11) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.client.plugins.platform | ||
|
||
internal expect object Platform { | ||
|
||
fun isCleartextTrafficPermitted(hostname: String): Boolean | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.client.plugins.platform | ||
|
||
import io.ktor.client.plugins.api.ClientPlugin | ||
import io.ktor.client.plugins.api.createClientPlugin | ||
import io.ktor.client.request.host | ||
import io.ktor.utils.io.errors.UnknownServiceException | ||
|
||
public val Platforming: ClientPlugin<Any> = createClientPlugin("Platforming", ::Any) { | ||
onRequest { request, _ -> | ||
val host = request.host | ||
if (!Platform.isCleartextTrafficPermitted(host)) { | ||
throw UnknownServiceException( | ||
"CLEARTEXT communication to $host not permitted by network security policy" | ||
) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.client.plugins.platform | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.client.plugins.platform | ||
|
||
internal actual object Platform { | ||
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. 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. Hey @Goooler, it will need to be implemented for To check the implementation in ide, you can invert the flag ktor.ide.jvmAndCommonOnly in gradle.properties https://github.com/ktorio/ktor/blob/92ece71d433f6d96754ef7d18ab2691e1f36160a/gradle.properties#L7C1-L7C1 and import project again 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. |
||
|
||
fun isCleartextTrafficPermitted(hostname: String): Boolean = false | ||
} |
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.
How can I install this plugin for CIO by default?
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.
let's make this in the separate PR, it looks like it require service loader or some similar mechanism