Skip to content

Commit 46aace9

Browse files
committed
internal-api: accept tab as boundary before charset= in MultipartContentDecoder
RFC 7230 optional whitespace (OWS) allows both space and horizontal tab. extractCharset() only accepted space, so "type;\tcharset=X" was treated as no-charset-declared, causing MultipartHelper to silently override the client's declared charset with UTF-8.
1 parent afbe420 commit 46aace9

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ public static Charset extractCharset(String contentType) {
4343
while (true) {
4444
int idx = indexOfIgnoreAsciiCase(contentType, "charset=", searchFrom);
4545
if (idx < 0) return null;
46-
// Require a parameter boundary before "charset=" so "xcharset=..." is not matched
47-
if (idx == 0 || contentType.charAt(idx - 1) == ';' || contentType.charAt(idx - 1) == ' ') {
46+
// Require a parameter boundary before "charset=" so "xcharset=..." is not matched.
47+
// RFC 7230 optional whitespace (OWS) allows both space and horizontal tab.
48+
if (idx == 0
49+
|| contentType.charAt(idx - 1) == ';'
50+
|| contentType.charAt(idx - 1) == ' '
51+
|| contentType.charAt(idx - 1) == '\t') {
4852
int nameStart = idx + 8;
4953
int end = contentType.length();
5054
for (int i = nameStart; i < end; i++) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ void extractCharsetFromStandardContentType(String contentType, String expectedCh
111111
assertEquals(expectedCharset, MultipartContentDecoder.extractCharset(contentType).name());
112112
}
113113

114+
@ParameterizedTest
115+
@ValueSource(
116+
strings = {
117+
"text/plain;\tcharset=ISO-8859-1",
118+
"text/plain; \tcharset=ISO-8859-1",
119+
"text/plain;\t charset=ISO-8859-1"
120+
})
121+
void extractCharsetAcceptsTabAsParameterBoundary(String contentType) {
122+
// RFC 7230 optional whitespace (OWS) allows both space and horizontal tab before a parameter
123+
assertEquals("ISO-8859-1", MultipartContentDecoder.extractCharset(contentType).name());
124+
}
125+
114126
@Test
115127
void extractCharsetIgnoresSubstringMatchInParameterName() {
116128
// "xcharset=UTF-16" must not match; the real "charset=UTF-8" that follows must be used

0 commit comments

Comments
 (0)