Skip to content

insecure ssl/tls #7

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -6,6 +6,7 @@ import java.security.KeyStore
import java.security.SecureRandom
import java.security.cert.Certificate
import java.security.cert.CertificateFactory
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManagerFactory
import javax.net.ssl.X509TrustManager
Expand Down Expand Up @@ -83,4 +84,32 @@ fun OkHttpClient.Builder.addKeyStore(keyStoreFile: File, keystorePassword: Strin
sslContext.socketFactory,
trustManager
)
}

/**
* Configures the [OkHttpClient.Builder] so that SSL/TLS certificate verification is disabled.
*
* This method creates a trust-all-certificates [X509TrustManager] which accepts any certificate.
* Using this configuration makes your application vulnerable to man-in-the-middle attacks because it
* will accept invalid or untrusted certificates.
*
* **IMPORTANT:** NEVER USE THIS METHOD IN PRODUCTION! Disabling SSL certificate validation exposes sensitive data
* and can lead to security breaches.
*/
fun OkHttpClient.Builder.insecure() {

val trustAllCerts = object : X509TrustManager {
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {}

override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {}

override fun getAcceptedIssuers(): Array<out X509Certificate>? = emptyArray()
}

val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, arrayOf(trustAllCerts), SecureRandom())


sslSocketFactory(sslContext.socketFactory, trustAllCerts)

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.amplicode.connekt
import io.amplicode.connekt.dsl.GET
import io.amplicode.connekt.dsl.addX509Certificate
import io.amplicode.connekt.dsl.addKeyStore
import io.amplicode.connekt.dsl.insecure
import io.amplicode.connekt.test.utils.server.ServerSslParams
import io.amplicode.connekt.test.utils.server.TestServer
import io.amplicode.connekt.test.utils.asUnit
Expand Down Expand Up @@ -113,4 +114,15 @@ class SslTest(server: TestServer) : TestWithServer(server) {
assertEquals("foo", body?.string())
}
}.asUnit()

@Test
fun `insecure via ext function`() = runScript {
configureClient {
insecure()
}

GET("$hostHttps/foo") then {
assertEquals("foo", body?.string())
}
}.asUnit()
}