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

Abierto Apto para principiantes
#52,647 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

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

Línea de trabajo

Empieza en SystemEnvironmentConfigDataResource#load, donde el issue indica que la cadena de la variable de entorno se codifica como UTF-8 antes de pasarla al cargador de properties. Lee las pruebas relacionadas de SystemEnvironmentConfigData* y añade cobertura para contenido de properties con caracteres no ASCII importado con el prefijo env:. Se considera terminado cuando el valor importado conserva los caracteres originales sin requerir una indicación de codificación.

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

Descripción

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.

Lenguaje dominante
Java
Estrellas
81.8k
Forks
43.5k
Merge medio
3 d 13 h
PR fusionados (30 d)
90

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

Todos los issues de spring-projects/spring-boot

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.