[DO NOT MERGE] Test with trusting self-signed certificates#5946
[DO NOT MERGE] Test with trusting self-signed certificates#5946chaoran-chen wants to merge 8 commits intomainfrom
Conversation
| ) | ||
|
|
||
| val sslContext = SSLContext.getInstance("TLS") | ||
| sslContext.init(null, trustAllCerts, java.security.SecureRandom()) |
Check failure
Code scanning / CodeQL
`TrustManager` that accepts all certificates High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 4 days ago
In general, the right fix is to stop using a TrustManager that blindly trusts all certificates and to stop disabling hostname verification. Instead, rely on the default JVM trust store or explicitly configure a KeyStore/trust store containing only the certificates (or CAs) you want to trust, and use a standard TrustManagerFactory and default hostname verification.
In this specific file, the minimal, safe change that preserves existing functionality as much as possible is:
- Remove the custom
X509TrustManagerthat accepts all certificates. - Stop creating a custom
SSLContextandSSLConnectionSocketFactorywithNoopHostnameVerifier. - Let the AWS SDK’s
ApacheHttpClientbuilder create a default HTTP client (ApacheHttpClient.builder().build()), which uses the platform’s default trust configuration and proper hostname verification.
If the application truly must talk to a service with a self‑signed certificate, the correct solution would be to configure the JVM’s trust store or an Apache HTTP client trust store with that specific certificate; however, that setup lies outside the shown snippet and cannot be implemented safely here without additional configuration details. Therefore, within backend/src/main/kotlin/org/loculus/backend/service/files/S3Service.kt, the best contained fix is to simplify createTrustAllHttpClient() so it no longer disables TLS checks, and to remove now‑unused TLS‑related imports.
Concretely:
- In
S3Service.kt, updatecreateTrustAllHttpClient()(lines 228–245) to:
private fun createTrustAllHttpClient(): SdkHttpClient {
return ApacheHttpClient.builder().build()
}- Remove the unused imports related to the insecure TLS customization:
NoopHostnameVerifier,SSLConnectionSocketFactory,X509Certificate,SSLContext,TrustManager, andX509TrustManager.
No other behavior of S3Client construction (endpoint override, region, credentials, S3 configuration) is changed.
| @@ -1,7 +1,5 @@ | ||
| package org.loculus.backend.service.files | ||
|
|
||
| import org.apache.http.conn.ssl.NoopHostnameVerifier | ||
| import org.apache.http.conn.ssl.SSLConnectionSocketFactory | ||
| import org.loculus.backend.config.S3BucketConfig | ||
| import org.loculus.backend.config.S3Config | ||
| import org.loculus.backend.controller.BadRequestException | ||
| @@ -32,11 +30,7 @@ | ||
| import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest | ||
| import software.amazon.awssdk.services.s3.presigner.model.UploadPartPresignRequest | ||
| import java.net.URI | ||
| import java.security.cert.X509Certificate | ||
| import java.time.Duration | ||
| import javax.net.ssl.SSLContext | ||
| import javax.net.ssl.TrustManager | ||
| import javax.net.ssl.X509TrustManager | ||
|
|
||
| private const val PRESIGNED_URL_EXPIRY_SECONDS = 60 * 30 | ||
|
|
||
| @@ -226,21 +219,7 @@ | ||
| .build() | ||
|
|
||
| private fun createTrustAllHttpClient(): SdkHttpClient { | ||
| val trustAllCerts = arrayOf<TrustManager>( | ||
| object : X509TrustManager { | ||
| override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {} | ||
| override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {} | ||
| override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf() | ||
| }, | ||
| ) | ||
|
|
||
| val sslContext = SSLContext.getInstance("TLS") | ||
| sslContext.init(null, trustAllCerts, java.security.SecureRandom()) | ||
|
|
||
| val socketFactory = SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE) | ||
|
|
||
| return ApacheHttpClient.builder() | ||
| .socketFactory(socketFactory) | ||
| .build() | ||
| } | ||
|
|
resolves #
Screenshot
PR Checklist
🚀 Preview: Add
previewlabel to enable