Skip to content

Commit 8fbceff

Browse files
nosansnicoll
authored andcommitted
Use StringUtils.uriDecode where feasible
See gh-46751 Signed-off-by: Dmytro Nosan <[email protected]>
1 parent a176849 commit 8fbceff

File tree

2 files changed

+3
-43
lines changed

2 files changed

+3
-43
lines changed

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

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

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

19-
import java.io.ByteArrayOutputStream;
2019
import java.nio.charset.StandardCharsets;
2120
import java.util.Collections;
2221
import java.util.LinkedHashMap;
@@ -129,7 +128,7 @@ private Map<String, String> getResourceAttributesFromEnv() {
129128
if (index > 0) {
130129
String key = attribute.substring(0, index);
131130
String value = attribute.substring(index + 1);
132-
attributes.put(key.trim(), decode(value.trim()));
131+
attributes.put(key.trim(), StringUtils.uriDecode(value.trim(), StandardCharsets.UTF_8));
133132
}
134133
}
135134
String otelServiceName = getEnv("OTEL_SERVICE_NAME");
@@ -143,43 +142,4 @@ private Map<String, String> getResourceAttributesFromEnv() {
143142
return this.systemEnvironment.apply(name);
144143
}
145144

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-
185145
}

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("Failed to decode percent-encoded characters at index 3 in the value: 'abc%ß'");
140+
.withMessage("Incomplete trailing escape (%) pattern");
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("Failed to decode percent-encoded characters at index 0 in the value: '%'");
153+
.withMessage("Incomplete trailing escape (%) pattern");
154154
}
155155

156156
@Test

0 commit comments

Comments
 (0)