From d795ce3b6ea577db9a7450ffea635f86e9624634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilo=20D=C3=ADaz=20Repka?= Date: Mon, 9 Nov 2015 00:45:25 -0300 Subject: [PATCH] XContentFactory.xContentType: allow for possible UTF-8 BOM for JSON XContentType Fixes #14442 --- .../common/xcontent/XContentFactory.java | 8 +++++++- .../common/xcontent/XContentFactoryTests.java | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java index c9e38f1d94e..6fbf814b006 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java @@ -329,8 +329,14 @@ public class XContentFactory { return XContentType.CBOR; } + int jsonStart = 0; + // JSON may be preceded by UTF-8 BOM + if (length > 3 && first == (byte) 0xEF && bytes.get(1) == (byte) 0xBB && bytes.get(2) == (byte) 0xBF) { + jsonStart = 3; + } + // a last chance for JSON - for (int i = 0; i < length; i++) { + for (int i = jsonStart; i < length; i++) { byte b = bytes.get(i); if (b == '{') { return XContentType.JSON; diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/XContentFactoryTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/XContentFactoryTests.java index fb12c7508c2..7489ea8f9dc 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/XContentFactoryTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/XContentFactoryTests.java @@ -97,4 +97,18 @@ public class XContentFactoryTests extends ESTestCase { is = new ByteArrayInputStream(new byte[] {(byte) 1}); assertNull(XContentFactory.xContentType(is)); } + + public void testJsonFromBytesOptionallyPrecededByUtf8Bom() throws Exception { + byte[] bytes = new byte[] {(byte) '{', (byte) '}'}; + assertThat(XContentFactory.xContentType(bytes), equalTo(XContentType.JSON)); + + bytes = new byte[] {(byte) 0x20, (byte) '{', (byte) '}'}; + assertThat(XContentFactory.xContentType(bytes), equalTo(XContentType.JSON)); + + bytes = new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf, (byte) '{', (byte) '}'}; + assertThat(XContentFactory.xContentType(bytes), equalTo(XContentType.JSON)); + + bytes = new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf, (byte) 0x20, (byte) '{', (byte) '}'}; + assertThat(XContentFactory.xContentType(bytes), equalTo(XContentType.JSON)); + } }