Use StringUtils.uriDecode where feasible

See gh-46751

Signed-off-by: Dmytro Nosan <dimanosan@gmail.com>
This commit is contained in:
Dmytro Nosan 2025-08-08 22:22:29 +03:00 committed by Stéphane Nicoll
parent a17684921d
commit 8fbceffd15
2 changed files with 3 additions and 43 deletions

View File

@ -16,7 +16,6 @@
package org.springframework.boot.opentelemetry.autoconfigure; package org.springframework.boot.opentelemetry.autoconfigure;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -129,7 +128,7 @@ public class OpenTelemetryResourceAttributes {
if (index > 0) { if (index > 0) {
String key = attribute.substring(0, index); String key = attribute.substring(0, index);
String value = attribute.substring(index + 1); 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"); String otelServiceName = getEnv("OTEL_SERVICE_NAME");
@ -143,43 +142,4 @@ public class OpenTelemetryResourceAttributes {
return this.systemEnvironment.apply(name); return this.systemEnvironment.apply(name);
} }
/**
* Decodes a percent-encoded string. Converts sequences like '%HH' (where HH
* represents hexadecimal digits) back into their literal representations.
* <p>
* 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;
}
} }

View File

@ -137,7 +137,7 @@ class OpenTelemetryResourceAttributesTests {
void illegalArgumentExceptionShouldBeThrownWhenDecodingIllegalHexCharPercentEncodedValue() { void illegalArgumentExceptionShouldBeThrownWhenDecodingIllegalHexCharPercentEncodedValue() {
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=abc%ß"); this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=abc%ß");
assertThatIllegalArgumentException().isThrownBy(this::getAttributes) assertThatIllegalArgumentException().isThrownBy(this::getAttributes)
.withMessage("Failed to decode percent-encoded characters at index 3 in the value: 'abc%ß'"); .withMessage("Incomplete trailing escape (%) pattern");
} }
@Test @Test
@ -150,7 +150,7 @@ class OpenTelemetryResourceAttributesTests {
void illegalArgumentExceptionShouldBeThrownWhenDecodingInvalidPercentEncodedValue() { void illegalArgumentExceptionShouldBeThrownWhenDecodingInvalidPercentEncodedValue() {
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=%"); this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=%");
assertThatIllegalArgumentException().isThrownBy(this::getAttributes) assertThatIllegalArgumentException().isThrownBy(this::getAttributes)
.withMessage("Failed to decode percent-encoded characters at index 0 in the value: '%'"); .withMessage("Incomplete trailing escape (%) pattern");
} }
@Test @Test