Skip to content

Commit

Permalink
Relax limit on max string size in CBOR, Smile, YAML (#103930) (#104088)
Browse files Browse the repository at this point in the history
Remove the rough limit on string length from Jackson 2.15. The limit was already relaxed for JSON in #96031, this extends that change to other XContent types.

Refs: #96031
Fixes: #104009
(cherry picked from commit a359b1f)
  • Loading branch information
stu-elastic authored Jan 8, 2024
1 parent 58bcceb commit 9d8ef6b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.xcontent.provider;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.TSFBuilder;

public class XContentImplUtils {
public static <F extends JsonFactory, B extends TSFBuilder<F, B>> F configure(TSFBuilder<F, B> builder) {
// jackson 2.15 introduced a max string length. We have other limits in place to constrain max doc size,
// so here we set to max value (2GiB) so as not to constrain further than those existing limits.
return builder.streamReadConstraints(StreamReadConstraints.builder().maxStringLength(Integer.MAX_VALUE).build()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.provider.XContentImplUtils;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -45,7 +46,7 @@ public static XContent cborXContent() {
}

static {
cborFactory = new CBORFactory();
cborFactory = XContentImplUtils.configure(CBORFactory.builder());
cborFactory.configure(CBORFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false); // this trips on many mappings now...
// Do not automatically close unclosed objects/arrays in com.fasterxml.jackson.dataformat.cbor.CBORGenerator#close() method
cborFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import com.fasterxml.jackson.core.JsonFactoryBuilder;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.StreamReadConstraints;

import org.elasticsearch.xcontent.XContent;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentGenerator;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.provider.XContentImplUtils;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -46,12 +46,7 @@ public static final XContent jsonXContent() {
}

static {
var builder = new JsonFactoryBuilder();
// jackson 2.15 introduced a max string length. We have other limits in place to constrain max doc size,
// so here we set to max value (2GiB) so as not to constrain further than those existing limits.
builder.streamReadConstraints(StreamReadConstraints.builder().maxStringLength(Integer.MAX_VALUE).build());

jsonFactory = builder.build();
jsonFactory = XContentImplUtils.configure(new JsonFactoryBuilder());
jsonFactory.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);
jsonFactory.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
jsonFactory.configure(JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false); // this trips on many mappings now...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.provider.XContentImplUtils;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -45,7 +46,7 @@ public static XContent smileXContent() {
}

static {
smileFactory = new SmileFactory();
smileFactory = XContentImplUtils.configure(SmileFactory.builder());
// for now, this is an overhead, might make sense for web sockets
smileFactory.configure(SmileGenerator.Feature.ENCODE_BINARY_AS_7BIT, false);
smileFactory.configure(SmileFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false); // this trips on many mappings now...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;

import org.elasticsearch.xcontent.XContent;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentGenerator;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.provider.XContentImplUtils;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -42,7 +44,10 @@ public static XContent yamlXContent() {
}

static {
yamlFactory = new YAMLFactory();
yamlFactory = XContentImplUtils.configure(YAMLFactory.builder());
// YAMLFactory.builder() differs from new YAMLFactory() in that builder() does not set the default yaml parser feature flags.
// So set the only default feature flag, EMPTY_STRING_AS_NULL, here.
yamlFactory.configure(YAMLParser.Feature.EMPTY_STRING_AS_NULL, true);
yamlFactory.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true);
yamlFactory.configure(JsonParser.Feature.USE_FAST_DOUBLE_PARSER, true);
yamlXContent = new YamlXContentImpl();
Expand Down

0 comments on commit 9d8ef6b

Please sign in to comment.