Skip to content

Commit b1d1651

Browse files
committed
Use StringUtils.uriDecode where feasible
Signed-off-by: Dmytro Nosan <[email protected]>
1 parent 2f91fe2 commit b1d1651

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

module/spring-boot-opentelemetry/src/main/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryResourceAttributes.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.opentelemetry.autoconfigure;
1818

19+
import java.io.ByteArrayOutputStream;
1920
import java.nio.charset.StandardCharsets;
2021
import java.util.Collections;
2122
import java.util.LinkedHashMap;
@@ -128,7 +129,7 @@ private Map<String, String> getResourceAttributesFromEnv() {
128129
if (index > 0) {
129130
String key = attribute.substring(0, index);
130131
String value = attribute.substring(index + 1);
131-
attributes.put(key.trim(), StringUtils.uriDecode(value.trim(), StandardCharsets.UTF_8));
132+
attributes.put(key.trim(), decode(value.trim()));
132133
}
133134
}
134135
String otelServiceName = getEnv("OTEL_SERVICE_NAME");
@@ -142,4 +143,43 @@ private Map<String, String> getResourceAttributesFromEnv() {
142143
return this.systemEnvironment.apply(name);
143144
}
144145

146+
/**
147+
* Decodes a percent-encoded string. Converts sequences like '%HH' (where HH
148+
* represents hexadecimal digits) back into their literal representations.
149+
* <p>
150+
* Inspired by {@code org.apache.commons.codec.net.PercentCodec}.
151+
* @param value value to decode
152+
* @return the decoded string
153+
*/
154+
private static String decode(String value) {
155+
if (value.indexOf('%') < 0) {
156+
return value;
157+
}
158+
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
159+
ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length);
160+
for (int i = 0; i < bytes.length; i++) {
161+
byte b = bytes[i];
162+
if (b != '%') {
163+
out.write(b);
164+
continue;
165+
}
166+
int u = decodeHex(bytes, i + 1);
167+
int l = decodeHex(bytes, i + 2);
168+
if (u >= 0 && l >= 0) {
169+
out.write((u << 4) + l);
170+
}
171+
else {
172+
throw new IllegalArgumentException(
173+
"Failed to decode percent-encoded characters at index %d in the value: '%s'".formatted(i,
174+
value));
175+
}
176+
i += 2;
177+
}
178+
return out.toString(StandardCharsets.UTF_8);
179+
}
180+
181+
private static int decodeHex(byte[] bytes, int index) {
182+
return (index < bytes.length) ? Character.digit(bytes[index], 16) : -1;
183+
}
184+
145185
}

module/spring-boot-opentelemetry/src/test/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryResourceAttributesTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void otelResourceAttributeValuesShouldBePercentDecodedWhenMultiByteSequences() {
137137
void illegalArgumentExceptionShouldBeThrownWhenDecodingIllegalHexCharPercentEncodedValue() {
138138
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=abc%ß");
139139
assertThatIllegalArgumentException().isThrownBy(this::getAttributes)
140-
.withMessage("Incomplete trailing escape (%) pattern");
140+
.withMessage("Failed to decode percent-encoded characters at index 3 in the value: 'abc%ß'");
141141
}
142142

143143
@Test
@@ -150,7 +150,7 @@ void replacementCharShouldBeUsedWhenDecodingNonUtf8Character() {
150150
void illegalArgumentExceptionShouldBeThrownWhenDecodingInvalidPercentEncodedValue() {
151151
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=%");
152152
assertThatIllegalArgumentException().isThrownBy(this::getAttributes)
153-
.withMessage("Incomplete trailing escape (%) pattern");
153+
.withMessage("Failed to decode percent-encoded characters at index 0 in the value: '%'");
154154
}
155155

156156
@Test

0 commit comments

Comments
 (0)