Skip to content

Commit e74778b

Browse files
committed
fix(appsec): use machine default charset as fallback in MultipartContentDecoder
Replaces hardcoded UTF-8 (no-charset default) and ISO-8859-1 (fallback) with Charset.defaultCharset() in both cases, per reviewer feedback.
1 parent b7a1a33 commit e74778b

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
import java.nio.charset.CharacterCodingException;
55
import java.nio.charset.Charset;
66
import java.nio.charset.CodingErrorAction;
7-
import java.nio.charset.StandardCharsets;
87
import java.util.Locale;
98

109
/** Decodes multipart file content bytes to String using the per-part Content-Type charset. */
1110
public final class MultipartContentDecoder {
1211

1312
public static String decodeBytes(byte[] buf, int length, String contentType) {
1413
Charset charset = extractCharset(contentType);
15-
if (charset == null) charset = StandardCharsets.UTF_8;
14+
if (charset == null) charset = Charset.defaultCharset();
1615
try {
1716
return charset
1817
.newDecoder()
@@ -21,7 +20,7 @@ public static String decodeBytes(byte[] buf, int length, String contentType) {
2120
.decode(ByteBuffer.wrap(buf, 0, length))
2221
.toString();
2322
} catch (CharacterCodingException e) {
24-
return new String(buf, 0, length, StandardCharsets.ISO_8859_1);
23+
return new String(buf, 0, length, Charset.defaultCharset());
2524
}
2625
}
2726

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.api.http
22

3+
import java.nio.charset.Charset
34
import spock.lang.Specification
45

56
class MultipartContentDecoderTest extends Specification {
@@ -13,7 +14,7 @@ class MultipartContentDecoderTest extends Specification {
1314
MultipartContentDecoder.decodeBytes(bytes, bytes.length, 'text/plain; charset=UTF-8') == text
1415
}
1516

16-
void 'decodeBytes falls back to UTF-8 when Content-Type has no charset'() {
17+
void 'decodeBytes falls back to machine default when Content-Type has no charset'() {
1718
given:
1819
def text = 'hello world'
1920
byte[] bytes = text.getBytes('UTF-8')
@@ -22,7 +23,7 @@ class MultipartContentDecoderTest extends Specification {
2223
MultipartContentDecoder.decodeBytes(bytes, bytes.length, 'text/plain') == text
2324
}
2425

25-
void 'decodeBytes falls back to UTF-8 when Content-Type is null'() {
26+
void 'decodeBytes falls back to machine default when Content-Type is null'() {
2627
given:
2728
def text = 'hello world'
2829
byte[] bytes = text.getBytes('UTF-8')
@@ -31,13 +32,13 @@ class MultipartContentDecoderTest extends Specification {
3132
MultipartContentDecoder.decodeBytes(bytes, bytes.length, null) == text
3233
}
3334

34-
void 'decodeBytes falls back to ISO-8859-1 when bytes are invalid for declared charset'() {
35+
void 'decodeBytes falls back to machine default when bytes cannot be decoded with default charset'() {
3536
given:
36-
// 0xE9 is 'é' in ISO-8859-1 but an invalid lone UTF-8 byte
3737
byte[] bytes = 'café'.getBytes('ISO-8859-1')
38+
String expected = new String(bytes, Charset.defaultCharset())
3839

3940
expect:
40-
MultipartContentDecoder.decodeBytes(bytes, bytes.length, null) == 'café'
41+
MultipartContentDecoder.decodeBytes(bytes, bytes.length, null) == expected
4142
}
4243

4344
void 'decodeBytes uses declared ISO-8859-1 charset'() {
@@ -62,13 +63,14 @@ class MultipartContentDecoderTest extends Specification {
6263
MultipartContentDecoder.decodeBytes(new byte[16], 0, null) == ''
6364
}
6465

65-
void 'decodeBytes falls back to ISO-8859-1 when declared charset cannot decode the bytes'() {
66+
void 'decodeBytes falls back to machine default when declared charset cannot decode the bytes'() {
6667
given:
6768
// bytes are ISO-8859-1 encoded but Content-Type explicitly declares UTF-8
6869
byte[] bytes = 'café'.getBytes('ISO-8859-1')
70+
String expected = new String(bytes, Charset.defaultCharset())
6971

7072
expect:
71-
MultipartContentDecoder.decodeBytes(bytes, bytes.length, 'text/plain; charset=UTF-8') == 'café'
73+
MultipartContentDecoder.decodeBytes(bytes, bytes.length, 'text/plain; charset=UTF-8') == expected
7274
}
7375

7476
void 'extractCharset returns null for null contentType'() {

0 commit comments

Comments
 (0)