Skip to content

Commit 6f0a3ec

Browse files
committed
fix(appsec): strip surrounding quotes from charset parameter in MultipartContentDecoder
RFC 2045 allows quoted parameter values (charset="UTF-8"). Without stripping the quotes Charset.forName rejects the name and decodeBytes falls back to the JVM default instead of the declared charset.
1 parent 55044ff commit 6f0a3ec

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

internal-api/src/main/java/datadog/trace/api/http/MultipartContentDecoder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public static Charset extractCharset(String contentType) {
2929
int idx = contentType.toLowerCase(Locale.ROOT).indexOf("charset=");
3030
if (idx < 0) return null;
3131
String name = contentType.substring(idx + 8).split("[;, ]")[0].trim();
32+
if (name.length() > 1 && name.charAt(0) == '"' && name.charAt(name.length() - 1) == '"') {
33+
name = name.substring(1, name.length() - 1);
34+
}
3235
try {
3336
return Charset.forName(name);
3437
} catch (IllegalArgumentException e) {

internal-api/src/test/java/datadog/trace/api/http/MultipartContentDecoderTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,23 @@ void extractCharsetHandlesAdditionalParameters() {
113113
MultipartContentDecoder.extractCharset("text/plain; charset=UTF-8; boundary=something")
114114
.name());
115115
}
116+
117+
@ParameterizedTest
118+
@CsvSource({
119+
"text/plain; charset=\"UTF-8\", UTF-8",
120+
"text/xml; charset=\"ISO-8859-1\", ISO-8859-1"
121+
})
122+
void extractCharsetHandlesQuotedCharsetValue(String contentType, String expectedCharset) {
123+
assertEquals(expectedCharset, MultipartContentDecoder.extractCharset(contentType).name());
124+
}
125+
126+
@Test
127+
void decodeBytesUsesQuotedDeclaredCharset() {
128+
String text = "café";
129+
byte[] bytes = text.getBytes(StandardCharsets.ISO_8859_1);
130+
assertEquals(
131+
text,
132+
MultipartContentDecoder.decodeBytes(
133+
bytes, bytes.length, "text/plain; charset=\"ISO-8859-1\""));
134+
}
116135
}

0 commit comments

Comments
 (0)