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
Open
Goooler
wants to merge
13
commits into
ktorio:main
Choose a base branch
from
Goooler:KTOR-6182
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7d3fb01
Add Platform
Goooler 1641149
Throw UnknownServiceException
Goooler f0733ef
Merge branch 'main' into KTOR-6182
Goooler ced616f
Fix typo
Goooler 044d2b6
Merge remote-tracking branch 'origin/main' into KTOR-6182
Goooler 54df7e7
Split Platform into a new plugin module
Goooler 9ff3467
Split to js and posix source sets
Goooler e8dd071
More comments
Goooler cb1e09c
Rename classes
Goooler ed199d2
Merge remote-tracking branch 'origin/main' into KTOR-6182
Goooler e048727
Define UnknownServiceException for posix
Goooler 140c37c
Define UnknownServiceException for js
Goooler a4a1c11
Try to fix style
Goooler File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
ktor-client/ktor-client-plugins/ktor-client-platform/api/ktor-client-platform.api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
7 changes: 7 additions & 0 deletions
7
ktor-client/ktor-client-plugins/ktor-client-platform/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
10 changes: 10 additions & 0 deletions
10
ktor-client/ktor-client-plugins/ktor-client-platform/common/src/Platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
21 changes: 21 additions & 0 deletions
21
ktor-client/ktor-client-plugins/ktor-client-platform/common/src/Platforming.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
ktor-client/ktor-client-plugins/ktor-client-platform/js/src/PlatformJs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* 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 { | ||
|
||
/** | ||
* Not supported on JS. | ||
*/ | ||
fun isCleartextTrafficPermitted(hostname: String): Boolean = false | ||
} |
66 changes: 66 additions & 0 deletions
66
ktor-client/ktor-client-plugins/ktor-client-platform/jvm/src/PlatformJvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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" | ||
|
||
/** | ||
* Used by Android to determine whether cleartext network traffic is permitted for all network communication from this process. | ||
* | ||
* See [NetworkSecurityPolicy#isCleartextTrafficPermitted()](https://developer.android.com/reference/android/security/NetworkSecurityPolicy#isCleartextTrafficPermitted()). | ||
*/ | ||
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 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
ktor-client/ktor-client-plugins/ktor-client-platform/posix/src/PlatformNative.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* 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 { | ||
|
||
/** | ||
* Not supported on native. | ||
*/ | ||
fun isCleartextTrafficPermitted(hostname: String): Boolean = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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