spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

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

Open Ludocius opened this issue 4 weeks ago • 1 comments

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.

Ludocius avatar Nov 07 '26 13:11 Ludocius

We have the same issue with 4.0.x but it's unfortunately too hard to fix there so we'll target this one to 4.1+

philwebb avatar Nov 12 '26 00:11 philwebb