Non-ASCII characters are corrupted when importing properties content with the env: prefix

Aperta Adatta ai principianti
#52,647 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Inizia da SystemEnvironmentConfigDataResource#load, dove l’issue indica che la stringa della variabile d’ambiente viene codificata in UTF-8 prima di essere passata al loader delle properties. Leggi i test correlati SystemEnvironmentConfigData* e aggiungi copertura per contenuti properties con caratteri non ASCII importati con il prefisso env:. Il lavoro è completato quando il valore importato conserva i caratteri originali senza richiedere un’indicazione di codifica.

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

Descrizione

status: waiting-for-triage

SystemEnvironmentConfigDataResource turns the environment variable value into bytes with content.getBytes(StandardCharsets.UTF_8), but then calls loader.load(name, resource, this.encoding) with encoding == null unless the [encoding=...] hint is used. OriginTrackedPropertiesLoader then decodes with its default ISO-8859-1, so any non-ASCII character in a .properties-style variable is corrupted.

Since the environment variable is already a String, there is no file encoding involved, and the bytes Spring Boot produces itself are always UTF-8.

Spring Boot 4.1.2

MY_CONF='greeting=Grüße' java -jar app.jar --spring.config.import=env:MY_CONF
environment.getProperty("greeting")  ->  "Grüße"      (expected "Grüße")

The same content imported as YAML (env:MY_CONF[.yaml]) is read correctly. Adding [encoding=utf-8] works around the problem, but users have no reason to expect they need it for an in-memory string.

Possible fix: in SystemEnvironmentConfigDataResource#load, default to the charset the content was encoded with:

private Charset getEncoding() {
	return (this.encoding != null) ? this.encoding : StandardCharsets.UTF_8;
}

I verified this end to end: I ran the packaged app as a separate process with the variable set and read the value over HTTP. With 4.1.2 the bytes are 47 72 c3 83 c2 bc .... With a locally built spring-boot jar including the change above, the result is Grüße (47 72 c3 bc c3 9f 65). The existing SystemEnvironmentConfigData* tests still pass. I'm happy to submit a PR with a test.

Lingua principale
Java
Stelle
81.8k
Fork
43.5k
Merge medio
3g 13h
PR unite (30g)
90

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-projects/spring-boot

Tutte le issue di spring-projects/spring-boot

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.