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

Aperta Adatta ai principianti
#4,395 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java, spring-boot
Ambito
backend, security

Direzione di ricerca

Leggi AbstractSslConfigurer.getTrustedX509CertificatesForTrustManager() e AbstractSslConfigurer.createKeyStore(), dove l’issue individua gli stream non chiusi. Racchiudi ogni url.openStream() in try-with-resources e controlla i test esistenti per i certificati attendibili e il caricamento del key-store. Il lavoro è completato quando entrambi gli stream vengono chiusi dopo l’uso e i test pertinenti passano.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Java
Stelle
4.9k
Fork
3.5k
Merge medio
1g 13h
PR unite (30g)
14

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di spring-cloud/spring-cloud-gateway

Tutte le issue di spring-cloud/spring-cloud-gateway

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.