diff --git a/module/spring-boot-opentelemetry/src/main/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryResourceAttributes.java b/module/spring-boot-opentelemetry/src/main/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryResourceAttributes.java index a8c2bf095d5a..a0a0c44dc8a7 100644 --- a/module/spring-boot-opentelemetry/src/main/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryResourceAttributes.java +++ b/module/spring-boot-opentelemetry/src/main/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryResourceAttributes.java @@ -16,7 +16,6 @@ package org.springframework.boot.opentelemetry.autoconfigure; -import java.io.ByteArrayOutputStream; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.LinkedHashMap; @@ -129,7 +128,7 @@ private Map getResourceAttributesFromEnv() { if (index > 0) { String key = attribute.substring(0, index); String value = attribute.substring(index + 1); - attributes.put(key.trim(), decode(value.trim())); + attributes.put(key.trim(), StringUtils.uriDecode(value.trim(), StandardCharsets.UTF_8)); } } String otelServiceName = getEnv("OTEL_SERVICE_NAME"); @@ -143,43 +142,4 @@ private Map getResourceAttributesFromEnv() { return this.systemEnvironment.apply(name); } - /** - * Decodes a percent-encoded string. Converts sequences like '%HH' (where HH - * represents hexadecimal digits) back into their literal representations. - *

- * Inspired by {@code org.apache.commons.codec.net.PercentCodec}. - * @param value value to decode - * @return the decoded string - */ - private static String decode(String value) { - if (value.indexOf('%') < 0) { - return value; - } - byte[] bytes = value.getBytes(StandardCharsets.UTF_8); - ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length); - for (int i = 0; i < bytes.length; i++) { - byte b = bytes[i]; - if (b != '%') { - out.write(b); - continue; - } - int u = decodeHex(bytes, i + 1); - int l = decodeHex(bytes, i + 2); - if (u >= 0 && l >= 0) { - out.write((u << 4) + l); - } - else { - throw new IllegalArgumentException( - "Failed to decode percent-encoded characters at index %d in the value: '%s'".formatted(i, - value)); - } - i += 2; - } - return out.toString(StandardCharsets.UTF_8); - } - - private static int decodeHex(byte[] bytes, int index) { - return (index < bytes.length) ? Character.digit(bytes[index], 16) : -1; - } - } diff --git a/module/spring-boot-opentelemetry/src/test/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryResourceAttributesTests.java b/module/spring-boot-opentelemetry/src/test/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryResourceAttributesTests.java index 7d5d245bdf0b..ebae139464ae 100644 --- a/module/spring-boot-opentelemetry/src/test/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryResourceAttributesTests.java +++ b/module/spring-boot-opentelemetry/src/test/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryResourceAttributesTests.java @@ -137,7 +137,7 @@ void otelResourceAttributeValuesShouldBePercentDecodedWhenMultiByteSequences() { void illegalArgumentExceptionShouldBeThrownWhenDecodingIllegalHexCharPercentEncodedValue() { this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=abc%ß"); assertThatIllegalArgumentException().isThrownBy(this::getAttributes) - .withMessage("Failed to decode percent-encoded characters at index 3 in the value: 'abc%ß'"); + .withMessage("Incomplete trailing escape (%) pattern"); } @Test @@ -150,7 +150,7 @@ void replacementCharShouldBeUsedWhenDecodingNonUtf8Character() { void illegalArgumentExceptionShouldBeThrownWhenDecodingInvalidPercentEncodedValue() { this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=%"); assertThatIllegalArgumentException().isThrownBy(this::getAttributes) - .withMessage("Failed to decode percent-encoded characters at index 0 in the value: '%'"); + .withMessage("Incomplete trailing escape (%) pattern"); } @Test