Skip to content

Commit bfccb31

Browse files
committed
Relax limit on max string size in CBOR, Smile, YAML (elastic#103930)
Remove the rough limit on string length from Jackson 2.15. The limit was already relaxed for JSON in elastic#96031, this extends that change to other XContent types. Refs: elastic#96031 Fixes: elastic#104009 (cherry picked from commit a359b1f)
1 parent 58bcceb commit bfccb31

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.xcontent.provider;
10+
11+
import com.fasterxml.jackson.core.JsonFactory;
12+
import com.fasterxml.jackson.core.StreamReadConstraints;
13+
import com.fasterxml.jackson.core.TSFBuilder;
14+
15+
public class XContentImplUtils {
16+
public static <F extends JsonFactory, B extends TSFBuilder<F, B>> F configure(TSFBuilder<F, B> builder) {
17+
// jackson 2.15 introduced a max string length. We have other limits in place to constrain max doc size,
18+
// so here we set to max value (2GiB) so as not to constrain further than those existing limits.
19+
return builder.streamReadConstraints(StreamReadConstraints.builder().maxStringLength(Integer.MAX_VALUE).build()).build();
20+
}
21+
}

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/cbor/CborXContentImpl.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.xcontent.XContentParser;
2222
import org.elasticsearch.xcontent.XContentParserConfiguration;
2323
import org.elasticsearch.xcontent.XContentType;
24+
import org.elasticsearch.xcontent.provider.XContentImplUtils;
2425

2526
import java.io.IOException;
2627
import java.io.InputStream;
@@ -45,7 +46,7 @@ public static XContent cborXContent() {
4546
}
4647

4748
static {
48-
cborFactory = new CBORFactory();
49+
cborFactory = XContentImplUtils.configure(CBORFactory.builder());
4950
cborFactory.configure(CBORFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false); // this trips on many mappings now...
5051
// Do not automatically close unclosed objects/arrays in com.fasterxml.jackson.dataformat.cbor.CBORGenerator#close() method
5152
cborFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT, false);

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

+2-7
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
import com.fasterxml.jackson.core.JsonFactoryBuilder;
1414
import com.fasterxml.jackson.core.JsonGenerator;
1515
import com.fasterxml.jackson.core.JsonParser;
16-
import com.fasterxml.jackson.core.StreamReadConstraints;
1716

1817
import org.elasticsearch.xcontent.XContent;
1918
import org.elasticsearch.xcontent.XContentBuilder;
2019
import org.elasticsearch.xcontent.XContentGenerator;
2120
import org.elasticsearch.xcontent.XContentParser;
2221
import org.elasticsearch.xcontent.XContentParserConfiguration;
2322
import org.elasticsearch.xcontent.XContentType;
23+
import org.elasticsearch.xcontent.provider.XContentImplUtils;
2424

2525
import java.io.IOException;
2626
import java.io.InputStream;
@@ -46,12 +46,7 @@ public static final XContent jsonXContent() {
4646
}
4747

4848
static {
49-
var builder = new JsonFactoryBuilder();
50-
// jackson 2.15 introduced a max string length. We have other limits in place to constrain max doc size,
51-
// so here we set to max value (2GiB) so as not to constrain further than those existing limits.
52-
builder.streamReadConstraints(StreamReadConstraints.builder().maxStringLength(Integer.MAX_VALUE).build());
53-
54-
jsonFactory = builder.build();
49+
jsonFactory = XContentImplUtils.configure(new JsonFactoryBuilder());
5550
jsonFactory.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);
5651
jsonFactory.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
5752
jsonFactory.configure(JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false); // this trips on many mappings now...

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/smile/SmileXContentImpl.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.xcontent.XContentParser;
2222
import org.elasticsearch.xcontent.XContentParserConfiguration;
2323
import org.elasticsearch.xcontent.XContentType;
24+
import org.elasticsearch.xcontent.provider.XContentImplUtils;
2425

2526
import java.io.IOException;
2627
import java.io.InputStream;
@@ -45,7 +46,7 @@ public static XContent smileXContent() {
4546
}
4647

4748
static {
48-
smileFactory = new SmileFactory();
49+
smileFactory = XContentImplUtils.configure(SmileFactory.builder());
4950
// for now, this is an overhead, might make sense for web sockets
5051
smileFactory.configure(SmileGenerator.Feature.ENCODE_BINARY_AS_7BIT, false);
5152
smileFactory.configure(SmileFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false); // this trips on many mappings now...

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/yaml/YamlXContentImpl.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
import com.fasterxml.jackson.core.JsonEncoding;
1212
import com.fasterxml.jackson.core.JsonParser;
1313
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
14+
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;
1415

1516
import org.elasticsearch.xcontent.XContent;
1617
import org.elasticsearch.xcontent.XContentBuilder;
1718
import org.elasticsearch.xcontent.XContentGenerator;
1819
import org.elasticsearch.xcontent.XContentParser;
1920
import org.elasticsearch.xcontent.XContentParserConfiguration;
2021
import org.elasticsearch.xcontent.XContentType;
22+
import org.elasticsearch.xcontent.provider.XContentImplUtils;
2123

2224
import java.io.IOException;
2325
import java.io.InputStream;
@@ -42,7 +44,10 @@ public static XContent yamlXContent() {
4244
}
4345

4446
static {
45-
yamlFactory = new YAMLFactory();
47+
yamlFactory = XContentImplUtils.configure(YAMLFactory.builder());
48+
// YAMLFactory.builder() differs from new YAMLFactory() in that builder() does not set the default yaml parser feature flags.
49+
// So set the only default feature flag, EMPTY_STRING_AS_NULL, here.
50+
yamlFactory.configure(YAMLParser.Feature.EMPTY_STRING_AS_NULL, true);
4651
yamlFactory.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true);
4752
yamlFactory.configure(JsonParser.Feature.USE_FAST_DOUBLE_PARSER, true);
4853
yamlXContent = new YamlXContentImpl();

0 commit comments

Comments
 (0)