diff --git a/client/rest/src/main/java/org/elasticsearch/client/RequestLogger.java b/client/rest/src/main/java/org/elasticsearch/client/RequestLogger.java index 7312d33c9fb..ad2348762dd 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/RequestLogger.java +++ b/client/rest/src/main/java/org/elasticsearch/client/RequestLogger.java @@ -152,7 +152,7 @@ final class RequestLogger { httpResponse.setEntity(entity); ContentType contentType = ContentType.get(entity); Charset charset = StandardCharsets.UTF_8; - if (contentType != null) { + if (contentType != null && contentType.getCharset() != null) { charset = contentType.getCharset(); } try (BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), charset))) { diff --git a/client/rest/src/test/java/org/elasticsearch/client/RequestLoggerTests.java b/client/rest/src/test/java/org/elasticsearch/client/RequestLoggerTests.java index b66a7bcea2d..68717dfe223 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RequestLoggerTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RequestLoggerTests.java @@ -44,6 +44,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import static org.hamcrest.CoreMatchers.equalTo; @@ -51,7 +52,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; public class RequestLoggerTests extends RestClientTestCase { - public void testTraceRequest() throws IOException, URISyntaxException { HttpHost host = new HttpHost("localhost", 9200, randomBoolean() ? "http" : "https"); String expectedEndpoint = "/index/type/_api"; @@ -69,7 +69,7 @@ public class RequestLoggerTests extends RestClientTestCase { expected += " -d '" + requestBody + "'"; HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request; HttpEntity entity; - switch(randomIntBetween(0, 3)) { + switch(randomIntBetween(0, 4)) { case 0: entity = new StringEntity(requestBody, StandardCharsets.UTF_8); break; @@ -82,6 +82,10 @@ public class RequestLoggerTests extends RestClientTestCase { case 3: entity = new NByteArrayEntity(requestBody.getBytes(StandardCharsets.UTF_8)); break; + case 4: + // Evil entity without a charset + entity = new StringEntity(requestBody, (Charset) null); + break; default: throw new UnsupportedOperationException(); } @@ -116,11 +120,20 @@ public class RequestLoggerTests extends RestClientTestCase { expected += "\n# \"field\": \"value\""; expected += "\n# }"; HttpEntity entity; - if (getRandom().nextBoolean()) { + switch(randomIntBetween(0, 2)) { + case 0: entity = new StringEntity(responseBody, StandardCharsets.UTF_8); - } else { + break; + case 1: //test a non repeatable entity entity = new InputStreamEntity(new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8))); + break; + case 2: + // Evil entity without a charset + entity = new StringEntity(responseBody, (Charset) null); + break; + default: + throw new UnsupportedOperationException(); } httpResponse.setEntity(entity); }