diff --git a/server/src/main/java/org/elasticsearch/cluster/coordination/PublicationTransportHandler.java b/server/src/main/java/org/elasticsearch/cluster/coordination/PublicationTransportHandler.java index 1d4ab1346ef36..4689ae20b5ac1 100644 --- a/server/src/main/java/org/elasticsearch/cluster/coordination/PublicationTransportHandler.java +++ b/server/src/main/java/org/elasticsearch/cluster/coordination/PublicationTransportHandler.java @@ -130,7 +130,7 @@ private void handleIncomingPublishRequest( ActionListener publishResponseListener ) throws IOException { assert ThreadPool.assertCurrentThreadPool(GENERIC); - final Compressor compressor = CompressorFactory.compressor(request.bytes()); + final Compressor compressor = CompressorFactory.compressorForUnknownXContentType(request.bytes()); StreamInput in = request.bytes().streamInput(); try { if (compressor != null) { diff --git a/server/src/main/java/org/elasticsearch/common/compress/CompressedXContent.java b/server/src/main/java/org/elasticsearch/common/compress/CompressedXContent.java index e83fff553c73c..e50b267dbc1a9 100644 --- a/server/src/main/java/org/elasticsearch/common/compress/CompressedXContent.java +++ b/server/src/main/java/org/elasticsearch/common/compress/CompressedXContent.java @@ -135,7 +135,7 @@ public CompressedXContent(ToXContent xcontent, ToXContent.Params params) throws * that may already be compressed. */ public CompressedXContent(BytesReference data) throws IOException { - Compressor compressor = CompressorFactory.compressor(data); + Compressor compressor = CompressorFactory.compressorForUnknownXContentType(data); if (compressor != null) { // already compressed... this.bytes = BytesReference.toBytes(data); @@ -148,7 +148,7 @@ public CompressedXContent(BytesReference data) throws IOException { } private void assertConsistent() { - assert CompressorFactory.compressor(new BytesArray(bytes)) != null; + assert CompressorFactory.compressorForUnknownXContentType(new BytesArray(bytes)) != null; assert this.sha256.equals(sha256(uncompressed())); assert this.sha256.equals(sha256FromCompressed(bytes)); } diff --git a/server/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java b/server/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java index adcdab7ac289d..dae92b545299c 100644 --- a/server/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java +++ b/server/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java @@ -22,7 +22,7 @@ public class CompressorFactory { public static final Compressor COMPRESSOR = new DeflateCompressor(); public static boolean isCompressed(BytesReference bytes) { - return compressor(bytes) != null; + return compressorForUnknownXContentType(bytes) != null; } @Nullable @@ -34,6 +34,15 @@ public static Compressor compressor(BytesReference bytes) { assert XContentHelper.xContentType(bytes) == null; return COMPRESSOR; } + return null; + } + + @Nullable + public static Compressor compressorForUnknownXContentType(BytesReference bytes) { + Compressor compressor = compressor(bytes); + if (compressor != null) { + return compressor; + } XContentType contentType = XContentHelper.xContentType(bytes); if (contentType == null) { @@ -56,13 +65,13 @@ private static boolean isAncient(BytesReference bytes) { * @throws NullPointerException a NullPointerException will be thrown when bytes is null */ public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException { - Compressor compressor = compressor(Objects.requireNonNull(bytes, "the BytesReference must not be null")); + Compressor compressor = compressorForUnknownXContentType(Objects.requireNonNull(bytes, "the BytesReference must not be null")); return compressor == null ? bytes : compressor.uncompress(bytes); } /** Decompress the provided {@link BytesReference}. */ public static BytesReference uncompress(BytesReference bytes) throws IOException { - Compressor compressor = compressor(bytes); + Compressor compressor = compressorForUnknownXContentType(bytes); if (compressor == null) { throw new NotCompressedException(); } diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java index c0eaee071b76c..f62fa6abff3a7 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java @@ -67,7 +67,7 @@ public static XContentParser createParser(NamedXContentRegistry registry, Deprec */ @Deprecated public static XContentParser createParser(XContentParserConfiguration config, BytesReference bytes) throws IOException { - Compressor compressor = CompressorFactory.compressor(bytes); + Compressor compressor = CompressorFactory.compressorForUnknownXContentType(bytes); if (compressor != null) { InputStream compressedInput = compressor.threadLocalInputStream(bytes.streamInput()); if (compressedInput.markSupported() == false) { @@ -568,7 +568,7 @@ public interface CustomMerge { @Deprecated public static void writeRawField(String field, BytesReference source, XContentBuilder builder, ToXContent.Params params) throws IOException { - Compressor compressor = CompressorFactory.compressor(source); + Compressor compressor = CompressorFactory.compressorForUnknownXContentType(source); if (compressor != null) { try (InputStream compressedStreamInput = compressor.threadLocalInputStream(source.streamInput())) { builder.rawField(field, compressedStreamInput); @@ -592,7 +592,7 @@ public static void writeRawField( ToXContent.Params params ) throws IOException { Objects.requireNonNull(xContentType); - Compressor compressor = CompressorFactory.compressor(source); + Compressor compressor = CompressorFactory.compressorForUnknownXContentType(source); if (compressor != null) { try (InputStream compressedStreamInput = compressor.threadLocalInputStream(source.streamInput())) { builder.rawField(field, compressedStreamInput, xContentType); @@ -677,7 +677,7 @@ public static BytesReference toXContent(ChunkedToXContent toXContent, XContentTy */ @Deprecated public static XContentType xContentTypeMayCompressed(BytesReference bytes) { - Compressor compressor = CompressorFactory.compressor(bytes); + Compressor compressor = CompressorFactory.compressorForUnknownXContentType(bytes); if (compressor != null) { try { InputStream compressedStreamInput = compressor.threadLocalInputStream(bytes.streamInput()); diff --git a/server/src/test/java/org/elasticsearch/cluster/coordination/PublicationTransportHandlerTests.java b/server/src/test/java/org/elasticsearch/cluster/coordination/PublicationTransportHandlerTests.java index 94146071773a4..e6e8236ba7c1e 100644 --- a/server/src/test/java/org/elasticsearch/cluster/coordination/PublicationTransportHandlerTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/coordination/PublicationTransportHandlerTests.java @@ -145,7 +145,7 @@ private static boolean isDiff(BytesTransportRequest request, TransportVersion ve StreamInput in = null; try { in = request.bytes().streamInput(); - final Compressor compressor = CompressorFactory.compressor(request.bytes()); + final Compressor compressor = CompressorFactory.compressorForUnknownXContentType(request.bytes()); if (compressor != null) { in = compressor.threadLocalStreamInput(in); } diff --git a/server/src/test/java/org/elasticsearch/common/xcontent/support/XContentHelperTests.java b/server/src/test/java/org/elasticsearch/common/xcontent/support/XContentHelperTests.java index a06fa296ab10a..84ed2c8727cbb 100644 --- a/server/src/test/java/org/elasticsearch/common/xcontent/support/XContentHelperTests.java +++ b/server/src/test/java/org/elasticsearch/common/xcontent/support/XContentHelperTests.java @@ -12,12 +12,14 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.compress.CompressedXContent; +import org.elasticsearch.common.compress.NotXContentException; import org.elasticsearch.common.util.Maps; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentParserConfiguration; import org.elasticsearch.xcontent.XContentType; @@ -421,4 +423,19 @@ public void testParseToType() throws IOException { assertThat(names, equalTo(Set.of("a", "c"))); } + + public void testGetParserWithInvalidInput() throws IOException { + assertThrows( + "Should detect bad JSON", + NotXContentException.class, + () -> XContentHelper.createParser(XContentParserConfiguration.EMPTY, new BytesArray("not actually XContent")) + ); + XContentParser parser = XContentHelper.createParser( + XContentParserConfiguration.EMPTY, + new BytesArray("not actually XContent"), + XContentType.JSON + ); + assertNotNull("Should not detect bad JSON", parser); // This is more like assertNotThrows + assertThrows("Should detect bad JSON at parse time", XContentParseException.class, parser::numberValue); + } }