AbstractSslConfigurer does not close the InputStream opened by URL.openStream() (trusted-x509-certificates / key-store)

Abierto Apto para principiantes
#4,395 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
78/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
java, spring-boot
Área
backend, security

Línea de trabajo

Lee AbstractSslConfigurer.getTrustedX509CertificatesForTrustManager() y AbstractSslConfigurer.createKeyStore(), donde el issue identifica los streams que no se cierran. Envuelve cada url.openStream() en try-with-resources y revisa las pruebas existentes para los certificados de confianza y la carga del key-store. Se completa cuando ambos streams se cierran después de usarse y pasan las pruebas pertinentes.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

waiting-for-triage
Describe the bug

AbstractSslConfigurer.getTrustedX509CertificatesForTrustManager() passes url.openStream() directly to
CertificateFactory.generateCertificates(...), and AbstractSslConfigurer.createKeyStore() passes url.openStream()
directly to KeyStore.load(...). Neither stream is closed (no try-with-resources and no close() call in the class).

// getTrustedX509CertificatesForTrustManager()
Collection<? extends Certificate> certs = certificateFactory.generateCertificates(url.openStream());

// createKeyStore()
store.load(url.openStream(), ...);

Neither CertificateFactory.generateCertificates nor KeyStore.load documents that it closes the stream it is
given, and in practice the underlying file handle stays open after the call (observed on Windows, see below) until
the unreferenced stream is garbage-collected.

Impact
  • With a file: location, the file handle stays open after the routing HttpClient is built. On Windows the
    certificate / key-store file cannot be deleted or replaced while the handle is open
    ("The process cannot access the file because it is being used by another process").
  • With jar: or http(s): locations, the connection / jar handle stays open in the same way.
  • The leak is bounded (once per configured file when the HttpClient is created), but it is still a resource leak.
Versions
  • Spring Cloud Gateway Server WebFlux 5.0.5 (also present on main as of 2026-11-30)
  • Java 17
To reproduce
  1. Configure spring.cloud.gateway.server.webflux.httpclient.ssl.trusted-x509-certificates[0]=file:<path>/ca.crt
    (a self-signed CA certificate in PEM form).
  2. Start the application so that the routing HttpClient is created.
  3. On Windows, try to delete <path>/ca.crt from the same JVM (for example in a test cleanup step).
    The deletion fails until a GC happens to clean up the unreferenced stream.
Suggested fix

Open the streams in try-with-resources:

try (InputStream in = url.openStream()) {
    Collection<? extends Certificate> certs = certificateFactory.generateCertificates(in);
    allCerts.addAll(certs);
}

and likewise in createKeyStore():

try (InputStream in = url.openStream()) {
    store.load(in, ...);
}

Configurations that use ssl-bundle are not affected, because that path obtains the trust managers from the
Spring Boot SslBundle.

Lenguaje dominante
Java
Estrellas
4.9k
Forks
3.5k
Merge medio
1 d 13 h
PR fusionados (30 d)
14

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de spring-cloud/spring-cloud-gateway

Todos los issues de spring-cloud/spring-cloud-gateway

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.