Skip to content

Commit 20a0f9e

Browse files
committed
Wrap parser creation so that bad char sequences do not cause 500
1 parent d37e09c commit 20a0f9e

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/JsonXContentImpl.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
import org.elasticsearch.xcontent.XContent;
1919
import org.elasticsearch.xcontent.XContentBuilder;
2020
import org.elasticsearch.xcontent.XContentGenerator;
21+
import org.elasticsearch.xcontent.XContentParseException;
2122
import org.elasticsearch.xcontent.XContentParser;
2223
import org.elasticsearch.xcontent.XContentParserConfiguration;
2324
import org.elasticsearch.xcontent.XContentType;
2425
import org.elasticsearch.xcontent.provider.XContentImplUtils;
2526

27+
import java.io.CharConversionException;
2628
import java.io.IOException;
2729
import java.io.InputStream;
2830
import java.io.OutputStream;
@@ -96,21 +98,37 @@ private XContentParser createParser(XContentParserConfiguration config, JsonPars
9698

9799
@Override
98100
public XContentParser createParser(XContentParserConfiguration config, String content) throws IOException {
99-
return createParser(config, jsonFactory.createParser(content));
101+
try {
102+
return createParser(config, jsonFactory.createParser(content));
103+
} catch (CharConversionException e) {
104+
throw new XContentParseException(null, e.getMessage(), e);
105+
}
100106
}
101107

102108
@Override
103109
public XContentParser createParser(XContentParserConfiguration config, InputStream is) throws IOException {
104-
return createParser(config, jsonFactory.createParser(is));
110+
try {
111+
return createParser(config, jsonFactory.createParser(is));
112+
} catch (CharConversionException e) {
113+
throw new XContentParseException(null, e.getMessage(), e);
114+
}
105115
}
106116

107117
@Override
108118
public XContentParser createParser(XContentParserConfiguration config, byte[] data, int offset, int length) throws IOException {
119+
try {
109120
return createParser(config, jsonFactory.createParser(data, offset, length));
121+
} catch (CharConversionException e) {
122+
throw new XContentParseException(null, e.getMessage(), e);
123+
}
110124
}
111125

112126
@Override
113127
public XContentParser createParser(XContentParserConfiguration config, Reader reader) throws IOException {
128+
try {
114129
return createParser(config, jsonFactory.createParser(reader));
130+
} catch (CharConversionException e) {
131+
throw new XContentParseException(null, e.getMessage(), e);
132+
}
115133
}
116134
}

0 commit comments

Comments
 (0)